1. 程式人生 > 其它 >centos 7下使用parted擴充套件根分割槽(非LVM)

centos 7下使用parted擴充套件根分割槽(非LVM)

非LVM意思是底層分割槽是物理分割槽不是通過lvm管理的邏輯卷。

問題是這樣的:

使用中突然發現根分割槽空間不足,結果發現分割槽配成下面這樣:

# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sr1 11:1 1 1024M 0 rom
vda 253:0 0 100G 0 disk
├─vda1 253:1 0 200M 0 part [SWAP]
└─vda2 253:2 0 1.8G 0 part /
vdb 253:16 0 300G 0 disk
└─vdb1 253:17 0 300G 0 part /data1

根分割槽是/dev/vda2,雖然/dev/vda共有100G,但是不知怎麼滴給vda2只分配了1.8G(我估計最初是要把vda除了vda1之餘的所有空間都給vda2的)。

如何處理?經過查詢資料發現parted有個resizepart命令可以使用,而檔案系統好像也有resize之類的命令(沒有細看)。因為錯誤的認知,以為resize之類的操作需要在檔案系統unmounted的狀態才能進行,所以考慮重啟系統進入一種不使用根檔案系統的模式,然後再進行處理(當然後面明白,這種做法是完全多餘的,因為檔案系統的擴大操作可以在檔案系統處於掛載狀態的時候進行——注意只是擴大)。雖然走了彎路,但是也記錄下。

根分割槽是xfs檔案系統,而且需要顯示終端連線了機器。

步驟:

1. 重啟系統進入啟動選單介面,然後編輯預設的啟動選單項。

2. 進入編輯介面後,找到linux啟動引數那一行。64-Bit IBM Power Series是linux

那一行,x86-64 BIOS-based systems是linux16那一行,UEFI systems是linuxefi那一行。然後先從這一行引數中刪除rhgbquiet ,再新增 rd.break enforcing=0 到引數行中。(這些操作是參考的RHEL7的system_administrators_guide中關於"Resetting the Root Password Using rd.break"的做法)

3. 按Ctrl-x,以修改後的引數啟動系統。

4. 啟動後,根檔案系統是個臨時的檔案系統,不包含parted命令。原有根分割槽以只讀的方式掛載在/sysroot下面,所以使用parted之前(實際上可以執行使用/sysroot下面的命令,然後當時以為需要解除安裝/dev/vda2上的檔案系統才能操作對/dev/vda進行操作,所以採取的方法是將parted拷貝出來執行),需要執行:
cp /sysroot/sbin/parted /sbin/
cp /sysroot/lib64/libparted.so.2 /lib64/
cp /sysroot/lib64/libdevmapper.so.1.02 /lib64/
cp /sysroot/lib64/libsepol.so.1 /lib64/
可能實際中因為版本不同導致這些需要拷貝的檔案不同。可以先拷貝parted可執行程式本身,然後執行/sbin/parted /dev/vda,如果缺某個庫檔案,會報錯提示,根據提示再拷貝即可。

5. 執行/sbin/parted /dev/vda,進入parted介面。先檢視下分割槽狀態,確認根分割槽正好是最後一個分割槽,然後執行 resizepart 2 100%,其中2是/dev/vda2的分割槽號(之前可用p命令檢視獲知),100%表示擴充套件到最尾部。

6. 然後以rw方式重新掛載原來的根分割槽:mount -o remount,rw /sysroot

7. 執行檔案系統擴充套件: /sysroot/sbin/xfs_growfs /sysroot 。注意xfs_growfs命令也不在那個臨時根檔案系統下。執行成功後,顯示資訊的最後一行會提示 "data blocks changed from xxxx to yyyyyy" 只在的資訊。

8. 重啟系統,根分割槽應該就擴充套件成功了。

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

實際上對於檔案系統增大(擴充套件)是可以線上(即檔案系統本身處於掛載狀態)進行的:

resize2fs的man page說明:

The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing. (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and ext4.).

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/storage_administration_guide/xfsgrow

An XFS file system may be grown while mounted using the xfs_growfs command:
# xfs_growfs /mount/point -D size
The -D size option grows the file system to the specified size (expressed in file system blocks). Without the -D size option, xfs_growfs will grow the file system to the maximum size supported by the device.

Note

While XFS file systems can be grown while mounted, their size cannot be reduced at all. 而parted對分割槽的操作,我感覺它是不管分割槽上的檔案系統是否處於掛載狀態的。 所以最後我感覺整個過程可以直接在系統正常啟動的情況下,在root賬戶下,先用parted對/dev/vda2進行resizepart操作,然後執行執行xfs_growfs / 即可。 我也沒有對此進行專門測試,但是參考下面兩篇文章,我認為是沒有問題的,因為這些文章裡面沒有提到需要作業系統進到某種特殊模式。 參考: https://www.cnblogs.com/youngchaolin/p/11478740.html https://www.jianshu.com/p/9db2df60fd0a