Partition In Linux with examples

Hello, everyone In this Article we are going to learn what is the Partition management in Linux n a very simple and easy step-by-step approach.

Yellow Fishing Boat Blog Banner 90

Partition in linux

1. How to check what are the disk attached in server with detail

To check how many disks are attached to server ten we can execute fdisk command with (-l ) option.

# fdisk -l

When we see there is ( * ) in the partition then it means that Our operating system is installed on that partition.

Screenshot 2 8

I have 5 Gb Of extra harddisk space on ( /dev/xvdf ) partition in my server Soo I’m using this space to do my practical.

In a hard disk, we cannot create more than 4 partitions in the hard disk. When we buy the harddisk there is a program called MBR ( master boot recorder ) is running in this program it is written that we can make 4 partitions in the hard disk.

These 4 Partition is either primary or extended.

How to check the number of disks attached to server with a mounting point

To check the number of disks attached to server with a mounting point we can execute lsblk command

# lsblk
Screenshot 3 9

How to create a partition in Linux

The fdisk is the command that is used to go inside the hard disk /dev/harddisk name where we want to go. All hard disks go inside /dev.

# fdisk /dev/xvdf
Screenshot 4 9

Now we are inside of the hard disk we can perform various operation on a harddisk

  • m – m option is use get the help how to create the Partition
Screenshot 5 9

n – n option is use to create new Partition in a hardisk.

Screenshot 6 8

We have 2 options that we want to create primary or extended Partition by default we don’t select anything then the primary partition will create Best practice is that make 3 primary then 4th will be extended than in extended make a logical partition.

  • Now it will ask to assign the sectors in the hard disk sector from 1-2048 will be allocated to the MBR program and we get to rest the sectors.
  • Now it will ask to assign the last value of sector here we are defining +1G this means that we can get the 1Gb as partition from the 5Gb of the hard disk.
Screenshot 7 6

p – press p to see the Partition that we have created.

Screenshot 8 6

Now we are making 1 more Partition with the same above method

  • n – Press n to make the new Partition
  • Now we have 3 free partition soo we are making this partition as primary partition again
  • Now we are making this Partition of the size of 2GB
Screenshot 9 6

Now we have 2 free partition soo we are making this partition as primary partition again

Now we are making 1 more partition with the same above method

  • n – Press n to make the new partition
  • Now we are making this partition of the size of 500 MB
Screenshot 10 6

Now only 1 partition is free either we can make the primary or extended partition.

If we make the primary partition then we cannot able to create more partitions so we need to go with the extended partition now.

Linux kernel know that we created 3 primary partitions now automatically Linux kernel is making 4th partition as extended one

Screenshot 11 6

p – Press p to view the partition that we created

Screenshot 12 6

Now when we create any new partition that will be created as a logical partition inside an extended partition.

  • n – Press n to make the new partition this will create an extended partition
  • Now we are making this extended partition of the size of 500 MB
Screenshot 13 4

Now when we create a new partition that will be created as a logical partition inside an extended partition.

  • n – Press n to make the new partition this will create as an extended partition
  • Now we are making this extended partition of the size of 700MB
Screenshot 14 2
  • p – Press p to view the partition that we created.
Screenshot 15 3

To save and quit press w

Screenshot 16 3

Now we have to update to kernel that we created a partition .

The partprobe is the command that is used to update all the partitions inside the server. with the disk name

# partprobe /dev/xvdf
Screenshot 17 3

#lsblk – Now when we run this command all partition will display

Screenshot 18 2

Before adding the data inside the disk we have to format the disk First then only we will able to add the data to a disk.

How to check what partition is formated

With the help of the blkid command we can see what partition is formated and which partition is not formatted

In this we can see the list of the partitions which are not formatted.

Screenshot 19 2

How to format the partition

We can format this partition with the mkfs command and we are formatting the partition in ext2 format.

# mkfs -t ext2 /dev/xvda1
Screenshot 20

When we again execute blkid command we can see that our partition is now formatted.

Screenshot 21

Now we have to mount the partition in a directory in order to save the data inside of a directory. We have created a directory as /Directorywe want our partition to mount on this directory.

How to mount the partition to directory

With the help of mount command with partition name and location of the directory where we want to mount we can able to mount our partition.

# mount /dev/xvda1 /Directory
Screenshot 23

How to check what partition is mounted to which directory

With the help of the df -h command, we can see that where our partition is mounted on which folders.

This check how many Disk free and show results in human-readable form

Screenshot 24

We can see that /dev/xvda1 is mounted on /Directory

When we create anything inside the /Directory the content will go inside the mount point, not in a Directory

This is the temporary mount when we reboot the server out mount point is removed. To make the mount point persistent we have to do entry in

# vi /etc/fstab 
Screenshot 25

Now run #mount -a command to update everything in a fstab file.

Screenshot 26

Now after reboot our partition will be there it will be permanenet mounted

How to mount using UUID. This UUID is the unique Id to every partition

We can also mount the filesystem using UUID

Again run # blkid command to get UUID for the partition

Screenshot 33

Here we are mounting /dev/xvdf1 portion using UUID

In company, we will mount the partition using UUID only soo that after formating the partition it will not effect

1st format the partition /xvdf1 using mkfs command shown above

Screenshot 29

Now we are adding an entry in /etc/fstab file of the UUID

This will mount the partion using th UUID

Screenshot 31

Run # mount -a command again too update all the partition .

Screenshot 32

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

If you want to read man page for the fdisk command then click on this link

If you want to read man page for the mkfs command then click on this link

This is the complete knowledge of partition management in the Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment