24.1.10

How to Format a Hard Drive in Linux

When you format a computer hard drive, you will lose everything that is on the drive!!! Therefore it is very important to back up anything you might later want!!!

Linux refers to hard drives as either "hdx" or "sdx" where x is a letter, starting with a, which represents the order in which the drive was added to or detected by the computer. The "hd" prefix is used for IDE and PAT), and the "sd" prefix is used for SCSI, SATA, and USB drives. Usually a number is also put at the end of "hdx" or "sdx" to denote different partitions on the same phisical drive, but for the purpose of formatting you only need to know which letter the drive you want to format is.

All actions done with root privileges.
You can check which drives are represented in your system. Do following commands:
ls /dev/hd*
   or (depending on which type of drive)
ls /dev/sd*
Simple output: /dev/hda  /dev/hda1  /dev/hda2  /dev/hdb  /dev/hdb1  /dev/hdd
OR...
# fdisk -l
Output:
Disk /dev/hda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        9726    78019672+  8e  Linux LVM

Disk /dev/hdb: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1        4865    39078081   83  Linux
In this examples we can see that the operating system is installed on hda, which has two partitions (hda1 and hda2), and there is one partition on hdb - hdb1.

First you will use the fdisk command to erase any old partitions on the drive and create a new one. Any changes you make using fdisk are only made permanent if you then issue the "w" command before quitting, so feel free to play around a little if you like. If at any time you find yourself stuck, you can quit the program without saving changes byusing Ctrl+C

At the command prompt, type "fdisk /dev/hdb" replacing the "hdb" with the letters for your drive. Upon opening, fdisk may give you a couple of warnings, all of which can be ignored. It then gives you a prompt that looks like this:
# fdisk /dev/hdb

The number of cylinders for this disk is set to 4865.
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):
Enter "p" to see the partition table of the drive. The first line of output from the "p" command will also tell you size of the drive. This is a good way to double check that you are working with the correct drive.
Command (m for help): p

Disk /dev/hdb: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1        4865    39078081   83  Linux
 To delete any existing partitions, press "d" then enter. It will ask you which partition number you wish to delete. The number of the partition is the number that follows hdb, so on our example system we enter 1. If there are multiple partitions repeat the "d" command for each one. You can always view the partition table again with the "p" command.
Once you have deleted all existing partitions on the drive you are ready to make a new one.

From here you may prefer check this article about creating and adding new partition in linux, which is more detailed.

Type "n" and hit enter. Then press "p" to create a primary partition. It asks you for a partition number, enter "1." Now you are asked which cylinder the partition should start at, the beginning of the drive is the default, so just hit Enter. Then you are asked for the last cylinder, the end of the drive is default so you can just press Enter again.
Now you are back at fdisk's command prompt. Use the "p" command to check the partition table. You should now see your new partition at the bottom of the output. In the example it lists "/dev/hdb1."

Now just issue the "w" command to write your new partition table and exit fdisk.

Now you need to create the filesystem on the drive. This is done with the "mkfs" command.
We will make Ext3 filesystem here:
# mkfs -t ext3 /dev/hdb1 
(#mkfs.ext3 -c /dev/hdb1 -- same command as original)
(# mkfs.ext3 -c -L SomeName /dev/hdb1 - Add SomeName label to partition)
mke2fs 1.27 (8-Mar-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2508352 inodes, 5016052 blocks
250802 blocks (5.00%) reserved for the super user
First data block=0
154 block groups
32768 blocks per group, 32768 fragments per group
16288 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

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

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


Finaly done. Now you can mount your new partition somewhere:
#mkdir /home/MountedHDB1
#mount /dev/hdb1 /home/MountedHDB1
Now you have additional free space on your system.
Edited original from here.
  • rss
  • Del.icio.us
  • Digg
  • Twitter
  • StumbleUpon
  • Reddit
  • Share this on Technorati
  • Post this to Myspace
  • Share this on Blinklist
  • Submit this to DesignFloat