In this article, I am going to show how to create partitions and how to increase and decrease static partition!
I am doing this practical on RedHat Linux.
First, let’s understand what is partitioning and why we need it?
🔰 Partitioning in Linux 🔰
- The process of dividing a disk into logical areas that can be worked with separately is called partitioning.
- Creating disk partitions enables you to split your hard drive into multiple sections that act independently.
- In Linux, users must structure storage devices (USB and hard drives) before using them.
- Partitioning is also useful when you are installing multiple operating systems on a single machine.
There are several reasons why you want to use partitioning:
- multiple operating systems on the same disk.
- different file systems on different partitions.
- more efficient disk space management.
- different security settings on different partitions.
- easier backup procedure.
Partitions types can be:
- Primary — Holds the operating system files. Only four primary partitions can be created.
- Extended — Special type of partition in which more than the four primary partitions can be created.
- Logical — Partition that has been created inside of an extended partition.
The logical partition is basically same as primary partition. You can create a file system in a logical partition and store files and directories in it. On the other hand, the extended partition is container of logical partitions, so you cannot create any file systems in it.
🔰 Creating Partition 🔰
👉Step-1 : List Existing Partitions
- Run the following command to list all the attached devices.
# fdisk -l
- The output contains information about storage disks and partitions.
👉Step-2 : Select Storage Disk
- Select the storage disk you want to create partitions on by running the following command:
# fdisk <disk-name>
👉Step-3 : Create a New Partition
- Enter ‘n’ command to create a new partition.
- Enter ‘p’ command to create a primary partition.
- Select the partition number by typing the default number ‘1’.
- After that, you are asked for the starting and ending sector of your hard drive.
- For starting sector, it is best to type the default number in this section or press Enter.
- The last prompt is related to the size of the partition. You can choose to have several sectors or to set the size in megabytes or gigabytes. For eg.-Type +5G to set the size of the partition to 5GB.
- A message appears confirming that the partition is created.
- After creating new partition, enter p again to display created partition.
- Don’t forget to press ‘w’ to save the changes made by partition in the disk.
👉Step-4 : Verify the Created Partition
- Verify that the partition is created by running the following command:
- The output shows that 5G partition is created from a device /dev/sdd of 10G.
👉Step-5 : Format the partition
- Once a partition has been created, format it before using it.
- Format the partition by running the following command:
# mkfs.ext4 <device-name>
👉Step-6 : Mount the partition
- To begin interacting with the disk, create a mount point or folder and mount the partition to it.
- Create a mount point or folder by running the following command:
👉Step-7 : Check the Mounted Folder
- Verify that the mounted folder of 5G is created by running the following command:
🔰 Store Data on Partitioned Disk 🔰
- To store data on partitioned disk, we already created a mounted folder.
- So, go inside ‘/static’ folder.
- Enter some data in the file.
🔰 Increasing Partition 🔰
👉Step-1 : Unmount the partition
- First unmount the partition using the following command:
# umount /static
- Then, to confirm that this folder is no longer available for storage run the following command again,
# df -h
👉Step-2 : Delete the partition
- Select the storage disk you want to delete partitions on by running the following command:
# fdisk <disk-name>
- Enter ‘d’ command to delete partition.
- Then follow same steps as before to create a primary partition.
- The last prompt is related to the size of the partition. Enter increased size as before.
- In this case, Typing +8G to increase the size of the partition from 5GB.
- Enter ‘N’ when asked to remove the signature.
- Make sure not to remove the signature when you the increase partition, otherwise you will lose the data stored on partition before.
👉Step-3 : Scan the Partition
- To scan the disk to check if any error or corrupt file exist in partition, run the following command:
# e2fsck -f <disk-name>
👉Step-4 : Reformat the Partition
- To reformat the partition, use the following command:
# resize2fs <disk-name>
👉Step-5 : Mount the Increased Partition
- Now, again mount the partition on same folder as before.
- Also, run ‘df -h’ to check if partition is mounted.
- The output shows that the size of ‘/static’ folder is 8G.
👉Step-6 : Check the Mounted Folder
- After reformatting and re-mounting the partition, check if previous data still exists.
- The output shows that data is preserved even after increasing the partition!
In this article, I have completed the task to create partition on a Linux system. And how to increase that partition without losing the data on that partitioned disk.