LVM EXT4縮小與擴容根目錄

本文章內容:

1. 縮小根目錄

2. 擴容根目錄

參考資料:http://www.unixlinux.online/unixlinux/linuxjc/gylinux/201703/72350.html

 

LVM可以對文件系統進行在線擴容,功能非常方便,但LVM並不支持在線縮容,需要在卸載分區的前提下才能操作。

1. 縮小根目錄

1.1 我們要將根目錄縮小至10G

1.2 通過BIOS設置光盤啟動,重啟服務器進入rescue救援模式

1.3 選擇語言、選擇鍵盤模式

1.4 選No

1.5 由於LVM不支持在線縮容,所以不選擇讓rescue模式自動掛載系統根分區至/mnt/sysimage,而是選擇skip跳過,直接開啟shell

1.6 手動激活系統根分區lv_root

1.7 縮容之前最好對分區進行強行掃描並檢查

1.8 先通過resize2fs命令將lv_root分區的文件系統縮小至10G,然後再通過lvm lvresize命令將lv_root分區的磁盤空間縮小至10G,最後再次對文件系統進行強行掃描並檢查

1.9 重啟系統,輸入exit後選擇reboot Reboot

2. 擴容根目錄

2.1 我們要將根目錄擴充

[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      9.8G  1.9G  7.4G  20% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             477M   35M  417M   8% /boot

2.2 為系統增加一顆硬碟/dev/sdb

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00034880

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes  #增加/dev/sdb硬碟
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x24480331

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  8e  Linux LVM  #將/dev/sdb硬碟分割成/dev/sdb1,LVM格式的

Disk /dev/mapper/VolGroup-lv_root: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

2.3 增加PV

2.3.1 執行pvs指令,看到目前只有一個PV

[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup lvm2 a--u 19.51g 7.57g

2.3.2 執行pvcreate /dev/sdb1,增加第二個PV

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup lvm2 a--u 19.51g  7.57g
  /dev/sdb1           lvm2 ---- 19.99g 19.99g  #第二個PV,目前不屬於任何VG

2.4 擴充VG

2.4.1 執行vgs指令,看到目前只有一個VG

[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   1   2   0 wz--n- 19.51g 7.57g

2.4.2 執行vgextend centos /dev/sdb1,擴充現有的VG

[root@localhost ~]# vgextend VolGroup /dev/sdb1
  Volume group "VolGroup" successfully extended
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   2   2   0 wz--n- 39.50g 27.56g  #VG擴充了,有27.56G還沒使用

2.5 擴充LV

2.5.1 執行lvs指令,看到目前有root、swap兩個LV

[root@localhost ~]# lvs
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root VolGroup -wi-ao---- 10.00g
  lv_swap VolGroup -wi-ao----  1.94g

2.5.2 執行lvextend -l +100%FREE /dev/VolGroup/lv_root,將剩餘的VG空間擴充到根目錄

[root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root
  Size of logical volume VolGroup/lv_root changed from 10.00 GiB (2560 extents) to 37.56 GiB (9616 extents).
  Logical volume lv_root successfully resized.
[root@localhost ~]# lvs
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root VolGroup -wi-ao---- 37.56g  #已擴充
  lv_swap VolGroup -wi-ao----  1.94g

2.5.3 執行df -Th,可以看到根目錄並沒有長大,需再執行xfs_growfs /dev/centos/root才會對擴充的LV進行xfs格式大小調整

[root@localhost ~]# df -Th
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                     ext4   9.8G  1.9G  7.4G  20% /  #雖然LV擴充了,但實際上"/"還沒長大
tmpfs                tmpfs  491M     0  491M   0% /dev/shm
/dev/sda1            ext4   477M   35M  417M   8% /boot
[root@localhost ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/VolGroup/lv_root to 9846784 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 9846784 blocks long.
[root@localhost ~]# df -Th
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                     ext4    37G  1.9G   34G   6% /  #擴充成功
tmpfs                tmpfs  491M     0  491M   0% /dev/shm
/dev/sda1            ext4   477M   35M  417M   8% /boot