Linux Logical Volume LVM Operation Documentation

Introduction to logical volumes:
A logical volume is a virtual disk formed by a logical disk, and can also be called a disk partition.
LVM (Logical Volume Manager), Logical Volume Manager.
LVM virtualizes storage, using logical volumes, you are not limited by the size of physical disks. In addition, hardware-related storage settings are hidden, and you can resize volumes or migrate data without stopping applications or unmounting file systems, which reduces operating costs. Logical volumes provide a convenient way to mirror your data and support snapshot functionality.
LVM logical volumes are a great space usage mechanism in Linux, because partitions cannot be made larger or smaller without formatting. With LVM, you can make your disk space flexible.
 

Basic logical volume management concepts:


PV (Physical Volume) - Physical Volume 
Physical volume is at the lowest level in logical volume management. It can be a partition on an actual physical hard disk, an entire physical hard disk , or a raid device .

VG ( 
Volumne Group A volume group is built on a physical volume. A volume group must include at least one physical volume. After the volume group is established, a physical volume can be dynamically added to the volume group. A logical volume management system project can have only one volume group or multiple volume groups.

LV (Logical Volume) - Logical Volume Logical 
volume is built on the volume group. The unallocated space in the volume group can be used to create a new logical volume. After the logical volume is created, the space can be dynamically expanded and reduced. Multiple logical volumes in the system can belong to the same volume group or to different volume groups.

Logical volume operations:
1 Create a logical volume
fdisk /dev/sdb
p
n
p
w
t
The logo of LVM is 8e
 
pvcreate /dev/sdb1 /dev/sdb2
(Note: If the /dev/sdb2 device is not recognized, the server needs to be restarted)
pvs
 
 
vgcreate vgname /dev/sdb1 /dev/sdb2
vgs
 
lvcreate -L 500M -n lvname vgname
lvs
 
mkfs.ext4  /dev/vgname/lvname
 
mount
 
2 Logical volume expansion
 
How to expand the volume group when the volume group is not enough
Recreate a partition sdb3 from the second hard drive
pvcreate /dev/sdb3
vgextend vgname /dev/sdb3
pvs
The logical volume lvname is not enough, how to expand
Adding space to a logical volume does not affect previous space usage, so there is no need to unmount the filesystem
Directly through the command:
lvextend –L +500M /dev/vgname/lvname
Or lvextend –l 2.5G /dev/vgname/lvname
Add 500M space to lvname (lvname is currently 2G space)
After the setup is complete, remember to use:
resize2fs /dev/vgname/lvname
command to synchronize the file system.
The centos7 series uses xfs_growfs /dev/centos/root because the file system is generally in xfs format
 
 
 
 
3 Logical volumes other
When the hard disk space is not enough, if the space of the logical volume is reduced, it will be released to other logical volumes.
 
1. Unmount the logical volume lvname first
 
umount /data
In some cases it can be done by lsof(8) or fuser(1)
Find useful information about the processes using the device

2. Then use the e2fsck command to detect the free space on the logical volume.

e2fsck -f /dev/mapper/vgname-lvname

3. Use resize2fs to reduce the file system to 700M.

resize2fs /dev/mapper/vgname-lvname 700M

4、 再使用lvreduce命令将逻辑卷减少到700M。

lvreduce -L 700M  /dev/mapper/vgname-lvname

注意:文件系统大小和逻辑卷大小一定要保持一致才行。如果逻辑卷大于文件系统,由于部分区域未格式化成文件系统会造成空间的浪费。如果逻辑卷小于文件系统,哪数据就出问题了。

完成之后,就可以通过mount命令挂载重新使用了。

 

4 转移数据

 

5 删除逻辑卷

 

1、先通过umount命令卸载掉逻辑卷lvname

umount /data

2、修改/etc/fstab里面逻辑卷的挂载信息,否则系统有可能启动不起来。

3、通过lvremove 删除逻辑卷lvname

lvremove /dev/mapper/vgname-lvname

4、通过vgremove 删除卷组vgname

vgremove vgname

5、通过pvremove 将物理卷转化成普通分区。

pvremove  /dev/sdb1 /dev/sdb2

pvs

vgs

lvs

删除完了,别忘了修改分区的id标识。修改成普通Linux分区即可。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326179749&siteId=291194637