LVM Configuration Guide - Linux
We all might be familiar with the term LVM i.e. Logical Volume Mapping. In Linux, this is a device mapper framework that provides the ability to logically manage the volumes for the Linux Kernel. The main intent of doing so, to dynamically resized the volumes as per the requirements on go. This is basically a layer of abstraction between the physical drives and you. One can expand and shrink the volumes on fly.
Let's see below step by step how one can achieve that
Scenario : 1 Root device, 2 External devices are attached to the ubuntu machine. Using all 3, will apply the LVM logic and see how it can be configured. Below Physical Hard drive could be anything like USB etc.
- Root Device
- External Device 1 i.e. Physical Hard drive
- External Device 2 i.e. Physical Hard drive
Now, if I give you a pictorial view how the logic works, this configures in 3 steps
- Looking physical Hard drives
- Create Physical Volume
- Create Volume Group
- Create Logical Volume
Login to your machine and open the terminal. Initially, you should know how many devices my machine has. This can be achieved with the command
- Looking for Physical Hard Drives
df -h The output would be as:
/dev/root 7.7G 2.8G 4.9G 37% / devtmpfs 486M 0 486M 0% /dev tmpfs 490M 0 490M 0% /dev/shm tmpfs 98M 812K 98M 1% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 490M 0 490M 0% /sys/fs/cgroup /dev/loop1 97M 97M 0 100% /snap/core/9804 /dev/loop0 29M 29M 0 100% /snap/amazon-ssm-agent/2012 /dev/loop2 98M 98M 0 100% /snap/core/9993 /dev/loop3 56M 56M 0 100% /snap/core18/1885 /dev/loop4 71M 71M 0 100% /snap/lxd/16922 tmpfs 98M 0 98M 0% /run/user/1000
This shows, we've /dev/root device having ~8GB of size. Currently it is not showing the 2 External Physical drives which we've attached
2. Now we need to create the Physical Volume with the commands, but before that we need to check and need to know the names of the External Drives. This can be achieved with the diskscan command - which basically tells what other external drives you've with the machine
lvmdiskscan
/dev/loop0 [ <28.09 MiB] /dev/loop1 [ 96.62 MiB] /dev/xvda1 [ <8.00 GiB] /dev/loop2 [ 97.06 MiB] /dev/loop3 [ 55.32 MiB] /dev/loop4 [ 70.56 MiB] /dev/xvdf [ 5.00 GiB] /dev/xvdg [ 3.00 GiB] 2 disks 6 partitions 0 LVM physical volume whole disks 0 LVM physical volumes
Now, you can see you've /dev/xvdf and /dev/xvdg External Drives and it is clearly showing 0 LVM physical volume whole disks
3. Now, as per the step we will create Physical Volume as per below
sudo pvcreate /dev/xvdf //successfully created sudo pvcreate /dev/xvdg //successfully created To verify the Physical Volumes, check with command sudo pvs
/dev/xvdf lvm2 --- 5.00g 5.00g /dev/xvdg lvm2 --- 3.00g 3.00g
The above shows that Physical Volume create
Now, we need to create Volume Group and to take both these external drives into that
sudo vgcreate mygroup /dev/xvdf /dev/xvdg
Volume group "mygroup" successfully created
//Successfully created ***Verify with the command*** sudo vgs
VG #PV #LV #SN Attr VSize VFree mygroup 2 0 0 wz--n- 7.99g 7.99g
Now, create the Logical Volumes
Please note: I've 2 External HDD of 5GB and 3GB. I am creating Logical volume of 4GB first, then 3GB and leftover will be as separate. So, ideally it will be 3 Logical volumes
//Creation of First Logical Volume sudo lvcreate -L 4GB -n logical1 mygroup //Creation of Second Logical volume sudo lvcreate -L 3GB -n logical2 mygroup //For Left-over space sudo lvcreate -l 100%FREE -n logical-final mygroup ***Results can be seen***
VG #PV #LV #SN Attr VSize VFree LSize LV mygroup 2 3 0 wz--n- 7.99g 0 4.00g logical1 mygroup 2 3 0 wz--n- 7.99g 0 3.00g logical2 mygroup 2 3 0 wz--n- 7.99g 0 1016.00m logical-final