LVM管理之减少LV的大小

时间:2021-07-05 04:23:47

LVM管理之减少LV的大小

规定动作
1.umount filesystem
2.e2fsck filesystem
3.resize2fs filesystem
4.lvredure

实例演示————减少/home目录的大小,由40G调整到15G:
1.当前环境的VG,PV,LV的相关信息:
[root@powerpd ~]# vgs
  VG         #PV #LV #SN Attr   VSize   VFree 
  vg_powerpd   1   3   0 wz--n- 278.38g 123.35g
[root@powerpd ~]# pvs
  PV         VG         Fmt  Attr PSize   PFree 
  /dev/sda2  vg_powerpd lvm2 a--  278.38g 123.35g
[root@powerpd ~]# lvs
  LV      VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  lv_home vg_powerpd -wi-ao 40.00g                                     
  lv_root vg_powerpd -wi-ao 50.00g                                     
  lv_swap vg_powerpd -wi-ao 65.03g
 
[root@powerpd ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_powerpd-lv_root
                       50G  4.4G   43G  10% /
tmpfs                  32G  260K   32G   1% /dev/shm
/dev/sda1             485M   37M  423M   8% /boot
/dev/mapper/vg_powerpd-lv_home
                       40G  176M   38G   1% /home

2.umount filesystem
#umount /home

3.e2fsck filesystem
[root@powerpd ~]# e2fsck /dev/vg_powerpd/lv_home
e2fsck 1.41.12 (17-May-2010)
/dev/vg_powerpd/lv_home: clean, 11/2621440 files, 209554/10485760 blocks

4.resize2fs filesystem
[root@powerpd ~]# resize2fs -f /dev/vg_powerpd/lv_home 15g
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_powerpd/lv_home to 3932160 (4k) blocks.
The filesystem on /dev/vg_powerpd/lv_home is now 3932160 blocks long.

5.lvreduce
[root@powerpd ~]# lvreduce -L15g /dev/vg_powerpd/lv_home
  WARNING: Reducing active logical volume to 15.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
  Reducing logical volume lv_home to 15.00 GiB
  Logical volume lv_home successfully resized
[root@powerpd ~]# lvs
  LV      VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  lv_home vg_powerpd -wi-a- 15.00g                                     
  lv_root vg_powerpd -wi-ao 50.00g                                     
  lv_swap vg_powerpd -wi-ao 65.03g

6.mount filesystem
[root@powerpd ~]# mount /dev/vg_powerpd/lv_home
[root@powerpd ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_powerpd-lv_root
                       50G  4.4G   43G  10% /
tmpfs                  32G  260K   32G   1% /dev/shm
/dev/sda1             485M   37M  423M   8% /boot
/dev/mapper/vg_powerpd-lv_home
                       15G  168M   14G   2% /home

经验总结:
减少文件的大小一定需要按照上面提高的4个规定动作顺序来做,在缩减LV大小前,首先要缩减filesystem的大小,否则将导致文件系统破坏等恶劣影响.

错误操作示例
将/home目录修改为10G大小.
1.umount filesystem
[root@powerpd ~]# umount /home
2.修改lv的大小
[root@powerpd ~]# lvreduce -L10G /dev/vg_powerpd/lv_home
  WARNING: Reducing active logical volume to 10.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
  Reducing logical volume lv_home to 10.00 GiB
  Logical volume lv_home successfully resized
 
[root@powerpd ~]# lvs
  LV      VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  lv_home vg_powerpd -wi-a- 10.00g                                     
  lv_root vg_powerpd -wi-ao 50.00g                                     
  lv_swap vg_powerpd -wi-ao 65.03g                                     
[root@powerpd ~]# vgs
  VG         #PV #LV #SN Attr   VSize   VFree 
  vg_powerpd   1   3   0 wz--n- 278.38g 153.35g
3.e2fsck filesystem时,会报很多错误
[root@powerpd ~]# e2fsck -f /dev/vg_powerpd/lv_home
e2fsck 1.41.12 (17-May-2010)
The filesystem size (according to the superblock) is 3932160 blocks
The physical size of the device is 2621440 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort? no

Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Error reading block 2621440 (Attempt to read block from filesystem
resulted in short read) while reading inode and block bitmaps.  Ignore
error? no

e2fsck: Can't read an block bitmap while retrying to read bitmaps for /dev/vg_powerpd/lv_home

4.修改文件系统大小
[root@powerpd ~]# resize2fs /dev/vg_powerpd/lv_home
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/vg_powerpd/lv_home' first.

[root@powerpd ~]# resize2fs -f /dev/vg_powerpd/lv_home
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_powerpd/lv_home to 2621440 (4k) blocks.
resize2fs: Can't read an block bitmap while trying to resize /dev/vg_powerpd/lv_home
Please run 'e2fsck -fy /dev/vg_powerpd/lv_home' to fix the filesystem
after the aborted resize operation.

操作失败,提示需要先fsck文件系统.
到此文件系统缩减大小失败,补救的方法是将lv修改为改变前的大小.
[root@powerpd ~]# lvextend -L15G /dev/vg_powerpd/lv_home
  Extending logical volume lv_home to 15.00 GiB
  Logical volume lv_home successfully resized
[root@powerpd ~]# mount /home
[root@powerpd ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_powerpd-lv_root
                       50G  4.4G   43G  10% /
tmpfs                  32G  260K   32G   1% /dev/shm
/dev/sda1             485M   37M  423M   8% /boot
/dev/mapper/vg_powerpd-lv_home
                       15G  168M   14G   2% /home