如何在 Linux 虛擬機上擴展根文件系統(tǒng)

問題描述

通過 Azure 平臺部署的 Linux 虛擬機默認的根文件系統(tǒng)容量有限,需要進行擴展。

問題分析

由于 Azure 平臺部署的 Linux 虛擬機默認根文件系統(tǒng)容量比較小,客戶在使用過程中,經(jīng)常會出現(xiàn)根文件系統(tǒng)用滿,導致虛擬機不可用的情況,需要進行手動對根文件系統(tǒng)進行擴容。

解決方案

Important

在執(zhí)行如下操作前,一定要針對虛擬機的系統(tǒng)盤進行備份。以下步驟基于 CentOS 6.8,其他 Linux 版本,可能會略有區(qū)別。

Important

在 CentOS 7.x 中,默認根分區(qū)是 /dev/sda2,僅需要擴展 /dev/sda2 分區(qū),且無需激活。

通過 Azure portal 關(guān)閉虛擬機。

執(zhí)行以下 Powershell 命令,對系統(tǒng)盤進行擴展:

復制

Get-AzureVM -ServiceName "vfldev" -Name "vfldev" | get-AzureOSDisk

## 使用正確的 ServiceName 和 VM Name 取代上述參數(shù)。

Update-AzureDisk –DiskName "vfldev-vfldev-0-201503091934500547" -Label "ResiZedOS" -ResizedSizeInGB 100

## 用步驟一獲取的 OSdisk 的名字取代上述的 DiskName,并輸入想要擴容的磁盤大小。

通過 Azure portal 啟動虛擬機。

登陸虛擬機,切換成 root 用戶,查看當前的虛擬機的根文件系統(tǒng)容量。

復制

[root@resizeSDA chpaadmin]# df -h

Filesystem? ? ? Size? Used Avail Use% Mounted on

/dev/sda1? ? ? ? 30G? 1.1G? 27G? 4% /

devtmpfs? ? ? ? 832M? ? 0? 832M? 0% /dev

tmpfs? ? ? ? ? 840M? ? 0? 840M? 0% /dev/shm

tmpfs? ? ? ? ? 840M? 8.3M? 832M? 1% /run

tmpfs? ? ? ? ? 840M? ? 0? 840M? 0% /sys/fs/cgroup

/dev/sdb1? ? ? ? 69G? 53M? 66G? 1% /mnt/resource

打開分區(qū)表

復制

[root@resizeSDA chpaadmin]# fdisk /dev/sda

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): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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

Disk label type: dos

Disk identifier: 0x00093e4e

## 請記錄分區(qū)信息

Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System

/dev/sda1? *? ? ? ? 2048? ? 62914559? ? 31456256? 83? Linux

## 切換為以 sector 作為計算單元

Command (m for help): u

Changing display/entry units to sectors.

## 如果顯示為上述內(nèi)容,則繼續(xù)下一步,如果顯示為:

Command (m for help): u

Changing display/entry units to cylinders (DEPRECATED!).

## 則繼續(xù)執(zhí)行 u,切換為sector。

## 刪除分區(qū)

Command (m for help): d

Selected partition 1

Partition 1 is deleted

## 新建分區(qū)

Command (m for help): n

Partition type:

p? primary (0 primary, 0 extended, 4 free)

e? extended

Select (default p): p

Partition number (1-4, default 1):

First sector (2048-209715199, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):

Using default value 209715199

Partition 1 of type Linux and of size 100 GiB is set

## 此時修改分區(qū)結(jié)束,打印分區(qū)信息,確認信息無誤

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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

Disk label type: dos

Disk identifier: 0x00093e4e

## 注意,這里的 start 的值,必須和此前的分區(qū)表里的信息一致

Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System

/dev/sda1? ? ? ? ? ? 2048? 209715199? 104856576? 83? Linux

## 激活分區(qū)

Command (m for help): a

Selected partition 1

## 再次打印分區(qū),確認已激活

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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

Disk label type: dos

Disk identifier: 0x00093e4e

Device Boot? ? ? Start? ? ? ? End? ? ? Blocks? Id? System

/dev/sda1? *? ? ? ? 2048? 209715199? 104856576? 83? Linux

## 如果信息有誤,或者不確定,請及時聯(lián)系我們,如果信息確認無誤,寫入分區(qū)表

Command (m for help): wr

The partition table has been altered!

Calling ioctl() to re-read partition table.

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)

Syncing disks.

分區(qū)表修改完畢,重啟虛擬機。

復制

[root@resizeSDA chpaadmin]# init 6

登陸虛擬機,切換到 root 用戶,檢查當前根文件系統(tǒng)的容量。

復制

[root@resizeSDA chpaadmin]# df -h

Filesystem? ? ? Size? Used Avail Use% Mounted on

/dev/sda1? ? ? ? 30G? 1.1G? 27G? 4% /

devtmpfs? ? ? ? 832M? ? 0? 832M? 0% /dev

tmpfs? ? ? ? ? 840M? ? 0? 840M? 0% /dev/shm

tmpfs? ? ? ? ? 840M? 8.3M? 832M? 1% /run

tmpfs? ? ? ? ? 840M? ? 0? 840M? 0% /sys/fs/cgroup

/dev/sdb1? ? ? ? 69G? 53M? 66G? 1% /mnt/resource

修改根文件系統(tǒng)的大小。

復制

[root@resizeSDA chpaadmin]# resize2fs /dev/sda1

resize2fs 1.42.9 (28-Dec-2013)

Filesystem at /dev/sda1 is mounted on /; on-line resizing required

old_desc_blocks = 4, new_desc_blocks = 13

The filesystem on /dev/sda1 is now 26214144 blocks long.

檢查根文件系統(tǒng)大小。

復制

[root@resizeSDA chpaadmin]# df -h

Filesystem? ? ? Size? Used Avail Use% Mounted on

/dev/sda1? ? ? ? 99G? 1.1G? 93G? 2% /

devtmpfs? ? ? ? 832M? ? 0? 832M? 0% /dev

tmpfs? ? ? ? ? 840M? ? 0? 840M? 0% /dev/shm

tmpfs? ? ? ? ? 840M? 8.3M? 832M? 1% /run

tmpfs? ? ? ? ? 840M? ? 0? 840M? 0% /sys/fs/cgroup

/dev/sdb1? ? ? ? 69G? 53M? 66G? 1% /mnt/resource

至此,根文件系統(tǒng)擴容完畢。

立即訪問http://market.azure.cn

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容