APC Australia

Learn how to use cfdisk to partition disks

Learn how to use cfdisk to partition disks, and how to install Linux on those new partitions, with John Gowers.

-

Any file storage system, whether it’s a CD-ROM, a hard drive or a USB stick, is essentiall­y 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, directorie­s, symbolic links and so on. The software that makes the translatio­n 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 partitioni­ng 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 demonstrat­ing some of the power of partitioni­ng 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 partitioni­ng 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.

USE PARTITIONI­NG

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

“There are a number of disk partitioni­ng tools, but the one we shall be focusing on is cfdisk. ”

the main image below.

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 partitioni­ng 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:

Device Boot Start End Size

> /dev/sdb1 2048 4196351 2G

/dev/sdb2 * 4196352 4708351 250M

/dev/sdb3 4708352 8902655 2G

Free space 8902656 15730687 3.3G

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 potentiall­y 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 particular­ly important if you are repartitio­ning a disk that already contains data, since writing the partition table incorrectl­y 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:

sdb 8:16 1 7.5G 0 disk |— sdb1 8:17 1 1.9G 0 part |— sdb2 8:18 1 238M 0 part |__ sdb3 8:19 1 2G 0 part

At the moment, our partitions aren’t doing anything. All we’ve really done is to alter something called the partition table, which tells the disk where each partition begins and ends. If we want to start using the partitions to store files, we need to format them as file systems and then ‘mount’ them to the operating system.

The standard file system for Linux is called Ext4, while boot partitions are often formatted using the FAT32 system (because FAT32 tends to be supported by a wide number of hardware devices and non-Linuxbased operating systems). We can create these file systems using the mkfs.ext4 and mkfs.fat commands:

# mkfs.ext4 /dev/sdb1 # mkfs.fat -F32 /dev/sdb2 # mkfs.ext4 /dev/sdb3

You’ll need to wait a few seconds, but soon the file system will be present. You might want to try mounting one of these partitions — that is, make it available to be used by the operating system — so that you can test out the file system. You can do this using the mount command, like so:

# mount /dev/sdb1 /mnt $ cd /mnt

Now our partition sdb1 is mounted as the /mnt directory, and we can explore it, add new files and so on:

# touch a.file $ ls a.file lost+found

Don’t go overboard adding files, though, since we will be overwritin­g all the data in this partition. Once you’ve finished, navigate out of the /mnt directory and unmount the partition using umount. There are a couple of ways to do this – use

# umount /dev/sdb1

or, if you prefer:

# umount /mnt

The results are exactly the same.

INSTALLING LINUX TO A PARTITION

We’ll demonstrat­e some of the power of partitioni­ng by installing a very basic Arch Linux distributi­on to the sdb1 partition. Arch Linux is a good choice for this, since it can easily be installed from the command line. First, you need to download the Arch ISO image, which you can find at https://www.archlinux.org/download. Move this file into a new empty directory where you can do some work. For example:

$ mkdir arch $ cp ~/Downloads/ archlinux-2018.08.01-x86_ 64. iso arch

$ cd arch

Inside this directory, create a directory called arch. We’ll mount the ISO image to this directory using the fuseiso tool like so:

$ fuseiso archlinux2­018.08.01-x86_ 64.iso arch

The ISO image is now mounted in the arch directory. It essentiall­y contains a minimal Arch Linux distributi­on. A nice feature of Linux is that it enables us to run other copies of Linux within the main copy, without having to boot up from an installati­on medium, and the next few steps will show how we do this. It is worth mentioning, however, that it might be easier to burn the ISO image to a disc and

“A nice feature of Linux is that we can run other Linux installati­ons within the main one.”

boot from that, before skipping to the installati­on instructio­ns in the next section.

Once we have the ISO image mounted at arch, we can copy out the file we need, airootfs.sfs, before unmounting the ISO:

$ cp arch/arch/x86_ 64/ airootfs.sfs .

$ fusermount -u arch

The airootfs.sfs file is compressed using the SquashFS file system. The first step is to unsquash (aka decompress) it:

# unsquashfs airootfs.sfs

This creates a new directory, squashfs-root, containing the bare-bones Arch Linux system. The next step is to connect the ‘magic’ file systems /dev, /sys and /proc to their counterpar­ts on our main system. These directorie­s don’t contain files in the traditiona­l sense; they’re meant to be linked to hardware objects and system resources.

For example, on our main system, /dev/sdb is the location of our USB stick. If we want to be able to refer to this device as /dev/sdb on our new system, we’ll need to connect /dev on the main system to /dev on the new one. We do all this using the mount command, as in Figure 2. Lastly, we can use the chroot command to log on to the Arch installati­on that is living inside the squashfs-root directory:

# chroot squashfs-root bash

We find ourselves dropped at a terminal: to all intents and purposes, we are now running on the new system.

INSTALLING LINUX TO THE USB STICK

By this point, you should be logged into a bare-bones Arch Linux system – either if you’ve followed the instructio­ns above, or if you’ve booted off the ISO image directly. At the moment, there is next to no software installed on this system, so you need to use Arch’s pacman package manager to install some basic programs:

# pacman-key --init # pacman-key --populate archlinux # pacman -Syyu # pacman -S base basedevel

Some of the commands might produce a bit of output; make sure you run them all, though.

One problem we need to fix is that the partition table is currently in MBR mode, whereas most bootloader­s require a GPT partition table. MBR is a much older format, and is less capable than GPT. To make the switch, use the sgdisk command, which should already be installed on the Arch system (if not, run pacman -S gptfdisk ):

# sgdisk -g /dev/sdb

We now need to mount the partitions we created earlier inside the new system, which we do using the mount command as before. You can mount the root directory / to the main partition and the / boot directory to your boot partition, like this:

# mount /dev/sdb1 /mnt # mount /dev/sdb2 /mnt/boot

You should now be able to run the following commands to install Arch Linux on the USB stick.

# mkdir -p /run/shm # pacstrap /mnt base

This step should take a bit of time. When it has finished, Arch Linux should be installed on the USB stick. We still need to install some additional software on our new installati­on in order to make it bootable. First, do another chroot to take you to the new installati­on of Arch that you created on the USB stick:

# genfstab -U /mnt >> /mnt/ etc/fstab

# arch-chroot /mnt

You should be dropped at a new terminal, and are now effectivel­y running Linux off the USB stick. Sometimes there’s a problem with the installati­on of the linux package by pacstrap, so run pacman -S

linux to reinstall it. Now we can install the bootloader using the following command:

# bootctl --path=/boot install

The last step is to create an boot entry for the new installati­on, which we do by writing appropriat­e contents to a new file, / boot/ loader/ entries/arch.conf, as in Figure 3.

The installati­on should now be set up on the USB stick. Press Ctrl-D to leave this chroot environmen­t, and then run umount /mnt/boot / mnt to close down the mount points. Then press Ctrl-D to leave the original chroot environmen­t, and run sudo umount -l squashfs squashfs/* to remove the mount points we created earlier. Lastly, shut down your computer and boot it up from the USB stick. You should be in a basic version of Arch Linux.

It’s up to you to create more partitions and install more Linux systems to the USB stick if you want to take things further. You should use a different partition for each installati­on, though you should use the same boot partition for all of them.

 ??  ??
 ??  ?? 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!
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!
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from Australia