Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Disks and Filesystems

Linux LVM commands: pvcreate, vgcreate, lvcreate

Reference for Linux LVM: create physical volumes, volume groups and logical volumes, extend them online with lvextend, resize filesystems, and take snapshots.

LVM (Logical Volume Manager) puts a flexible layer between disks and filesystems. You hand whole disks or partitions to LVM as physical volumes (PV), pool them into a volume group (VG), and carve the pool into logical volumes (LV) that look like ordinary block devices. A filesystem is then created on the logical volume.

The payoff is flexibility: a logical volume can be grown (and, for some filesystems, shrunk) while in use, can span several disks, and can be snapshotted. The cost is one more layer to understand and, as always with storage, a real chance of destroying data if you point a command at the wrong device.

Terminal
sudo pvcreate /dev/sdb /dev/sdc
sudo vgcreate vg0 /dev/sdb /dev/sdc
sudo lvcreate -L 20G -n data vg0
sudo mkfs.ext4 /dev/vg0/data

Try it

LVM commands need root and real block devices, and the sandbox has none of them. Both examples are read-only simulations that compute what LVM computes, so you can predict sizes before touching a disk.

Bash Extents: how LVM rounds sizes (simulated)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Reading lvs-style output (simulated)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
pvcreate [-f] DEVICE...
vgcreate [-s PE_SIZE] VG DEVICE...
lvcreate -L SIZE|-l EXTENTS [-n NAME] VG
lvcreate -s -L SIZE -n SNAPNAME VG/LV
lvextend -L +SIZE [-r] VG/LV
Command Description
pvcreate DEVICE Initialize a disk or partition as a PV (writes an LVM label; destroys existing signatures after confirmation).
vgcreate VG DEVICE... Create a volume group from PVs. -s 16M sets the extent size.
vgextend VG DEVICE Add another PV to an existing group.
lvcreate -L 20G -n NAME VG Create an LV of an absolute size. -l 100%FREE uses all remaining space; -l 50%VG is relative.
lvextend -L +5G -r VG/LV Grow an LV; -r (--resizefs) also grows the filesystem.
lvreduce Shrink an LV. Shrink the filesystem first; data loss otherwise.
lvremove VG/LV Delete an LV.
pvs, vgs, lvs Short reports. pvdisplay, vgdisplay, lvdisplay give detail.
lvcreate -s Create a snapshot of an LV.

An LV appears as /dev/VG/LV (a link) and /dev/mapper/VG-LV. Dashes in names are doubled in the mapper name.

Returns: 0 on success. Common failures: "Device ... excluded by a filter" (already in use or in the exclusion list), "Insufficient free space", and "Logical volume ... contains a filesystem in use" on removal.

Usage notes

  • Order matters: PV, then VG, then LV, then mkfs, then mount. Remember to add the mount to /etc/fstab.
  • Growing online is the main reason to use LVM. For ext4 and xfs: lvextend -r -L +10G vg0/data. XFS can only grow, never shrink; ext4 can shrink but only while unmounted.
  • Never shrink in the wrong order. Shrink the filesystem first (resize2fs), then lvreduce. lvreduce -r does both in order, but make a backup first.
  • Snapshots let you back up a consistent view of a changing filesystem. The snapshot size is only the space for changes made after it was taken; when it fills up, it becomes invalid. Remove it when done.
  • Wipe carefully. pvcreate on the wrong disk destroys its data. Check with lsblk and blkid first.
  • Moving data off a disk: pvmove /dev/sdb migrates extents to the other PVs in the group, online; then vgreduce vg0 /dev/sdb removes it.
  • Thin provisioning (lvcreate --type thin-pool) lets LVs be larger than the real space and allocate on write; monitor it, because a full pool causes I/O errors.
  • LVM does not add redundancy by itself. Losing one PV in a striped or spanned VG loses data. Use RAID underneath (mdadm) or --type raid1.
  • Naming: use clear names (vg_data, lv_postgres), because they show up in /dev/mapper, logs and mount tables.