由lvextend -r -l +100%FREE vg/lv 自动同步文件系统,引发的一点思考
1. lvm扩容一般流程:
1)先扩充vg,vgextend
2) 再扩lv,如:lvextend,lvresize
3)最后做文件系统同步,如:xfs_growfs
2. 以前一直是如此扩容的,今天突然看到一个简化命令:可以把第二步和第三步一条命令搞定:
lvextend -r -l +100%FREE vg/lv
或
lvextend -r vg/lv pv
-r :--resizefs: 自动同步(扩容)文件系统
注:
环境:一次在麒麟arm环境下,发现使用xfs_growfs /dev/mapper/kylin-root失败, 报错:不是xfs文件系统,但是df -Th查看确实是xfs文件系统, 操作:修改命令为 xfs_growfs / ,然后成功了, 结论:文件系统同步时使用的其实是挂载点,而不是卷
3. lvresize --help发现-r选项可以自动同步文件系统
于是直接缩根分区试试:
测试环境,切勿模仿!!!
[root@localhost ~]# lvresize -r -L 32G /dev/nfschina-server-4.0/root Do you want to unmount "/" ? [Y|n] y umount: /: target is busy. fsadm: Cannot proceed with mounted filesystem "/". /usr/sbin/fsadm failed: 1 Filesystem resize failed. [root@localhost ~]# lvresize -L 32G /dev/nfschina-server-4.0/root WARNING: Reducing active and open logical volume to 32.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce nfschina-server-4.0/root? [y/n]: y Size of logical volume nfschina-server-4.0/root changed from <42.20 GiB (10802 extents) to 32.00 GiB (8192 extents). Logical volume nfschina-server-4.0/root successfully resized. [root@localhost ~]# e2fsck -f /dev/nfschina-server-4.0/root e2fsck 1.44.6 (5-Mar-2019) /dev/nfschina-server-4.0/root 已挂载。 e2fsck: 无法继续,已中止。 [root@localhost ~]# resize2fs /dev/nfschina-server-4.0/root resize2fs 1.44.6 (5-Mar-2019) /dev/nfschina-server-4.0/root 上的文件系统已被挂载于 /;需要进行在线调整大小 resize2fs: 不支持在线缩小块 [root@localhost ~]#
玩犊子,文件系统同步不过来了,这种情况该怎么办?
心态要稳,此时写入4G的数据先做个测试,一切正常
[root@localhost ~]# dd if=/dev/zero of=test.txt bs=4M count=1024
记录了1024+0 的读入
记录了1024+0 的写出
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 5.9795 s, 718 MB/s
但是问题是一直在的,重启系统就完犊子了
没办法只能重新把分区空间添加回来
操作失误,把所有剩余空间都回收到vg了,似乎彻底完犊子了。。。
lvresize -l +100%FREE /dev/nfschina-server-4.0/root
测试环境,切勿模仿!!!
[root@localhost ~]# lvresize -r -l 100%FREE /dev/nfschina-server-4.0/root Do you want to unmount "/" ? [Y|n] y umount: /: target is busy. fsadm: Cannot proceed with mounted filesystem "/". /usr/sbin/fsadm failed: 1 Filesystem resize failed. [root@localhost ~]# lvresize -l 100%FREE /dev/nfschina-server-4.0/root WARNING: Reducing active and open logical volume to <10.20 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce nfschina-server-4.0/root? [y/n]: y Size of logical volume nfschina-server-4.0/root changed from 32.00 GiB (8192 extents) to <10.20 GiB (2610 extents). /etc/lvm/backup/nfschina-server-4.0.tmp: fsync failed: Input/output error /etc/lvm/backup/nfschina-server-4.0.tmp: fclose failed: Read-only file system Backup of volume group nfschina-server-4.0 metadata failed. Logical volume nfschina-server-4.0/root successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root nfschina-server-4.0 -wi-ao---- <10.20g swap nfschina-server-4.0 -wi-ao---- 4.80g [root@localhost ~]# lvextend -l 100%FREE /dev/nfschina-server-4.0/root Cannot archive volume group metadata for nfschina-server-4.0 to read-only filesystem. [root@localhost ~]# lvresize -l +100%FREE /dev/nfschina-server-4.0/root Cannot archive volume group metadata for nfschina-server-4.0 to read-only filesystem. [root@localhost ~]# lvextend -l +100%FREE /dev/nfschina-server-4.0/root Cannot archive volume group metadata for nfschina-server-4.0 to read-only filesystem. [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree nfschina-server-4.0 1 2 0 wz--n- <47.00g 32.00g [root@localhost ~]#
此时发现系统核心转储,几乎所有系统命令都无法执行了
[root@localhost ~]# ls Segmentation fault
重启后,直接进入了救援模式,起一个livecd,然后把lv扩容回去。然后重启系统,结果系统成功起来。
在未重启前,正常情况下,可以通过将lv扩充回来解决系统文件系统与lv不合的问题
[root@localhost setup]# lvresize -L 32G /dev/nfschina-server-4.0/root WARNING: Reducing active and open logical volume to 32.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce nfschina-server-4.0/root? [y/n]: y Size of logical volume nfschina-server-4.0/root changed from <42.20 GiB (10802 extents) to 32.00 GiB (8192 extents). Logical volume nfschina-server-4.0/root successfully resized. [root@localhost setup]# e2fsck -f /dev/nfschina-server-4.0/root e2fsck 1.44.6 (5-Mar-2019) /dev/nfschina-server-4.0/root 已挂载。 e2fsck: 无法继续,已中止。 [root@localhost setup]# lvresize -l +100%FREE /dev/nfschina-server-4.0/root Size of logical volume nfschina-server-4.0/root changed from 32.00 GiB (8192 extents) to <42.20 GiB (10802 extents). Logical volume nfschina-server-4.0/root successfully resized. [root@localhost setup]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root nfschina-server-4.0 -wi-ao---- <42.20g swap nfschina-server-4.0 -wi-ao---- 4.80g [root@localhost setup]# vgs VG #PV #LV #SN Attr VSize VFree nfschina-server-4.0 1 2 0 wz--n- <47.00g 0
注1:推测,若此时文件系统写入数据量过大,接近乃至于"超过"实际硬盘空间,数据"溢出",则可能导致文件系统出现不可逆转的损坏。
注2:经测试,使用ll与du查看,lv缩容前后使用dd写入的文件大小一致。
注3:如果vg余量不为0,且不确定vg剩余空间大小,则会导致lv实际空间比文件系统要大,肯定是需要再次同步文件系统的,具体操作暂未实验。