Managing disks in Linux

Managing disks in Linux

Hard disk drives (HDDs) and Solid state drives (SSDs) are currently the most widespread high capacity storage devices. When such a drive is connected to a computer running Linux, it can be found in the device directory /dev/ under the name sdX, where X is a letter changing for each drive.

Partitions

The memory of a HDD can be split into multiple partitions. This can be useful to install several operating systems on a single HDD. In Linux, partitions are listed by adding a number after the drive name. Thus, the 3rd partition of the drive sdb can be found at /dev/sdb3. Drives and partitions can be listed using the fdisk tool with root privileges, using the option l:

sudo fdisk -l

fdisk can also be used to edit partitions. However, newer tools like parted might be more appropriate for this purpose. parted offers an interactive interface to create and delete partitions on a drive. parted can be started using the parted command. It is recommended to specify which drive to work for example for the drive sdc:

sudo parted /dev/sdc

Creating label

The way partitions are structured is described in the drive's label. A new label can be created with parted using mklabel. Note that doing so basically tells the drive to forget all about the previous content. Different label types exist but GPT seems on its way to become the new standard.

(parted) mklabel
New disk label type? gpt
Warning: The existing disk label on /dev/nvme0n1 will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes

Creating a partition

A new partition can be created using the mkpart command. parted will ask for the required parameters such as partition name, start and end. Note that the name of the partition does not constitute its label. I usually use primary as name when creating a single partition spanning the whole drive. Additionally, for reasons I haven't studied, a partition cannot be made to begin at the very first memory sector of the drive and its start point cannot be made aribrary. On the Seagate Barracuda HDD I own, setting the start and end points at 2048s and 100% happened to work.

(parted) mkpart
Partition name?  []? primary
File system type?  [ext2]? ext4
Start? 2048s
End? 100%

File systems

If a partition of an HDD is to be used to store files, a file system needs to be installed on the former. A popular Linux file system is ext4. which can be installed on a partition (here /dev/sda1 for example) using the mkfs (make file system) command:

mkfs.ext4 /dev/sda1 -L MY_LABEL

Here, the L option allows the user to choose a label for the file system. Setting a label can be useful to automated the mounting of drives using fstab. Creating a file system on a partition will mark all the available memory of the partition as free. Thus, the data which was previously stored there is considered as erased by the drive and will be written over when files are stored.

Once a filesystem is installed on the partition of a HDD, files can be transfered to it. Usually, files are being transferred to a HDD from a computer, running an OS with its own file system. Thus, the root of the HDD's file system needs to be made accessible from the computer's. This process is called mounting. Mounting a drive basically puts its filesystem's root into a directory of the computer's filesystem. The command used to mount drive is, simply enough, mount. For example, the filesystem contained within the 3rd partition of the drive sda can be mounted in the directory /mnt/mydir using:

mount /dev/sda3 /mnt/mydir

Automatic mount

By default, the mounting process has to be done manually when a drive is connected to the computer. However, this can be automated using fstab. fstab lists known drives and their mounting location so it can be consulted by the system on boot or when a drive is connected. Thus, setting automatic mounting for a drive is as simple as adding an entry to fstab. Here's an example fstab entry that mounts the ext4 formatted drive identified by the label MY_HDD at /mnt/mydrive. I haven't studied the role of the other options.

LABEL=MY_HDD  /mnt/mydrive  ext4  defaults,nofail,noatime 0 0

Alternatively, the drive can be identified by its device path as so:

/dev/sda3  /mnt/mydrive  ext4  defaults,nofail,noatime 0 0

Check read and write speed

Finally, when dealing with storage devices such as hard drives, it can be useful to know wether they're delivering their full potential in terms of writing speed. This can be done by writing a massive file to the drive and measure how long the process takes. The command dd can take care of this to provide accurate results. Here is an usage example:

dd if=/dev/zero of=/mnt/hdd/output bs=8k count=100k

This will create a file full of zeros called output on the root of a hard drive mounted at /mnt/hdd. Here, /mnt/hdd is to be replaced to wherever the hard drive is mounted.

The file created can then be used to test the reading speed of the drive. This can be done using the following command:

dd if=/mnt/hdd/output of=/dev/null bs=8k

What this command does it actually measure how long it takes to send the content of the test file to /dev/null