LVM

From LQWiki
Jump to navigation Jump to search

LVM is an abbreviation for Logical Volume Manager, a device mapper which has been available under Linux since 1997, when IBM released the source code to its Journaled File System (JFS) and Logical Volume Manager (LVM) from the AIX operating system under the name Enterprise Volume Management System (EVMS), to allow the open source community to determine the evolution of both. The v2.4 Linux kernel has the original LVM iteration adapted from the IBM code but with a command line syntax that imitates Hewlett Packard's LVM knock-off in that era's HP-UX. In 2003, a ground-up refactor of the original project was released under the GPL as LVM2 by Sistina Software (which was quickly acquired by Red Hat) and merged early the following year into the v2.6 Linux kernel. LVM allows one or more physical devices to be treated as a storage pool from which logical drives may be created whose size can altered dynamically. Assuming your filesystem supports dynamic resizing, the upshot of this is you can grow/shrink logical drives (mount points) as you wish and distribute them over several partitions located on multiple physical drives. In addition to combining drives into logical devices, you can also control the spread of data amongst devices through the use of striping, however this is often better left to a RAID implementation. LVM functions on top of Software-RAID solutions without issue, typically.

Concept

Physical volume
A physical volume (PV) can be created using any block device you choose. Typically, the block device would be a physical disk, RAID volume, partition or even a file. PVs are sliced into logical allocation units knows as physical extents (PEs); the default PE size is 4 MiB. A single PE is the smallest storage area (Logical volume, or LV) you can create, so if you have a 128 MiB PE size, then you could create a 128 MiB storage area, or any size that is a multiple of 128 MiB.
Volume group
In LVM, a volume group (VG) is a set of physical volumes. All physical volumes in a volume group should have the same extent size. It is from the volume group that you actually allocate extents to create your logical volumes (see below). If you have 2 VGs each holding two 250 GiB physical volumes (for instance, two 250 GiB solid state SATA drives) then you could create, at most, two logical volumes each 500 GiB in size (less the filesystem overhead). Alternatively, if you had a single VG holding all four of your 250 GiB PVs, you could create a single 1 TiB logical volume. Note that you don't have to just create a single LV for each VG; in these examples, you could create, say, eight 128 GiB LVs, or 16 64 GiB LVs.
Logical volume
A logical volume (LV) is a set of extents allocated from a volume group, a concept which in practice is analogous to a typical disk partition. A logical volume can be resized as necessary, without the need for reformatting and explicit backup and copying of all its contents back to the new filesystem. Note that this cannot be done for the root filesystem mounted at / without booting from a live image on a USB flash drive, for example. A filesystem can be created on a logical volume in the usual way, and can be mounted afterwards just as any other filesystem. Logical volumes are seen as devices with names like /dev/VolGroup00/LogVolume00 or /dev/StudentVG/year1.

Commands

Quite a large set of commands are used to manipulate LVM configurations.

LVM2 command matrix
Function Physical volume (pv) Volume group (vg) Logical volume (lv)
Show formatted status report pvs vgs lvs
Scan for configured objects pvscan vgscan lvscan
Initialize new object pvcreate vgcreate lvcreate
Enumerate all object attributes pvdisplay vgdisplay lvdisplay
Modify object attributes pvchange vgchange lvchange(8)
Deactivate configured object pvremove vgremove(8) lvremove(8)
Extend capacity of existing objects vgextend(8) lvextend
Reduce the capacity of existing objects vgreduce(8) lvreduce
Rename objects vgrename(8) lvrename(8)
Recreate vg directory & lv special files vgmknodes
Backup configuration descriptors vgcfgbackup(8)
Convert metadata format vgconvert(8)
Integrate exported vg vgimport(8)
Import & rename existing vg snapshot vgimportclone(8)
Merge existing vgs vgmerge(8)
Split existing vg into two parts vgsplit(8)
Validate object metadata pvck(8) vgck(8)
Relocate physical extents pvmove(8)
Change size of logical volume lvresize(8)

Using webmin it is possible to use and configure LVM from within a browser. Fedora has a utility system-config-lvm.

How to…

…create a logical volume

