Basics of partitioning with fdisk and formatting with mkfs
Gone are the days are absolutely having to know fdisk and mkfs to get started with Linux as a desktop. Most installers now set up everything for you on the install, in fact, most also setup logical volume management as well. But fdisk and mkfs are still very useful tools when you need to manually partition and format a disk. Throughout this tutorial I’ll walk you through creating and deleting partitions and the basic functions of fdisk and mkfs in linux. A follow-up tutorial will be written specifically for swap partitions and will touch back on this fdisk tutorial as well.
fdisk is a partition table manipulator for Linux. I will assume you have a spare disc to play with currently that’s not your main disk.
First lets explain the structure of discs within linux.
hda = first drive attached to ide 1
hdb = second drive attached to ide 1
hdc = first drive attached to ide 2
hdd = second drive attached to ide 2
The same scheme goes for SCSI hard drives. The only difference is that the drives name will start with an S
sda = first hard drive attached to the scsi interface, and so on.
the sda, sdb, sdc, etc scheme is also used within logical partitioning. If you allowed Debian, Ubuntu, Fedora, or openSuse do a default install, chances are this is the way you’re discs are labeled.
Now we depict partitions by adding a reference number to that naming scheme. For instance:
- /dev/hda1 - is the first partition on hda
- /dev/hda2 - is the second partition on hda
- /dev/hdb1 - is the first partition on hdb
The same scheme goes for the scssi and logical volume management schemes.
So if you have just installed a second hard-drive onto your system, this device will be labelled as /dev/hdb or /dev/sdb.
To get a list of the disks currently within your system we need to enter this command as ‘root’
- for IDE drives: ls /dev/hd*
- for SCSI drives: ls /dev/sd*
From this point on I’ll assume we’re working with /dev/hdb but make changes as you see fit.
First lets look at /dev/hdb within fdisk, so again as root: fdisk /dev/hdb
The first thing we want to do is type ‘m’ for the menu. As you can see fdisk lists a number of options we can perform on this disk.
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Lets go ahead and type ‘p’ to print the partition table. Your disc may or may not have partitions already on the disk. This will display the currently layout of the disc itself. For instance:
Device Boot Start End Blocks Id System
/dev/hdb1 * 1 4678 37576003+ 83 Linux
Mine currently states that my hdb1 starts at block 1 and ends at block 4678, it tells us how many blocks are taken up by this, and the system ID number. The ID number tells the system what kind of partition it is.
The most common ID’s you will come across will be:
- 83 - Linux Partition
- 82 - Swap Partition
- 85 - Linux extended
To see what ID’s are available simpy type ‘t’, specify a partition, and type ‘l’ (lowercase L) to list the id’s available. You can type Ctrl+Z to exit the menu if you do not wish to make any changes, but you will have to fdisk /dev/hdb to get back into fdisk.
So lets say there’s a partition /dev/hdb1 and we need to delete that partion. In fdisk we type ‘d’ . It will ask us for our partition number that was listed when we used ‘p’ to print the fdisk table for /dev/hdb to the screen. In this case our partition number is 1.
Now if we use ‘p’ to print the fdisk table again, we shouldn’t see any partitions. So lets go ahead and type ‘n’ for a new partition. It will ask us if we want a primary partition or a logical partition. lets touch base on this quickly.
- A linux system can only have 4 primary partitions per disk.
- primary partitions can be made bootable
- A linux system can have many logical partitions.
- logical partitions can be contained within primary partitions
For the sake of this tutorial we’ll keep it easy and simply select ‘p’ for a primary partition, we’ll assign it a partition number (1-4), you’ll be selecting 1 for this disk. We’ll just hit Enter twice for the default beginning and ending block.
Now if you type ‘w’ this will save this to your partition table and exit the fdisk application.
mkfs is what we use to format the partition after we created them with fdisk. mkfs is pretty simple to use for this function. We type the following:
mkfs -t ext3 /dev/hdb1
To explain:
- mkfs calls the application
- -t tells mkfs that we’re going to specify a file system
- ext3 is the filesystem I chose for this example, we could’ve chose ext2 or reiserfs, or vfat or many others.
- /dev/hdb1 is the partition we are formatting.
Please refer to these other tutorials for getting a larger grasp of what we’re doing here:














Post a comment