[Tutorial] Understanding Partitioning and Formatting

A tutorial about partitioning and formatting for beginners.

By Nadim Issa

Data storage:
Data storage is always an important aspect of computing. There are some essentials that need to be addressed when determining how you want to manage this. Some things to keep in mind are: how you like to organize things, what is important to your task at hand, and how much space is needed. I will give a brief overview then give a little more detail on using some of my more favorite tools for partitioning and formatting.

Partitioning:
The easiest way for me to explain this is through metaphor. Almost everyone I know has tried to put together one of those fiberboard bookshelves that you get from big box store. Now, before you put books, movies, toys or tools into this fiberboard bookshelf you actually have to install the shelves. Normally there are adjustable pegs so you can adjust the height between these shelves. The space between these shelves are partitions. If you need to adjust the size after loading up the shelf with stuff, you have to take everything off it that you want to keep. There are some tools that will allow you to adjust the size without moving stuff, but there is always a risk of loosing data. So a little planning on what or how much you want to store on that shelf will help prevent you from having to remove all the important stuff and changing its size. Most people adjust their partitions when doing a fresh install, or adding another storage device. The act of slicing up where data can be stored on the storage device is called partitioning.

File system:
"File system" can most easily be described as how things are going to be stored on your shelves. For example, all of the books will be laying on their side, standing up, or back to front. In all truth it is more complicated than that, but thankfully it is all transparent to the end user. There are various filing systems to use when formatting a partition (e.g. ext2, ext3, FAT32, NTFS, etc). Figuring out which one you want to use is beyond the scope of this document. The act of putting a filing system onto a partition is called formatting.

Summary:
The overall bookshelf in my metaphor is equal to our data storage device (hard drive, flash stick, solid state drive, etc.). Installing the shelves is equivalant to partitioning the data storage device. Defining how we want stuff stored on the shelves is like formatting the partition.

You might be asking "how can I see the partitions that are already on my system?"

The sweet and simple answer is, open up your terminal of choice and enter the following command:

fdisk -l #that is minus L in the lowercase for list.

On my system it shows the information below. I will explain what and why I chose to partition my drive this way.

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7bca3e72

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1050623 524288 83 Linux
/dev/sda2 1050624 1255423 102400 7 HPFS/NTFS/exFAT
/dev/sda3 1255424 62490623 30617600 7 HPFS/NTFS/exFAT
/dev/sda4 62492670 488396799 212952065 5 Extended
/dev/sda5 62492672 121083903 29295616 83 Linux
/dev/sda6 121085952 484560895 181737472 b W95 FAT32
/dev/sda7 484562944 488396799 1916928 82 Linux swap

The first line tells us we have a data storage device "/dev/sda" with 250 GB of storage capacity. The next 5 lines gives us more detailed information about our data storage device. "/dev/sda" sda is the first data storage device on the virtual scsi chain. "/dev/sdb" sdb is the second data storage device on this chain. The more data storage devices you have the higher it goes: sda, sdb, sdc and so on. There are also hda, hdb, hdc. These were more common when we were using an ide connected data storage device. On rare occation you will see uba, ubb and ubc. On older kernels they treated usb connected data storage devices as such just usb--hence uba, ubb, and so on. Now they are coupled into the virtual scsi chain, so when you plug in a usb data storage device it will take the next available scsi chain device name. So if we have sda and an sdb, it will take the next in line sdc.

From the 9 line and going down we have /dev/sda1, /dev/sda2 and so on. Those are the number of the partition that is on device. "/dev/sda1" sda1 is the first partition on data storage device sda. sda2 is the second partition on the same data storage device. sdb2 is the second partition on data storage device sdb (physically different than data storage device sda and of no relation to sda2). There are different types of partitions: primary, extended and logical. On older operating systems it was a requirement that the OS need to be installed to one of the primary partitions. I will explain why my drive is partitioned like this.

/dev/sda1 524 MB -- This partition is a primary formatted ext3 and I mount this to my /boot. I keep my kernel and bootloader stored there. It is small because it does not need to have loads of space. If you do this, keep it smaller than 2 GB. On older systems the bootloader could not load the kernel if it was physically located past the 2 GB point on the drive. I don't believe this is still a problem on the newer systems. Old habits die hard.

/dev/sda2 102 MB -- This partition is a primary formatted ntfs, also with hidden windows 7 restore crap.

/dev/sda3 30 GB -- This partition is a primary formatted ntfs-- the windows C: drive, where I installed windows 7. This can also be referred to as the root drive for windows.

/dev/sda4 212GB -- This is an extended partition. Basically it is a primary partition that is split up into smaller blocks. This partition is typically transparent to end user. The reason for this is that the logical partitions are created within the space of the extended partition.

/dev/sda5 29 GB -- This is a logical partition formatted ext3. This is the root of my linux install. Its mount point is /.

