Hey everyone! it's me again. Today (and for the next few posts actually) I'll bring you a series of tutorials and introductions to Linux and its inner workings. Some people have been asking me for some tutorials on Linux and some technical stuff about it and I thought some of you might also benefit from this.
First of all, and before I get my hands dirty with this: today we are going to mess with hard drives, partitions and partition tables so please, BE CAREFUL! if you are simply going to test these commands it is always a good idea to do it on a virtual machine with a virtual HDD.
Partition tables, partitions and file systems
There is a lot of absolutely great documentation on the subject if you want to go into more details which are outside of what I want to talk about in this post but, to give you an idea your physical hard drive probably looks like this:
Here is an image from Wikipedia to better explain how primary, extended and logical partitions are organized |
In the very first position we have the MBR partition table, which is a small amount of bytes dedicated to point to primary partitions and whether they are bootable or not (are they an OS partition or simply a data partition?).
Then we find some reserved space, some very interesting reserved space. This space is basically like "rounding" in disk terms and although it's technically unused, some OS use this space to save some session data when rebooting or maybe even some boot sector viruses.
Then we have our partitions, they can be primary or extended which may contain more (logical, in this case) partitions "inside" so you are not limited to the 4 partition limit with MBR partition tables.
These partitions need to be formatted into a properly structured partition using a file system. Some examples? ext4, NTFS, exFAT, FAT, FAT32... There are a few gazillion small differences between these (and the other 95% of other file systems I didn't mention) which you can find here if you are into this sort of stuff.
TL;DR Hard drives are like... sitting on an airplane: The stewardess (Partition table) is at the front and knows who is sitting where, in the first row there is some empty space that no one really uses, and yet there are some weird bags there sometimes (reserved partition). Then we have all the seats (partitions) which have been properly cushioned according to the users' preference (file system) where the users (OS/data) rest their butts on. Oh and that lady with 3 kids in a single seat? that's an extended partition.
Let's get our hands dirty: how to format hard drives in Linux
Note: these tests have been done on a 15GB HDD using a recent arch linux DVD on a VirtualBox VM. No hard drives were harmed during the course of this tutorial.
Also, I will not write the commands directly (you can still see them in the screenshots). Why? to avoid readers copy-pasting the commands and messing their drives.
1) Let's see what we have and what we want
For that we use the -l argument (l from list, get it?)
In the image above you can see we have our empty 15GB HDD as /dev/sda (which, in case you don't know, marks it as a drive 'a', if it were in /dev/sdb, it'd be the second drive 'b')
For this demo, we are going to set the partitions as follows:
- 512MB swap partition
- 5GB ext4 partition
- The rest as another ext4 partition for data
This is by no means the most optimum way or partitioning (swap is usually last for instance), nor may this ratio work for your particular case, you'll have to decide how you want to cushion your seats yourself. (but I think 512MB swap and ext4 is a pretty solid bet as of 2014)
2) Split!
It's time to 'split' the Hard drive into partitions. For that we write fdisk and the drive we are interested in partitioning (/dev/sda in our case). This will take us to a prompt where we can enter our commands (no idea what do? press 'm').
To create our partition we press 'n' (new), set it as primary, partition 1 (this will make our partition /dev/sda1) and size 512MB, typing +512M. Done!
But hey, you lied! this is not swap! Well you're right, it isn't. for now we are simply 'splitting' the drive, then we'll get back to file systems.
Then we do the same for the other two partitions (I'm not including a screenshot or this post will get too long), we simply set the size to be +5G for the next partition (2) and set it as default to fill the rest of the drive in the last one. Once we are done we press 'w' (write to disk)
The result?
3) File systems
Time to set our file systems. You can do this from within fdisk by going back to fdisk <drive> (remember, drive, not partition!) and setting it with 't', but we are going to do this using mkswap and mkfs, to learn more stuff and other ways of doing it.
mkswap /dev/<partition>
will make your partition (in this case it should be /dev/sda1) a swap partition.
Now, to format sda2 and sda3 as ext4 partitions, we make use of mkfs.ext4
Now, to format sda2 and sda3 as ext4 partitions, we make use of mkfs.ext4
mkfs.ext4 /dev/<partition>for both sda2 and sda3.