Day 6: LVM in action

sudo pvscan

Marking the Physical Devices as Physical Volumes:

sudo lvmdiskscan

Mark them as physical volumes within LVM using the pvcreate command:

sudo pvcreate /dev/xvdb /dev/xvdc

Output:

Physical volume "/dev/xvdb" successfully created. Physical volume "/dev/xvdc" successfully created.

sudo pvs

Output:

PV VG Fmt Attr PSize PFree 
/dev/xvdb lvm2 --- 2.00g 2.00g 
/dev/xvdc lvm2 --- 3.00g 3.00g

Adding the Physical Volumes to a Volume Group:

sudo vgcreate LVMVolGroup /dev/xvdb /dev/xvdc

Output: Volume group "LVMVolGroup" successfully created

sudo pvs

Output:

PV VG Fmt Attr PSize PFree 
/dev/xvdb LVMVolGroup lvm2 a-- <2.00g <2.00g 
/dev/xvdc LVMVolGroup lvm2 a-- <3.00g <3.00g
sudo vgs

Output:

VG #PV #LV #SN Attr VSize VFree 
LVMVolGroup 2 0 0 wz--n- 4.99g 4.99g

Creating Logical Volumes from the Volume Group Pool:

sudo lvcreate -L 1G -n projects LVMVolGroup 
sudo lvcreate -L 2G -n www LVMVolGroup 
sudo lvcreate -l 100%FREE -n workspace LVMVolGroup

Output:

Logical volume "projects" created. 
Logical volume "www" created. 
Logical volume "workspace" created.
sudo vgs -o +lv_size,lv_name

Output:

VG #PV #LV #SN Attr VSize VFree LSize LV 
LVMVolGroup 2 3 0 wz--n- 4.99g 0 1.00g projects 
LVMVolGroup 2 3 0 wz--n- 4.99g 0 2.00g www 
LVMVolGroup 2 3 0 wz--n- 4.99g 0 1.99g workspace

Formatting and Mounting the Logical Volumes:

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

After formatting, create mount points:

sudo mkdir -p /mnt/{projects,www,workspace}
ls /mnt
sudo mount /dev/LVMVolGroup/projects /mnt/projects
sudo mount /dev/LVMVolGroup/www /mnt/www
sudo mount /dev/LVMVolGroup/workspace /mnt/workspace
sudo df -Th
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/workspace /mnt/workspace ext4 defaults,nofail 0 0
mount -a
cd /mnt/workspace/
sudo touch file{1..10} 
sudo cp -r * /mnt/www/

Extend volume

# Create and attatch new volume
sudo pvcreate /dev/xvdbz
sudo vgs
sudo vgextend LVMVolGroup /dev/xvdbz
sudo vgs
sudo lvextend -l +100%FREE /dev/LVMVolGroup/projects
# For XFS file System
# sudo xfs_growfs /dev/mapper/LVMVolGroup-projects
sudo resize2fs /dev/LVMVolGroup/projects

Shrink volume

sudo e2fsck -f /dev/LVMVolGroup/projects
sudo resize2fs /dev/LVMVolGroup/projects 2G
sudo lvreduce -L 2G /dev/LVMVolGroup/projects
0
Subscribe to my newsletter

Read articles from SRINIVAS TIRUNAHARI directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SRINIVAS TIRUNAHARI
SRINIVAS TIRUNAHARI