3 Working With Logical Volume Manager

Logical Volume Manager (LVM) enables you to manage multiple physical volumes and configure mirroring and striping of logical volumes. Through its use of the device mapper (DM) to create an abstraction layer, LVM provides you the capability to by which you can configure physical and logical volumes. With LVM, you obtain data redundancy as well increased I/O performance.

In LVM, you first create volume groups from physical volumes. Physical volumes are storage devices such as disk array LUNs, software or hardware RAID devices, hard drives, and disk partitions. Over these physical volumes, you create volume groups. In turn, you configure logical volumes in a volume group. Logical volumes become the foundation for configuring software RAID, encryption, and other storage features.

You create file systems on logical volumes and mount the logical volume devices in the same way as you would a physical device. If a file system on a logical volume becomes full with data, you can increase the volume's capacity by using free space in the volume group. You can then grow the file system, if the file system supports that capability. Physical storage devices can be added to a volume group to further increase its capacity.

LVM is non disruptive and transparent to users. Thus, management tasks such as increasing logical volume sizes, changing their layouts dynamically, or reconfiguring physical volumes don't require any system downt time.

Before setting up logical volumes on the system, complete the following requirements:

  • Backup the data on the devices assigned for the physical volume.

  • Unmount those devices. Creating physical volumes fails on mounted devices.

Configuring logical volumes with LVM involves the following tasks which you perform sequentially.

  1. Creating physical volumes from selected storage devices.

  2. Creating a volume group from physical volumes.

  3. Configuring logical volumes over the volume group.

  4. As needed, creating snapshots of logical volumes.

Initializing and Managing Physical Volumes

The following example sets up /dev/sdb, /dev/sdc, /dev/sdd, and /dev/sde as physical volumes:

sudo pvcreate -v /dev/sd[bcde]
Set up physical volume for “/dev/sdb” with 6313482 available
sectors
Zeroing start of device /dev/sdb
Physical volume “/dev/sdb” successfully created
...

To display information about physical volumes, use the pvdisplay, pvs, and pvscan commands.

To remove a physical volume from the control of LVM, use the pvremove command:

sudo pvremove device

Other commands that are available for managing physical volumes include pvchange, pvck, pvmove, and pvresize.

For more information, see the lvm(8), pvcreate(8), and other LVM manual pages.

Creating and Managing Volume Groups

The following example creates the volume group myvg from the newly created physical volumes:

sudo vgcreate -v myvg /dev/sd[bcde]

The following output is displayed:

Wiping cache of LVM-capable devices
Adding physical volume ‘/dev/sdb’ to volume group ‘myvg’
Adding physical volume ‘/dev/sdc’ to volume group ‘myvg’
Adding physical volume ‘/dev/sdd’ to volume group ‘myvg’
Adding physical volume ‘/dev/sde’ to volume group ‘myvg’
Archiving volume group “myvg” metadata (seqno 0).
Creating volume group backup “/etc/lvm/backup/myvg” (seqno 1).
Volume group “myvg” successfully created

LVM divides the storage space within a volume group into physical extents An extent, with a default size of 4 MB, is the smallest unit that LVM uses when allocating storage to logical volumes.

The allocation policy determines how LVM allocates extents from either a volume group or a logical volume. The default allocation policy for a volume group is normal, whose rules include, for example, not placing parallel stripes on the same physical volume. For a logical volume, the default allocation policy is inherit, which means that the logical volume uses the same policy as the volume group. Other allocation policies are anywhere, contiguous and cling, and cling_by_tags.

To change allocation policies, use the lvchange or vgchange commands. As an alternative, set a preferred allocation policy directly when creating a volume group or logical volume.

The vgextend and vgreduce commands add physical volumes to a volume group or removes them. The commands enable you to manipulate the size of the volume group.

sudo vgextend | vgreduce [options] vol_group physical_vol

To display information about volume groups, use the vgdisplay, vgs, and vgscan commands.

To remove a volume group from LVM, use the vgremove command:

sudo vgremove vol_group

The command warns you if logical volumes exist in the group and prompts for confirmation.

Other commands that are available for managing volume groups include vgchange, vgck, vgexport, vgimport, vgmerge, vgrename, and vgsplit.

For more information, see the lvm(8), vgcreate(8), and other LVM manual pages.

Creating and Managing Logical Volumes

This example creates the logical volume mylv of size 2 GB in the volume group myvg:

sudo lvcreate -v --size 2g --name mylv myvg

The following output is displayed:

Archiving volume group “myvg” metadata (seqno 1).
Creating logical volume mylv
Create volume group backup “/etc/lvm/backup/myvg” (seqno 2).
Activating logical volume myvg/mylv.
...
Logical volume "mylv" created.

lvcreate uses the device mapper to create a block device file entry under /dev for each logical volume. The command also uses udev to set up symbolic links to this device file from /dev/mapper and /dev/ volume_group. For example, the device that corresponds to the logical volume mylv in the volume group myvg might be /dev/dm-3, to which /dev/mapper/myvg-mylv and /dev/myvg/mylv are symbolically linked.

In commands or scripts, always refer to the devices in /dev/mapper or /dev/ volume_group, rather than to /dev/dm-*. Those names are persistent and are created automatically by the device mapper early in the boot process. In contrast, the /dev/dm-* devices aren't guaranteed to be persistent across reboots.

You manage and use a logical volume as you would a physical storage device, such as configuring a logical volume as a file system, a swap partition, an Automatic Storage Management (ASM) disk, or a raw device.

To display information about logical volumes, use the lvdisplay, lvs, and lvscan commands.

To remove a logical volume from a volume group, use the lvremove command:

sudo lvremove vol_group/logical_vol

Other commands that are available for managing logical volumes include lvchange, lvconvert, lvmdiskscan, lvmsadc, lvmsar, lvrename, and lvresize.

For more information, see the lvm(8), lvcreate(8), and other LVM manual pages.

Creating and Managing Grouping With Tags

A tag is a string of characters that groups LVM objects of the same type. Only objects in a volume group (VG) can be tagged, such as physical volumes (PV) and logical volumes (LV). PVs lose their tags if they are removed from a VG because tags are stored as part of the VG metadata and are deleted when a PV is removed.

Multiple LVM command can use tags. When you use a tag in a command, the command adds all objects that possess the tag that are of the type expected by its position in the command. You can also use tags to automate actions such as activating all objects within a tagged group.

To create and manage LVM object groupings with tags, do the following:
  1. To add a tag to one or more existing objects, use the following command:
    pvchange --addtag @tag PV 
    pvchange --addtag @tag VG
    pvchange --addtag @tag LV
    In the previous, tag is the name of the tag and should always be prefix with the "@" character. PV is one or more physical volume, VG is one or more volume group, and LV is one or more logical volume. The --addtag option can be repeated to add multiple tags with one command.

    Note:

    Valid tag characters are A-Z a-z 0-9 _ + . - / = ! : # & and can be up to 1024 characters. Tags cannot start with a hyphen.
  2. To add a tag when creating objects, use the following command:
    vgcreate --addtag @tag VG
    lvcreate --addtag @tag LV
  3. To delete a tag from one or more existing objects, use the following command:
    pvchange --deltag @tag PV 
    pvchange --deltag @tag VG
    pvchange --deltag @tag LV

    The --deltag option can be repeated to add multiple tags with one command.

  4. To display tags, use the following command:
    pvs -o tags PV 
    pvs -o tags VG
    pvs -o tags LV

    For more information about tags, see the lvm(8), pvcreate(8), and other LVM manual pages.

Managing Activation and Automatic Activation

You can activate or deactivate logical volumes using the lvchange command. Activating a logical volume makes the logical volume usable through a block device. Deactivating a logical volume makes the logical volume inactive and inaccessible to the kernel.

Additionally, you can enable automatic activation for logical volumes or volume groups, which applies to all logical volumes in the volume group. Automatic activation activates logical volumes in response to attaching an LVM device to a machine. When you attach all physical volumes in a volume group, the volume group is complete, and the logical volumes in the volume group automatically activate. This automatic behavior can be disabled or enabled using vgchange or lvchange with the --setautoactivation option.

You can also control which logical volumes can be automatically activated by listing which logical volumes can be activated in the /etc/lvm/lvm.conf auto_activation_volume_list parameter.

To manage the activation status of a logical volume, do the following:
  1. To deactivate a logical volume, use the following command:
    lvchange -an VG LG 

    In the previous, VG is the name of the volume group and LG is the name of the logical volume to be deactivated.

  2. To deactivate all logical volumes in a volume group, use the following command:
    lvchange -an VG
  3. To activate a deactivated logical volume, use the following command:
    lvchange -ay VG LG

    Note:

    systemd automatically mounts LVM volumes using the mount points specified in the /etc/fstab file.
  4. To activate all deactivated logical volumes in a volume group, use the following command:
    lvchange -ay VG
  5. To control which logical volumes can be activated using lvchange commands described above, you can also use the /etc/lvm/lvm.conf configuration file, with the activation/volume_list configuration option. The following example shows that the volume list can specify an entire volume group or just one logical volume within a volume group:
    volume_list = [ "VG", "VG/LV"]
    You can also use one or more tags to specify which logical devices can be activated. For example:
    volume_list = ["@tag1", "@tag2, "@tag3"]
    If you leave the brackets empty, then no device can be activated.
    volume_list = []
To manage the automatic activation of logical volumes, do the following:
  1. To enable or disable automatic activation for a volume group, use the following command:
    vgchange --setautoactivation <y|n>
  2. To enable or disable automatic activation for a logical volume, use the following command:
    lvchange --setautoactivation <y|n>
  3. To control automatic activation using the /etc/lvm/lvm.conf configuration file, specify volume groups, or a volume group and a specific logical volume within it, to the activation/auto_activation_volume_list configuration option:
    auto_activation_volume_list = [ "VG", "VG/LV"]
    You can also use tags to enable autoactivation. For example:
    auto_activation_volume_list = ["@tag1", "@tag2, "@tag3"]
    If you leave the brackets empty, the automatic activation functionality is completely disabled.
    auto_activation_volume_list = []

Creating Logical Volume Snapshots

To create a snapshot of an existing logical volume, use lvcreate --snapshot, for example:

sudo lvcreate --size 500m --snapshot --name mylv-snapshot myvg/mylv
Logical volume “mylv-snapshot” created

You can mount and modify the contents of the snapshot independently of the original volume. Or, you can preserve the snapshot as a record of the state of the original volume at the time that the snapshot was taken.

The snapshot usually occupies less space than the original volume, depending on how much the contents of the volumes diverge over time. In the example, assume that the snapshot only requires one quarter of the space of the original volume. To calculate how much data is allocated to the snapshot, do the following:

  1. Issue the lvs command.

  2. From the command output, check the value under the Snap% column.

    A value approaching 100% indicates that the snapshot is low on storage space.

  3. Use lvresize to either grow the snapshot or reduce its size to save storage space.

To merge a snapshot with its original volume, use the lvconvert --merge command.

To remove a logical volume snapshot from a volume group, use the lvremove command as you would for a logical volume, for example:

sudo lvremove myvg/mlv-snapshot

For more information, see the lvcreate(8) and lvremove (8) manual pages.

Using Thinly-Provisioned Logical Volumes

Thinly provisioned logical volumes have virtual sizes that are typically greater than the physical storage on which you create them. You create thinly provisioned logical volumes from storage that you have assigned to a special type of logical volume called a thin pool. LVM assigns storage on demand from a thin pool to a thinly-provisioned logical volume as required by the applications that access the volume. You need to use the lvs command to monitor the usage of the thin pool so that you can increase its size if its available storage is in danger of being exhausted.

Configuring and Managing Thinly-Provisioned Logical Volumes

Creating thinly provisioned logical volumes involves two steps:

  1. Create a thin pool.

    sudo lvcreate --size size --thin vol_group/pool_name
  2. Create a thinly provisioned logical volume.

    sudo lvcreate --virtualsize size --thin vol_group/thin_pool_name --name logical_vol

In the following example, the thin pool mytp of size 1 GB is first created from the volume group myvg:

sudo lvcreate --size 1g --thin myvg/mytp
Logical volume "mytp" created

Then, the thinly provisioned logical volume mytv is created with a virtual size of 2 GB:

sudo lvcreate --virtualsize 2g --thin myvg/mytp --name mytv
Logical volume "mytv" created

Note that the size of mytp is less than that of mytv.

To create a snapshot of mytv, don't specify the size of the snapshot. Otherwise, its storage would not be provisioned from mytp, for example:

sudo lvcreate --snapshot --name mytv-snapshot myvg/mytv
Logical volume “mytv-snapshot” created

If the volume group has sufficient space, use the lvresize command as needed to increase the size of a thin pool, for example:

sudo lvresize -L+1G myvg/mytp
Extending logical volume mytp to 2 GiB
Logical volume mytp successfully resized

For more information, see the lvcreate(8) and lvresize(8) manual pages.

Using snapper With Thinly-Provisioned Logical Volumes

The snapper utility is another tool for creating and maintaining thin snapshots of thinly-provisioned logical volumes.

To set up the snapper configuration for an existing mounted volume:

sudo snapper -c config_name create-config -f "lvm(fs_type)" fs_name
config_name

Name of the configuration

fs_type

File system type (ext4 or xfs)

fs_name

Path of the file system.

The command adds an entry for config_name to /etc/sysconfig/snapper, creates the configuration file /etc/snapper/configs/config_name , and sets up a .snapshots subdirectory for the snapshots.

By default, snapper sets up a cron.hourly job to create snapshots in the .snapshot subdirectory of the volume and a cron.daily job to clean up old snapshots. You can edit the configuration file to disable or change this behavior. For more information, see the snapper-configs(5) manual page.

With snapper, you can create 3 types of snapshots:

post

A post snapshot records the state of a volume after a modification. A post snapshot should always be paired with a pre snapshot that you take immediately before you make the modification.

pre

A pre snapshot records the state of a volume immediately before a modification. A pre snapshot should always be paired with a post snapshot that you take immediately after you have completed the modification.

single

A single snapshot records the state of a volume but does not have any association with other snapshots of the volume.

For example, the following commands create a pre snapshot and a post snapshots of a volume:

sudo snapper -c config_name create -t pre -p N
... Modify the volume's contents ...
sudo snapper -c config_name create -t post --pre-num N -p N'

The -p option causes snapper to display the number of the snapshot so that you can reference it when you create the post snapshot or when you compare the contents of the pre and post snapshots.

To display the files and directories that have been added, removed, or modified between the pre and post snapshots, use the status subcommand:

sudo snapper -c config_name status N .. ..

To display the differences between the contents of the files in the pre and post snapshots, use the diff subcommand:

sudo snapper -c config_name diff .. N'

To list the snapshots that exist for a volume:

sudo snapper -c config_name list

To delete a snapshot, specify its number to the delete subcommand:

sudo snapper -c config_name delete N''

To undo the changes in the volume from post snapshot N' to pre snapshot N:

sudo snapper -c config_name undochange N .. N'

For more information, see the snapper(8) manual page.