/dev/sda6 181GB -- This is a logical partition formatted fat32. My intention for this partition was to have a common media storage partition for both linux and windows. Windows will see this as a drive D:. I will mount this as /volume/media/. You need to be aware when setting up a partition as fat32 that it does not retain any ownership or permissions information on the data stored there. I did this so I can have a common workspace for both operating systems. I have not used it at all. Poor planning on my part.

/dev/sda7 1.9 GB -- This is a logical partition that is formatted as swap. The swap partition is used like a virtual RAM. There are many differing opinions on what the size of your swap should be. Basically, as your RAM fills up depending on your kernel it will swap less used memory blocks in the RAM to your swap partition. If you realize that you have not created a swap or enough swap space, no need to worry. You can create a swap file on one of your partitions. That will be for another tutorial.

Also "fdisk -l /dev/sd(whatever)" will list partitions only on specific data storage device. Without the /dev/sd(whatever) it will list all partitions it can detect on your system. More information about fdisk can be found in the terminal by using the following command:

man fdisk

Cautions:
When partitioning a data storage device, chances are you will loose all data on that device. Back up your important data.
When formatting a partition, you will loose all data on that partition. Again make sure you back up all important data.
If you make either of these mistakes, data recovery becomes very expensive. Don't forget store all your important data, on two different data storage devices: one on your computer and the other on a thumb drive or external harddrive that you keep with you or in safe deposit box. There are also on-line services that can do this for you too, but be wary of them and do your research. Just in case of disaster.

You might be wondering, "well how do I partition this data storage device?"

My favorite partitioning tool is cfdisk. It comes by default with Porteus. You open your favorite terminal, and begin keying away.

cfdisk /dev/sda


Warning: do not do this unless you are aware of the risks. No changes will be made to the data storage device until you select the write option.

This tool is pretty easy to use: just grab ahold of your arrow keys. Use the up and down arrow keys for selecting the different partitions or free space and use the left and right arrow keys to select an action. For example, to remove partitions from the data storage device, select the partition by using the up/down arrow keys then press the right and left keys until you have 'Delete' highlighted, then press enter. For creating partitions, you need to have some free space listed. Again using the up/down arrow keys, highlight the free space (this shows up under FS Type), then use the right/left arrow keys to highlight 'New', then press enter. You specify the size you want the partition to be and press enter one more time. Next you can specify the file system type you want to put on there (not totally necessary). Select the partition, use the left/right arrows to highlight 'Type', and press enter. This will give a huge list of file system types. You don't really need to do this step because when you format the partition it takes the file system format that you specify when formatting. When all space is allocated to partitions, or you feel satisfied with your partitioning job, use the right/left arrow keys until to select the 'Write' option, then press enter.

Note: if you select the 'Quit' option and press enter (and did not issue the 'Write' command), no changes will be been made to your data storage device. But if you did issue the write command, then all changes are permanent at that time. So, if you make more changes after you write, you need to write again before you quit in order to keep your new changes.

For more info about cfdisk just issue the following command in your terminal:

man cfdisk

"Well that was pretty nifty, how do I format these partitions?"

This one is also pretty easy. After determining which filing system you would like to use, just open up your terminal and type 'mkfs' (without the quotes), and press tab twice. This will show all the different file system types with which you are able to format partitions, with example output.

mkfs [tab key] [tab key]

This doesn't work if you do not have tab completion enabled. Just give it a try. Anyway, on my system it shows the following:

mkfs mkfs.cramfs mkfs.ext3 mkfs.ext4dev mkfs.minix mkfs.reiserfs mkfs.xfs mkfs.bfs mkfs.ext2 mkfs.ext4 mkfs.jfs mkfs.msdos mkfs.vfat

All of these commands can format a partition differently.

Note: mkfs -t [fs] is just a frontend for the mkfs[dot]whatever.

So, to format the first partition of data storage device sda as ext2 filesystem, we will use the following command:

Warning: Issuing the following command will wipe out whatever is on the specified partition. Make sure you have all data backed up. If it is a freshly created partition, don't worry too much, but make sure it is the correct partition you are formatting. Do not cut and paste the following command unless you are 100 % sure that this is the partition you want to format as ext2.

mkfs -t ext2 /dev/sda1

or, you can issue:

mkfs.ext2 /dev/sda1

If your operating system is set up to automount storage devices, it will detect and mount your new partition. If automounting is not set up, you can mount it to where ever you want using the following command:

mount -t ext2 /dev/sda1 [destination]
#destination directory needs to be empty in order to mount a device to it.


Once mounted you can access the data storage device to copy data to and from it.

More info can be found on both of these commands:

man mkfs # learning about formatting and options
man mount #learn to mount and unmount data storage devices


This is not the 10 commandments of data storage and data management. Just a guide to help assist others in making an informed decision.

Thanks for reading!



To view the discussion about this article, make a suggestion, or ask a question about it, please visit this thread on our forum.