Fdisk the good old way to manage MBR disks

Computer hard drive with the protective casing removed

Managing disks is a common and fundamental skill for every system administrator, and knowing a bit more (rather than a bit less) about CLI tools surely will come in handy in many occasions.

Storage Basics

Before we start, it’s a good thing to understand basics, concept like Partition, Partition Table and File System should be crystalline clear before moving to practice. If you miss something in your bookshelf, I suggest you read my article about Sotrage Basics. Done? Let’s move on.

Fdisk

The first tool you will come across when you’re operating disks is fdisk. It is probably the most used tool since it is the oldest one. Fdisk operates on MBR-based disks, you should not use fdisk on GPT-based disks. To fire up fdisk just do:

fdisk DEVICENAME # Example fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

Now you’re inside fdisk, and the first thing that you will read is the useful information about changes you make inside the program. Everything you do inside the shell won’t be actually written to disk until you say so by using the w command. If you want to discard the changes you made, q command will do. If you ever find yourself not remembering a command use the m command. The first thing you might want to do is use the p command in order to print the partition table. Let’s analyse its output for one of my disks:

Disk /dev/sda: 21.5 GB 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 412 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal: 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000995fc

Device Boot      Start         End      Blocks   Id  System
/dev/sda1     *       2048      616477      307200   83  Linux
/dev/sda2           616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3          4810752    41943039    18566144   83  Linux

This gives us many useful informations about the disk layout and mostly the partition layout. As you can clearly see there are three partition (/dev/sda1, /dev/sda2, /dev/sda3). One of which has bootable flag, start/end and size (in sectors) of the partition. And finally the partition type (Id/System). To get a list of the partitions you can use the l command. The other commands you need to perform basic operations are basically three: n to create a new partition; d to delete a partition; t to change partition ID.

So let’s say I wanted to get rid of /dev/sda3. I would just do:

Command (m for help): d
Partition number (1-3, default3): 3
Partition 3 is deleted

# let's confirm by using p

Device Boot      Start         End      Blocks   Id  System
/dev/sda1     *       2048      616477      307200   83  Linux
/dev/sda2           616448     4810751     2097152   82  Linux swap / Solaris

Gone. Keep in mind the partition is not gone, all the modifies are still in memory and data are intact. Now let’s suppose I want to create a new partition.

Command (m for help): n
Partition type:
p   primary (2 primary, 0 extended, 2 free)
e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (4810752-41943039, default 4810752): 4810752
Last sector, +sectors or + size{K,M,G} (3810752-41943039, default 41943039): +10G
Partition 3 of type Linux and of size 10 GiB is set


Done. Notice you are asked to work with sectors, but when selecting the last sector you can instead specify a size in Bytes (K, M, G). Now let’s change the partition ID:

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

And that’s it. Last step is to save everything using the w command. If you get a warning about the kernel still using the old table the resource you were modifying was probably in use and you can resolve that by rebooting (or running partprobe, but there are still cases in which you have to reboot).

Conclusions

You now have a basic understanding on how to operate MBR-based disks. After reading this article you should be comfortable with: listing, creating, deleting partitions and with changing their type. As a end-note, be sure to double check before writing actual changes; recovering from an error might become complicated, especially if technologies like LVM are in use.

Image courtesy of William Warby.

Image courtesy of mark | marksei

The following two tabs change content below.

mark

The IT guy with a slight look of boredom in his eyes. Freelancer. Current interests: Kubernetes, Tensorflow, shiny new things.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.