Detailed explanation of LVM logical volume configuration process

Introduction : LVM is a mechanism for managing disk partitions in the Linux environment. It is a logical layer built on top of hard disks and partitions and under the file system, which can improve the flexibility of disk partition management.

introduce

Many Linux users will encounter such a dilemma when installing the operating system: how to accurately evaluate and allocate the capacity of each hard disk partition. If the initial evaluation is not accurate, once the system partition is not enough, it may have to back up, delete related data, or even be forced to renew Plan partitions and reinstall the operating system to meet the needs of the application system.

LVM is a mechanism for managing disk partitions in the Linux environment. It is a logical layer built on top of hard disks and partitions and under the file system, which can improve the flexibility of disk partition management. The default partition format of KYLIN V3 is the LVM logical volume format. It should be noted that the /boot partition cannot be created based on LVM and must be independent. 

The following figure parses the entire process of LVM creation.

 

  

Detailed configuration

lab environment

First create two partitions sdb1 1G, sdb2 2G from the empty hard disk sdb. Prepare for the next LVM.

 

  

In order to facilitate maintenance and management later, remember to add a mark to the partition, so that even if you are not there, others will not easily move this area when they see the mark. The logo of LVM is 8e, remember to press w after setting to save.

 

 

Create logical volumes

Convert the two newly created partitions /dev/sdb1 /dev/sdb2 into physical volumes, mainly adding LVM attribute information and dividing PE storage units.

Create a volume group vgdata and add the two physical volumes just created to the volume group. It can be seen that the default PE size is 4MB, and PE is the smallest storage unit of the volume group. The size can be modified by the –s parameter.

Split 500M from the physical volume vgdata to the new logical volume lvdata1.

Create an ext4 file system on the logical volume lvdata1 using the mkfs.ext4 command.

Mount the created file system /data1 to /data1. (After creation, a soft connection named "Volume Group-Logical Volume" will be generated in /dev/mapper/)

To facilitate automatic mounting after server restart, you need to add the created file system mount information to /etc/fstab. UUID can be queried through the blkid command.

为了查看/etc/fstab是否设置正确,可以先卸载逻辑卷data1,然后使用mount –a 使内核重新读取/etc/fstab,看是否能够自动挂载.

逻辑卷lvdata1不够用了,如何扩展

给逻辑卷增加空间并不会影响以前空间的使用,所以无需卸载文件系统,直接通过命令lvextend –L +500M /dev/vgdata/lvdata1或者lvextend –l 2.5G /dev/vgdata/lvdata1 给lvdata1增加500M空间(lvdata1目前是2G空间)设置完成之后,记得使用resize2fs命令来同步文件系统。


当卷组不够用的情况下,如何扩大卷组

重新从第二块硬盘上创建一个分区sdb3,具体操作步骤省略。并将创建好的分区加入到已经存在的卷组vgdata中。通过pvs命令查看是否成功。

当硬盘空间不够用的情况下,如果减少逻辑卷的空间释放给其他逻辑卷使用

减少逻辑卷空间,步骤如下:

1、 先卸载逻辑卷data1;

2、 然后通过e2fsck命令检测逻辑卷上空余的空间;

3、 使用resize2fs将文件系统减少到700M;

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

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

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

如果某一块磁盘或者分区故障了如何将数据快速转移到相同的卷组其他的空间去

1、通过pvmove命令转移空间数据 ;

2、通过vgreduce命令将即将坏的磁盘或者分区从卷组vgdata里面移除除去;

3、通过pvremove命令将即将坏的磁盘或者分区从系统中删除掉;

4、手工拆除硬盘或者通过一些工具修复分区。

删除整个逻辑卷

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

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

3、通过lvremove 删除逻辑卷lvdata1;

4、通过vgremove 删除卷组vgdata; 

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

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

总结

LVM逻辑卷是Linux里面一个很棒的空间使用机制,因为分区在没有格式化的情况下是没有办法加大或者放小的。通过LVM可以将你的磁盘空间做到灵活自如。

逻辑卷虽然给磁盘操作带来了很大的灵活性,却同时给数据的安全性增添了更多隐患。例如建立在LVM上的分区,如果LVM分区信息损坏,则很难恢复数据。

用户如果对数据安全性要求较高,不推荐使用LVM管理磁盘,直接使用标准分区方式管理磁盘。

Guess you like

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