Day 5 Linux: Introduction to LVM

Day 5 Linux: Introduction to LVM

Zero To Hero Linux Series

Launch ec2 instance and add two volumes of 2GB at the time of ec2 creation itself.

Login to ec2 instance

sudo lvmdiskscan

Create Physical Volumes

sudo pvcreate /dev/xvdb /dev/xvdc

Display Physical Volumes

sudo pvs

Create Volume Group and Adding the Physical Volumes to it

sudo vgcreate LVMVolGroup /dev/xvdb /dev/xvdc

sudo pvs

sudo vgs

Volume group of ~4GB is created.

Creating Logical Volumes from the Volume Group Pool

sudo lvcreate -L 1G -n projects LVMVolGroup
sudo lvcreate -L 1G -n www LVMVolGroup
sudo lvcreate -L 1.5G -n db LVMVolGroup

You can view the logical volumes and their relationship to the volume group by selecting a custom output from the vgs command:

sudo vgs -o +lv_size,lv_name

In this example, you added the last two columns of the output. It indicates how much space is allocated to your logical volumes.

Now, you can allocate the rest of the space in the volume group to the "workspace" volume using the -l flag, which works in extents. You can also provide a percentage and a unit to better communicate your intentions. In this example, allocate the remaining free space, so you can pass in 100%FREE

sudo lvcreate -l 100%FREE -n workspace LVMVolGroup

sudo vgs -o +lv_size,lv_name

Formatting and Mounting the Logical Volumes

sudo mkfs.ext4 /dev/LVMVolGroup/projects
sudo mkfs.ext4 /dev/LVMVolGroup/www
sudo mkfs.ext4 /dev/LVMVolGroup/db
sudo mkfs.ext4 /dev/LVMVolGroup/workspace

create mount points

sudo mkdir -p /mnt/{projects,www,db,workspace}

Then mount the logical volumes to the appropriate location:

sudo mount /dev/LVMVolGroup/projects /mnt/projects
sudo mount /dev/LVMVolGroup/www /mnt/www
sudo mount /dev/LVMVolGroup/db /mnt/db
sudo mount /dev/LVMVolGroup/workspace /mnt/workspace
sudo mount -a

Permanent mounting

To make the mounts persistent, use your preferred text editor to add them to /etc/fstab file. Open with nano editor

sudo nano /etc/fstab
/dev/LVMVolGroup/projects /mnt/projects ext4 defaults,nofail 0 0
/dev/LVMVolGroup/www /mnt/www ext4 defaults,nofail 0 0
/dev/LVMVolGroup/db /mnt/db ext4 defaults,nofail 0 0
/dev/LVMVolGroup/workspace /mnt/workspace ext4 defaults,nofail 0 0

After editing your file, save and exit. If you’re using nano, press CTRL+c, then y, then ENTER.

The operating system should now mount the LVM logical volumes automatically at boot.

df -Th