Chapter 4. Configuring LVM logical volumes

The following procedures provide examples of basic LVM administration tasks.

4.1. Using CLI commands

The following sections describe some general operational features of LVM CLI commands.

Specifying units in a command line argument

When sizes are required in a command line argument, units can always be specified explicitly. If you do not specify a unit, then a default is assumed, usually KB or MB. LVM CLI commands do not accept fractions.

When specifying units in a command line argument, LVM is case-insensitive; specifying M or m is equivalent, for example, and powers of 2 (multiples of 1024) are used. However, when specifying the --units argument in a command, lower-case indicates that units are in multiples of 1024 while upper-case indicates that units are in multiples of 1000.

Specifying volume groups and logical volumes

Note the following when specifying volume groups or logical volumes in an LVM CLI command.

  • Where commands take volume group or logical volume names as arguments, the full path name is optional. A logical volume called lvol0 in a volume group called vg0 can be specified as vg0/lvol0.
  • Where a list of volume groups is required but is left empty, a list of all volume groups will be substituted.
  • Where a list of logical volumes is required but a volume group is given, a list of all the logical volumes in that volume group will be substituted. For example, the lvdisplay vg0 command will display all the logical volumes in volume group vg0.

Increasing output verbosity

All LVM commands accept a -v argument, which can be entered multiple times to increase the output verbosity. The following examples shows the default output of the lvcreate command.

# lvcreate -L 50MB new_vg
  Rounding up size to full physical extent 52.00 MB
  Logical volume "lvol0" created

The following command shows the output of the lvcreate command with the -v argument.

# lvcreate -v -L 50MB new_vg
  Rounding up size to full physical extent 52.00 MB
    Archiving volume group "new_vg" metadata (seqno 1).
    Creating logical volume lvol0
    Creating volume group backup "/etc/lvm/backup/new_vg" (seqno 2).
    Activating logical volume new_vg/lvol0.
    activation/volume_list configuration setting not defined: Checking only host tags for new_vg/lvol0.
    Creating new_vg-lvol0
    Loading table for new_vg-lvol0 (253:0).
    Resuming new_vg-lvol0 (253:0).
    Wiping known signatures on logical volume "new_vg/lvol0"
    Initializing 4.00 KiB of logical volume "new_vg/lvol0" with value 0.
  Logical volume "lvol0" created

The -vv, -vvv and the -vvvv arguments display increasingly more details about the command execution. The -vvvv argument provides the maximum amount of information at this time. The following example shows the first few lines of output for the lvcreate command with the -vvvv argument specified.

# lvcreate -vvvv -L 50MB new_vg
#lvmcmdline.c:913         Processing: lvcreate -vvvv -L 50MB new_vg
#lvmcmdline.c:916         O_DIRECT will be used
#config/config.c:864       Setting global/locking_type to 1
#locking/locking.c:138       File-based locking selected.
#config/config.c:841       Setting global/locking_dir to /var/lock/lvm
#activate/activate.c:358       Getting target version for linear
#ioctl/libdm-iface.c:1569         dm version   OF   [16384]
#ioctl/libdm-iface.c:1569         dm versions   OF   [16384]
#activate/activate.c:358       Getting target version for striped
#ioctl/libdm-iface.c:1569         dm versions   OF   [16384]
#config/config.c:864       Setting activation/mirror_region_size to 512
...

Displaying help for LVM CLI commands

You can display help for any of the LVM CLI commands with the --help argument of the command.

# commandname --help

To display the man page for a command, execute the man command:

# man commandname

The man lvm command provides general online information about LVM.

4.2. Creating an LVM logical volume on three disks

