qcow2 磁盤在線擴容方法
直接擴展現有qcow2格式磁盤大小的方法
注: 對應虛擬機的分區為vda,虛擬機系統為centos7
1. 查看磁盤文件信息,進行擴展
[root@vp-01 export]# qemu-img info test.qcow2
image: test.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
新增磁盤容量大小20G
[root@vp-01 export]# qemu-img resize test.qcow2 +20G
Image resized.
[root@vp-01 export]# qemu-img info test.qcow2
image: test.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 2.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
對比后發現磁盤已經由原來的20G變為40G了
2. 啟動虛擬機查看磁盤信息
[root@172-16-20-171 ~]#
[root@172-16-20-171 ~]# df -Th
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root xfs 18G 1.9G 16G 11% /
devtmpfs devtmpfs 487M 0 487M 0% /dev
tmpfs tmpfs 497M 0 497M 0% /dev/shm
tmpfs tmpfs 497M 6.6M 490M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/vda1 xfs 497M 130M 368M 27% /boot
tmpfs tmpfs 100M 0 100M 0% /run/user/0
[root@172-16-20-171 ~]#
3. 開始分區
[root@172-16-20-171 ~]#
[root@172-16-20-171 ~]# fdisk /dev/vda
歡迎使用 fdisk (util-linux 2.23.2)。
更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。
命令(輸入 m 獲取幫助):p
磁盤 /dev/vda:42.9 GB, 42949672960 字節,83886080 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x0003f814
設備 Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 41943039 20458496 8e Linux LVM
命令(輸入 m 獲取幫助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分區號 (3,4,默認 3):3
起始 扇區 (41943040-83886079,默認為 41943040):
將使用默認值 41943040
Last 扇區, +扇區 or +size{K,M,G} (41943040-83886079,默認為 83886079):
將使用默認值 83886079
分區 3 已設置為 Linux 類型,大小設為 20 GiB
命令(輸入 m 獲取幫助):t
分區號 (1-3,默認 3):3
Hex 代碼(輸入 L 列出所有代碼):8e
已將分區“Linux”的類型更改為“Linux LVM”
命令(輸入 m 獲取幫助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
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)
正在同步磁盤。
[root@172-16-20-171 ~]#
此步驟當中,最終要的地方就是 分配新的區需要修改分區的system id,將分區類型改為LVM(Hex代碼為8e)。
4. 創建物理卷、加入卷組、擴展邏輯卷
創建物理卷
[root@172-16-20-171 ~]# pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created
[root@172-16-20-171 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- 19.51g 40.00m
/dev/vda3 lvm2 --- 20.00g 20.00g
加入卷組
[root@172-16-20-171 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- 19.51g 40.00m
[root@172-16-20-171 ~]# vgextend centos /dev/vda3
Volume group "centos" successfully extended
[root@172-16-20-171 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 39.50g 20.04g
擴展邏輯卷
[root@172-16-20-171 ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 17.47 GiB (4472 extents) to 37.50 GiB (9601 extents).
Logical volume root successfully resized.
[root@172-16-20-171 ~]# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block 當嘗試打開 /dev/centos/root 時
找不到有效的文件系統超級塊.
發現報錯,分析原因是因為使用dh -T查看系統分區類型為xfs
因此直接使用xfs_growfs擴展即可
[root@172-16-20-171 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=1144832 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=4579328, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4579328 to 9831424
5. 確認磁盤是否增長
[root@172-16-20-171 ~]# df -Th
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root xfs 38G 1.9G 36G 6% /
devtmpfs devtmpfs 487M 0 487M 0% /dev
tmpfs tmpfs 497M 0 497M 0% /dev/shm
tmpfs tmpfs 497M 6.6M 490M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/vda1 xfs 497M 130M 368M 27% /boot
tmpfs tmpfs 100M 0 100M 0% /run/user/0
發現系統容量已經由20G擴展為40G