Home  >  Article  >  Operation and Maintenance  >  How to use LVM disk operation commands in Linux disk management

How to use LVM disk operation commands in Linux disk management

WBOY
WBOYforward
2023-05-23 13:58:453416browse

LVM, Logical Volume Manger, is a logical volume management function provided by the Linux kernel. It is composed of kernel drivers and application layer tools. It creates a logical layer based on the hard disk partition, which can be very flexible and very Conveniently manage storage devices.

LVM uses the device-mapper function of the Linux kernel to implement virtualization of the storage system (the system partition is independent of the underlying hardware). Through LVM, storage space can be abstracted and virtual partitions can be created on it. Partitions can be expanded and reduced more easily. When adding or deleting partitions, there is no need to worry about not having enough contiguous space on a certain hard disk to avoid being in use. It eliminates the trouble of disk repartitioning and the inconvenience of having to move other partitions to adjust partitions. It can manage disks more flexibly than traditional partitioning systems.

1. The basic components of LVM

1. Physical volume (PV, Physical Volume)

A block device that can store LVM. Such as hard disk partition (MBR or GPT Partition), SAN hard disk, RAID or LUN, a loopback file, a kernel-mapped device (such as dm-crypt), which contains a special LVM header, which is the actual hardware or storage system built by LVM.

2. Volume Group (VG, Volume Group)

A volume group is a collection of one or more physical volumes and is displayed as /dev/VG_NAME in the device file system.

3. Logical Volume (LV, Logical Volume)

Logical volumes are the final meta-devices available for use by the system. They are created and managed in volume groups and are composed of physical blocks. In fact It is a virtual partition and appears as /dev/VG_NAME/LV_NAME, on which file systems can usually be created.

4. Physical Blocks (PE, Physical Extends)

If a logical volume needs to allocate multiple physical blocks, they will become the smallest contiguous area in a volume group (default is 4 MiB ). You can think of it as part of a physical volume that can be assigned to a logical volume.

Below I drew a picture of the location of lvm in Linux disk management:

How to use LVM disk operation commands in Linux disk management

## The order is: disk -> partition -> PV - > VG -> LV -> fs, that is, disk->partition->physical volume->volume group->logical volume->file system.

The creation is also in this order, which will be introduced in detail below.

2. Advantages and Disadvantages of LVM

1. Advantages

Compared with the traditional hard disk partition management method, LVM is more flexible:

  • Treat multiple hard disks as one large hard disk

  • Using logical volumes (LV), you can create partitions that span many hard disk spaces.

  • You can create a small logical volume (LV) and dynamically adjust its size when there is insufficient space.

  • When adjusting the size of a logical volume (LV), you don’t need to consider the location of the logical volume on the hard disk, and you don’t have to worry about no available contiguous space.

  • It is feasible to create, delete, resize and other operations online for logical volumes and volume groups. For file systems on LVM, resizing is required, but some file systems (such as ext4) support online operations.

  • Without restarting the service, the logical volume (LV) used in the service can be migrated online/live to another hard disk.

  • Allows the creation of snapshots, which can save backups of the file system while minimizing the downtime of the service.

  • Supports various device-mapper targets, including transparent file system encryption and caching of frequently used data. This will allow you to create a system with one or more disks, encrypted with LUKS, and use LVM on top to easily manage and adjust these independent encrypted volumes (e.g. /, /home, /backup, etc.) without booting the trouble of entering the key multiple times.

2. Disadvantages

  • Requires more complicated extra steps during system setup.

  • Windows system does not support LVM. If you use dual systems, you will not be able to access the LVM partition on Windows.

3. Use of LVM

1. Create a partition

Before configuring lvm, the storage device must be partitioned. You can use Use fdisk or parted tools. When creating a partition, pay attention to the partition type setting (type is linux lvm):

  • If you are using MBR, set the partition type to 8e.

  • If you are using GPT, set the partition type to E6D6D379-F507-44C2-A23C-238F2A3DF928.

I added a new disk /dev/sdb to my virtual machine. Let’s create an 8G partition:

How to use LVM disk operation commands in Linux disk management

How to use LVM disk operation commands in Linux disk management

In the same way, create another 10G linux lvm type partition:

How to use LVM disk operation commands in Linux disk management

2. Physical volume (PV) related operations

①. Use the lvmdiskscan command to list the devices that can be used as PV

