How to Create Logical Volumes (LV) in LVM

Logical Volume Management (LVM) Concepts

Logical volumes and logical volume management make it easier to manage disk space. If a file system that hosts a logical volume needs more space, it can be allocated to its logical volume from the free space in its volume group and the file system can be resized. If a disk starts to fail, a replacement disk can be registered as a physical volume with the volume group and the logical volume’s extents can be migrated to the new disk.

LVM Definitions

LVM basic components and terminology

Physical devices

Physical devices are the storage devices used to save data stored in a logical volume. These are block devices and could be disk partitions, whole disks, RAID arrays, or SAN disks. A device must be initialized as an LVM physical volume in order to be used with LVM. The entire device will be used as a physical volume.

Physical volumes (PVs)

You must initialize a device as a physical volume before using it in an LVM system. LVM tools segment physical volumes into physical extents (PEs), which are small chunks of data that act as the smallest storage block on a physical volume.

Volume groups (VGs)

Volume groups are storage pools made up of one or more physical volumes. This is the functional equivalent of a whole disk in basic storage. A PV can only be allocated to a single VG. A VG can consist of unused space and any number of logical volumes.

Logical volumes (LVs)

Logical volumes are created from free physical extents in a volume group and provide the “storage” device used by applications, users, and the operating system. LVs are a collection of logical extents (LEs), which map to physical extents, the smallest storage chunk of a PV. By default, each LE maps to one PE. Setting specific LV options changes this mapping; for example, mirroring causes each LE to map to two PEs.

Implementing LVM Storage

Creating LVM storage requires several steps. The first step is to determine which physical devices to use. After a set of suitable devices have been assembled, they are initialized as physical volumes so that they are recognized as belonging to LVM. The physical volumes are then combined into a volume group. This creates a pool of disk space out of which logical volumes can be allocated.

Logical volumes created from the available space in a volume group can be formatted with a file system, activated as swap space, and mounted or activated persistently. LVM provides a comprehensive set of command-line tools for implementing and managing LVM storage. These command-line tools can be used in scripts, making them suitable for automation.

Creating a Logical Volume

To create a logical volume, perform the following steps:

1. Prepare the physical device.

Use parted, gdisk, or fdisk to create a new partition for use with LVM. Always set the partition type to Linux LVM on LVM partitions; use 0x8e for MBR partitions. If necessary, use partprobe to register the new partition with the kernel.

Alternatively, use a whole disk, a RAID array, or a SAN disk. A physical device only needs to be prepared if there are none prepared already and a new physical volume is required to create or extend a volume group.

[[email protected] ~]# parted -s /dev/vdb mkpart primary 1MiB 769MiB
[[email protected] ~]# parted -s /dev/vdb mkpart primary 770MiB 1026MiB
[[email protected] ~]# parted -s /dev/vdb set 1 lvm on
[[email protected] ~]# parted -s /dev/vdb set 2 lvm on

2. Create a physical volume.

Use pvcreate to label the partition (or other physical device) as a physical volume. The pvcreate command divides the physical volume into physical extents (PEs) of a fixed size, for example, 4 MiB blocks. You can label multiple devices at the same time by using space-delimited device names as arguments to pvcreate.

[[email protected] ~]# pvcreate /dev/vdb2 /dev/vdb1

This labels the devices /dev/vdb2 and /dev/vdb1 as PVs, ready for allocation into a volume group. A PV only needs to be created if there are no PVs free to create or extend a VG.

3. Create a volume group

Use vgcreate to collect one or more physical volumes into a volume group. A volume group is the functional equivalent of a hard disk; you will create logical volumes from the pool of free physical extents in the volume group.

The vgcreate command-line consists of a volume group name followed by one or more physical volumes to allocate to this volume group.

[[email protected] ~]# vgcreate vg01 /dev/vdb2 /dev/vdb1

This creates a VG called vg01 that is the combined size, in PE units, of the two PVs /dev/vdb2 and /dev/vdb1. A VG only needs to be created if none already exist. Additional VGs may be created for administrative reasons to manage the use of PVs and LVs. Otherwise, existing VGs can be extended to accommodate new LVs when needed.

4. Create a logical volume.

Use lvcreate to create a new logical volume from the available physical extents in a volume group. At a minimum, the lvcreate command includes the -n option to set the LV name, either the -L option to set the LV size in bytes or the -l option to set the LV size in extents, and the name of the volume group hosting this logical volume.

[[email protected] ~]# lvcreate -n lv01 -L 700M vg01

