在之前 LVM分区无损增减 篇中有提到过LVM分区的无损增大和减小,不同的是,当是只是针对LVM下的ext分区的,在新版的RHEL7/centos7中默认使用的分区格式是xfs ,本篇就针对LVM下ext4和xfs分区的增大和缩小再唠叨下。
一、ext4分区下LVM的动态调整
在使用lvresize调整大小时结常需要注意与resize2fs执行的顺序。缩小空间时,一定要先执行resize2fs再执行lvresize;而增大时一定要先执行lvextend再执行resize2fs 。
1、减小大小
减小LVM分区时,一定要先umount掉挂载点再进行操作。
- [root@361way.com ~]# df -H /mnt/
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vg-lvol0 805M 2.7M 760M 1% /mnt
- [root@361way.com ~]# umount /mnt/
- [root@361way.com ~]# resize2fs -f /dev/vg/lvol0 100M
- resize2fs 1.41.12 (17-May-2010)
- Resizing the filesystem on /dev/vg/lvol0 to 102400 (1k) blocks.
- The filesystem on /dev/vg/lvol0 is now 102400 blocks long.
- [root@361way.com ~]# lvresize -L 100M /dev/vg/lvol0
- WARNING: Reducing active logical volume to 100.00 MiB
- THIS MAY DESTROY YOUR DATA (filesystem etc.)
- Do you really want to reduce lvol0? [y/n]: y
- Size of logical volume vg/lvol0 changed from 800.00 MiB (200 extents) to 100.00 MiB (25 extents).
- Logical volume lvol0 successfully resized
- [root@361way.com ~]# mount /dev/vg/lvol0 /mnt/
- [root@361way.com ~]# df -H /mnt/
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vg-lvol0 94M 1.6M 87M 2% /mnt
- [root@361way.com ~]#
2、增大空间
- [root@361way.com ~]# lvextend -L 800M /dev/vg/lvol0
- Size of logical volume vg/lvol0 changed from 100.00 MiB (25 extents) to 800.00 MiB (200 extents).
- Logical volume lvol0 successfully resized
- [root@361way.com ~]# resize2fs -f /dev/vg/lvol0
- resize2fs 1.41.12 (17-May-2010)
- Filesystem at /dev/vg/lvol0 is mounted on /mnt; on-line resizing required
- old desc_blocks = 1, new_desc_blocks = 4
- Performing an on-line resize of /dev/vg/lvol0 to 819200 (1k) blocks.
- The filesystem on /dev/vg/lvol0 is now 819200 blocks long.
- [root@361way.com ~]# df -H /mnt/
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vg-lvol0 805M 2.7M 761M 1% /mnt
3、一个命令搞定增大和减小
一会儿是增大,一会儿是减小,搞不好很容易搞错并造成分区损坏、数据受损,有没有不管增大减小一个命令全搞定的方法?当然有lvresize命令里有一个-r选项,该选项的作用是在执行lvresize命令的时候一并执行resize2fs ,强烈推荐该方法。
- [root@361way.com ~]# df -H /mnt/
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vg-lvol0 805M 2.7M 761M 1% /mnt
- [root@361way.com ~]# lvresize -L 300M -r /dev/vg/lvol0 -->注意此处的指定,增大时同样使用该指令
- Do you want to unmount "/mnt"? [Y|n] y
- fsck from util-linux-ng 2.17.2
- /dev/mapper/vg-lvol0: 11/203200 files (0.0% non-contiguous), 36387/819200 blocks
- resize2fs 1.41.12 (17-May-2010)
- Resizing the filesystem on /dev/mapper/vg-lvol0 to 307200 (1k) blocks.
- The filesystem on /dev/mapper/vg-lvol0 is now 307200 blocks long.
- Size of logical volume vg/lvol0 changed from 800.00 MiB (200 extents) to 300.00 MiB (75 extents).
- Logical volume lvol0 successfully resized
- [root@361way.com ~]#
- [root@361way.com ~]# df -H /mnt/
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vg-lvol0 297M 2.2M 279M 1% /mnt
上面已经由800M 减小到了300M,而且更神奇的是,减小前我并没有umount分区,它自动提示让我们umount掉。此处使用同样的指令增大到500M 。
- [root@361way.com ~]# lvresize -L 500M -r /dev/vg/lvol0
- Size of logical volume vg/lvol0 changed from 300.00 MiB (75 extents) to 500.00 MiB (125 extents).
- Logical volume lvol0 successfully resized
- resize2fs 1.41.12 (17-May-2010)
- Filesystem at /dev/mapper/vg-lvol0 is mounted on /mnt; on-line resizing required
- old desc_blocks = 2, new_desc_blocks = 2
- Performing an on-line resize of /dev/mapper/vg-lvol0 to 512000 (1k) blocks.
- The filesystem on /dev/mapper/vg-lvol0 is now 512000 blocks long.
- [root@361way.com ~]# df -H /mnt/
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vg-lvol0 500M 2.4M 472M 1% /mnt
见证到奇迹了吧,就是这么任性。
二、XFS分区
xfs分区是不支持减小操作的,这点挺蛋疼。我们先当做不知道该特性,同lvresize -r 指定试下,结果如下:
- # lvresize -L 50M /dev/vg2/xfstest -r
- Phase 1 - find and verify superblock...
- Phase 2 - using internal log
- - scan filesystem freespace and inode maps...
- - found root inode chunk
- Phase 3 - for each AG...
- - scan (but don't clear) agi unlinked lists...
- - process known inodes and perform inode discovery...
- - agno = 0
- - agno = 1
- - agno = 2
- - agno = 3
- - agno = 4
- - process newly discovered inodes...
- Phase 4 - check for duplicate blocks...
- - setting up duplicate extent list...
- - check for inodes claiming duplicate blocks...
- - agno = 0
- - agno = 1
- - agno = 2
- - agno = 3
- - agno = 4
- No modify flag set, skipping phase 5
- Phase 6 - check inode connectivity...
- - traversing filesystem ...
- - traversal finished ...
- - moving disconnected inodes to lost+found ...
- Phase 7 - verify link counts...
- No modify flag set, skipping filesystem flush and exiting.
- fsadm: Xfs filesystem shrinking is unsupported -->看到相关信息了,不好意思,xfs不支持
- fsadm failed: 1
- Filesystem resize failed.
增大看下:
- # df -Th /mnt
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/vg2-xfstest xfs 97M 5.3M 92M 6% /mnt
- 先使用lvextend增大LVM分区的大小
- # lvextend -L 120M /dev/vg2/xfstest
- Extending logical volume vo to 120.00 MiB
- Logical volume vo successfully resized
- 再使用xfs_growfs在物理分区上调整
- # xfs_growfs /dev/vg2/xfstest
- meta-data=/dev/mapper/vg2-xfstest isize=256 agcount=5, agsize=5120 blks
- = sectsz=512 attr=2, projid32bit=1
- = crc=0
- data = bsize=4096 blocks=25600, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0 ftype=0
- log =internal bsize=4096 blocks=853, version=2
- = sectsz=512 sunit=0 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
- data blocks changed from 25600 to 30720
- # df -Th /mnt
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/vg2-xfstest xfs 117M 5.3M 92M 6% /mnt