This example procedure creates an LVM logical volume called mylv that consists of the disks at /dev/sda1, /dev/sdb1, and /dev/sdc1.

  1. To use disks in a volume group, label them as LVM physical volumes with the pvcreate command.

    Warning

    This command destroys any data on /dev/sda1, /dev/sdb1, and /dev/sdc1.

    # pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1
      Physical volume "/dev/sda1" successfully created
      Physical volume "/dev/sdb1" successfully created
      Physical volume "/dev/sdc1" successfully created
  2. Create the a volume group that consists of the LVM physical volumes you have created. The following command creates the volume group myvg.

    # vgcreate myvg /dev/sda1 /dev/sdb1 /dev/sdc1
      Volume group "myvg" successfully created

    You can use the vgs command to display the attributes of the new volume group.

    # vgs
      VG   #PV #LV #SN Attr   VSize  VFree
      myvg   3   0   0 wz--n- 51.45G 51.45G
  3. Create the logical volume from the volume group you have created. The following command creates the logical volume mylv from the volume group myvg. This example creates a logical volume that uses 2 gigabytes of the volume group.

    # lvcreate -L 2G -n mylv myvg
      Logical volume "mylv" created
  4. Create a file system on the logical volume. The following command creates an ext4 file system on the logical volume.

    # mkfs.ext4 /dev/myvg/mylv
    mke2fs 1.44.3 (10-July-2018)
    Creating filesystem with 524288 4k blocks and 131072 inodes
    Filesystem UUID: 616da032-8a48-4cd7-8705-bd94b7a1c8c4
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information: done

    The following commands mount the logical volume and report the file system disk space usage.

    # mount /dev/myvg/mylv /mnt
    # df
    Filesystem             1K-blocks     Used  Available Use% Mounted on
    /dev/mapper/myvg-mylv    1998672     6144    1871288   1% /mnt

4.3. Creating a RAID0 (striped) logical volume

A RAID0 logical volume spreads logical volume data across multiple data subvolumes in units of stripe size.

The format for the command to create a RAID0 volume is as follows.

lvcreate --type raid0[_meta] --stripes Stripes --stripesize StripeSize VolumeGroup [PhysicalVolumePath ...]

Table 4.1. RAID0 Command Creation parameters

ParameterDescription

--type raid0[_meta]

Specifying raid0 creates a RAID0 volume without metadata volumes. Specifying raid0_meta creates a RAID0 volume with metadata volumes. Because RAID0 is non-resilient, it does not have to store any mirrored data blocks as RAID1/10 or calculate and store any parity blocks as RAID4/5/6 do. Hence, it does not need metadata volumes to keep state about resynchronization progress of mirrored or parity blocks. Metadata volumes become mandatory on a conversion from RAID0 to RAID4/5/6/10, however, and specifying raid0_meta preallocates those metadata volumes to prevent a respective allocation failure.

--stripes Stripes

Specifies the number of devices to spread the logical volume across.

--stripesize StripeSize

Specifies the size of each stripe in kilobytes. This is the amount of data that is written to one device before moving to the next device.

VolumeGroup

Specifies the volume group to use.

PhysicalVolumePath …​

Specifies the devices to use. If this is not specified, LVM will choose the number of devices specified by the Stripes option, one for each stripe.

This example procedure creates an LVM RAID0 logical volume called mylv that stripes data across the disks at /dev/sda1, /dev/sdb1, and /dev/sdc1.

  1. Label the disks you will use in the volume group as LVM physical volumes with the pvcreate command.

    Warning

    This command destroys any data on /dev/sda1, /dev/sdb1, and /dev/sdc1.

    # pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1
      Physical volume "/dev/sda1" successfully created
      Physical volume "/dev/sdb1" successfully created
      Physical volume "/dev/sdc1" successfully created
  2. Create the volume group myvg. The following command creates the volume group myvg.

    # vgcreate myvg /dev/sda1 /dev/sdb1 /dev/sdc1
      Volume group "myvg" successfully created

    You can use the vgs command to display the attributes of the new volume group.

    # vgs
      VG   #PV #LV #SN Attr   VSize  VFree
      myvg   3   0   0 wz--n- 51.45G 51.45G
  3. Create a RAID0 logical volume from the volume group you have created. The following command creates the RAID0 volume mylv from the volume group myvg. This example creates a logical volume that is 2 gigabytes in size, with three stripes and a stripe size of 4 kilobytes.

    # lvcreate --type raid0 -L 2G --stripes 3 --stripesize 4 -n mylv myvg
      Rounding size 2.00 GiB (512 extents) up to stripe boundary size 2.00 GiB(513 extents).
      Logical volume "mylv" created.
  4. Create a file system on the RAID0 logical volume. The following command creates an ext4 file system on the logical volume.

    # mkfs.ext4 /dev/myvg/mylv
    mke2fs 1.44.3 (10-July-2018)
    Creating filesystem with 525312 4k blocks and 131376 inodes
    Filesystem UUID: 9d4c0704-6028-450a-8b0a-8875358c0511
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information: done

    The following commands mount the logical volume and report the file system disk space usage.

    # mount /dev/myvg/mylv /mnt
    # df
    Filesystem             1K-blocks     Used  Available Use% Mounted on
    /dev/mapper/myvg-mylv    2002684     6168    1875072   1% /mnt

