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.qcow2virsh start LVM-Tutorial-VMqemu-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 virtioLogin into VM:
virsh console LVM-Tutorial-VMsudo pvcreate /dev/vdb
sudo vgcreate vg_data /dev/vdb
sudo vgdisplay vg_datasudo lvcreate -L 10G -n lv_data vg_data
sudo lvdisplayCreate a new ext4 filesystem on the logical volume /dev/vg_data/lv_data.
sudo mkfs.ext4 /dev/vg_data/lv_datasudo mkdir /data
sudo mount /dev/vg_data/lv_data /dataAdding 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 virtioInside 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_dataqemu-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 virtioNow 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_dataNow 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_dataModify 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 /datavirsh 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 --persistentYou successfully:
- Created a VM
- Attached multiple disks
- Configured LVM from scratch
- Extended LVs live
- Demonstrated LVM snapshot and recovery
Authored by Prashant S.B.










