Managing Partitions

Harddisk: It is a storage device which is used to store any kind of data, Currently there are two types of HDD available in market:
  • SATA (Serial Advanced Technology Attachment).
  • PATA (Parallel Advanced Technology Attachment).
So the basic command to check your file system structure is fdisk -l, it will show you list of your harddisk's partition table.
For more information on this you can see its man page by firing a command as man fdisk.

Usually when a hard disk is inserted in a machine it acts like a file and is stored in /dev partition.

So suppose that there are 3 hard disk in your machine then there naming convention would be /dev/sda (for the first HDD), /dev/sdb (for the second HDD), /dev/sdc (for the third HDD).

parted: Stands for GNU Parted - a partition manipulation program, it is  a  disk  partitioning  and  partition resizing program.  It allows you to create, destroy, resize, move and copy ext2,  linux-swap,FAT,  FAT32,  and reiserfs partitions.  It can create, resize, and move Macintosh HFS partitions, as well as detect jfs,  ntfs,  ufs,  and  xfs partitions.  It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks.

Now suppose that you want to check free space in your HDD, so for that the simplest method is just firing a command print free in parted prompt. i.e. first go to parted prompt by firing a command as parted then fire print free and if you want to quit from parted just simply fire q. The output will look like:

[root@sunny1]# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free                                                      
Model: ATA HITACHI HTS72323 (scsi)
Disk /dev/sda: 320GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  211MB   210MB   primary  ext4         boot
 2      211MB   320GB   320GB   primary
        320GB   320GB   352kB            Free Space

(parted) q
[root@sunny1]#

Some of the basic terms:
Head: It is used to read and write data on the hard disk using magnetic mechanism.
Sector: It is the smallest unit which is addressable by hard disk.
Track: It is a collection of circular strip of sectors.
Cylinder: It is a collection of more than one track. 
Partitions: There are few advantages and few disadvantages of HDD.
  • Advantages: 
    • Relative data can be stored in specific partitions.
    • it becomes easier to take back-up on another partitions in case of HDD/partition failure.
  • In case of multiple partitions the performance of the HDD is degraded due to multiple read/write operations on different partitions.
  • Partitions are basically of two types:
    • Primary(Max up-to 4, there should be atleast 1 primary partition because /boot resides on it).
    • Secondary.
So in the standard partition type layout there was a demerit suppose that you have a HDD of 100GB and you have created 4 partitions each of 20GB that too primary. In this case as you see that the total used size is 20x4=80GB and still we have 20GB of free space available but you cannot create a partition with that 20GB which is left that will be a wastage of our HDD storage.

But in another case if you have created only 3 primary partitions that too of 20GB each which means that you have allocated 60GB of the HDD. And if you create a Extended partition as the 4th one then you will be able to use rest of the HDD space.

So to over-come this demerit LVM came into existence.

Now let us create a simple partition,

Firstly we need to check free space in a HDD.
[root@sunny3 ~]# parted
GNU Parted 2.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free                                                      
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
        32.3kB  1049kB  1016kB            Free Space
 1      1049kB  2098MB  2097MB  primary   ext4            boot
 2      2098MB  5244MB  3146MB  primary   ext4
 3      5244MB  8390MB  3146MB  primary   ext4
 4      8390MB  21.5GB  13.1GB  extended
        8390MB  8390MB  96.8kB            Free Space
 5      8392MB  10.5GB  2097MB  logical   ext4
 6      10.5GB  12.6GB  2097MB  logical   ext4
 7      12.6GB  14.7GB  2097MB  logical   ext4
 8      14.7GB  16.8GB  2097MB  logical   ext4
 9      16.8GB  18.9GB  2097MB  logical   ext4
10      18.9GB  20.5GB  1573MB  logical   linux-swap(v1)
        20.5GB  21.5GB  1019MB            Free Space

(parted) q                                                               
[root@sunny3 ~]#

In the last line you can see the free space currently it is showing in around 1019MB free space available.

Now to check you HDD naming convention just fire a command as fdisk -l it will show you multiple entries like: 
/dev/vda9           32523       36586     2048000   83  Linux
So from here what we got to know is our HDD is named as /dev/vda.




Now fire a command as fdisk /dev/vda this will bring you to fdisk utility for your HDD.

[root@sunny3 ~]# fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):

Here you use various types of commands like p for printing partition table, n for new partition etc... for whole list of commands which you can fire on this prompt just type in m.

Command (m for help): n
First cylinder (16257-41611, default 16257):
Using default value 16257
Last cylinder, +cylinders or +size{K,M,G} (16257-16260, default 16260):  +47M




==> /dev/vda11          16257       16300        5000+  83  Linux

Command (m for help): w [For writing the changes which you made].
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@sunny3 ~]#

Now if you want then you can reboot your machine so that your kernel knows that you have added a new partition or you can fire a command as partx -a /dev/vda two times.

[root@sunny3 ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition 1
..........
..........
BLKPG: Device or resource busy
error adding partition 10
[root@sunny3 ~]#

For the first time it will show you till partition number 10.

[root@sunny3 ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition 1
..........
..........
BLKPG: Device or resource busy
error adding partition 10
BLKPG: Device or resource busy
error adding partition 11
[root@sunny3 ~]#

When you will run again it will show you partition number 11, this is done mostly in case of servers where we cannot reboot our machine.

Now format the file system.

[root@sunny3 ~]# mkfs.ext4 /dev/vda11
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
248 inodes, 1984 blocks
99 blocks (4.99%) reserved for the super user
First data block=1
Maximum filesystem blocks=2097152
1 block group
8192 blocks per group, 8192 fragments per group
248 inodes per group

Writing inode tables: done                           

Filesystem too small for a journal
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@sunny3 ~]#

Now create a new directory in which you want to mount that partition.

[root@sunny3 ~]# mkdir /NewDrive

Now mount the partition which you made to /NewDrive.

[root@sunny3 ~]# mount /dev/vda11 /NewDrive/





Check the result by firing df -h command.

[root@sunny3 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda2             2.9G  247M  2.5G   9% /
tmpfs                 499M  260K  499M   1% /dev/shm
/dev/vda1             2.0G   55M  1.8G   3% /boot
/dev/vda5             2.0G   35M  1.8G   2% /home
/dev/vda6             2.0G   35M  1.8G   2% /opt
/dev/vda7             2.0G   35M  1.8G   2% /tmp
/dev/vda3             2.9G  2.0G  774M  73% /usr
/dev/vda8             2.0G   36M  1.8G   2% /usr/local
/dev/vda9             2.0G  124M  1.8G   7% /var
/dev/vda11            50.0M   4M  43M   2% /NewDrive
[root@sunny3 ~]#

Now do a cat of mtab file for exact layout so that you can make a entry in fstab file. if in case you didn't make entry in fstab file then when you next reboot your machine then that partition will disappear and you need to mount that again.

[root@sunny3 ~]# cat /etc/mtab
/dev/vda2 / ext4 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs rw,rootcontext="system_u:object_r:tmpfs_t:s0" 0 0
/dev/vda1 /boot ext4 rw 0 0
/dev/vda5 /home ext4 rw 0 0
/dev/vda6 /opt ext4 rw 0 0
/dev/vda7 /tmp ext4 rw 0 0
/dev/vda3 /usr ext4 rw 0 0
/dev/vda8 /usr/local ext4 rw 0 0
/dev/vda9 /var ext4 rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0
gvfs-fuse-daemon /root/.gvfs fuse.gvfs-fuse-daemon rw,nosuid,nodev 0 0
/dev/vda11 /NewDrive ext4 rw 0 0
[root@sunny3 ~]#

The highlighted line is the one which we need to add to fstab file. Add the line which you copied to fstab file at the last by the help you any text editor.

[root@sunny3 ~]# vim /etc/fstab

You have successfully created a partition.

Comments

Post a Comment

Popular posts from this blog

Installing Tomcat8 on RHEL6

Manage existing resources via Helm

Configuration and setting-up of AIDE on RHEL6