LVM logical volume management operation guide

Time:2020-3-10

This paper introduces the common operations of managing LVM logical volumes

Create LVM logical volume

  • Use pvcreate to mark three disks as LVM physical volumes
$ pvcreate /dev/sda1 /dev/sdb1 /dev/adc1
  • Make LVM physical volumes into volume groups called myg, and use the VGS command to view the status
$ vgcreate myvg /dev/sda1 /dev/sdb1 /dev/sdc1
  • Create a logical volume on the volume group and a logical volume mylv in the volume group myvg
$ lvcreate -L 2G -n mylv myvg
  • Creating a file system on a logical volume
$ mkfs.ext4 /dev/myvg/mylv
  • Mount logical volume to / MNT directory
$ mount /dev/myvg/mylv /mnt

Cutting and dividing group

  • Use pvscan to see how much space is left in a volume group
  • Use the pvmove command to move all the physical expansion units from / dev / SDC1 to / dev / sdb1. After the move is completed, you can see that the / dev / SDC1 use space is empty through pvscan
  • Use vgplit to slice your RVG volume groups and VGS to view two volume groups
$ lvcgange -a n /dev/myvg/mylv
$ vgsplit myvg yourvg /dev/sdc1
  • Create a logical volume on a new volume group
$ lvcreate -L 5G -n yourlv yourvg
  • Create a file system and mount it
$ mkfs.ext4 /dev/yourvg/yourlv
$ mount /dev/yourvg.yourlv /mnt

Remove disks from logical volumes

  • Use the PVs – O + PV uused command to see which physical volumes the volume group spans
  • If other physical volumes have space left, you can use the pvremove command to move the expansion unit on / dev / sdb1 to other physical volumes
$ pvremove /dev/sdb1
  • Use the vgreduce command to remove the physical volume / dev / sdb1 from the volume group
$ vgreduce myvg /dev/sdb1
  • If there is not enough space for other physical volumes in the volume group, you can add a new physical volume and move / dev / sdb1 to the new physical volume and then delete / dev / sdb1
$ pvcreate /dev/sdd
$ vgextend myvg /dev/sdd1
$ pvmove /dev/sdb1 /dev/sdd1
$ vgreduce myvg /dev/sdb1

Merge volume group

Merge volume groups can merge inactive volume groups into one active volume group or another inactive volume group

 $ vgmerge -v dest_vg source_vg

Modify logical volume size

  • Use the lvextend command to increase the logical volume size
  • Expand logical volume to specified size
$ lvextend -L 12G /dev/myvg/homevol
  • Increase logical volume specified size
$ lvextend -L +1G /dev/myvg/homevol
  • In general, changing the size of a logical volume changes the size of the file system automatically

Manage LVM physical volumes

  • Use the lvmdiskscan command to scan block devices available for physical volumes
  • Use pvdisplay to check whether the physical volume is used by the logical volume
$ pvdisplay /dev/hda1
  • Use LVM command to view LVM help and configuration
$ lvm help
  • View physical volumes
$ pvs
$ pvdisplay
$ pvscan
  • View volume group
$ vgs
$ vgdisplay
$ vgscan
  • View logical volumes
$ lvs
$ lvdisplay
$ lvscan

Logical volume snapshot

LVM snapshot function supports the creation of virtual image at a certain time without service interruption, but snapshot cannot replace backup.

  • Create a logical volume origin from a volume group
$lvreate -L 1G -n origin VG
  • Create a 100m snapshot logical volume snap
$ lvcreate --size 100M --snapshot --name snap /dev/VG/origin
  • View original logical volume status
$ lvdisplay /dev/VG/origin
  • Displays the usage ratio of the original and snapshot volumes
$ lvs -a -o +devices

When the snapshot volume is full, it will become unavailable. The mounted file system will be forced to unload to avoid the read-write error of the mount point. You can configure snapshot auto extend threshold and snapshot auto extend percent in / etc / LVM / lvm.conf to execute the auto expand policy. However, LVM does not allow the creation of snapshot volumes larger than the original volume plus metadata, so automatic growth to a certain size limit will not automatically grow again.

Snapshot volume merge

$ lvconvert --merge VG/snap

LVM supports merging multiple snapshots. If multiple snapshots have tag tags, it will merge one by one

$ lvconvert --merge @some_tag