在之前 LVM分区无损增减 篇中有提到过LVM分区的无损增大和减小,不同的是,当是只是针对LVM下的ext分区的,在新版的RHEL7/centos7中默认使用的分区格式是xfs ,本篇就针对LVM下ext4和xfs分区的增大和缩小再唠叨下。

一、ext4分区下LVM的动态调整

在使用lvresize调整大小时结常需要注意与resize2fs执行的顺序。缩小空间时,一定要先执行resize2fs再执行lvresize;而增大时一定要先执行lvextend再执行resize2fs 。

1、减小大小

减小LVM分区时,一定要先umount掉挂载点再进行操作。

 1[root@361way.com ~]# df -H /mnt/
 2Filesystem            Size  Used Avail Use% Mounted on
 3/dev/mapper/vg-lvol0  805M  2.7M  760M   1% /mnt
 4[root@361way.com ~]# umount /mnt/
 5[root@361way.com ~]# resize2fs -f /dev/vg/lvol0 100M
 6resize2fs 1.41.12 (17-May-2010)
 7Resizing the filesystem on /dev/vg/lvol0 to 102400 (1k) blocks.
 8The filesystem on /dev/vg/lvol0 is now 102400 blocks long.
 9[root@361way.com ~]# lvresize -L 100M /dev/vg/lvol0
10  WARNING: Reducing active logical volume to 100.00 MiB
11  THIS MAY DESTROY YOUR DATA (filesystem etc.)
12Do you really want to reduce lvol0? [y/n]: y
13  Size of logical volume vg/lvol0 changed from 800.00 MiB (200 extents) to 100.00 MiB (25 extents).
14  Logical volume lvol0 successfully resized
15[root@361way.com ~]# mount /dev/vg/lvol0 /mnt/
16[root@361way.com ~]# df -H /mnt/
17Filesystem            Size  Used Avail Use% Mounted on
18/dev/mapper/vg-lvol0   94M  1.6M   87M   2% /mnt
19[root@361way.com ~]# 

2、增大空间

 1[root@361way.com ~]# lvextend -L 800M /dev/vg/lvol0
 2  Size of logical volume vg/lvol0 changed from 100.00 MiB (25 extents) to 800.00 MiB (200 extents).
 3  Logical volume lvol0 successfully resized
 4[root@361way.com ~]# resize2fs -f /dev/vg/lvol0
 5resize2fs 1.41.12 (17-May-2010)
 6Filesystem at /dev/vg/lvol0 is mounted on /mnt; on-line resizing required
 7old desc_blocks = 1, new_desc_blocks = 4
 8Performing an on-line resize of /dev/vg/lvol0 to 819200 (1k) blocks.
 9The filesystem on /dev/vg/lvol0 is now 819200 blocks long.
10[root@361way.com ~]# df -H /mnt/
11Filesystem            Size  Used Avail Use% Mounted on
12/dev/mapper/vg-lvol0  805M  2.7M  761M   1% /mnt

3、一个命令搞定增大和减小

一会儿是增大,一会儿是减小,搞不好很容易搞错并造成分区损坏、数据受损,有没有不管增大减小一个命令全搞定的方法?当然有lvresize命令里有一个-r选项,该选项的作用是在执行lvresize命令的时候一并执行resize2fs ,强烈推荐该方法

 1[root@361way.com ~]# df -H /mnt/
 2Filesystem            Size  Used Avail Use% Mounted on
 3/dev/mapper/vg-lvol0  805M  2.7M  761M   1% /mnt
 4[root@361way.com ~]# lvresize -L 300M -r /dev/vg/lvol0   -->注意此处的指定,增大时同样使用该指令
 5Do you want to unmount "/mnt"? [Y|n] y
 6fsck from util-linux-ng 2.17.2
 7/dev/mapper/vg-lvol0: 11/203200 files (0.0% non-contiguous), 36387/819200 blocks
 8resize2fs 1.41.12 (17-May-2010)
 9Resizing the filesystem on /dev/mapper/vg-lvol0 to 307200 (1k) blocks.
