ubuntu, updated
LVM volumes
Create, list, resize and delete LVM physical volumes, volume groups and logical volumes.
New to LVM? Read How disks, partitions and volumes fit together first. It takes two minutes.
Create: disk to mounted volume
sudo pvcreate /dev/sdb1 # 1. hand the partition (or whole disk) to LVM
sudo vgcreate data-vg /dev/sdb1 # 2. create a pool from it
sudo lvcreate -n data-lv -L 50G data-vg # 3. cut a 50 GB volume out of it
# or use the whole pool: sudo lvcreate -n data-lv -l 100%FREE data-vg
sudo mkfs.ext4 /dev/data-vg/data-lv # 4. format
Then mount it like any other device: Format and mount.
Cheat sheet
| What | Command |
|---|---|
| Overview | sudo pvs, sudo vgs, sudo lvs |
| Details | sudo pvdisplay, sudo vgdisplay, sudo lvdisplay |
| Add a disk to an existing pool | sudo pvcreate /dev/sdc && sudo vgextend data-vg /dev/sdc |
| Grow a volume by 20 GB + its file system | sudo lvextend -r -L +20G /dev/data-vg/data-lv |
| Grow a volume to all free space | sudo lvextend -r -l +100%FREE /dev/data-vg/data-lv |
| Rename a volume | sudo lvrename data-vg data-lv new-name |
| Snapshot (10 GB for changes) | sudo lvcreate -s -n data-snap -L 10G /dev/data-vg/data-lv |
| Remove a snapshot | sudo lvremove /dev/data-vg/data-snap |
-L takes a size (50G, +20G), -l takes extents or percentages (100%FREE). -r resizes the file system in the same step. Don’t forget it.
Delete
Always top-down, the reverse of creating:
sudo umount /data # 1. unmount, remove the line from /etc/fstab
sudo lvremove /dev/data-vg/data-lv # 2. delete the volume (data is gone)
sudo vgremove data-vg # 3. delete the pool (only if empty)
sudo pvremove /dev/sdb1 # 4. release the disk/partition
Remove one disk from a pool
sudo pvmove /dev/sdc # move all data off this disk (needs free space on the others)
sudo vgreduce data-vg /dev/sdc # take it out of the pool
sudo pvremove /dev/sdc
Shrinking
Possible for ext4 only, and only while unmounted: sudo lvreduce -r -L 30G /dev/data-vg/data-lv. XFS cannot shrink at all. Take a backup first. Usually it is easier to create a new, smaller volume and copy the data.
Gotchas
- LVM snapshots slow writes down and become invalid when their space fills up. Use them short-term (for an upgrade), not as backups.
lvextendwithout-rgrows the volume, butdf -hstill shows the old size. Fix:sudo resize2fs(ext4) orsudo xfs_growfs /mountpoint(XFS).