Skip to content

Instantly share code, notes, and snippets.

@jstrassburg
Last active April 13, 2017 17:52
Show Gist options
  • Save jstrassburg/6afecff7cb05d236e836582d9cbb868e to your computer and use it in GitHub Desktop.
Save jstrassburg/6afecff7cb05d236e836582d9cbb868e to your computer and use it in GitHub Desktop.
Linux Logical Volume Management [LVM]

Linux Logical Volume Management [LVM]

Source: https://www.howtoforge.com/linux_lvm

LVM Layout

  • /dev/sdX or /dev/hdX are physical HDDs (SCSI or SATA)
  • /dev/vdX are virtual HDDs (virtualization aware driver)
  • /dev/sda1 would be the first partition on the /dev/sda disk

Display Information

fdisk -l    # list info about disks
pvdisplay   # physical volume display
pvscan      # another command to show physical volumes
vgdisplay   # volume group display
vgscan      # another command to show volume groups
lvdisplay   # logical volume display
lvscan      # another command to show logical volumes
lvmdiskscan # scan for block devices that may be used as physical volumes

Create Partitions, Physical Volumes, Volume Groups, Logical Volumes

fdisk /dev/sdX                     # add partition (n) delete partition (d) on /dev/sdX
pvcreate /dev/sdX1 /dev/sdY1       # initialize a block device(s) to be used as a physical volume (format)
vgcreate vg0 /dev/sdX1 /dev/sdY1   # create a volume group named vg0 across /dev/sdX1 and /dev/sdY1, will be /dev/vg0
lvcreate --name foo --size 10G vg0 # create a logical volume foo (/dev/vg0/foo) of 10G on volume group vg0

Create a filesystem and mount it

mkfs.ext4 /dev/vg0/bar      # create an ext4 filesystem on logical volume bar on volume group vg0
mkdir /var/foo              # create a directory to mount the filesystem to
mount /dev/vg0/bar /var/foo # mount the logical volume to the directory, create entry in /etc/fstab to survive reboot

Rename Volume Group / Logical Volume

vgrename vg0 vgnew   # rename volume group vg0 to vgnew
lvrename vg0 foo bar # rename logical volume foo to bar on volume group vg0

Remove Physical Volume / Volume Group / Logical Volume

pvremove /dev/sdX1 /dev/sdY1 # remove the physical volume created with pvcreate
vgremove vg0                 # remove volume group vg0
lvremove /dev/vg0/bar        # remove logical volume bar on volume group vg0

Extend Volume Group / Resize Logical Volumes

vgextend vg0 /dev/sdZ1      # add physical volume /dev/sdZ1 to the vg0 volume group
umount /var/bar             # unmount filesystem before resizing
lvextend -L15G /dev/vg0/bar # resize logical volume
e2fsck -f /dev/vg0/bar      # force check the filesystem
resize2fs /dev/vg0/bar      # resize the filesystem on the logical volume
mount /dev/vg0/bar /var/bar # re-mount the filesystem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment