Linux下調整根目錄的空間大小

http://blog.csdn.net/andy_yf/article/details/7831482

一、目的

在使用CentOS6.3版本linux系統的時候,發現根目錄(/)的空間不是很充足,而其他目錄空間有很大的空閒,所以本文主要是針對現在已有的空間進行調整。首先,先來查看一下系統的空間分配情況:

 

  1. [root@CentOS-78 /]# df -h  

  2. Filesystem            Size  Used Avail Use% Mounted on  

  3. /dev/mapper/vg_centos-lv_root  

  4.                        50G   14G   34G  30% /  

  5. tmpfs                 1.9G     0  1.9G   0% /dev/shm  

  6. /dev/sda1             485M   37M  423M   8% /boot  

  7. /dev/mapper/vg_centos-lv_home  

  8.                       404G  670M  382G   1% /home  

[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
                      404G  670M  382G   1% /home
下面的詳細步驟部分將從vg_centos-lv_home分區下取出100G的空間添加到/vg_centos-lv_root分區上去。  

 

二、詳細步驟

1、卸載vg_centos-lv_home分區

 

  1. [root@CentOS-78 /]# umount /home  

[root@CentOS-78 /]# umount /home
此時在用df命令查看將會發現/home目錄已經看不見了,如下所示:  

 

 

  1. [root@CentOS-78 /]# df -h  

  2. Filesystem            Size  Used Avail Use% Mounted on  

  3. /dev/mapper/vg_centos-lv_root  

  4.                        50G   14G   34G  30% /  

  5. tmpfs                 1.9G     0  1.9G   0% /dev/shm  

  6. /dev/sda1             485M   37M  423M   8% /boot  

[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

 

2、從新設定vg_home-lv_home的大小

 

  1. [root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G  

  2. resize2fs 1.41.12 (17-May-2010)  

  3. Please run 'e2fsck -f /dev/mapper/vg_centos-lv_home' first.  

[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/mapper/vg_centos-lv_home' first.
這一步設定vg_home-lv_home大小沒有成功,系統提示我們先運行下面的命令,操作如下:  

 

 

  1. [root@CentOS-78 /]# e2fsck -f /dev/mapper/vg_centos-lv_home  

  2. e2fsck 1.41.12 (17-May-2010)  

  3. Pass 1: Checking inodes, blocks, and sizes  

  4. Pass 2: Checking directory structure  

  5. Pass 3: Checking directory connectivity  

  6. Pass 4: Checking reference counts  

  7. Pass 5: Checking group summary information  

  8. /dev/mapper/vg_centos-lv_home: 1386/26836992 files (0.9% non-contiguous), 1855856/107344896 blocks  

[root@CentOS-78 /]# e2fsck -f /dev/mapper/vg_centos-lv_home
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的大小:  

 

 

  1. [root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G  

  2. resize2fs 1.41.12 (17-May-2010)  

  3. Resizing the filesystem on /dev/mapper/vg_centos-lv_home to 73924608 (4k) blocks.  

  4. Begin pass 2 (max = 43)  

  5. Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  

  6. Begin pass 3 (max = 3276)  

  7. Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  

  8. Begin pass 4 (max = 266)  

  9. Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  

  10. The filesystem on /dev/mapper/vg_centos-lv_home is now 73924608 blocks long.  

[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 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.
檢查是否成功:  

 

 

  1. [root@CentOS-78 /]# mount /home  

  2. [root@CentOS-78 /]#  

  3. [root@CentOS-78 /]# df -h  

  4. Filesystem            Size  Used Avail Use% Mounted on  

  5. /dev/mapper/vg_centos-lv_root  

  6.                        50G   14G   34G  30% /  

  7. tmpfs                 1.9G     0  1.9G   0% /dev/shm  

  8. /dev/sda1             485M   37M  423M   8% /boot  

  9. /dev/mapper/vg_centos-lv_home  

  10.                       278G  663M  263G   1% /home  

  11. [root@CentOS-78 /]#  

[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邏輯分區減小到指定大小  

 

 

  1. [root@CentOS-78 /]# lvreduce -L 282G /dev/mapper/vg_centos-lv_home  

  2.   WARNING: Reducing active and open logical volume to 282.00 GiB  

  3.   THIS MAY DESTROY YOUR DATA (filesystem etc.)  

  4. Do you really want to reduce lv_home? [y/n]: y  

  5.   Reducing logical volume lv_home to 282.00 GiB  

  6.   Logical volume lv_home successfully resized  

  7. [root@CentOS-78 /]#  

[root@CentOS-78 /]# lvreduce -L 282G /dev/mapper/vg_centos-lv_home
  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空間拿到系統公共的區域,可以被其他分區加載和利用。

 

查詢卷組的信息:

 

  1. [root@CentOS-78 /]# vgdisplay  

  2.   --- Volume group ---  

  3.   VG Name               vg_centos  

  4.   System ID  

  5.   Format                lvm2  

  6.   Metadata Areas        1  

  7.   Metadata Sequence No  5  

  8.   VG Access             read/write  

  9.   VG Status             resizable  

  10.   MAX LV                0  

  11.   Cur LV                3  

  12.   Open LV               3  

  13.   Max PV                0  

  14.   Cur PV                1  

  15.   Act PV                1  

  16.   VG Size               465.27 GiB  

  17.   PE Size               4.00 MiB  

  18.   Total PE              119109  

  19.   Alloc PE / Size       86472 / 337.78 GiB  

  20.   Free  PE / Size       32637 / 127.49 GiB  

  21.   VG UUID               1k4ooN-RFV9-uyf1-uMYf-aERG-YaGs-ZNoSD6  

[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分區上面:

 

  1. [root@CentOS-78 /]# lvextend -L +127.40G /dev/mapper/vg_centos-lv_root  

  2.   Rounding up size to full physical extent 127.40 GiB  

  3.   Extending logical volume lv_root to 177.40 GiB  

  4.   Logical volume lv_root successfully resized  

  5. [root@CentOS-78 /]#  

[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 /]#
從新設定該分區的大小:
  1. [root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_root  

  2. resize2fs 1.41.12 (17-May-2010)  

  3. Filesystem at /dev/mapper/vg_centos-lv_root is mounted on /; on-line resizing required  

  4. old desc_blocks = 4, new_desc_blocks = 12  

  5. Performing an on-line resize of /dev/mapper/vg_centos-lv_root to 46504960 (4k) blocks.  

  6. The filesystem on /dev/mapper/vg_centos-lv_root is now 46504960 blocks long.  

[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、再次查看分區大小  

 

 

  1. [root@CentOS-78 /]# df -h  

  2. Filesystem            Size  Used Avail Use% Mounted on  

  3. /dev/mapper/vg_centos-lv_root  

  4.                       175G   14G  153G   9% /  

  5. tmpfs                 1.9G     0  1.9G   0% /dev/shm  

  6. /dev/sda1             485M   37M  423M   8% /boot  

  7. /dev/mapper/vg_centos-lv_home  

  8.                       278G  663M  263G   1% /home  

[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即可:

 

  1. [root@CentOS-78 /]# fuser -m /home  

  2. [root@CentOS-78 /]#  

[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的空間。


==================================================================

參考:

http://hi.baidu.com/%B7%E7%B6%C0/blog/item/c4aa9a81b204dbc89023d956.html

http://qianxunniao.iteye.com/blog/1393726

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章