Logical Volume management LVM is a versatile hard disk system tool. Both Linux and other similar systems are very easy to use. It is very troublesome to adjust the size of a traditional partition by using a fixed size partition. However, LVM can create and manage logical volumes, rather than directly using physical hard drives.
Logical Volume management LVM is a versatile hard disk system tool. Both Linux and other similar systems are very easy to use. It is very troublesome to adjust the size of a traditional partition by using a fixed size partition. However, LVM can create and manage "logical" volumes, rather than directly using physical hard disks. The administrator can elastically scale up and down the management logic volume, and the operation is simple without damaging the stored data. You can add new hard disks to the LVM at will to directly expand existing logical volumes. The LVM does not need to be restarted to let the kernel know the existence of the partition.
LVM uses a layered structure, as shown in.
At the top of the figure, the actual physical disk, its partition, and its physical volume (PV) are used ). One or more physical volumes can be used to create a volume Group (VG ). Then, you can create logical volumes (LV) based on the volume group ). As long as there is space available in the volume group, you can create logical volumes as you like. The file system is created on a logical volume and can be mounted and accessed in the operating system.
LVM test description
This article describes how to create and manage LVM volumes in linux. We will divide it into two parts. First, create multiple logical volumes on one hard disk and mount them to the/lvm-mount directory. Then we will resize the created volume. In the second part, we will add additional volumes from the other hard disk to the LVM.
Prepare disk partitions
Create a disk partition by using fdisk. We need to create three 1G partitions. Note that the partition size is not required to be consistent. Similarly, partitions must use the '8e' type to make them available for LVM.
- # fdisk /dev/sdb
- Command (m for help): n # Create
- Command action
- E extended
- P primary partition (1-4)
- P # primary partition
-
- Partition number (1-4): 1 # Partition number
- First cylinder (1-1044, default 1): # default 1 for vehicle return
- Last cylinder, + cylinders or + size {K, M, G} (1-1044, default 1044): + 1G # size
-
- Command (m for help): t # change the type
- Selected partition 1
- Hex code (type L to list codes): 8e # LVM partition code
- Changed system type of partition 1 to 8e (Linux LVM)
Repeat the preceding operation to create the other two partitions. After the partition is created, we should have output similar to the following:
- # fdisk -l
- Device Boot Start End Blocks Id System
- /dev/sdb1 1 132 1060258+ 8e Linux LVM
- /dev/sdb2 133 264 1060290 8e Linux LVM
- /dev/sdb3 265 396 1060290 8e Linux LVM
Prepare physical volume (PV)
The created partition is used to store physical volumes. LVM can use physical volumes of different sizes.
- # pvcreate /dev/sdb1
- # pvcreate /dev/sdb2
- # pvcreate /dev/sdb3
Run the following command to check the creation of a physical volume. The following part of the output is truncated. "/Dev/sdb2" is a new physical volume of "1.01 GiB.
- # pvdisplay
- --- NEW Physical volume ---
- PV Name /dev/sdb2
- VG Name
- PV Size 1.01 GiB
- Allocatable NO
- PE Size 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID jszvzz-ENA2-g5Pd-irhV-T9wi-ZfA3-0xo092
Use the following command to delete a physical volume.
- # pvremove /dev/sdb1
Prepare a volume Group (VG)
Run the following command to create a volume group named 'volume-group1' and use/dev/sdb1,/dev/sdb2 and/dev/sdb3.
- # vgcreate volume-group1 /dev/sdb1 /dev/sdb2 /dev/sdb3
Use the following command to verify the volume group.
- # vgdisplay
- --- Volume group ---
- VG Name volume-group1
- System ID
- Format lvm2
- Metadata Areas 3
- Metadata Sequence No 1
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 0
- Open LV 0
- Max PV 0
- Cur PV 3
- Act PV 3
- VG Size 3.02 GiB
- PE Size 4.00 MiB
- Total PE 774
- Alloc PE / Size 0 / 0
- Free PE / Size 774 / 3.02 GiB
- VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK
From the output, we can see the volume group usage/total. The physical volume provides space for the volume group. As long as there is space available in this volume group, we can create logical volumes at will.
Use the following command to delete a volume group.
- # vgremove volume-group1
Create logical volume (LV)
The following command creates a logical volume named '1v1 'with a size of MB. We use small partitions to reduce execution time. This logical volume uses the space of the previously created volume group.
- # lvcreate -L 100M -n lv1 volume-group1
You can run the lvdisplay command to view logical volumes.
- # lvdisplay
- --- Logical volume ---
- LV Name /dev/volume-group1/lv1
- VG Name volume-group1
- LV UUID YNQ1aa-QVt1-hEj6-ArJX-I1Q4-y1h1-OFEtlW
- LV Write Access read/write
- LV Status available
- # open 0
- LV Size 100.00 MiB
- Current LE 25
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:2
Now that the logical volume is ready, we can format and mount the logical volume, just like other ext2/3/4 partitions!
- # mkfs.ext4 /dev/volume-group1/lv1
- # mkdir /lvm-mount
- # mount /dev/volume-group1/lv1 /lvm-mount/
Once the logical volume is mounted, we can read and write it to the mount point/lvm-mount. To create and Mount other logical volumes, repeat this process.
Finally, we can use lvremove to delete the logical volume.
- # umount /lvm-mount/
- # lvremove /dev/volume-group1/lv1
Expand an LVM volume
The most useful feature of LVM is to adjust the logical volume size. This section describes how to expand an existing logical volume. Next, we will expand the logical volume 'lv1' created earlier to 200 MB.
Note: After adjusting the logical volume size, you also need to adjust the file system size to match. This extra step varies depending on the type of the file system to be created. In this article, we use 'lv1' to create an ext4 file system, so the operation here is for the ext4 file system. (Ext2/3 file systems are also similar ). The command execution sequence is very important.
First, we unmount the lv1 volume.
- # umount /lvm-mount/
Then, set the volume size to 200 MB.
- # lvresize -L 200M /dev/volume-group1/lv1
Next, check for disk errors
- # e2fsck -f /dev/volume-group1/lv1
After you run the following command to expand the file system, the ext4 information is updated.
- # resize2fs /dev/volume-group1/lv1
Now, the logical volume has been expanded to MB. We check the LV status for verification.
- # lvdisplay
- --- Logical volume ---
- LV Name /dev/volume-group1/lv1
- VG Name volume-group1
- LV UUID 9RtmMY-0RIZ-Dq40-ySjU-vmrj-f1es-7rXBwa
- LV Write Access read/write
- LV Status available
- # open 0
- LV Size 200.00 MiB
- Current LE 50
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:2
Now, this logical volume can be mounted again, and this method can also be used for other partitions.
Scale down an LVM volume
This section describes how to reduce the LVM volume size. Command Order is equally important. In addition, the following commands are equally valid for the ext2/3/4 file system.
Note: If the size of the logical volume is smaller than the size of the stored data, the data stored in the logical volume will be lost.
First, uninstall the volume.
- # umount /dev/volume-group1/lv1
Then, check for disk errors.
- # e2fsck -f /dev/volume-group1/lv1
Next, narrow down the file system and update the ext4 information.
- # resize2fs /dev/volume-group1/lv1 100M
After completion, reduce the logical volume size
- # lvresize -L 100M /dev/volume-group1/lv1
WARNING: indexing active logical volume to 100.00 MiB this may destroy your data (filesystem etc.) Do you really want to reduce lv1? [Y/n]: y Cing logical volume lv1 to 100.00 MiB Logical volume lv1 successfully resized
Finally, verify the adjusted logical volume size.
- # lvdisplay
- --- Logical volume ---
- LV Name /dev/volume-group1/lv1
- VG Name volume-group1
- LV UUID 9RtmMY-0RIZ-Dq40-ySjU-vmrj-f1es-7rXBwa
- LV Write Access read/write
- LV Status available
- # open 0
- LV Size 100.00 MiB
- Current LE 25
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:2
Expand a volume Group
This section describes how to extend a volume group by adding a physical volume to the volume group. Let's assume that our volume group 'volume-group1' is full and needs to be expanded. The hard drive (sdb) has no other free partitions. we have added another hard drive (sdc ). We will see how to add the sdc partition to the volume group for expansion.
Detect current volume group status
- # vgdisplay volume-group1
- --- Volume group ---
- VG Name volume-group1
- System ID
- Format lvm2
- Metadata Areas 3
- Metadata Sequence No 8
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 1
- Open LV 0
- Max PV 0
- Cur PV 3
- Act PV 3
- VG Size 3.02 GiB
- PE Size 4.00 MiB
- Total PE 774
- Alloc PE / Size 25 / 100.00 MiB
- Free PE / Size 749 / 2.93 GiB
- VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK
First, create a 2 GB partition sdc1 with the type of LVM (8e), as described earlier in the tutorial.
- # fdisk /dev/sdc
- Command (m for help): n
- Command action
- e extended
- p primary partition (1-4)
- p
- Partition number (1-4): 1
- First cylinder (1-1044, default 1):
- Using default value 1
- Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +2G
-
- Command (m for help): t
- Selected partition 1
- Hex code (type L to list codes): 8e
- Changed system type of partition 1 to 8e (Linux LVM)
-
- Command (m for help): w
- The partition table has been altered!
Then, we create a physical volume/dev/sdc1
- # pvcreate /dev/sdc1
Now that the physical volume is ready, we can simply add it to the existing volume group 'volume-group1.
- # vgextend volume-group1 /dev/sdc1
Use vgdisplay for verification (you can see that the volume group size has increased ).
- # vgdisplay
- --- Volume group ---
- VG Name volume-group1
- System ID
- Format lvm2
- Metadata Areas 4
- Metadata Sequence No 9
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 1
- Open LV 0
- Max PV 0
- Cur PV 4
- Act PV 4
- VG Size 5.03 GiB
- PE Size 4.00 MiB
- Total PE 1287
- Alloc PE / Size 25 / 100.00 MiB
- Free PE / Size 1262 / 4.93 GiB
- VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK
Note that although we use a separate disk for demonstration, as long as it is an '8e' type disk partition, it can be used to expand the volume group.
To sum up, LVM is a powerful tool for creating and managing variable-size partitions. This article describes how to create and use dynamic partitions in LVM. We also introduced how to expand/contract logical volumes and volume groups, and how to add a new disk to LVM.
Hope to help you.