洞悉本質在不確定的世界預見未來
日記

阿里雲Linux服務器磁盤擴容

目前磁盤10G,已用97%,隻有372M,需要擴容10G。

$ sudo df -h
/dev/vdb1        9.8G  9.4G  372M  97% /mnt

1、擴容物理盤

先做磁盤快照,備份雲盤數據。進入阿里雲找到需要擴容的雲盤,點擊選擇在線擴容,點擊磁盤擴容按步驟完成付款。擴容過程中不要格式化硬盤。需要在 ECS 控制台重啟實例之後才可以生效。

$ sudo fdisk -l
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6e808324

Device     Boot Start      End  Sectors Size Id Type
/dev/vda1  *     2048 83884031 83881984  40G 83 Linux


Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x647d4204

Device     Boot Start      End  Sectors Size Id Type
/dev/vdb1        2048 20971519 20969472  10G 83 Linux

2、安裝growpart和resize2fs工具

$ sudo apt install cloud-guest-utils xfsprogs 

3、使用growpart工具擴容分區

运行growpart 命令扩容分区。示例命令表示扩容系统盘的第一个分区(/dev/vdb1)

$ sudo growpart /dev/vdb 1
CHANGED: partition=1 start=2048 old: size=20969472 end=20971520
new: size=41940959,end=41943007

若運行命令後報錯,可運行LANG=en_US.UTF-8切換ECS實例的字符編碼類型。

取消挂載,修改/etc/fstab

$ sudo umount /dev/vdb1
$ sudo vim /etc/fstab

然后控制台重啟服務器

4、系統內查看磁盤,已經是升級後的

$ sudo fdisk -l
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6e808324

Device     Boot Start      End  Sectors Size Id Type
/dev/vda1  *     2048 83884031 83881984  40G 83 Linux


Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x647d4204

Device     Boot Start      End  Sectors Size Id Type
/dev/vdb1        2048 41943006 41940959  20G 83 Linux

5、使用resize2fs工具擴容文件系統

先強制檢查文件系統,再使用resize2fs工具擴容文件系統

$ sudo e2fsck -f /dev/vdb1
e2fsck 1.44.5 (15-Dec-2018)
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: 70215/655360 files (5.2% non-contiguous), 2387938/2621184 blocks
$ sudo resize2fs /dev/vdb1
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/vdb1 to 5242619 (4k) blocks.
The filesystem on /dev/vdb1 is now 5242619 (4k) blocks long.

挂載,修改/etc/fstab

$ sudo mount /dev/vdb1 /mnt
$ sudo vim /etc/fstab

然后控制台重啟服務器

6、檢查擴容結果

顯示為20G,成功擴容。

$ sudo df -h
/dev/vdb1        20G  8.9G  9.8G  48% /mnt

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。

back to top