10The filesystem on /dev/mapper/vg-lvol0 is now 307200 blocks long.
11  Size of logical volume vg/lvol0 changed from 800.00 MiB (200 extents) to 300.00 MiB (75 extents).
12  Logical volume lvol0 successfully resized
13[root@361way.com ~]#
14[root@361way.com ~]# df -H /mnt/
15Filesystem            Size  Used Avail Use% Mounted on
16/dev/mapper/vg-lvol0  297M  2.2M  279M   1% /mnt

上面已经由800M 减小到了300M,而且更神奇的是,减小前我并没有umount分区,它自动提示让我们umount掉。此处使用同样的指令增大到500M 。

 1[root@361way.com ~]# lvresize -L 500M -r /dev/vg/lvol0
 2  Size of logical volume vg/lvol0 changed from 300.00 MiB (75 extents) to 500.00 MiB (125 extents).
 3  Logical volume lvol0 successfully resized
 4resize2fs 1.41.12 (17-May-2010)
 5Filesystem at /dev/mapper/vg-lvol0 is mounted on /mnt; on-line resizing required
 6old desc_blocks = 2, new_desc_blocks = 2
 7Performing an on-line resize of /dev/mapper/vg-lvol0 to 512000 (1k) blocks.
 8The filesystem on /dev/mapper/vg-lvol0 is now 512000 blocks long.
 9[root@361way.com ~]# df -H /mnt/
10Filesystem            Size  Used Avail Use% Mounted on
11/dev/mapper/vg-lvol0  500M  2.4M  472M   1% /mnt

见证到奇迹了吧,就是这么任性。

二、XFS分区

xfs分区是不支持减小操作的,这点挺蛋疼。我们先当做不知道该特性,同lvresize -r 指定试下,结果如下:

 1# lvresize -L 50M /dev/vg2/xfstest -r
 2Phase 1 - find and verify superblock...
 3Phase 2 - using internal log
 4        - scan filesystem freespace and inode maps...
 5        - found root inode chunk
 6Phase 3 - for each AG...
 7        - scan (but don't clear) agi unlinked lists...
 8        - process known inodes and perform inode discovery...
 9        - agno = 0
10        - agno = 1
11        - agno = 2
12        - agno = 3
13        - agno = 4
14        - process newly discovered inodes...
15Phase 4 - check for duplicate blocks...
16        - setting up duplicate extent list...
17        - check for inodes claiming duplicate blocks...
18        - agno = 0
19        - agno = 1
20        - agno = 2
21        - agno = 3
22        - agno = 4
23No modify flag set, skipping phase 5
24Phase 6 - check inode connectivity...
25        - traversing filesystem ...
26        - traversal finished ...
27        - moving disconnected inodes to lost+found ...
28Phase 7 - verify link counts...
29No modify flag set, skipping filesystem flush and exiting.
30fsadm: Xfs filesystem shrinking is unsupported   -->看到相关信息了,不好意思,xfs不支持
31  fsadm failed: 1
32  Filesystem resize failed.

增大看下:

 1# df -Th /mnt
 2Filesystem                 Type  Size  Used Avail Use% Mounted on
 3/dev/mapper/vg2-xfstest xfs    97M  5.3M   92M   6% /mnt
 4先使用lvextend增大LVM分区的大小
 5# lvextend -L 120M /dev/vg2/xfstest
 6  Extending logical volume vo to 120.00 MiB
 7  Logical volume vo successfully resized
 8再使用xfs_growfs在物理分区上调整
 9# xfs_growfs /dev/vg2/xfstest
10meta-data=/dev/mapper/vg2-xfstest isize=256    agcount=5, agsize=5120 blks
11         =                       sectsz=512   attr=2, projid32bit=1
12         =                       crc=0
13data     =                       bsize=4096   blocks=25600, imaxpct=25
14         =                       sunit=0      swidth=0 blks
15naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
16log      =internal               bsize=4096   blocks=853, version=2
17         =                       sectsz=512   sunit=0 blks, lazy-count=1
18realtime =none                   extsz=4096   blocks=0, rtextents=0
19data blocks changed from 25600 to 30720
20# df -Th /mnt
21Filesystem                 Type  Size  Used Avail Use% Mounted on
22/dev/mapper/vg2-xfstest xfs    117M  5.3M   92M   6% /mnt