4.4. Renaming LVM logical volumes

This procedure renames an existing logical volume using the command-line LVM interface.

Procedure

  1. If the logical volume is currently mounted, unmount the volume.
  2. If the logical volume exists in a clustered environment, deactivate the logical volume on all nodes where it is active. Use the following command on each such node:

    [root@node-n]# lvchange --activate n vg-name/lv-name
  3. Use the lvrename utility to rename an existing logical volume:

    # lvrename vg-name original-lv-name new-lv-name

    Optionally, you can specify the full paths to the devices:

    # lvrename /dev/vg-name/original-lv-name /dev/vg-name/new-lv-name

Additional resources

  • The lvrename(8) man page

4.5. Removing a disk from a logical volume

These example procedures show how you can remove a disk from an existing logical volume, either to replace the disk or to use the disk as part of a different volume. In order to remove a disk, you must first move the extents on the LVM physical volume to a different disk or set of disks.

4.5.1. Moving extents to existing physical volumes

In this example, the logical volume is distributed across four physical volumes in the volume group myvg.

# pvs -o+pv_used
  PV         VG   Fmt  Attr PSize  PFree  Used
  /dev/sda1  myvg lvm2 a-   17.15G 12.15G  5.00G
  /dev/sdb1  myvg lvm2 a-   17.15G 12.15G  5.00G
  /dev/sdc1  myvg lvm2 a-   17.15G 12.15G  5.00G
  /dev/sdd1  myvg lvm2 a-   17.15G  2.15G 15.00G

This examples moves the extents off of /dev/sdb1 so that it can be removed from the volume group.

  1. If there are enough free extents on the other physical volumes in the volume group, you can execute the pvmove command on the device you want to remove with no other options and the extents will be distributed to the other devices.

    In a cluster, the pvmove command can move only logical volume that are active exclusively on a single node.

    # pvmove /dev/sdb1
      /dev/sdb1: Moved: 2.0%
     ...
      /dev/sdb1: Moved: 79.2%
     ...
      /dev/sdb1: Moved: 100.0%

    After the pvmove command has finished executing, the distribution of extents is as follows:

    # pvs -o+pv_used
      PV         VG   Fmt  Attr PSize  PFree  Used
      /dev/sda1  myvg lvm2 a-   17.15G  7.15G 10.00G
      /dev/sdb1  myvg lvm2 a-   17.15G 17.15G     0
      /dev/sdc1  myvg lvm2 a-   17.15G 12.15G  5.00G
      /dev/sdd1  myvg lvm2 a-   17.15G  2.15G 15.00G
  2. Use the vgreduce command to remove the physical volume /dev/sdb1 from the volume group.

    # vgreduce myvg /dev/sdb1
      Removed "/dev/sdb1" from volume group "myvg"
    # pvs
      PV         VG   Fmt  Attr PSize  PFree
      /dev/sda1  myvg lvm2 a-   17.15G  7.15G
      /dev/sdb1       lvm2 --   17.15G 17.15G
      /dev/sdc1  myvg lvm2 a-   17.15G 12.15G
      /dev/sdd1  myvg lvm2 a-   17.15G  2.15G

The disk can now be physically removed or allocated to other users.

4.5.2. Moving Extents to a New Disk

In this example, the logical volume is distributed across three physical volumes in the volume group myvg as follows:

# pvs -o+pv_used
  PV         VG   Fmt  Attr PSize  PFree  Used
  /dev/sda1  myvg lvm2 a-   17.15G  7.15G 10.00G
  /dev/sdb1  myvg lvm2 a-   17.15G 15.15G  2.00G
  /dev/sdc1  myvg lvm2 a-   17.15G 15.15G  2.00G

This example procedure moves the extents of /dev/sdb1 to a new device, /dev/sdd1.

  1. Create a new physical volume from /dev/sdd1.

    # pvcreate /dev/sdd1
      Physical volume "/dev/sdd1" successfully created
  2. Add the new physical volume /dev/sdd1 to the existing volume group myvg.

    # vgextend myvg /dev/sdd1
      Volume group "myvg" successfully extended
    # pvs -o+pv_used
      PV         VG   Fmt  Attr PSize  PFree  Used
      /dev/sda1   myvg lvm2 a-   17.15G  7.15G 10.00G
      /dev/sdb1   myvg lvm2 a-   17.15G 15.15G  2.00G
      /dev/sdc1   myvg lvm2 a-   17.15G 15.15G  2.00G
      /dev/sdd1   myvg lvm2 a-   17.15G 17.15G     0
  3. Use the pvmove command to move the data from /dev/sdb1 to /dev/sdd1.

    # pvmove /dev/sdb1 /dev/sdd1
      /dev/sdb1: Moved: 10.0%
    ...
      /dev/sdb1: Moved: 79.7%
    ...
      /dev/sdb1: Moved: 100.0%
    
    # pvs -o+pv_used
      PV          VG   Fmt  Attr PSize  PFree  Used
      /dev/sda1   myvg lvm2 a-   17.15G  7.15G 10.00G
      /dev/sdb1   myvg lvm2 a-   17.15G 17.15G     0
      /dev/sdc1   myvg lvm2 a-   17.15G 15.15G  2.00G
      /dev/sdd1   myvg lvm2 a-   17.15G 15.15G  2.00G
  4. After you have moved the data off /dev/sdb1, you can remove it from the volume group.

    # vgreduce myvg /dev/sdb1
      Removed "/dev/sdb1" from volume group "myvg"

You can now reallocate the disk to another volume group or remove the disk from the system.

4.6. Configuring persistent device numbers

Major and minor device numbers are allocated dynamically at module load. Some applications work best if the block device is always activated with the same device (major and minor) number. You can specify these with the lvcreate and the lvchange commands by using the following arguments:

--persistent y --major major --minor minor

Use a large minor number to be sure that it has not already been allocated to another device dynamically.

If you are exporting a file system using NFS, specifying the fsid parameter in the exports file may avoid the need to set a persistent device number within LVM.

4.7. Specifying LVM extent size

When physical volumes are used to create a volume group, its disk space is divided into 4MB extents, by default. This extent is the minimum amount by which the logical volume may be increased or decreased in size. Large numbers of extents will have no impact on I/O performance of the logical volume.

You can specify the extent size with the -s option to the vgcreate command if the default extent size is not suitable. You can put limits on the number of physical or logical volumes the volume group can have by using the -p and -l arguments of the vgcreate command.

4.8. Managing LVM logical volumes using RHEL System Roles

This section describes how to apply the storage role to perform the following tasks:

  • Create an LVM logical volume in a volume group consisting of multiple disks.
  • Create an ext4 file system with a given label on the logical volume.
  • Persistently mount the ext4 file system.

Prerequisites

  • An Ansible playbook including the storage role

For information on how to apply an Ansible playbook, see Applying a role.

4.8.1. Example Ansible playbook to manage logical volumes

This section provides an example Ansible playbook. This playbook applies the storage role to create a LVM logical volume called mylv in a volume group called myvg. The volume group consists of the following disks:

  • /dev/sda
  • /dev/sdb
  • /dev/sdc

The playbook creates an ext4 file system on the logical volume, and persistently mounts the file system.

- hosts: all
  vars:
    storage_pools:
      - name: myvg
        disks:
          - sda
          - sdb
          - sdc
        volumes:
          - name: mylv
            size: 2G
            fs_type: ext4
            mount_point: /mnt
  roles:
    - rhel-system-roles.storage
Note

If a volume group called myvg already exists, the logical volume is added to it.

If a volume group called myvg does not exist, it is created.

4.8.2. Additional resources

4.9. Removing LVM logical volumes

This procedure removes an existing logical volume using the command-line LVM interface.

The following commands remove the logical volume /dev/vg-name/lv-name from the volume group vg-name.

Procedure

  1. If the logical volume is currently mounted, unmount the volume.
  2. If the logical volume exists in a clustered environment, deactivate the logical volume on all nodes where it is active. Use the following command on each such node:

    [root@node-n]# lvchange --activate n vg-name/lv-name
  3. Remove the logical volume using the lvremove utility:

    # lvremove /dev/vg-name/lv-name
    Do you really want to remove active logical volume "lv-name"? [y/n]: y
      Logical volume "lv-name" successfully removed
    Note

    In this case, the logical volume has not been deactivated. If you explicitly deactivated the logical volume before removing it, you would not see the prompt verifying whether you want to remove an active logical volume.

Additional resources

  • The lvremove(8) man page