Linux creates a logical volume, sets up auto-start mounting at boot, and expands it

1. LVM logical volume management

1. What are LV logical volumes, PV physical volumes, and VG volume groups?

Logical volumes are devices created using Logic Volume Manager (Logic Volume Manager)
Physical Volume (PV): This is the physical disk partition. If you want to use LVM to manage this partition, you can use fdisk to change its ID to a value that LVM can recognize, that is, 8e.
Volume Group (VG): a collection of PVs
Logic Volume (LV): a logical disk drawn in VG
A physical disk or disk partition is converted into a physical volume. One or more physical volumes are aggregated to form one or more volume groups, and a logical volume is a piece of disk space abstracted from a certain volume group. The specific structure is as follows:

2, The role of logical volumes

Logical volume management consists of multiple hard disk partitions. The underlying hard disk can be randomly matched without considering the actual hard disk partition. It can be regarded as a partition that can be dynamically expanded, quickly created and conveniently managed. LVM technology can easily manage storage space, such as dynamically increasing or reducing the size of logical volumes, expanding and shrinking, and moving the location of logical volumes. Back up and restore logical volumes, implement hot backup, etc. In addition, LVM can also combine the storage space of different physical hard disks to provide larger storage space for applications.

3, why use logical volumes

For physical disks, we can use them directly after partitioning and formatting them into a file system. So why do we need to use logical volumes to manage disks? There are mainly 2 reasons:
(1) Use large-capacity disks for business. For example, we need to mount 30TB of storage under /data, but a single disk cannot meet the requirements because there are no such large single disks on the market. But if we use logical volumes to aggregate multiple small-capacity disks into one large logical disk, we can meet the demand.
(2) Expand and shrink the disk. When planning disks in the early stages of business, we do not fully know how much disk space is reasonable to allocate. If we use physical volumes, we cannot expand and shrink them later. If we use logical volumes, we can manually expand or shrink them based on later demand.
To create a logical volume, you need to first have a physical disk or disk partition, then use the physical disk or disk partition to create a physical volume, then use the physical volume to create a volume group, and finally use the volume group to create a logical volume. Next, create logical volumes step by step.

4, LVM management commands

Second, create a logical volume

1, add hard disk

//Click on the hard disk and select Add

//Click next

//Select the capacity of the newly added hard disk. I chose 5G here, and then click Next

//Click OK and close. Because the capacity will be expanded later, I followed this method and added another 5G hard drive, so I added two 5G hard drives in total.

//Restart the virtual machine and use the lsblk command to view it after logging in. The two newly added hard drives I just added are shown here. If one is added, then there is only one. Note: Everyone’s hard drive name may be different

2. Create a primary partition. You need to create a primary partition before creating an LVM logical volume

//Use fdisk /dev/sdb to create the primary partition. Note: I have two newly added hard disks here, so this operation needs to be performed twice, but the hard disk names are different. Enter n, p, 1 in sequence, then press Enter, enter w and save.

//Use fdisk /dev/sdc to create the primary partition. Enter n, p, 1 in sequence, then press Enter, enter w and save.

//Check the results and find that the partition has been successful

3, Create a physical volume

//Use pvcreate /dev/sdb1 /dev/sdc1. The latter two paths are the paths after you create two primary hard disk partitions yourself.

4, packaged into a volume group

  • Format: vgcreate+volume group name+device 1+device 2+device. . .

//The volume group name is your own name, which can be followed by multiple devices.

//View detailed information of the packed volume group. Note: The created size VG Size will be smaller than the total size because a partition table is created internally during creation, which occupies a certain amount of space.

5, Create a logical volume

  • Format: lvcreate -L specified size -n specified logical volume name volume group name

//The name of the logical volume is the name I chose. I call it wn_lv here.

//Use lvdisplay /dev/wn_vg/wn_lv to view the created logical volume. wn_vg is the name of the volume group created previously. It can be seen that the size of the logical volume LV Size is the size we just allocated.

6, Formatting

  • Format: mkfs.xfs+ created LVM volume group

//mkfs.xfs /dev/volume group name/logical volume name

7, Mounting–Auto-start mounting at boot (permanent mounting)

//The method of self-starting mounting at boot is not the only one. I am using the blkid-UUID method here.

//Use mkdir to create a mounted directory mkdir /wn_data

//Use blkid to check your own UUID code, and then copy the bottom UUID code without double quotes.

//Edit vim /etc/fstab and then copy the format of the /boot line at the bottom, replace the UUID with the UUID just copied, replace /boot with your own mounting directory, save and exit

//Use mount -a command to refresh, then df -h to view

//Mounted successfully, the size is 5G. You can then expand the capacity, or not, depending on your needs.

8, logical volume expansion

Requires manual operation

lvextend -L + 5G /dev/wn_vg/wn_lv - Expand the logical volume by 5G
xfs_growfs /dev/wn_vg/wn_lv - Logical volume refresh
df -h--View

//Use the command to expand the capacity. Note: This expansion is to pull space from the volume group. Because the volume group initially allocated 5G to the logical volume, I still have space in the volume group at this time. Before using this command, make sure that the volume group has the corresponding space.

//Logical volume refresh

//Result display, this is the result after expansion.