LVM (Logical Volume Manager) is a subsystem of Linux operating systems that allows you to use different areas of a hard drive or different hard disks as one logical volume. LVM is built into the Linux kernel and is implemented using a device mapper. The main advantages of LVM are a high level of abstraction from HDDs, flexibility, and scalability. You can resize the logical volume at runtime, and add (and remove) new disks. For LVM volumes, mirroring, snapshots (persistent snapshot), and striping (stratification of data between several disks in order to increase performance) are supported. In this article, we will look at the use of LVM partitions using Linux CentOS 8 as an example, show the process of combining two disks into one LVM group, see how to create groups, volumes, mount, expand and reduce the size of LVM partitions.
Content:
First, it’s necessary to understand the layers of LVM disk abstraction.
Physical Volume (PV) – the physical layer. Physical drives are initialized for use in LVM.
Volume Group (VG) – volume group level. Initialized disks are combined into logical groups with a name.
Logical Volume (LV) – a logical volume is created on a volume group that hosts the file system and data.
т Installing the lvm2 utility
To get started with LVM, you need to install the lvm2 utility. Let’s execute the following commands: apt-get install lvm2 – for Ubuntu, Mint, Debian yum install lvm2 – for Centos, Red Hat, Fedora In different versions of Linux, only the method of installing the lvm2 utility is different (installation via yum/ dnf or apt-get), further commands for working with LVM are the same.Creating LVM Partitions
So, we have a KVM virtual machine, to which two additional disks are connected. Check that they are available on the system using the command: fdisk -l As you can see, I have two drives available /dev/vdb and /dev/vdc . When setting up LVM on your virtual or physical server, use your disk labels. In order for disks to be available for LVM, they must be marked (initialized) with the pvcreate utility: pvcreate /dev/vdb /dev/vdc Now, to make sure these disks can be used for LVM, run the pvdisplay command: As you can see, both disks are displayed. Let’s analyze the information from the output of the command:- PV Name – the name of the disk or partition
- VG Name – the volume group that this disk belongs to (we haven’t created a group yet)
- PV Size – disk size or size
- Allocatable – distribution by groups. In our case, there was no distribution, so NO is indicated
- PE Size – the size of the physical fragment. If the drive is not added to any group, the value will always be 0
- Total PE – number of physical fragments
- Free PE – the number of free physical fragments
- Allocated PE – distributed fragments
Volume group “test” successfully createdTo check the result, type vgdisplay: As you can see, the disks are combined into the test group and VG Size shows the total size of the disks. Let’s analyze the information from the listing of the vgdisplay command:
- VG Name is the volume group that this disk belongs to.
- Format – version of the lvm subsystem that is used to create the group (in our case, version 2)
- Metadata Areas – metadata area
- VG Access – access level to a group of logical volumes
- VG Size – the total volume of disks that are included in the group
- PE Size – physical fragment size
- Alloc PE / Size – allocated space (number and volume of fragments)
- VG UUID – Group ID
As you can see from the listing, a logical volume with the name lvol0 and a size of 5G was created in the test group. If you want to set the name yourself, use the -n flag: # lvcreate -L 5G -n test1 testLogical volume "lvol0" created.
Some examples for creating logical volumes with different sizes: lvcreate -l 40%VG test – 40% of the disk space of the test group lvcreate -l 100%FREE test – use all free space of the test group To display information about a logical volume, use lvdisplay: Let’s take a look at the listing of this command:Logical volume "test1" created
- LV Path – path to the logical volume device (disk or partition)
- LV Name – the name of the logical volume
- VG Name – volume group name
- LV UUID – logical volume identifier
- LV Write Access – access level to the logical volume
- LV Creation host, time – information about the host, the date when the logical volume was created
- LV Size – disk size available for use by the logical volume
- Current LE – number of logical fragments
LVM: create a filesystem, mount logical volume
To create a file system on a logical volume, use the mkfs utility: Create an ext4 filesystem on an LVM volume: mkfs.ext4 /dev/test/test1 The file system was created without errors. Now let’s create a test directory and mount the logical volume to this directory: # mkdir /var/www/home # mount /dev/test/test1 /var/www/home/ As you can see, everything went without errors and now the /var/www/home directory exists as a separate partition. In order for the logical volume to be mounted at system boot, you need to add it to fstab and assign a directory for mounting. Let’s open the file: nano /etc/fstab And add the following information to it:After that, you can mount the volume using mount -a: # mount -a # df -h/dev/test/test1 /var/www/home ext4 defaults 1 2
To check general information about disks, partitions, and volumes, enter the lsblk command: As you can see, our created volume is displayed, and the directory to which it is mounted is indicated. The lvmdiskscan command allows you to scan available disks, showing their size and belonging to LVM.Filesystem Size Used Avail Use% Mounted on devtmpfs 485M 0 485M 0% /dev tmpfs 496M 0 496M 0% /dev/shm tmpfs 496M 6.7M 489M 2% /run tmpfs 496M 0 496M 0% /sys/fs/cgroup /dev/vda2 20G 1.3G 19G 7% / /dev/vda1 488M 100M 353M 23% /boot tmpfs 100M 0 100M 0% /run/user/0 /dev/mapper/test-test1 4.8G 20M 4.6G 1% /var/www/home
Extending an LVM Logical Volume
To add an additional disk to a volume group, you need to use the already familiar scheme: pvcreate /dev/ourdisk – disk initialization for lvm vgextend test /dev/ourdisk – adding a disk to a volume group To expand a logical volume, use the following scheme: lvextend -L10G /dev/test/test1 In this way, you will expand the partition by 10 GB. A few more examples of including an LVM partition: lvextend -L+10G /dev/test/test1 – add 10 GB to your volume lvextend -l +100%FREE /dev/test/test1- allocate all unallocated space in the test group It remains to increase the file system partition: resize2fs /dev/test/test1 –for ext4 xfs_growfs /dev/test/test1 – for xfsLVM Volumes Shrinking
LVM allows you to reduce the volume size. But for the safety of reducing the size of the partition, it must be disabled. Let’s unmount the volume from the directory: umount /var/www/home/ Let’s check the disk: e2fsck -fy /dev/test/test1 Let’s reduce the file system partition by 4 GB: # resize2fs /dev/test/test1 4G# lvreduce -L-4G /dev/test/test1resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/test/test1 to 1048576 (4k) blocks. The filesystem on /dev/test/test1 is now 1048576 blocks long.
Now let’s reduce the size of the LVM volume itself: lvreduce -L-4G /dev/test/test1 After that, you need to mount the volume back and check the current size: As you can see, the size has decreased to 4 GB.WARNING: Reducing active logical volume to 6.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce test/test1? [y/n]: y The size of logical volume test/test1 changed from 10.00 GiB (2560 extents) to 6.00 GiB (1536 extents). Logical volume test/test1 successfully resized.
LVM groups and volumes removing
To remove LVM volumes, use the lvremove command: lvremove /dev/test/test1 To remove a logical volume group, use: # vgremove testRemove labels from LVM disks: # pvremove /dev/vdb /dev/vdcVolume group "test" successfully removed
Labels on physical volume "/dev/vdb" successfully wiped. Labels on physical volume "/dev/vdc" successfully wiped.
Creating mirrored LVM volumes
LVM allows you to create mirrored volumes to improve storage fault tolerance. In an LVM mirror, data is simultaneously stored on two (or more) physical drives (similar to RAID-1). Procedure for creating a mirrored volume in LVM.- Инициализация дисков: pvcreate /dev/sd{b,c}
- Create an LVM group: vgcreate mirror1 /dev/sd{b,c}
- Create a mirror LVM volume: lvcreate -L 5g -m1 -n lvMirr1 VGmirror1
VPS Linux
VPS hosting with KVM virtualization