The first step for putting LVM on a disk is to pick an empty partition and change its type to Linux LVM (hex code 8e).

Using gdisk:

# gdisk /dev/sde
  Command (m for help): t
  Partition number (1-5): 5
  Hex code (type L to list codes): 8e

Using cgdisk:

# cgdisk /dev/sde

To select the operation, cgdisk displays a text user interface (TUI) with lists of partitions and commands, from which the sde5 partition is selected, followed by the Type command, and finally entering 8e at the input prompt shown. Cgdisk reports the partition type hex code as unrecognized, but the modification is successful nonetheless after writing the partition table to the disk and exiting. To verify this, run gdisk again and confirm that it recognizes the partition type as Linux LVM.

To create the physical volume, use pvcreate:

# pvcreate /dev/sde5

Then to create a volume group named myGroup using the physical volume on /dev/sde5, use vgcreate:

# vgcreate myGroup /dev/sde5

Finally to review the status of the newly created volume group, use vgdisplay:

# vgdisplay myGroup

If everything reported is what was desired, the next step is to create a logical volume called myVol using extents from myGroup using lvcreate:

# lvcreate --size 10G --name myVol myGroup

Next, format the logical volume for a filesystem with a label of myNewLvmFs (this example uses the Ext4 filesystem created by the mke2fs command) and then assign the new 10 GiB filesystem a mount point at /mnt/myVol with mount:

# mke2fs -t ext4 -L myNewLvmFs /dev/myGroup/myVol
# mkdir -p /mnt/myVol
# mount -t ext4 -o defaults,noatime,errors=remount-ro /dev/myGroup/myVol /mnt/myVol

The myVol logical volume can now be accessed at /mnt/myVol. Like any other filesystem mounts, logical volumes can be mounted automatically during the boot process by adding them to /etc/fstab.

…extend a logical volume

In this example, we want to increase the /tmp logical volume's size by 5 GiB. The process follows these general steps:

  1. Find out the logical volume's name using lvdisplay:
    # lvdisplay

      --- Logical volume ---
      LV Name /dev/vg0/tmp
      VG Name vg0
      LV UUID nqtEqA-GVKK-RUG4-HVcT-NfP4-N83O-faJ6u2
      LV Write Access read/write
      LV Status available
      # open 1
      LV Size 15.00 GB
      Current LE 3840
      Segments 2
      Allocation inherit
      Read ahead sectors 0
      Block device 253:0

      --- Logical volume ---
      LV Name /dev/vg0/swap
      VG Name vg0
      LV UUID dbiYzB-56AM-O61X-XJLY-ZgzA-M4oS-LlPeoc
      LV Write Access read/write
      LV Status available
      # open 2
      LV Size 2.00 GB
      Current LE 512
      Segments 1
      Allocation inherit
      Read ahead sectors 0
      Block device 253:1
  2. Extend the volume /dev/vg0/tmp by 5 GiB using lvextend:
    # lvextend --size +5G /dev/vg0/tmp

      Extending logical volume root to 15.00 GB
      Logical volume root successfully resized
  3. Discover which file system is used on the logical volume using lsblk:
    # lsblk -f

    NAME FSTYPE LABEL UUID MOUNTPOINT
    sdc

    ├─sdc7 LVM2_member 7thP hTUUoY-i7R9-zAsh-CqCp-5lqp-h1qw-uU0nQV
      ├─vg_vg0-lv_tmp (dm-0) ext4 Temp 87bab36e-c72a-48d1-a12d-4dcfc1736545 /tmp
            In this case, the filesystem in use is ext4.
  4. Extend the filesystem on the logical volume using that filesystem's utilities (for this example using ext4 that would be resize2fs):
    # resize2fs -p /dev/vg0/tmp

      resize2fs 1.41.12 (17-May-2010)
      Filesystem at /dev/vg0/tmp is mounted on /tmp; on-line resizing required.
      Performing an on-line resize of /dev/vg0/tmp to 5242880 (4k) blocks.
      Begin pass 1 (max = 7)
      Extending the inode table.
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - 100%
      The filesystem on /dev/vg0/tmp is now 5242880 blocks long.

External links