Pages

Tuesday, August 2, 2011

Linux Partition Management

Share it Please

Partitions is an act of dividing one physical hard drive into multiple logical partitions such that each partition acts as a independent disk are continuous block of memory working as an independent disk.
In this article, we will see how we can create and delete partition in the Red Hat Linux and also various commands on how to perform operations on partitions.
Basic Information
It is always better to get basic information on the available physical drive and partitions that are created while installing the linux. In order to get the basic information we can use fdisk command like,
[root@localhost etc]# fdisk

Usage: fdisk [-l] [-b SSZ] [-u] device
E.g.: fdisk /dev/hda  (for the first IDE disk)
  or: fdisk /dev/sdc  (for the third SCSI disk)
  or: fdisk /dev/eda  (for the first PS/2 ESDI drive)
  or: fdisk /dev/rd/c0d0  or: fdisk /dev/ida/c0d0  (for RAID devices)

The command gives the basic information. It says to select a disk for getting more information.We can use fdisk –l
[root@localhost etc]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks        Id  System
/dev/sda1   *           1         382     3068383+    83  Linux
/dev/sda2             383        2294    15358140     83  Linux
/dev/sda3            2295        3569    10241437+  83  Linux
/dev/sda4            3570       60801   459716040   5  Extended
/dev/sda5            3570        4589     8193118+   83  Linux
/dev/sda6            4590        5481     7164958+   83  Linux
/dev/sda7            5482        5736     2048256     82  Linux swap / Solaris

Disk /dev/sdb: 3869 MB, 3869544448 bytes
32 heads, 63 sectors/track, 3748 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes

 Device Boot      Start         End      Blocks          Id  System
/dev/sdb1   *           1        3748     3777952+   b  W95 FAT32

The command gives complete information about the type of disk available and partitions that are already created while installing linux.
We can use once more command called parted like
[root@localhost ~]# parted -l

Model: ATA ST3500413AS (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
 1      32.3kB  3142MB  3142MB  primary   ext3         boot
 2      3142MB  18.9GB  15.7GB  primary   ext3             
 3      18.9GB  29.4GB  10.5GB  primary   ext3             
 4      29.4GB  500GB   471GB   extended                   
 5      29.4GB  37.7GB  8390MB  logical   ext3             
 6      37.7GB  45.1GB  7337MB  logical   ext3             
 7      45.1GB  47.2GB  2097MB  logical   linux-swap       
 8      47.2GB  52.2GB  5009MB  logical   ext3

This command gives us the complete information about the type of disk available in the system and the partition done while installing the linux. The command also gives information about the USB drives if connected like,
Model: Kingston DT Dell Lite G1 (scsi)
Disk /dev/sdb: 3870MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End           Size     Type       File system  Flags
 1         32.3kB  3869MB  3869MB  primary  fat32           boot

Create Partation

Once we have the basic information about the disk and partitions, now its time to create a partition. For this we can use
fdisk /dev/sda like

[root@localhost etc]# fdisk /dev/sda

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

Once we enter the command \mode, it goes into a command mode which asks for the command to enter.
Enter “m” to get the list of available commands like
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)


Enter “P’ to see the partition table like,

Command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         382     3068383+  83  Linux
/dev/sda2             383        2294    15358140   83  Linux
/dev/sda3            2295        3569    10241437+  83  Linux
/dev/sda4            3570       60801   459716040    5  Extended
/dev/sda5            3570        4589     8193118+  83  Linux
/dev/sda6            4590        5481     7164958+  83  Linux
/dev/sda7            5482        5736     2048256   82  Linux swap / Solaris

Enter “q” to quit without saving changes.
Now lets see how can create a new partition.Enter “n” to create a new parathion like,
Command (m for help): n
First cylinder (5737-60801, default 5737):
Using default value 5737
Last cylinder or +size or +sizeM or +sizeK (5737-60801, default 60801): +5GB

It asks for the first Cylinder information, just press “enter”. it takes the default value. For last Cylinder information we can give the size of the partition like +5GB which gives 5GB to the new parathion.

Once we enter this information,we need to enter the “w” command to save changes like,

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Once syncing of disks is done, we come out of the command mode and new partition is created.

Partprobe: once the new partition is created,we need to issue a “partprobe” command.The kernel of the Operating system reads the infomarion about the parathion only while it is starting,but when the new partition is created,we need to inform the kernel to re-read the partition table for getting information about the new partations created and this is done by using the “Partprobe” command.

