Logical Volumes (LV) are a Logical Volume Manager (LVM) component which is a device mapper framework provided by Linux to manage the disk drives. It abstracts the physical storage which allows the users to create logical storage volumes.
Some of the common uses of the LVM include:
- Resize the volume groups online by absorbing new physical volumes (PV) or ejecting the existing ones
- Resize the logical volumes online
- Create snapshot volumes
- Move the volumes across physical volumes
- Split or merge the volume groups without data loss
Components of LVM
The following are some standard components of the LVM in Linux:
- Physical Volumes – It represents storage devices.
- Volume Groups – It acts as a container for one or more PVs.
- Logical Volumes – It resides within VG and represent the block devices that are analogous to partitions in a disk.
This tutorial explores how we can work with the LVOL module in Ansible to configure the LVM logical volumes.
Ansible LVOL Module
The ansible LVOL module is part of the “community.general” collections. It allows us to create, remote, or resize the logical volumes in a Linux system.
If you do not have this collection installed, you can use the ansible-galaxy command as follows:
Once installed, you can use the module that we will provide in the following examples.
Parameters of LVOL Module
The following are some of the parameters for the LVOL module which allows you to customize the functionality of the module:
- vg – It specifies the name of the volume group that the logical volume is a part of.
- lv – It specifies the name of the logical volume.
- size – It defines the size of the logical volume, by default, in megabytes or optionally with one of [bBsSkKmMgGtTpPeE].
- resizefs – It resizes the underlying filesystem with the logical volume (true or false).
- state – It controls if the LV should be present, absent, or resized.
- thinpool – It specifies the thin pool volume name.
Let us explore some basic examples on how we can use this module to perform various tasks with LVMs.
Example 1: Create a New Logical Volume
The most basic task is setting up a new logical volume. For that, we can use the playbook as shown in the following:
- name: Create LV
- name: Create a logical volume of 1G
This should create a new logical volume with the specified name and size in the defined volume group.
Example 2: Resize a Logical Volume
The second use case is resizing an existing logical volume. An example playbook is as follows:
- name: Resize LV
- name: Resize logical_v to 2G
This should resize the logical volume from 1G to 2G. Keep in mind that we use the “resizefs: true” parameter to ensure that we resize the filesystem alongside the logical volume.
Example 3: Remove a Logical Volume
To remove an existing logical volume, we can use the state parameter as shown in the following playbook example:
- name: Remove LV
- name: Remove Volume
Example 4: Create a Thin-Provisioned Logical Volume
We can use the thinpool parameter to create the thin-provisioned LVs as provided in the following sample playbook:
- name: Create thin provisioned LV
- name: Create a thin pool named th_pool of size 5G
- name: Create a thin LV named 'thinlv' using th_pool
As you can see, the previous playbook starts by creating a thin pool and then creating a thin logical volume in the resulting thin pool.
We explored the workings of the LVOL module in Ansible to manage the logical volumes in remote hosts. Learning how to work with the LVOL module can quickly help you to solve a lot of complexities that are involved in managing the LVM devices. This is because it abstracts the complexities of direct LVM command-line management into simpler, human-readable YAML definitions.