LVM管理之減少LV的大小

djb1008發表於2014-03-18

 

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
                      

                                               

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/32980/viewspace-1123851/,如需轉載,請註明出處,否則將追究法律責任。

相關文章