Aug 19 2008

How To: Partitioning A Linux Hard Drive

A lot of my friends who use linux still don’t use it for partitioning and repartitioning their hard drives or for making and deleting partitions.  From reading some of the information available about formatting hard drives under linux, you would think that it is infinitely hard.  Most of the stuff I’ve come across recommends that you use partition magic or other similar software, log in under windows and partition the drive, or use some type of cd-based linux distribution such as Knoppix.  While all these options are fine and work for the most part, there is an easier solution in most cases.  Just partition the drive using your current linux distribution.  This articles details how I recently repartitioned a drive that had a bad partition table on my ubuntu box.  But the tools I use are standard on just about every linux distribution so the steps I use here should work on other flavors of linux also.

(*Note this does not detail how to rewrite over a drive.  That requires tools such as dd and shred which will be the subject of another how to.)

The Bad Hard Drive:

I’ve had a hard drive which I couldn’t access for a couple of months because my operating system could read the drive.  Since it is an extra hard drive, I have just been putting off repartitioning it.  But now I want to use it to hold some video footage and photos.  From outward appearances it seems that linux is reading my hard drive okay because I can see in my list of drives.  The human readable name for the bad drive is 80.0 GB Media.

However when I click on the drive to access it, I get an error message stating that my disk can’t be properly mounted.

Error messages are notorious for providing useless and indecipherable information, but this particular error messge gives me some important information.  It tells me how linux refers to the drive.  The device name is /dev/sda5.  So /dev means that it is a device, /sda means that linux is recognizing the drive as a type of scsi drive, and the 5 usually refers to a partition on the drive.  Since I’m not worried about saving any partitions on the drive, the information I’m concerned with is /dev/sda.

Now with this information in hand, I can open a terminal window and type the following command:

sudo fdisk -l /dev/sda

Now fdisk is just a disk partitioning utility that comes with most operating systems. The -l /dev/sda option is just is telling fdisk that I just want it to list information about the drive /dev/sda.  Otherwise it would pull up the information on all my other hard drives as well.  This command yields the following result for my drive:

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×00000000

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               2        9729    78140160    f  W95 Ext’d (LBA)
/dev/sda5               2        9729    78140128+   7  HPFS/NTFS

If this drive was formatted correctly, the above listing, would mean that my sda drive has two partitions.  However the problem with my drive is that I have a Windows 95 fat partition and a NTFS partition that overlap each other.  So I know that I can continue with my plan and repartition the entire drive.  Note you can also reformat individual partitions also.

With all this information on hand, I can now start using fdisk to format my drive.  At the terminal prompt I type the following:

sudo fdisk /dev/sda

I get the following message:

The number of cylinders for this disk is set to 9729.
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):

At the fdisk command prompt I type in p to view the partition table for the /dev/sda drive once more:

Command (m for help): p

This command yields the following verifying the information I found earlier:

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×00000000

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               2        9729    78140160    f  W95 Ext’d (LBA)
/dev/sda5               2        9729    78140128+   7  HPFS/NTFS

Command (m for help):

Now I enter the d command which is for deleting partitions:

Command (m for help): d

After issuing this command, I am asked to enter the partition number:

Partition number (1-5):

Remember the partition number is the that comes after your sd* or hd* drive.  So in my case, sda is my main drive and sda5 denotes a partition on the main drive.  So the partition I want to delete is 5.  I enter the following: (*note I could have also entered 1 because in my case sda1 and sda5 were overlapping each other)

Partition number (1-5): 5

Now I check the partition table to see if the partitions were indeed deleted:

Command (m for help): p

I get the following confirming that no partitions exist on the drive anymore:

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×00000000

Device Boot      Start         End      Blocks   Id  System

Command (m for help):

Now, since my old partitions have been deleted, I need to create a new one in order to use the drive.  You can also create multiple partitions just make sure your start and end blocks for them don’t overlap.  Here I am going to make just one large partition since this is just an extra hard drive. The n command will create a new partition:

Command (m for help): n

Now I have to decide whether to make it an extended partition or a primary partition.  In the case of an extra hard drive, it’s not going to matter a whole lot which you choose.  But basically a primary partition usually just contains one file system such as FAT32, NTFS, or EXT3.  An extended partition is a type a secondary partition and it can be subdivided.  In any case I made a primary partition and labeled it 1:

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

Partition number (1-4): 1

The default is for the partition is for the beginning to be located at the first cylinder of the drive and the end to be last cylinder on the drive. This essentially means that the partition is the entire disk.  I went with the default so all I had to do was press enter:

First cylinder (1-9729, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-9729, default 9729):
Using default value 9729

Now to make sure the partition was created, I issue the p command again and get the following results:

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0×00000000

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        9729    78148161   83  Linux

Everything is cool, so now I want to write this entry in the system partition table so I issue the w command:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Mounting A Drive:

That’s it for the partitioning part, now I need so setup a place to mount the drive.  In ubuntu most drives are in the /media directory so that is were I am going to mount this one also.  I am going to mount mine with the name hivemind so the command looks like this:

sudo mount /dev/sda1 /media/hivemind

Okay, now I have the new partition setup and saved to the partition table, but I don’t have a usable file systems installed on it yet so I can’t use it.

Installing a file system:

Since this drive is only going to be used on my linux box, I’m putting the ext3 file system on it using the following command:

sudo mkfs.ext3 /dev/sda1

This yields the following results:

Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
4890624 inodes, 19537040 blocks
976852 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
597 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

And that is it.  My drive is ready for use.

KJ

Related Articles

Subscribe To HighTechSister.com

1 Comments on this post

Trackbacks

  1. How To: Dual Booting Linux & Windows On Separate Hard Drives Part 2 | HighTechSister.com | High Tech Sister wrote:

    [...] did a whole other how to on disk partitioning with fdisk so there’s no need to discuss it here.  Here’s the resultant output from that [...]

    September 12th, 2008 at 12:41 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog