2 Working With Logical Volume Manager

This chapter describes Logical Volume Manager and its use to provide data redundancy and increase performance through the implementation of volumes.

About Logical Volume Manager

You can use Logical Volume Manager (LVM) to manage multiple physical volumes and configure mirroring and striping of logical volumes to provide data redundancy and increase I/O performance. In LVM, you first create volume groups from physical volumes, which are storage devices such as disk array LUNs, software or hardware RAID devices, hard drives, and disk partitions. You can then create logical volumes in a volume group. A logical volume functions as a partition that in its implementation might be spread over multiple physical disks.

You can create file systems on logical volumes and mount the logical volume devices in the same way that you would a physical device. If a file system on a logical volume becomes full with data, you can increase the capacity of the volume by using free space in the volume group so that you can then grow the file system (provided that the file system has that capability). If necessary, you can add physical storage devices to a volume group to increase its capacity.

LVM is non-disruptive and transparent to users. You can increase the size of logical volumes and change their layout dynamically without needing to schedule system down time to reconfigure physical storage.

LVM uses the device mapper (DM) that provides an abstraction layer that enables the creation of logical devices above physical devices and provides the foundation for software RAID, encryption, and other storage features.

Initializing and Managing Physical Volumes

Before you can create a volume group, you must initialize the physical devices that you want to use as physical volumes with LVM.

Caution:

If the devices contain any existing data, back up the data.

To set up a physical device as a physical volume, use the pvcreate command:

sudo pvcreate [options] device ...

For example, set 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, you can 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

Having initialized the physical volumes, you can add them to a new or existing volume group.

To create a volume group, use the vgcreate command:

sudo vgcreate [options] volume_group physical_volume ...

For example, create the volume group myvg from the physical volumes /dev/sdb, /dev/sdc, /dev/sdd, and /dev/sde:

sudo vgcreate -v myvg /dev/sd[bcde]
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, which are the smallest unit that LVM uses when allocating storage to logical volumes. The default size of an extent is 4 MB.

The allocation policy for the volume group and logical volume determines how LVM allocates extents from a volume group. The default allocation policy for a volume group is normal, which applies rules such as not placing parallel stripes on the same physical volume. The default allocation policy for a logical volume is inherit, which means that the logical volume uses the same policy as for the volume group. You can change the default allocation policies by using the lvchange or vgchange commands, or you can override the allocation policy when you create a volume group or logical volume. Other allocation policies include anywhere, contiguous and cling.

To add physical volumes to a volume group, use the vgextend command:

sudo vgextend [options] volume_group physical_volume ...

To remove physical volumes from a volume group, use the vgreduce command:

sudo vgreduce [options] volume_group physical_volume ...

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

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

sudo vgremove volume_group

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

Having create a volume group of physical volumes, you can create logical volumes from the storage space that is available in the volume group.

To create a logical volume, use the lvcreate command:

sudo lvcreate [options] --size size --name logical_volume volume_group

For example, create the logical volume mylv of size 2 GB in the volume group myvg:

sudo lvcreate -v --size 2g --name mylv myvg
Setting logging type to disk
Finding volume group “myvg”
Archiving volume group “myvg” metadata (seqno 1).
Creating logical volume mylv
Create volume group backup “/etc/lvm/backup/myvg” (seqno 2).
...

lvcreate uses the device mapper to create a block device file entry under /dev for each logical volume and 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, which is symbolically linked by /dev/mapper/myvolg-myvol and /dev/myvolg/myvol.

Note:

Always use the devices in /dev/mapper or /dev/ volume_group. These names are persistent and are created automatically by the device mapper early in the boot process. The /dev/dm-* devices are not guaranteed to be persistent across reboots.

Having created a logical volume, you can configure and use it in the same way that you would a physical storage device. For example, you can configure a logical volume as a file system, swap partition, Automatic Storage Management (ASM) disk, or raw device.

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

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

sudo lvremove volume_group/logical_volume

Note:

You must specify both the name of the volume group and the logical volume.

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 Logical Volume Snapshots

You can also use lvcreate with the --snapshot option to create a snapshot of an existing logical volume such as mylv in the volume group myvg, 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 preserve it as a record of the state of the original volume at the time that you took the snapshot. The snapshot usually takes up less space than the original volume, depending on how much the contents of the volumes diverge over time. In the example, we assume that the snapshot only requires one quarter of the space of the original volume. You can use the value shown by the Snap% column in the output from the lvs command to see how much data is allocated to the snapshot. If the value of Snap% approaches 100%, indicating that a snapshot is running out of storage, use lvresize to grow it. Alternatively, you can reduce a snapshot's size to save storage space. To merge a snapshot with its original volume, use the lvconvert command, specifying the --merge option.

To remove a logical volume snapshot from a volume group, use the lvremove command the same way that you would use the command for a logical volume:

sudo lvremove volume_group/logical_volume_snapshot

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

Creating and Managing 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 termed 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.

To create a thin pool, use the lvcreate command with the --thin option:

sudo lvcreate --size size --thin volume_group/thin_pool_name

For example, create the thin pool mytp of size 1 GB in the volume group myvg:

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

You can then use lvcreate with the --thin option to create a thinly-provisioned logical volume with a size specified by the --virtualsize option, for example:

sudo lvcreate --virtualsize size --thin volume_group/thin_pool_name --name logical_volume

For example, create the thinly-provisioned logical volume mytv with a virtual size of 2 GB using the thin pool mytp, whose size is currently less than the size of the volume:

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

If you create a thin snapshot of a thinly-provisioned logical volume, do not specify the size of the snapshot, for example:

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

If you were to specify a size for the thin snapshot, its storage would not be provisioned from the thin pool.

If there is sufficient space in the volume group, you can use the lvresize command 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 details of how to use the snapper command to create and manage thin snapshots, see Using snapper with Thinly-Provisioned Logical Volumes.

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

Using snapper with Thinly-Provisioned Logical Volumes

You can use the snapper utility to create and manage 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

Here config_name is the name of the configuration, fs_type is the file system type (ext4 or xfs), and fs_name is the 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.

You can create three types of snapshots with snapper:

post

You use a post snapshot to record 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

You use a pre snapshot to record the state of a volume 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

You can use a single snapshot to record the state of a volume but it does not have any association with other snapshots of the volume.

For example, the following commands create pre and 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 .. 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 .. 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.