Lab Objective:
Learn how to configure disk partitions and filesystems.
Lab Purpose:
In this lab, you will learn about partitioning disks using tools such as parted and creating filesystems with tools such as mkfs.
Note: fdisk was covered extensively in another lab, and so will not be covered here.
Lab Tool:
Ubuntu 18.04 (or another distro of your choice).
Lab Topology:
A single Linux machine, or virtual machine.
Lab Walkthrough:
Task 1:
The first step is to create a dummy block device to be used in the rest of the lab, so you don’t erase any of your machine’s actual data. Open a terminal and run:
dd if=/dev/zero of=block.img bs=100M count=10
This file is only 1GB in size. It doesn’t need to be large, but if you have more space, you can make it larger for more flexibility. Now run:
- sudo losetup -fP block.img
- losetup -a | grep block.img | cut -d: -f1
This should print a device name such as /dev/loop0. This is the loopback block device, which should be substituted everywhere you see [dev] for the rest of this lab.
Task 2:
Now you will partition this new block device. gdisk is a useful tool for creating GPT partition tables. It is based on fdisk, and the syntax is nearly identical; please refer to the ‘Manage Linux Printers and Printing’ lab to learn about gdisk. For now, use parted instead:
sudo parted [dev]
[dev] : for example /dev/sda
You can use parted non-interactively, but here, run the following commands in interactive mode:
- mklabel gpt
- mkpart primary ext4 0 50%
- mkpart primary linux-swap 50% -1s
- quit
These changes may trigger some warnings; they are not important in this case, so respond in the affirmative and enter “Ignore”.
It is worth noting that, unlike fdisk and gdisk, parted does not store all changes in memory and write them at the end, but writes every change made as you make it. Thus, it might be considered less safe than the previous two.
Task 3:
Now create an ext4 filesystem on the first partition of your block device. The name should end in “p1” – for example, if your block device is /dev/loop0, the first partition should be /dev/loop0p1: sudo mkfs.ext4 [dev]p1
Finally, create a swap partition as well: sudo mkswap [dev]p2
If you were actually setting up a new system, you would then activate the swap partition with swapon, but in this case, you should not do that. You could also mount the [dev]p1 partition and use it as a normal ext4 filesystem if you wanted to.
Task 4:
Clean up:
- sudo losetup -d [dev]
- rm block.img
Notes:
In this lab, you only created an ext4 filesystem – ext4 is one of the most common filesystems used on Linux today, but it is far from the only option. Under what circumstances might you choose XFS, VFAT, exFAT, or Btrfs instead?