This creates an LV called lv01, 700 MiB in size, in the VG vg01. This command will fail if the volume group does not have a sufficient number of free physical extents for the requested size. Note also that the size will be rounded to a factor of the physical extent size if the size cannot match exactly.

You can specify the size using the -L option, which expects sizes in bytes, mebibytes (binary megabytes, 1048576 bytes), gibibytes (binary gigabytes), or similar. Alternatively, you can use the -l option, which expects sizes specified as a number of physical extents.

The following list provides some examples of creating LVs:

  • lvcreate -L 128M: Size the logical volume to exactly 128 MiB.
  • lvcreate -l 128: Size the logical volume to exactly 128 extents. The total number of bytes depends on the size of the physical extent block on the underlying physical volume.

5. Add the file system

Use mkfs to create an XFS file system on the new logical volume. Alternatively, create a file system based on your preferred file system, for example, ext4.

[[email protected] ~]# mkfs -t xfs /dev/vg01/lv01

To make the file system available across reboots, perform the following steps:

1. Use mkdir to create a mount point.

[[email protected] ~]# mkdir /mnt/data

2. Add an entry to the /etc/fstab file:

/dev/vg01/lv01  /mnt/data xfs  defaults 1 2

3. Run mount /mnt/data to mount the file system that you just added in /etc/fstab.

[[email protected] ~]# mount /mnt/data

Removing a Logical Volume

To remove all logical volume components, perform the following steps:

1. Prepare the file system.

Move all data that must be kept to another file system. Use the umount command to unmount the file system and then remove any /etc/fstab entries associated with this file system.

[[email protected] ~]# umount /mnt/data

2. Remove the logical volume

Use lvremove DEVICE_NAME to remove a logical volume that is no longer needed.

[[email protected] ~]# lvremove /dev/vg01/lv01

Unmount the LV file system before running this command. The command prompts for confirmation before removing the LV. The LV’s physical extents are freed and made available for assignment to existing or new LVs in the volume group.

3. Remove the volume group

Use vgremove VG_NAME to remove a volume group that is no longer needed.

[[email protected] ~]# vgremove vg01

The VG’s physical volumes are freed and made available for assignment to existing or new VGs on the system.

4. Remove the physical volumes. Usepvremove to remove physical volumes that are no longer needed. Use a space-delimited list of PV devices to remove more than one at a time. This command deletes the PV metadata from the partition (or disk). The partition is now free for reallocation or reformatting.

[[email protected] ~]# pvremove /dev/vdb2 /dev/vdb1

Reviewing LVM Status Information

Physical Volumes

Use pvdisplay to display information about physical volumes. To list information about all physical volumes, use the command without arguments. To list information about a specific physical volume, pass that device name to the command.

pvdisplay fields meaning linux
  1. PV Name maps to the device name.
  2. VG Name shows the volume group where the PV is allocated.
  3. PV Size shows the physical size of the PV, including any unusable space.
  4. PE Size is the physical extent size, which is the smallest size a logical volume can be allocated. It is also the multiplying factor when calculating the size of any value reported in PE units, such as Free PE; for example: 26 PEs x 4 MiB (the PE Size) equals 104 MiB of free space. A logical volume size is rounded to a factor of PE units. LVM sets the PE size automatically, although it is possible to specify it.
  5. Free PE shows how many PE units are available for allocation to new logical volumes.

Volume Groups

Use vgdisplay to display information about volume groups. To list information about all volume groups, use the command without arguments. To list information about a specific volume group, pass that VG name to the command.

vgdisplay fields meaning linux
  1. VG Name is the name of the volume group.
  2. VG Size is the total size of the storage pool available for logical volume allocation.
  3. Total PE is the total size expressed in PE units.
  4. Free PE/Size shows how much space is free in the VG for allocating to new LVs or to extend existing LVs.

Logical Volumes

Use lvdisplay to display information about logical volumes. If you provide no argument to the command, it displays information about all LVs; if you provide an LV device name as an argument, the command displays information about that specific device.

lvdisplay fields meaning linux
  1. LV Path shows the device name of the logical volume. Some tools may report the device name as /dev/mapper/vgname-lvname; both represent the same LV.
  2. VG Name shows the volume group that the LV is allocated from.
  3. LV Size shows the total size of the LV. Use file-system tools to determine the free space and used space for storage of data.
  4. Current LE shows the number of logical extents used by this LV. An LE usually maps to a physical extent in the VG, and therefore the physical volume.