To partition a Physical Drive attach to the machine we have to follow three steps:
- We need unallocated space from the physical hard drive and create a partition from it.
- Formatting the partition.
- Mounting it to a folder.
Step 1:
To see the list of total physical disks available:
Command: # fdisk -l
Here I have attached two hard drives: /dev/sbd and /dev/sdc respectively as you can see in the figure above. Also you can verify it with this command:
Command:
# lsblk
So let’s select /dev/sdc to be partitioned. To do that we have to use:
Command:
# fdisk /dev/sdc
To see all the functions available and help press “m”. To create a new partition press “n”.
There are two types of Partition: Normal and Extended.
To choose normal press “p”. And then select a number of partitions you want to create. Range= 1 — 4.
Now select the size for the partition. Note it is in sectors. So 1 Sector = 512 bytes. So we will select 19GB i.e. approx 40,000,000 sectors, It takes a range: So for starting lets take default i.e 2048 and press enter. and the ending sector will be: 40,002,048. And press “w” to save.
As we can see new /dev/sdc1 partition is created with size 19.1GiB.
After this we have to load the driver to complete the step 1.
Command:
# udevadm settle
Step 2:
Formatting the partition created with required format type. To see the list of partition type “mkfs” in terminal and press tab. But in Linux we commonly use .ext4 format type.
Command:
# mkfs.ext4 /dev/sdc1
Step 3:
Now the last step is to mount it to a folder so as to use it.
Command:
# mkdir /myNewPartition
# mount /dev/sdc1 /myNewPartition
# df -h
So our partition is now successfully created.
To Increase or Decrease the size of the partition created.
Step 1:
Unmount the partition.
Command:# unmount /myNewPartition
Step 2:
Clean the Inode Table.
Command:# e2fsck -f /dev/sdc1
Step 3:
Reformat the partition with mentioning total size partition should have.
To increase from 20GB till 30GB:
Command:# resize2fs /dev/sdc1 30G
To decrease from 30GB to 10GB:
Command:# resize2fs /dev/sdc1 10G
And then mount it back to the folder.
Logical Volume Group (LVM):
We can say LVM is the brings out more feature as compared with normal partitioning. In normal partitioning there was a constraint that if you cannot create a partition with more size than that of the physical hard drive it use. To solve this LVM came up with a concept with which we can logically combine the space of two physical device and use them to create a new partition with their combined increased size.
To create a LVM we have four steps:
- Creating Physical Volume (PV’s).
- Creating a Volume Group (VG’s).
- Creating a Logical Volume (LV’s).
- Formatting and Mounting.
Step 1:
Converting physical hard drives into Physical Volume.
Command:# pvcreate /dev/sdc
# pvcreate /dev/sdd
# pvdisplay
Step 2:
Creating a Group Volume with name vgnewgroup.
Command:# vgcreate vgnewgroup /dev/sdc /dev/sdd
# vgdisplay vgnewgroup
Step 3:
Creating a Logical Volume with name lvnew and size 50GB.
Command:# lvcreate --size 50G --name lvnew vgnewgroup
# lvdisplay vgnewgroup/lvnew
Step 4:
Formatting and Mounting.
Command:# mkfs.ext4 /dev/vgnewgroup/lvnew
# mkdir /lvfolder
# mount /dev/vgnewgroup/lvnew /lvfolder
Python Script for the same:
import oswhile(1):
os.system("fdisk -l") disks = input("Enter disks with space in between: ").split(" ")
for disk in disks:
os.system(f"pvcreate {disk}")
os.system(f"pvdisplay") print() vgName= input("Enter Name of the Volume Group: ")
os.system(f"vgcreate {vgName} {disks}")
os.system(f"vgdisplay {vgName}")
print()
lvName = input("Name of the LVM: ")
size = input("Enter the size: ")
os.system(f"lvcreate --size {size} --name {lvName} {vgName}")
os.system(f"lvdisplay {vgName}/{lvName}") print() mountPoint=input("Create a name for Folder to mount: ")
os.system(f"mkdir /{mountPoint}")
os.system(f"mkfs.ext4 /dev/{vgName}/{lvName}")
os.system(f"mount /dev/{vgName}/{lvName} {mountPoint}") print()
print("Done with LVM Configuration")