为Linux操作系统所在的logical volumn扩容

感谢Lieven和Tom的协助,这个问题才得以解决。我在这里把解决问题的步骤总结一下,帮助自己学习。


问题描述

===========

笔者有一台linux的物理机,其上名为centos-root的logical volume没有空间了,

image

可以看到centos-root是mount在Linux系统的根目录的。也就是说,我的linux操作系统没空间了。


问题解决后的效果

============

image

达到这个结果可不容易呢。


第一次尝试-失败

============

由于logical volume,volume group, 和physical volume之间的关系如下:

image

所以,我们先想到的是直接修改当前centos-root这个lv的大小。


我的环境里,只有一个volume group,名叫centos。而这个vg也只包含一个physical volume,名为/dev/sda3. 大概长这个样子。

image

具体如下:

image


image


于是就想,如果直接增大/dev/sda3这个physical volume,那么centos这个volume group就自动扩大了,之后再扩大/dev/centos/root这个logical volume,那么我们的操作系统不就有空间了吗。


我们直接通过命令pvresize,扩大了/dev/sda3这个physical volume的大小. 之后运行了lvresize, 扩大了/dev/centos/root这个logical volume,之后就收到提示:logical volume is suspended。操作系统直接宕机了。


后来分析,发生这个问题的原因是,虽然pv和lv的大小都变大了,但是操作系统所在分区的分区大小并没有变大。相当于装东西的篮子没变大,东西却变大了,自然就装不下了。


通过插入Linux的启动盘,进入rescue mode,把lv的大小恢复回去,pv的大小恢复回去,系统才被救回来。


这里还涉及到一个紧张有趣的计算。我们往回改lv的时候,怕制定的大小过小了从而截断了原本的数据卷,所以制定的lv偏大了一些,系统提示了原来的lv的sector的数量,还有现在的sector的数量。通过这个提示,我们才精确的还原了LV和PV。


第二次尝试

============

这次换了方法,决定原来的pv不去动它,再加一个新的pv,然后把新建的pv加入vg里,之后再把lv扩容,一样可以解决问题。


开门就碰壁,fdisk居然无法创建新的分区,报错如下:

image


使用parted这个分区工具就可以了。网上讨论fdisk和parted哪个好,额,我这里投parted一票。

1. 使用parted工具创建分区的过程如下。注意这里为了追求保险,从17G处开始新分区。

image


2. 运行一下partprobe,让操作系统重新读取分区表,让新分区生效,避免重启。网上的文章解释如下:

If you are using hot swappable hard disk and created a new partition using the fdisk, then you need to reboot Linux based system to get partition recognized. Without reboot, you will NOT be able to create a filesystem on your newly created or modified partitions with the mke2fs command.
The kernel still uses the old table. The new table will be utilized at the next reboot or after you run partprobe or kpartx command. Both of these programs informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table.

image


3. 之后再进入fdisk把分区的类型修改为Linux LVM。

image

修改好了,检查一下。

image


4. 创建pv:/dev/sda4.

image


5. 扩展vg。

image


6. 使用lvresize命令扩展lv。

image


7. 尝试用resize2fs命令来扩展文件系统的大小,失败。报错:Bad magic number in super-block while trying to open /dev/centos/root. Couldn’t find valid filesytem superblock.

image


8. LV已经扩大了,只剩下最后一步,扩大其上的文件系统了。改用xfs_growfs /dev/centos/root来扩展文件系统的大小,因为我们的文件系统在第1步的时候,配了文件系统为xfs,所以要用这个工具才可以修改。

image


9.大功告成。

image


总结:为了解决这个问题,一下子pv,vg,lv,partition,file system这些概念以及他们之间的关系都差不多搞清楚了。花了这么多时间整理这个问题,感觉还是值得的。

原文地址:https://www.cnblogs.com/awpatp/p/8797122.html