如何在Linux虛擬機上擴展根文件系統linux
v 問題描述shell
經過Azure平臺部署的Linux虛擬機默認的根文件系統容量有限,須要進行擴展。ide
v 問題分析ui
因爲Azure平臺部署的Linux虛擬機默認根文件系統容量比較小,客戶在使用過程當中,常常會出現根文件系統用滿,致使虛擬機不可用的狀況,須要進行手動對根文件系統進行擴容。spa
v 解決方案orm
注意:在執行以下操做前,必定要針對虛擬機的系統盤進行備份。如下步驟基於CentOS 7,其餘Linux版本,可能會略有區別。ci
1. 經過Azure portal關閉虛擬機。rem
2. 執行如下Powershell腳本,對系統盤進行擴展:部署
Get-AzureVM -ServiceName "vfldev" -Name "vfldev" | get-AzureOSDisk get
## 用正確的service name和vm的name來取代上述參數
Update-AzureDisk –DiskName "vfldev-vfldev-0-201503091934500547" -Label "ResiZedOS" -ResizedSizeInGB 100
## 用步驟一獲取的OSdisk的名字來取代上述的DiskName,並輸入想要擴容的size
3. 經過Azure portal啓動虛擬機。
4. 登錄虛擬機,切換成root用戶,查看當前的虛擬機的根文件系統容量
[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
5. 打開分區表
[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
## 請記錄分區信息
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 62914559 31456256 83 Linux
## 刪除分區
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): 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
## 到這裏,修改分區結束,打印分區信息,確認信息無誤
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的值,必定要跟此前的分區表裏的信息一致
Device Boot Start End Blocks Id System
/dev/sda1 2048 209715199 104856576 83 Linux
##激活分區
Command (m for help): a
Selected partition 1
## 再次打印分區,確認已激活
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
## 若是信息有誤,或者不肯定,請及時聯繫咱們,若是信息確認無誤,寫入分區表
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.
6. 分區表修改完畢,重啓虛擬機。
[root@resizeSDA chpaadmin]# init 6
7. 登錄虛擬機,切換到root用戶,檢查當前根文件系統的容量。
[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
8. 修改根文件系統的大小。
[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.
9. 檢查根文件系統大小。
[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
10. 至此,根文件系統擴容完畢。