1. 程式人生 > >擴容資料盤_Linux

擴容資料盤_Linux

1,前提條件
例項處於 執行中 (Running) 或 已停止(Stopped) 狀態。

資料盤的狀態為 待掛載 或 使用中。

資料盤已做分割槽。

建議在擴容資料盤之前手動建立快照,以備份資料。

假設資料盤掛載在一臺Linux例項上,例項的作業系統為CentOS 7.3 64位,未擴容前的資料盤只有一個主分割槽(/dev/vdb1,ext4檔案系統),檔案系統的掛載點為 /kdata,檔案系統擴容完成後,資料盤仍然只有一個主分割槽。

1,先解除安裝主分割槽 /dev/vdb1

umount /dev/vdb1

如果

[[email protected] /]# umount /dev/sdc1
umount: /data1: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

顯示佔用的程序

[[email protected] /]# fuser -m /dev/sdc1
-bash: fuser: command not found

沒有fuser 安裝psmisc

yum install -y psmisc

在次檢視

[[email protected] /]# fuser -m /dev/sdc1
/dev/sdc1:           12766c
[[email protected] /]# ps aux | grep 12766
root     10246  0.0  0.0 116460  3212 pts/0    Ss+  Oct19   0:00 -bash

使用kill命令關閉程序:(如果程序多,可能要多殺幾次)

kill -9 12766

2,檢視分割槽是否存在

df -h

3,使用fdisk命令刪除原來的分割槽並建立新分割槽(輸入後回車)–fdisk

1,fdisk -l
2,fdisk 【‘資料盤裝置名’】
輸入d		--刪除原來的分割槽
輸入n		--開始建立新的分割槽
輸入p		--開始建立新的分割槽。		
輸入1		--建立主分割槽。
輸入第一個可用的扇區編號:為了保證資料的一致性,First sector需要與原來的分割槽保持一致。在本示例中,按回車鍵採用預設值。
輸入最後一個扇區編號:因為這裡僅建立一個分割槽,所以按回車鍵採用預設值。
輸入wq並按回車鍵,開始分割槽。

--------------------------------demo---------------------------
fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-62914559, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):
Using default value 62914559
Partition 1 of type Linux and of size 30 GiB is set
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

注意:demo裡面可能有WARNING

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)

解決方式:例項重啟或者reboot或者partprobe重新讀取分割槽

[[email protected] ~]# partprobe
-d:不更新核心;
-s:顯示摘要和分割槽;
-h:顯示幫助資訊;
-v:顯示版本資訊。

執行partprobe。錯誤提示

Error: Error informing the kernel about modifications to partition /dev/vdb1 
-- Device or resource busy.  
This means Linux won't know about any changes you made to /dev/vdb1 until you reboot
 -- so you shouldn't mount it or use it in any way before rebooting.
Error: Failed to add partition 1 (Device or resource busy)
錯誤:通知核心修改分割槽/DEV/VBD1的錯誤-裝置或資源忙。
這意味著在重新啟動之前,Linux不會知道對/dev/vdb1所做的任何更改——因此在重新啟動之前,不應該以任何方式掛載或使用它。
錯誤:新增分割槽1失敗(裝置或資源忙)

最後只能重啟例項。

4,部分作業系統裡,修改分割槽後可能會重新自動掛載檔案系統。

df -h 檢視是否掛載    如果掛載,執行umount /dev/vdb1

5,檢查檔案系統,並變更檔案系統大小。

e2fsck -f /dev/vdb1 # 檢查檔案系統
resize2fs /dev/vdb1 # 變更檔案系統大小

說明

使用 e2fsck 時,由於系統需要檢查並訂正檔案系統元資料,所以速度較慢、耗時較長,請耐心等待。
正確使用 e2fsck 和 resize2fs 指令,不會造成原有資料丟失。

事例

[email protected] ~]# e2fsck -f /dev/vdb1
e2fsck 1.42.9 (28-Dec-2013)
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/vdb1: 11/1835008 files (0.0% non-contiguous), 159218/7339776 blocks
[[email protected] ~]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 7864064 (4k) blocks.
The filesystem on /dev/vdb1 is now 7864064 blocks long.

6,將擴容完成的檔案系統掛載到原來的掛載點(/kdata)

mount /dev/vdb1 /kdata

7, 執行df -h檢視。–success

通過–parted 操作
說明 如果發現First sector顯示的位置和之前記錄的不一致,說明之前可能使用 parted 來分割槽,那麼就停止fdisk 操作,使用 parted 重新操作。
parted /dev/vdb
rm+ 序列號 刪除老的分割槽表
unit s 定義起始位置
mkpart primary ext4 1 100G 命令來建立即可
p 檢視