How to Create/Delete/Update/Format Disk Partitioning in Linux

0
53
How-to-Create-Delete-Update-Format-Disk-Partitioning-in-Linux

Partitioning is very important task and regular task for the Linux / Hadoop admins. Usually when we start doing the build activity when come to work with disks first tasks is to create the disks and partitions.

What is Partitioning?

In simple terms, process of dividing a single hard drive into many logical drives. In process terms, A partition is a continuous set of blocks on a drive that are treated as an independent disk.

Disk Partitioning:

1. MBR Disk partitioning
2. GPT DIsk Partitioning

MBR Disk Partitioning

MBR – Master Boot Record

Each disk has 4 Partitions. (3 Primary + 1 Extended).
Primary partition is a partition which usually holds the operating system.
Extended partition is subdivided into multiple logical partitions. There can be only one extended partition per disk.
From extended partition we can create upto 15 partitions.

Usually it locates at where the OS is installed. It contains two important utilities.
1. IPL (Initial Program Loader)
2. PTI (Partition Table Information).

IPL is responsible for booting the OS. because it contains boot loader.
– RHEL 4 the default boot loader is LILO (Linux Loader).
– RHEL 5 onwards boot loader is Grub (Grand Unified Boot Lodader).
– RHEL 7 onwards boot loader is GRub2.

The PTI (Partition Table Information) is the information about the no. of partitions on the disk, sizes of the partition and types of partitions.

GPT Disk Partitioning

It supports up to 128 partitions per disk.

Disk Identification

IDE drives will be shown as – /dev/hd
SCSI/SATA drives shown as – /dev/sd
Virtual drives shown – /dev/vd

Types of File Systems

The supported file systems are ext2, ext3, ext4, vfat and xfs in RHEL 6 and 7.
vfs – The file system is used to maintain a common storage between linux and windows.
ext4/xfs – Widely used file system.

S. NoExt2Ext3Ext4xfs
1Second Extended FSThird Extended FSFourth Extended FSXtents FS
21993200120081993
3Does not have journaling FeatureSupports Journaling Feature*Supports Journaling Feature*Supports Journaling Feature*
4Max. File size can be 16GB to 2TBMax. File size can be 16GB to 2TBMax. File size can be 16GB to 16TBMax. File size can be 16TB to 8EB
5Max. ext2 FS size can be from 2TB to 32TB.Max. ext3 FS size can be from 2TB to 32TB.Max. ext4 FS size can be from 1EB.Max. xfs file system size is 16EB. 1EB = 1024PB
6Cannot convert ext FS To ext2Convert from ext2 to ext3All ext FS can be converted to ext4NA

Journaling: It will keep track of changes which are not committed to FS. This can be used to reconstruct if incase of any corruptions which occur due to the system crash or power failures.

How to create a partition?

To see the existing partitions

In linux we have two commands to see the existing partitions.
1. fdisk -l
2. parted -l

fdisk – It will display the cylinders information

To see the existing partitions

Physical disks info in the server:

Physical disks info in the server:

Physical and Logical disks info in the server:

Physical and Logical disks info in the server:

parted – It will show the disk type information.

parted - It will show the disk type information.

To enter into the disk utility, the syntax is fdisk <disk name>
ex: fdisk /dev/sda

OptionUse
mhelp
lList the partitions
nCreate a new partition
pPrint the partition table
dDelete a partition
vVerify the partition table
wWrite table to disk and exit
qQuit without saving changes

Print partition table

Print partition table

Creating the partition:

Creating the partition:

Verify the partition:

Verify the partition:

Check remaining disk space in disk:

Check remaining disk space in disk:

Saving the partition table to disk:

Saving the partition table to disk:

Delete a partition in disk:

Delete a partition in disk:

Updating the partition table

By default, partition table is updated after the restart. We can use partprobe or partx command to update the partition information with out restarting the system.

Updating the partition table

Even though it is deleted still it is showing in partition table. Now check with partprobe or partx command to update the partition table.
1. partx -a <disk name> # When added the new partition.
2. partx -d <disk name> # When deleted the partition.
3. kpartx <disk name> # It will update automatically irrespective of addition /deletion of partition.
4. partprobe <disk name> # It will update automatically.

Updating the partition table /proc/partitions

Formatting the Partition

After creating a partition we need to assign some file system to it. so that we can start storing the data into it.
To format a partition the following syntax is used.
mkfs.<file system type> <partition name>
ex: mkfs.ext4 /dev/sdd2

Formatting the Partition
Note: Even after formatting the partition, we cannot add the data into the partition. In order to add the data in the partition it is required to be mounted.

Mounting

It is a method of attaching a directory to the file system for accessing the partitions.
/etc/mtab – It is a file which stores the information of all the currently mounted file systems.
/etc/fstab – It is a file which keeps information about the permanent mount points.

Mounting is a procedure where we attach a directory to the file system. There are two types of mounting which will be used in Linux.
1. Temporary Partition
2. Permanent Partition

Temporary Partition

In a temporary mount point we will create a directory and mount it, but this mount point will last only till the system is up, once it is rebooted the mounting will be lost.

mount <partition name> <mount point>
mount /dev/sdd2 /mnt_sdd2

i. First create a directory
ii. Second mount the directory

Temporary Partition

To unmount a partition
umount <mount point directory>

Permanent Mount

Permanent mounting procedure is exactly same like temp mounting, but here we will update the /etc/fstab file with mounting details.
i. create a mount directory.

Permanent Mount

ii. Get the block id of a disk or disk name directory. but the recommended one is blkid.

Permanent Mount

iii. Add the entry to the /etc/fstab.

Add the entry to the /etc/fstab.
Device name/Device IDMount pointType of File systemMount OptionsDumpingCheck Sequence

Use mount -a command to check whether it is mounted or not.

mount -a

Troubleshooting

Sometimes a directory reflects error while unmounting, the possible causes for it are.
1. Check pwd before going further, you may be in the mount directory and trying to unmount it. It wont happen.
2. Some users are still present on the server and using the contents in it.
3. Check with fuser -cu <mount point> or lsof <mount point>

unmount
lsof mount

4. Kill the processes using fuser -ck <mount point>.

LEAVE A REPLY

Please enter your comment!
Please enter your name here