Managing LVM Logical Volumes and Snapshots on Linux
Understanding and Managing LVM Components
LVM (Logical Volume Manager) introduces an abstraction layer over physical storage, enabling flexible volume management.
- Physical Volume (PV): A block device—such as a disk partition or RAID array—formatted for use by LVM.
- Volume Group (VG): A pool of storage created by combining one or more PVs. The VG is subdivided into fixed-size units called Physical Extents (PEs).
- Logical Volume (LV): A virtual partition carved from the VG’s PEs. LVs can be formattted with a filesystem and mounted like regular partitions. Their size can be resized dynamically without data loss, provided sufficient space exists in the VG.
- Physical Extent (PE): The smallest allocatable unit within a VG. All LVs are composed of PEs.
Key Management Commands
Physical Volume Tools
pvs: Brief PV summarypvdisplay: Detailed PV infopvcreate /dev/device: Initialize a device as a PV
Volume Group Tools
vgs: Brief VG summaryvgdisplay: Detailed VG infovgcreate [-s PE_size] vg_name /dev/device...: Create a VG (default PE = 4 MiB)vgextend vg_name /dev/device: Add a PV to a VGpvmove /dev/device_to_remove: Migrate data off a PV before removalvgreduce vg_name /dev/device: Remove a PV from a VGvgremove vg_name: Delete a VG
Logical Volume Tools
lvs: Brief LV summarylvdisplay: Detailed LV info (path:/dev/vg_name/lv_name)lvcreate -L size -n lv_name vg_name: Create an LVlvremove /dev/vg_name/lv_name: Delete an LV
Resizing Logical Volumes
Extending an LV (ext4 example):
umount /dev/vg_name/lv_name
lvextend -L +1G /dev/vg_name/lv_name
resize2fs /dev/vg_name/lv_name
mount /dev/vg_name/lv_name /mount/point
Shrinking an LV (ext4 only; insure data fits in new size):
umount /dev/vg_name/lv_name
e2fsck -f /dev/vg_name/lv_name
resize2fs /dev/vg_name/lv_name 2G
lvreduce -L 2G /dev/vg_name/lv_name
mount /dev/vg_name/lv_name /mount/point
Note:
resize2fsworks only with ext2/3/4. For XFS, usexfs_growfs; XFS cannot be shrunk.
Practical Example
Assume three 5 GiB partitions (/dev/sda5, /dev/sda6, /dev/sda7) with type 8e (Linux LVM):
-
Initialize PVs:
pvcreate /dev/sda5 /dev/sda6 /dev/sda7 -
Create a VG named
storage_vgwith 8 MiB PEs:vgcreate -s 8M storage_vg /dev/sda5 /dev/sda6 -
Add another PV later:
vgextend storage_vg /dev/sda7 -
Create a 2 GiB LV named
data_lv:lvcreate -L 2G -n data_lv storage_vg -
Format and mount:
mkfs.ext4 /dev/storage_vg/data_lv mkdir -p /mnt/data mount /dev/storage_vg/data_lv /mnt/data -
Enable automatic mounting at boot: Add to
/etc/fstab:/dev/storage_vg/data_lv /mnt/data ext4 defaults 0 0
Using dd for Low-Level Operations
The dd command copies and converts files at the byte level.
Common uses:
- Disk cloning:
dd if=/dev/sda of=/dev/sdb - Backup MBR:
dd if=/dev/sda of=mbr.img bs=512 count=1 - Wipe bootloader:
dd if=/dev/zero of=/dev/sda bs=446 count=1
Special devices:
/dev/zero: Produces a continuous stream of null bytes./dev/null: Discards all data written to it.