一、目的
在使用CentOS6.3版本Linux系統的時候,發現根目錄(/)的空間不是很充足,而其他目錄空間有很大的空閒,所以本文主要是針對現在已有的空間進行調整。首先,先來檢視一下系統的空間分配情況:
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 4.0G 321M 3.5G 9% /
tmpfs 1.8G 0 1.8G 0% /dev/shm
/dev/sda1 194M 26M 159M 14% /boot
/dev/sda7 431G 199M 409G 1% /home
/dev/sda2 7.9G 479M 7.0G 7% /usr
/dev/sda3 7.9G 180M 7.3G 3% /var
dev/mapper/vg_centos-lv_home 404G 670M 382G 1% /home
下面的詳細步驟部分將從/dev/sda7分割槽下取出100G的空間新增到/dev/sda6分割槽上去。
vg_centos-lv_home /vg_centos-lv_root
二、詳細步驟
1、解除安裝/dev/sda7分割槽
[root@CentOS-78 /]# umount /home
此時在用df命令檢視將會發現/home目錄已經看不見了,如下所示:
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 4.0G 3.8G 12M 100% /
tmpfs 1.8G 0 1.8G 0% /dev/shm
/dev/sda1 194M 26M 159M 14% /boot
/dev/sda2 7.9G 666M 6.8G 9% /usr
/dev/sda3 7.9G 293M 7.2G 4% /var
2、從新設定/dev/sda7的大小
[root@CentOS-78 /]# resize2fs -p /dev/sda7 231G
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/sda7' first.
這一步設定vg_home-lv_home大小沒有成功,系統提示我們先執行下面的命令,操作如下:
[root@CentOS-78 /]# e2fsck -f /dev/sda7
e2fsck 1.41.12 (17-May-2010)
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
/dev/mapper/vg_centos-lv_home: 1386/26836992 files (0.9% non-contiguous), 1855856/107344896 blocks
重新設定vg_home-lv_home的大小:
[root@CentOS-78 /]# resize2fs -p /dev/sda7 282G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg_centos-lv_home to 73924608 (4k) blocks.
Begin pass 2 (max = 43)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 3276)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 266)
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/vg_centos-lv_home is now 73924608 blocks long.
檢查是否成功:
[root@CentOS-78 /]# mount /home
[root@CentOS-78 /]#
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
50G 14G 34G 30% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
/dev/mapper/vg_centos-lv_home
278G 663M 263G 1% /home
[root@CentOS-78 /]#
3、將lv_home邏輯分割槽減小到指定大小
[root@CentOS-78 /]# lvreduce -L 282G /dev/sda7
WARNING: Reducing active and open logical volume to 282.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 282.00 GiB
Logical volume lv_home successfully resized
[root@CentOS-78 /]#
我認為這個命令是將已經減少的100G空間拿到系統公共的區域,可以被其他分割槽載入和利用。
查詢卷組的資訊:
[root@CentOS-78 /]# vgdisplay
--- Volume group ---
VG Name vg_centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 465.27 GiB
PE Size 4.00 MiB
Total PE 119109
Alloc PE / Size 86472 / 337.78 GiB
Free PE / Size 32637 / 127.49 GiB
VG UUID 1k4ooN-RFV9-uyf1-uMYf-aERG-YaGs-ZNoSD6
Free PE / Size指定的應該是現在可在分配的空間。
4、增加vg_centos-lv_root分割槽的大小
將可用的空間新增到vg_centos-lv_root分割槽上面:
[root@CentOS-78 /]# lvextend -L +127.40G /dev/mapper/vg_centos-lv_root
Rounding up size to full physical extent 127.40 GiB
Extending logical volume lv_root to 177.40 GiB
Logical volume lv_root successfully resized
[root@CentOS-78 /]#
從新設定該分割槽的大小:
[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_centos-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 12
Performing an on-line resize of /dev/mapper/vg_centos-lv_root to 46504960 (4k) blocks.
The filesystem on /dev/mapper/vg_centos-lv_root is now 46504960 blocks long.
5、再次檢視分割槽大小
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
175G 14G 153G 9% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
/dev/mapper/vg_centos-lv_home
278G 663M 263G 1% /home
我們發現vg_centos-lv_root分割槽的空間已經增加了125G,之所以比lv_home減少的空間要多25G主要是由於我們把系統所有的可用的空間都加在了lv_root分割槽。
三、所遇到的問題
1、在解除安裝/home目錄的時候失敗
可先執行如下fuser命令,然後再umount即可:
[root@CentOS-78 /]# fuser -m /home
[root@CentOS-78 /]#
2、設定完lv_home的大小,再次mount該分割槽時,發現用df命令無法看到給分割槽,此時只要在mount一次即可
3、在設定lv_root的大小時,不要把Free PE / Size的空間全部都用上,這很可能會出現Free PE空間不足的現象,建議保留一點Free PE的空間。