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:

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:

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:

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.

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:

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.
The IT guy with a little boredom look in his eyes, fond of computers since forever he now works as a freelancer in the IT and shares his experiences through this blog.

You may also like...