How to use LVM disk operation commands in Linux disk management

Note: If the system boot program does not support LVM, /boot cannot be placed in LVM. At this moment, an independent /boot partition must be created and directly formatted and mounted to /boot. The only known bootloader that supports LVM is GRUB.

As you can see from the above figure, //dev/sda2 is already a PV, so only dev/sda1, /dev/sdb1, and /dev/sdb2 can be used to create PV, and because /dev/sda1 is boot boot area, so below we can create PV

② for /dev/sdb1 and /dev/sdb2, and use the pvcreate command to create pv
root# pvcreate device1 device2 ...

How to use LVM disk operation commands in Linux disk management

③ , View all current PV information

You can view PV information through the three commands pvs, pvscan, and pvdisplay

How to use LVM disk operation commands in Linux disk management

Note: If you are using an SSD that has not been formatted and the erase block size is less than 1M, please use the following command pvcreate --dataalignment 1m /dev/sda to set the alignment. .

3. Volume group (VG) related operations

①. Create a volume group

Use the command vgcreate to create a volume group

root# vgcreate vg_name pv1 pv2 ...

How to use LVM disk operation commands in Linux disk management

Create the volume group vg_fedora_yg and add pv /dev/sdb1 to the volume group.

②. View volume group information

How to use LVM disk operation commands in Linux disk management

At this time, you can also view the volume group where each physical volume is located through pvs:

How to use LVM disk operation commands in Linux disk management

4. Logical volume (LV) related operations

①.Create LV

Use the lvcreate command

root# lvcreate -L <lv_size>  <vg_name> -n <lv_name>
 
# 将卷组vg_name下所有剩余空间给创建的lv_name逻辑卷
root# lvcreate -l +100%FREE  <vg_name> -n <lv_name>
root# lvcreate -L <lv_size> <vg_name> -n <lv_name>

How to use LVM disk operation commands in Linux disk management

After the logical volume is created, you can access it through /dev/mapper/vg_fedora_yg-lv_yg01 or /dev/vg_fedora_yg/lv_yg01:

How to use LVM disk operation commands in Linux disk management

②、View lv

Command lvs, lvscan, lvdisplay to view

How to use LVM disk operation commands in Linux disk management

##③、Expand logical volume
By command lvextend:

root# lvextend -L <extend_size> <lv_path>

How to use LVM disk operation commands in Linux disk management

Note: If the expanded logical volume has been mounted to a specific file system, you need to execute resize2fs or xfs_growfs (for xfs files System) command to make the modification effective, You can use df -Th or blkid to check the file system type mounted by lv.

5. Format and mount LV (logical volume)

After the above logical volume LV is created, it can usually be found under /dev/mapper/ or /dev/vg_name/ If the logical volume cannot be found, you can execute the following command:

# modprobe dm-mod
# vgscan
# vgchange -ay

Finally, you can see the following:

How to use LVM disk operation commands in Linux disk management

①. Format the logical volume
Now you can create a file system on this logical volume:

# mkfs.<filesystem_type> /dev/mapper/<vg_name>-<lv_name>
 
# 
# mkfs.xfs /dev/mapper/vg_fedora_yg-lv_yg01

How to use LVM disk operation commands in Linux disk management

②, mount
# mount /dev/mapper/<vg_name>-<lv_name> <mount_point>

How to use LVM disk operation commands in Linux disk management

Note: Please select the new logical volume you created for the mount point (for example:

/dev/mapper/vg_fedora_yg-lv_yg01), Do not use the actual partition device where the logical volume is located (That is, do not use: /dev/sdb1)

Finally, I drew a picture to show linux lvm:

How to use LVM disk operation commands in Linux disk management

You can refer to the above content to understand the disk management mechanism of Linux lvm.

illustrate:

① In the picture, /dev/sda1 is the boot boot area and cannot be managed by lvm, so it is directly formatted and mounted to the directory /boot. In addition, /dev/sdb2 is also mounted to the directory without being directly formatted by lvm. directory.

②. The capacity of volume group vg_fedora1 is 139G, from which 40 5 45 = 90G is allocated, and there is still 49G free. These free capacities can be lvextended to the following lv; you can also create another lv and allocate go out.

③. Disk settings /dev/sdc still has 50G free space that is not partitioned and can be used after partitioning.

The above is the detailed content of How to use LVM disk operation commands in Linux disk management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete