LVM logical volume management
brief introduction
LVM is the abbreviation of Logical Volume Manager (logical volume management). It is a mechanism for managing disk partitions in Linux environment, which can dynamically adjust the partition size.
- Physical volume (PV): a real physical hard disk or partition.
- Volume group (VG): multiple physical volumes are combined to form a volume group. The physical volumes forming the same volume group can be different partitions of the same hard disk or different partitions on different hard disks. The volume group can be imagined as a logical hard disk.
- Logical volume (LV): volume group is a logical hard disk. The hard disk must be partitioned before it can be used. This partition is called logical volume. Logical volumes can format and write data, and logical volumes can be imagined as component areas.
- Physical expansion (PE): the smallest unit used by PE to store data. Our data is actually written to PE. The size of PE can be configured. The default is 4MB.
Establish LVM
step
- Divide the physical hard disk into partitions. Of course, it can also be a whole physical hard disk.
- Then build the physical partition into a physical volume, or directly build the whole hard disk into a physical volume.
- Consolidate physical volumes into volume groups, which can also be resized.
- The volume group is divided into logical volumes, which can also be resized. The logical volume can be imagined as a component area, which also needs to be formatted and mounted.
Physical volume management
Note: the boot partition must be placed in the standard partition, not in the LVM volume group partition, otherwise it cannot be started.
- Hard disk partition
Use the fdisk interactive command to partition, but finally change the file system to LVM, from 83 to 8e. - Create physical volume
[root@localhost ~]# pvcreate device file name
If you do not have this command, you can install lvm2.
View physical volumes:
[root@localhost ~]# pvscan or [root@localhost ~]# pvdisplay
Example:
[root@localhost ~]# pvcreate /dev/sdb5 Physical volume "/dev/sdb5" successfully created. [root@localhost ~]# pvcreate /dev/sdb6 Physical volume "/dev/sdb6" successfully created. [root@localhost ~]# pvcreate /dev/sdb7 Physical volume "/dev/sdb7" successfully created.
Volume group management
- Create volume group
[root@localhost ~]# vgcreate option volume group name physical volume name
Options:
- -s PE size: Specifies the size of PE. The unit can be MB, GB, TB, etc. if not written, the default is 4MB.
Example:
[root@localhost ~]# vgcreate -s 4MB wvg /dev/sdb5 /dev/sdb6 Volume group "wvg" successfully created #Integrate / dev/sdb5 and / dev/sdb6 into a volume group. The PE size is 4MB and the volume group name is wvg.
- View volume groups:
[root@localhost ~]# vgscan or [root@localhost ~]# vgdisplay
- Volume group expansion
#Add / dev/sdb7 to the volume group wvg [root@localhost ~]# vgextend wvg /dev/sdb7 Volume group "wvg" successfully extended
- Reduce volume group capacity
[root@localhost ~]# vgreduce volume group name partition name
In theory, you can use the vgraduce command to reduce the volume group capacity, but try not to do so because it may cause data loss.
- Delete volume group
[root@localhost ~]# vgremove volume group name
Logical volume management
- Create logical volume
[root@localhost ~]# lvcreate option - n logical volume name volume group name
Common options:
- -L capacity: Specifies the size of the logical volume.
- -l Number: specify the logical volume size according to the number of PE. This parameter needs to convert the capacity, which is too troublesome.
- -n logical volume name: Specifies the logical volume name.
Example:
Create a size of 5 G A logical volume named ulv1. [root@localhost ~]# lvcreate -L 5G -n ulv1 wvg Logical volume "ulv1" created.
- Viewing logical volumes
[root@localhost ~]# lvscan or [root@localhost ~]# lvdisplay
- Resize logical volumes
[root@localhost ~]# lvresize option logical volume device file name
Options:
- -L capacity: the installation capacity is resized in KB, GB, TB, etc. use + to increase the space, and - to reduce the space. If the direct write capacity is used, it means to set the logical volume size to the specified size.
- -l Number: adjust the size of the logical volume according to the PE size.
After resizing, run the following command:
[root@localhost ~]# resize2fs option resizes the device file name
Options:
-f: Forced adjustment
Example:
[root@localhost ~]# lvresize -L 5.5G /dev/wvg/ulv1 Size of logical volume wvg/ulv1 changed from 5.00 GiB (1280 extents) to 5.50 GiB (1408 extents). Logical volume wvg/ulv1 successfully resized.
- Delete logical volume
[root@localhost ~]# lvremove logical volume device file name
After the logical volume is created, it can be mounted and used normally.
ps: Shang Silicon Valley linux video course Study notes