Essential Linux: cfdisk
Learn how to use cfdisk to partition disks, and how to install Linux on those new partitions
Learn how to use cfdisk to partition disks, and how to install Linux
Any file storage system, whether it’s a CD-ROM, a hard drive or a USB stick, is essentially a device for storing a long chain of 1s and 0s. When we interact with it, however, we don’t see this; instead, we see files, directories, symbolic links and so on. The software that makes the translation for us is called a file system.
There’s one step in between, though, and it’s often overlooked. A particular file system need not take up the whole of the disk it lives on. Instead, the disk may be split up into individual sections, called partitions, each of which can have its own file system – or none at all.
The disk keeps track of its partitions through its partition table, which tells it how many partitions there are, and where each one begins and ends.
There are a number of disk partitioning tools, but the one we shall be focusing on is one of the easiest to use, fdisk. In fact, we shall mainly be using its fancier cousin, cfdisk, which makes things easier still through its ncurses interface.
We’ll be demonstrating some of the power of partitioning by installing a complete Linux system to a USB stick, while saving some space ‘on the side’ for file storage. The stick needs to be at least 8GB in capacity – and obviously, first make sure there’s no data on it you want to keep, because we’ll be removing everything.
Before partitioning your USB stick, you should find out what your computer identifies it as. Insert the stick into your computer’s port (if it’s already in, eject it and put it in again). Once it’s in, run dmesg in a terminal and look for output near the end similar to the following:
scsi host4: usb-storage 2-2:1.0 scsi 4:0:0:0: Direct-Access Generic Flash Disk sd 4:0:0:0: [sdb] 512-byte logical blocks sd 4:0:0:0: [sdb] Write Protect is off
The thing we’re looking for is the name sdb. Depending on your setup, you might see something different, such as sdX or hdp – but normally on a Linux system, it’ll be sd followed by a single letter.
Let’s suppose that your USB stick is available at sdb. You can verify this by running the lsblk command, which gives an overview of the storage on your system. You might see something like the output in Figure 1. Here, sda is our computer’s hard disk, which is already split
into partitions sda1 and sda2 (there may be more on your system). Meanwhile, our USB stick, sdb, has a single partition, sdb1.
Once you’ve identified the device name for your USB stick, you can fire up cfdisk:
# cfdisk /dev/sdb
If your USB stick is not at sdb, use the other value here instead. You should see a screen like the one in the main image (see facing page).
Using the arrow keys, you can navigate between the partitions currently on the stick. Each row of the table represents a contiguous range of memory addresses that is either a partition or an area of empty space. Most likely, you’ll start off with a single partition and empty space, as in Figure 1. Use the left/right arrow keys to select the [Delete] option and press Enter to delete this partition. If there are any other partitions, use up/down to select them and then delete them as well, so that the entire USB stick is free space.
Let’s start partitioning the drive. Our first partition will be 2GB in size. Use the left/right keys to select the [New] option at the bottom and press Enter. At the prompt, enter 2G, and press Enter again to create the partition.
This should have been fairly easy, so let’s make a new partition. Press the down arrow to move the cursor at the top into the Free space row: we will create the next partition in here. Select [New] as before, and this time type in 250M rather than 2G, before pressing Enter to create a new partition of size 250MB.
This partition is going to play a special role: it’s going to be the boot partition for our USB stick. Any bootable medium, whether it’s a Live CD, your laptop’s internal hard drive or a live USB stick, has to have a special partition called the boot partition that contains various files necessary for booting up the system. The boot partition may contain other files as well – indeed, various older systems are entirely contained inside one bootable partition – but it is a bit more robust to only use the boot partition for booting purposes.
For now, all we need to do is to make this partition bootable. Keeping the cursor at the top focused on your new partition (which might be named /dev/sdb2), use the left/right arrows to select the [Bootable] option, and press Enter to set the bootable flag. You should see a small star * appear next to /dev/sdb2 at the top, in the Boot column.
Now let’s create another 2GB partition after the one we’ve just made. Press the down key to move into the Free space, and then select [New] at the bottom again, typing in 2G and pressing Enter as before. At the top of the display, we might see something like the following:
Having the free space at the end means that we can come back and use the [Resize] option to grow the sdb3 partition if we want to later on. Growing the sdb1 and sdb2 partitions will be much harder once all the partitions have data written to them: we will need to recreate the partition table in full in cfdisk, and then use another tool (such as dd) to shift the data into the new positions. This is both time-consuming and potentially dangerous to the data if any errors occur.
The very last step is to write this partition table to the USB stick. At the moment, nothing we have done has actually altered the state of the disk at all, which is a very good thing: if you make any mistakes, you can correct them (or select [Quit] to start again completely) and be sure that you won’t have messed anything up. This is particularly important if you are repartitioning a disk that already contains data, since writing the partition table incorrectly could destroy some of that data.
Using the left/right keys, select [Write] at the bottom, and press Enter. cfdisk will check one last time that this is what we actually want to do:
Are you sure you want to write the partition table to disk?
Type "yes" or "no", or press ESC to leave this dialog.
Type yes and then press Enter to write the partition table. Lastly, navigate to [Quit] and press Enter to close cfdisk. We can check what we’ve done by running lsblk again. After these changes, the sdb portion of the output should look something like this:
There are a number of disk partitioning tools, but the one we shall be focusing on is cfdisk
Above cfdisk makes it easy to partition disks through its user-friendly interface. Admittedly, this might not look all that friendly with so many numbers floating around, but it’s a lot easier than using the command line!
Above We can use the lsblk command for a useful highlevel overview of the partitions on the devices on our system