Wednesday, July 23, 2014

Ubuntu - How to Format Hard Drive

If you just intalled a new hard drive and try to format it, or you have a hard drive that is not mounted or formated, you can format the hard drive by following steps below:

Assume it is /dev/sdb you want to format.

Make sure the hard drive is not mounted:
# mount | grep sdb

Invoke fdisk on /dev/sdb:
# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xfc819528.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):

Make sure you delete all the partions:
Command (m for help): d
No partition is defined yet!

Make new partitions:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-976773167, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-976773167, default 976773167):
Using default value 976773167

Note: Unless you want more than four partitions, go for a primary. The mbr partition table limits you to four primary partitions. An extended partition is a special type of primary which is used only as a placeholder for a number of logical partitions so that you can exceed the 4 limitation. If you create an extended partition, you will have to create one or more logical partitions before you can store data. You don't store data in an extended partition.

Write the new partion and exit:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Formatting the new partiton (I am using ext4 format):
# mkfs.ext4 /dev/sdb1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
30531584 inodes, 122096390 blocks
6104819 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
3727 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, 20480000, 23887872, 71663616, 78675968,
 102400000

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

Note: We use sdb1 instead of sdb because we are making the filesystem in that partion.

Mount the new file system:
# mkdir /data1
# mount /dev/sdb1 /data1

Mount at boot time:
Add the following line into /etc/fstab
d45d1073-0e70-498b-8cf1-b0e87420b947 /dev/sdb1     /data1                                      ext4    defaults 0     0

You can use blkid command to find UUID:
# blkid
/dev/sdb1: UUID="d45d1073-0e70-498b-8cf1-b0e87420b947" TYPE="ext4"

No comments: