Expand FreeBSD Disk Space in KVM Machine

Expand FreeBSD Disk Space in KVM Machine

Expand FreeBSD Disk Space in KVM Machine
Page content

Expand freeBSD disk space in KVM

一个泡在KVM物理机中的freeBSD虚拟机磁盘满了,需要扩容

[root@freeBSD-template ~]# df -hT
Filesystem   Type     Size    Used   Avail Capacity  Mounted on
/dev/ada0p2  ufs      7.3G    7.2G   -489M   107%    /
devfs        devfs    1.0K    1.0K      0B   100%    /dev
[root@freeBSD-template ~]# gpart show ada0
=>      40  16777136  ada0  GPT  (8.0G)
        40      1024     1  freebsd-boot  (512K)
      1064  15934464     2  freebsd-ufs  (7.6G)
  15935528    839680     3  freebsd-swap  (410M)
  16775208      1968        - free -  (984K)

# 由于要扩容的分区为ada0磁盘的第二个分区,也就是ada0p2,所以需要将其后面的分区删除,即删除ada0p3,是个swap分区
[root@freeBSD-template ~]# swapoff /dev/ada0p3
[root@freeBSD-template ~]# gpart delete -i 3 ada0
ada0p3 deleted

# 关机后到KVM中修改qcow2磁盘的大小
[root@freeBSD-template ~]# shutdown now

===========KVM=========
# 查看一下目前的大小,可以看到vm的磁盘大小disk size已经和virtual size一样大
[root@corp_server_3 ~]# qemu-img info /ENT1/freeBSD-template-11.3-patched
image: /ENT1/freeBSD-template-11.3-patched
file format: qcow2
virtual size: 7.5G (8053063680 bytes) 
disk size: 7.5G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

# 扩容10个G
[root@corp_server_3 ~]# qemu-img resize freeBSD-template-11.3-patched +10G
Image resized.

# 再看virtual size 比disk size大出差不多10G
[root@corp_server_3 ~]# qemu-img info /ENT1/freeBSD-template-11.3-patched
image: /ENT1/freeBSD-template-11.3-patched
file format: qcow2
virtual size: 18G (19327352832 bytes)
disk size: 7.5G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
===========KVM=========

# 回到freebsd, 可以看到显示[CORRUPT]
# 这是应为磁盘分区格式为GPT,而GPT备份分区表是放在最后一个分区的末尾的,
# 需要使用gpart recover来恢复GPT备份分区表
[root@freeBSD-template ~]# gpart show
=>      40  16777136  ada0  GPT  (18G) [CORRUPT]
        40      1024     1  freebsd-boot  (512K)
      1064  15934464     2  freebsd-ufs  (7.6G)
  15935528    841648        - free -  (411M)

# 没有gpart rcover时,无法扩容分区
[root@freeBSD-template ~]# gpart resize -i 2 -s 17G -a 4k ada0
gpart: table 'ada0' is corrupt: Operation not permitted
# recover
[root@freeBSD-template ~]# gpart recover ada0
ada0 recovered

# 扩容
[root@freeBSD-template ~]# gpart resize -i 2 -s 17G -a 4k ada0
ada0p2 resized

# 虽然分区扩容了,但是文件系统还不知道
[root@freeBSD-template ~]# df -hT
Filesystem   Type     Size    Used   Avail Capacity  Mounted on
/dev/ada0p2  ufs      7.3G    7.2G   -489M   107%    /
devfs        devfs    1.0K    1.0K      0B   100%    /dev
[root@freeBSD-template ~]# gpart show
=>      40  16777136  ada0  GPT  (18G) [CORRUPT]
        40      1024     1  freebsd-boot  (512K)
      1064  15934464     2  freebsd-ufs  (7.6G)
  15935528    841648        - free -  (411M)

# 将swap分区加回去 
[root@freeBSD-template ~]# gpart add -t freebsd-swap -a 4k ada0
ada0p3 added

# 让文件系统知道分区大小已经变更
[root@freeBSD-template ~]# growfs /dev/ada0p2
Device is mounted read-write; resizing will result in temporary write suspension for /.
It\'s strongly recommended to make a backup before growing the file system.
OK to grow filesystem on /dev/ada0p2, mounted on /, from 7.6GB to 17GB? [yes/no] yes
super-block backups (for fsck_ffs -b #) at:
 16669312, 17951552, 19233792, 20516032, 21798272, 23080512, 24362752, 25644992, 26927232, 28209472, 29491712, 30773952, 32056192, 33338432, 34620672
[root@freeBSD-template ~]# df -hT
Filesystem   Type     Size    Used   Avail Capacity  Mounted on
/dev/ada0p2  ufs       16G    7.2G    7.9G    48%    /
devfs        devfs    1.0K    1.0K      0B   100%    /dev

[root@freeBSD-template ~]# gpart show
=>      40  37748656  ada0  GPT  (18G)
        40      1024     1  freebsd-boot  (512K)
      1064  35651584     2  freebsd-ufs  (17G)
  35652648   2096048     3  freebsd-swap  (1.0G)