This tutorial walks through a complete LVM demonstration using a KVM virtual machine. It includes:
- Creating a VM
- Attaching new disks
- Creating Physical Volumes (PVs), Volume Groups (VGs), Logical Volumes (LVs)
- Mounting and resizing
- Creating and restoring LVM snapshots
Clone the master VM image:
virt-clone --original <source-vm> --name LVM-Tutorial-VM \
--file <destination-path>/LVM-Tutorial-VM.qcow2
virsh start LVM-Tutorial-VM
qemu-img create -f qcow2 -o preallocation=metadata \
/<path-to-your-disk>/LVM-Tutorial-VM_disk2.qcow2 20G
virsh attach-disk --domain LVM-Tutorial-VM \
--source /<path-to-your-disk>/LVM-Tutorial-VM_disk2.qcow2 \
--target vdb --persistent --targetbus virtio
Login into VM:
virsh console LVM-Tutorial-VM
sudo pvcreate /dev/vdb
sudo vgcreate vg_data /dev/vdb
sudo vgdisplay vg_data
sudo lvcreate -L 10G -n lv_data vg_data
sudo lvdisplay
Create a new ext4 filesystem on the logical volume /dev/vg_data/lv_data.
sudo mkfs.ext4 /dev/vg_data/lv_data
sudo mkdir /data
sudo mount /dev/vg_data/lv_data /data
Adding the UUID entry to the /etc/fstab file ensures that the logical volume is automatically mounted at boot time. Without this, you'd need to manually mount the volume every time the system restarts.
Using the UUID (instead of device paths like /dev/vg_data/lv_data) is a more reliable and persistent method, as device names can change across reboots or if hardware changes occur. The UUID uniquely identifies the filesystem and helps avoid mount failures due to dynamic device naming.
Create and attach a second disk:
qemu-img create -f qcow2 -o preallocation=metadata \
/mnt/qa220250516104235_prashant_test_July_03/LVM-Tutorial-VM_disk3.qcow2 20G
virsh attach-disk --domain LVM-Tutorial-VM \
--source /mnt/qa220250516104235_prashant_test_July_03/LVM-Tutorial-VM_disk3.qcow2 \
--target vdc --persistent --targetbus virtio
Inside VM:
sudo pvcreate /dev/vdc
sudo vgextend vg_data /dev/vdc
sudo lvextend -l +100%FREE /dev/vg_data/lv_data
sudo resize2fs /dev/vg_data/lv_data
qemu-img create -f qcow2 -o preallocation=metadata \
/mnt/qa220250516104235_prashant_test_July_03/LVM-Tutorial-VM_disk4.qcow2 30G
virsh attach-disk --domain LVM-Tutorial-VM \
--source /mnt/qa220250516104235_prashant_test_July_03/LVM-Tutorial-VM_disk4.qcow2 \
--target vdd --persistent --targetbus virtio
Now I have added another disk size of 30G vdd for showcasing the snapshot feature
Inside VM:
sudo pvcreate /dev/vdd
sudo vgextend vg_data /dev/vdd
sudo lvextend -L +15G /dev/vg_data/lv_data
sudo resize2fs /dev/vg_data/lv_data
Now create a file in the /data directory
cd /data
touch demo.txt
echo "Initial content" | tee demo.txt
Create snapshot:
sudo lvcreate -L 10G -s -n lv_data_snap /dev/vg_data/lv_data
Modify data under /data...
Restore from snapshot:
sudo umount /data
# If busy:
sudo fuser -km /data
sudo lvchange -an /dev/vg_data/lv_data
sudo lvconvert --merge /dev/vg_data/lv_data_snap
sudo lvchange -ay /dev/vg_data/lv_data
sudo mount /dev/vg_data/lv_data /data
virsh detach-disk --domain LVM-Tutorial-VM --target vdb --persistent
virsh detach-disk --domain LVM-Tutorial-VM --target vdc --persistent
virsh detach-disk --domain LVM-Tutorial-VM --target vdd --persistent
You successfully:
- Created a VM
- Attached multiple disks
- Configured LVM from scratch
- Extended LVs live
- Demonstrated LVM snapshot and recovery
Authored by Prashant S.B.