[root@localhost etc]# partprobe (returns nothing)
So now we have the partition, just get the information about the new one using fdisk –l,

[root@localhost etc]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks           Id  System
/dev/sda1   *           1         382       3068383+     83  Linux
/dev/sda2             383        2294     15358140      83  Linux
/dev/sda3            2295        3569    10241437+   83  Linux
/dev/sda4            3570       60801   459716040    5  Extended
/dev/sda5            3570        4589    8193118+      83  Linux
/dev/sda6            4590        5481    7164958+      83  Linux
/dev/sda7            5482        5736    2048256        82  Linux swap / Solaris
/dev/sda8            5737        6345    4891761        83  Linux

So we are aware the new partition created is /dev/sda8.

Format

Once we got the information about the newly created partition, its time to format the partition. We can format using mkfs command like,

[root@localhost etc]# mkfs.ext3 /dev/sda8
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
611648 inodes, 1222940 blocks
61147 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1254096896
38 block groups
32768 blocks per group, 32768 fragments per group
16096 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

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

It writes the inode information, create journal and other information.

MOUNT

Once we format the partition, we need to mount the partition.

Mount command says to the operating system that a file system (new Partition) is available and is associated with particular point in the file system.

Umount command says that the file system needs to be disassociated from the file system.

Lets see what are the available mount point available using “mount” command like,

[root@localhost etc]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda5 on /home type ext3 (rw)
/dev/sda6 on /var type ext3 (rw)
/dev/sda3 on /usr type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /media/KINGSTON type vfat (rw,noexec,nosuid,nodev,shortname=winnt,uid=0)

We can see that the new partition is not yet mounted.

Now create a directory disk1 in root like

[root@localhost etc]# mkdir /disk1

Mount the new partition using

[root@localhost etc]# mount /dev/sda8 /disk1

[root@localhost etc]# mount
.
.
.
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /media/KINGSTON type vfat (rw,noexec,nosuid,nodev,shortname=winnt,uid=0)
/dev/sda8 on /disk1 type ext3 (rw)

Now we are sure that the new partition is mounted on disk1.

Now we have mounted the file system, we need to make changes to a file /etc/fstab.This file contains information on file systems that are to be mounted while booting the system. We need to add the line at the bottom like

/dev/sdb8           /disk          ext3        defaults     0   0

Let’s see what each column says

/dev/sdb8: the partition name
/disk1: mount point on file system
Ext3: file system type
Defaults: mount options
0 | 1: zero
0 | 1: ‘1” makes use of “fsck” command to check for errors on the mount point while booting and “O” disable checking.

Now once we added the line,we can check the disk usage using “df” command like,

[root@localhost etc]# df -hT
Filesystem    Type    Size  Used      Avail   Use% Mounted on
/dev/sda2     ext3     15G  3.2G     11G    24%    /
/dev/sda5     ext3    7.6G  146M     7.1G   2%      /home
/dev/sda6     ext3    6.7G  178M      6.2G   3%     /var
/dev/sda3     ext3    9.5G  1.7G     7.3G  19%     /usr
/dev/sda1     ext3    2.9G   75M     2.7G   3%      /boot
tmpfs           tmpfs 998M  0         998M   0%     /dev/shm
/dev/sdb1     vfat    3.6G    3.1G    596M  84%    /media/KINGSTON
/dev/sda8   ext3  4.6G   138M   4.3G   4% /disk1


File System Check

The newly created file system can be checked using the “fsck” command like ,

[root@localhost ~]# fsck /dev/sda8
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
/dev/sda8 is mounted. 

WARNING!!!  Running e2fsck on a mounted filesystem may cause
SEVERE filesystem damage.

Do you really want to continue (y/n)? yes

/dev/sda8: recovering journal
/dev/sda8: clean, 11/611648 files, 54398/1222940 blocks

Partition Information

If we need to get the information about the partition, we can use “blkid” command like,

[root@localhost ~]# blkid /dev/sda8
/dev/sda8: UUID="49f12e36-55c4-4c32-8d56-676c874c8ee7" SEC_TYPE="ext2" TYPE="ext3"

Delete Partition

For deleting the partition , we can use the same fdisk command as,

[root@localhost ~]# fdisk /dev/sda

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

Enter “d” for deleting the parathion,

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

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

Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

And finally execute the partprobe command.

By this, we complete how we can create and delete partition.

More articles to come .Happy CodingJ

No comments :

Post a Comment