Disk Partitioning & Managing Partitions in linux

When we format a computer hard drive, we will lose everything that is on the drive. Therefore, it is very important to back up anything you might later want.To format a secondary drive, we need root access. Linux allows only 4 primary partitions.

on an IDE drive, the first drive is called hda, and the partitions are shown as hda1, hda2 . . . . etc. etc. Your second drive is called hdb.On an IDE drive you can have up to 63 partitions, 3 primary and 60 logical ( contained in one extended partition )

On a SCSI drive, the first drive is called sda, the partitions are sda1, sda2 . . The second drive is called sdb.On an IDE drive you can have up to 63 partitions, 3 primary and 60 logical ( contained in one extended partition )

An extended partition is the only kind of partition that can have multiple partitions inside. Think of it like a box that contains other boxes, the logical partitions. The extended partition can't store anything, it's just a holder for logical partitions.


The extended partitions is a way to get around the fact you can only have four primary partitions on a drive. You can put lots of logical partitions inside it.


We can see all the drives attached to your system by typing the command "ls /dev/hd*" or "ls /dev/sd*", depending on which type (IDE, SATA and so forth) the drives are. On the example system, the result of this command looks like "/dev/hda /dev/hda1 /dev/hda2 /dev/hdb /dev/hdb1". The operating system is installed on hda, which has two partitions (hda1 and hda2), and there is one partition on hdb and hdb1.

Steps for Creating partition using fdisk command : -

Step:1 To list available drives on you machine type:

# fdisk -l

Choose the drive you want to make changes to and engage it using fdisk:
# fdisk /dev/sdc
replace the "sdc" with the drive you want to edit.

The basic fdisk commands you need are:
  • m - print help
  • p - print the partition table
  • n - create a new partition
  • d - delete a partition
  • q - quit without saving changes

w - write the new partition table and exit

Step:2 Enter "p" to see the partition table of the drive. The first line of output from the "p" command will also tell you the size of the drive. This is a good way to double-check that you are working with the correct drive.

Step:3 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 the default, so you can just press "Enter" again.

Step:4 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.


Step:5 Now we need to set the filesystem type for your new partition with the "t" command. We are asked for the Hex code of the filesystem you wish to use. We will use the standard Linux ext2 filesystem, which is "83." If you are doing something special and know of a particular filesystem that you need to use, you can press "L" to see all the codes, which are one or two characters made up of the numbers 0 to 9 and the letters a to f.

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

Step:7 # partprobe (command used to force the kernel to re-read the new partition table)

To Delete Partitions using fdisk command

>

Let us as suppose that we want to remove a partition from /dev/hdb disk. Type the following command:

Step:1 # fdisk /dev/hdb

Now type p command to list partition:
Command (m for help): p


Step:2 Now let us say you want to delete /dev/hdb3 (3rd partition). Type the d command to delete a partition:


Command (m for help): d
Partition number (1-4): 3

Step:3 It will prompt you for the partition number. Type 3:
Verify that partition deleted:
Command (m for help): p
Now save the changes and exit to shell prompt. Type the w command:
Command (m for help): w
Reboot the system OR run partprobe command

Formatting the Partitions

To use the partition we need to format the partitions using the different filesystem. We can format the partitions using either mkfs or mke2fs command.

# mkfs.ext2 /dev/sdb1 (ext2 filesystem)
# mkfs.ext3 /dev/sdb1 (ext3 filesystem)
# mkfs.ext4 /dev/sdb1 (ext4 filesystem)
# mkfs.vfat /dev/sdb1 (DOS filesystem)

Mount the new disk using mount command
First create a mount point /data and use mount command to mount /dev/sdb1, enter:

# mkdir /data
# mount /dev/sdb1 /data
# df -H

Update /etc/fstab file

Open /etc/fstab file, enter:

# vi /etc/fstab

Append as follows:

/dev/sdb1 /data ext3 defaults 1 2

Save and close the file.


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post