Wednesday, March 17, 2010

How To: Partitioning Hard Drive using FDisk utility in Linux

You got a new hard drive and attach it to your laptop/desktop but before you can use it, you need to partition and format the hard drive.

This can be done with a series of easy steps. This is a small and easy tutorial which will show you how to use fdisk, one of very famous utility for disk partitioning.

Fdisk is the utility which we will be using for this example. Though there are other options also like parted/gparted and use of them depends totally on personal preference.

For Fdisk, you need to provide the device name as an argument. For that the possible options could be:

/dev/hda
/dev/hdb
/dev/sda
/dev/sdb

Where

/dev/hd[a-h] stands for IDE disks
/dev/sd[a-p] stands for SCSI disks
/dev/ed[a-d] stands for ESDI disks
/dev/xd[ab] stands for XT disks.

Hard disks can be divided into one or more logical disks called partitions. The partition info is stored in the partition table which is present in the first 512 bytes of the disk.

By default, a hard drive can be divided into 4 partitions but you can have much larger number of logical partitions by dividing one of the primary partition. And only one of the partition can be sub-divided into logical partitions.

Only for USB drives:
Before attaching the hard drive to your system, tail the system logs so that you could know whether the hard drive is detected by your system and some other details which will be used later. 
tail -f /var/log/messsages 
This is the default location of system messages in most of the OS's.
When you will attach your hard drive then you will see new entries in the logs like this
Feb  8 17:49:29 Napster-PC kernel: [2596897.844838] scsi 4:0:0:0: Direct-Access     Kingston DataTraveler 120 1.00 PQ: 0 ANSI: 2
Feb  8 17:49:29 Napster-PC kernel: [2596897.845266] sd 4:0:0:0: Attached scsi generic sg2 type 0
Feb  8 17:49:29 Napster-PC kernel: [2596897.850317] sd 4:0:0:0: [sdb] 31252024 512-byte logical blocks: (16.0 GB/14.9 GiB)
Feb  8 17:49:29 Napster-PC kernel: [2596897.850812] sd 4:0:0:0: [sdb] Write Protect is off
Feb  8 17:49:29 Napster-PC kernel: [2596897.853446]  sdb: sdb
Feb  8 17:49:29 Napster-PC kernel: [2596897.857072] sd 4:0:0:0: [sdb] Attached SCSI removable disk
Looking at the above output one can see some details about the hard drive, like
  • The manufacturer of the hard drive
  • If this is a USB drive then whether this is write protected or not
  • What is the device name assigned to your drive, for ex in this : sdb
Out of things given above, right now the last point is the only one in whom we should be interested. The last point says that our drive have got “sdb” name in the system. This means that when you will do into the dev directory (/dev) then you will find a batch file like this.
brw-rw----  1 root disk      8,   6     2010-01-09 16:27 sdb
If you are attaching a non-USB drive to your system then after restarting your system you should run this command

# fdisk -l

Output:

Disk /dev/sdb: 160.0 GB, 160041885696 bytes
127 heads, 32 sectors/track, 19456 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x7c97a33b
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2550    20480000   83  Linux
/dev/sda2            2551       12526    80132220    5  Extended
/dev/sda5            2551        2799     2000061   82  Linux swap / Solaris
/dev/sda6            2800       10094    58597056   83  Linux
/dev/sda7           10095       12526    19535008+  83  Linux
Disk /dev/sdb: 80.0 GB, 80020942848 bytes
127 heads, 32 sectors/track, 9728 cylinders
Units = cylinders of 9728 * 512 =4980736  bytes
Disk identifier: 0×7c97a33c
Device Boot      Start         End      Blocks   Id  System

This output shows that we have two hard drives:

>> /dev/sda - 160GB
>> /dev/sdb - 80GB

As we can see from the output that the first drive is well partitioned, but not the second one.

Before going beyond this point, please backup your important data. A small mistake and you will be ready to kill me :)

Now start with /dev/sdb partition. Run this command
fdisk /dev/sdb

Output:

The number of cylinders for this disk is set to 19457.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help):

You can press “m” which will show you all the help and the commands you can run into the fdisk interface
Command (m for help): m
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)
Before going forward confirm that there are no other partition on this drive.
Command (m for help): p

Output:
Disk /dev/sdb: 80.0 GB, 80020942848 bytes
127 heads, 32  sectors/track, 9728 cylinders
Units = cylinders of 9728 * 512 =4980736  bytes
Disk identifier: 0x7c97a33c
Device Boot      Start          End      Blocks   Id  System

This clarifies that we don’t have any other partition available in this disk. This will make sure that you are not going to erase your main drive.

After this we have to create a new partition. For this either we can create 3 primary partitions and one extended partition.

Later on we can use extended partition to create more logical partitions. But we don’t really need 3 primary partitions.

So i will make one primary partition and one extended partition which i will use to make further logical partitions.
Command (m for help): n

Output:
Command action
e   extended
p   primary partition (1-4)

Now press “p”
Partition number (1-4):

Now give the partition number which usually should be 1.
Partition number (1-4): 1
First cylinder (1-9728, default 1):

Over here you need to provide the starting cylinder number for the partition. Leaving it blank will take the default to the starting position. I recommend leave it blank and just press “Enter”.

Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-9728, default 9728):

This is the value for last cylinder, means how big you want your partition to be. Over here either you can give value in cylinders, or could provide value in KB/MB/GB.

Unless you are very good in mathematics, and could calculate that exactly how many cylinders you will be needing for your partition space, I will suggest to use MB/GB. In this example i will make a partition of 20GB.

Last cylinder, +cylinders or +size{K,M,G} (1-19457, default 9728): 20G

This will create a primary partition of 20GB. After this we will create a extended partition. Extended partition is nothing but a way of fooling system.

A system doesn’t allow you to create more than 4 partitions, so a work around for this is to create a single partition and then further break that partition at OS level to create more partition.

This is the same thing we are going to do.

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
select “e” over here
Partition number (1-4):

I would recommend to give “4″ to the extended partition because in future if more than one primary partition is needed then you are free to create 2 more primary partitions at number 2 and 3.

Partition number (1-4): 4

Again you need to define the size of this partition. Over here there is one thing to note is that you have to create the extended partition with rest of all the space.

To do this, you don’t need to provide any value to the system, Just keep pressing “enter” and the system will keep on taking the default values which is the rest of the complete space.

First cylinder (21-19457, default 21):
Using default value 21
Last cylinder, +cylinders or +size{K,M,G} (21-9728, default 9728):
Using default value 9728

This is not the end. You can’t use the extended partition just like that. It needs to be partitioned further to make it usable into logical partitions.

It’s complete your choice or need that how many logical partitions you wanted to create. Normally you can make at least of 64 logical partitions in an extended partition (Again this thing depends how much you OS is supporting).

To create a logical partition follow this step
Command (m for help): n
Command action
l   logical (5 or over)
p   primary partition (1-4)

Now press “l”. After that it will ask for the starting cylinder, for which use the default one and press “enter”.

After that it will ask for the Last cylinder, for which use the default one if you want only single logical partition or provide the required partition size.

I will take the default value and will create only one logical partition.

First cylinder (21-19457, default 21):
Using default value 21
Last cylinder, +cylinders or +size{K,M,G} (21-19457, default 19457):
Using default value 19457

Looks good. After this the last task is to assign a file system to the partition. This totally depends on your choice.

I will use “Linux” in the example.
Command (m for help): t
Partition number (1-5): 1
Hex code (type L to list codes): 83

Do the same for other partition also. After this just verify that the partition table is in the way you wanted.

Command (m for help): p
Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×7c97a33b
Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          20      160618+  83  Linux
/dev/sda4              21       19457   156127702+   5  Extended
/dev/sda5              21       19457   156127671   83  Linux

There is one thing to note over here. Till this point whatever work you have done, is not really done. It is just into the memory, so if you quit the fdisk interface without saving it, you will see that everything is gone. So you need to save this work.

Command (m for help): wq
w - save
q - quit

The partitioning is done but still your OS doesn’t know anything about that. For this you can do anyone of the following:

# partprobe             This will inform the operating system kernel of partition table changes,

by requesting that the operating system re-read the partition table

# init 6                    This will do the same thing by rebooting

That’s it. Now your hard drive is ready with two partitions, one 20GB and the other one of 60GB.

No comments:

Post a Comment