Expand FreeBSD Disk Space in KVM Machine

Expand freeBSD disk space in KVM

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

 1[root@freeBSD-template ~]# df -hT
 2Filesystem   Type     Size    Used   Avail Capacity  Mounted on
 3/dev/ada0p2  ufs      7.3G    7.2G   -489M   107%    /
 4devfs        devfs    1.0K    1.0K      0B   100%    /dev
 5[root@freeBSD-template ~]# gpart show ada0
 6=>      40  16777136  ada0  GPT  (8.0G)
 7        40      1024     1  freebsd-boot  (512K)
 8      1064  15934464     2  freebsd-ufs  (7.6G)
 9  15935528    839680     3  freebsd-swap  (410M)
10  16775208      1968        - free -  (984K)
11
12# 由于要扩容的分区为ada0磁盘的第二个分区,也就是ada0p2,所以需要将其后面的分区删除,即删除ada0p3,是个swap分区
13[root@freeBSD-template ~]# swapoff /dev/ada0p3
14[root@freeBSD-template ~]# gpart delete -i 3 ada0
15ada0p3 deleted
16
17# 关机后到KVM中修改qcow2磁盘的大小
18[root@freeBSD-template ~]# shutdown now
19
20===========KVM=========
21# 查看一下目前的大小,可以看到vm的磁盘大小disk size已经和virtual size一样大
22[root@corp_server_3 ~]# qemu-img info /ENT1/freeBSD-template-11.3-patched
23image: /ENT1/freeBSD-template-11.3-patched
24file format: qcow2
25virtual size: 7.5G (8053063680 bytes) 
26disk size: 7.5G
27cluster_size: 65536
28Format specific information:
29    compat: 1.1
30    lazy refcounts: true
31
32# 扩容10个G
33[root@corp_server_3 ~]# qemu-img resize freeBSD-template-11.3-patched +10G
34Image resized.
35
36# 再看virtual size 比disk size大出差不多10G
37[root@corp_server_3 ~]# qemu-img info /ENT1/freeBSD-template-11.3-patched
38image: /ENT1/freeBSD-template-11.3-patched
39file format: qcow2
40virtual size: 18G (19327352832 bytes)
41disk size: 7.5G
42cluster_size: 65536
43Format specific information:
44    compat: 1.1
45    lazy refcounts: true
46===========KVM=========
47
48# 回到freebsd, 可以看到显示[CORRUPT]
49# 这是应为磁盘分区格式为GPT,而GPT备份分区表是放在最后一个分区的末尾的,
50# 需要使用gpart recover来恢复GPT备份分区表
51[root@freeBSD-template ~]# gpart show
52=>      40  16777136  ada0  GPT  (18G) [CORRUPT]
53        40      1024     1  freebsd-boot  (512K)
54      1064  15934464     2  freebsd-ufs  (7.6G)
55  15935528    841648        - free -  (411M)
56
57# 没有gpart rcover时,无法扩容分区
58[root@freeBSD-template ~]# gpart resize -i 2 -s 17G -a 4k ada0
59gpart: table 'ada0' is corrupt: Operation not permitted
60# recover
61[root@freeBSD-template ~]# gpart recover ada0
62ada0 recovered
63
64# 扩容
65[root@freeBSD-template ~]# gpart resize -i 2 -s 17G -a 4k ada0
66ada0p2 resized
67
68# 虽然分区扩容了,但是文件系统还不知道
69[root@freeBSD-template ~]# df -hT
70Filesystem   Type     Size    Used   Avail Capacity  Mounted on
71/dev/ada0p2  ufs      7.3G    7.2G   -489M   107%    /
72devfs        devfs    1.0K    1.0K      0B   100%    /dev
73[root@freeBSD-template ~]# gpart show
74=>      40  16777136  ada0  GPT  (18G) [CORRUPT]
75        40      1024     1  freebsd-boot  (512K)
76      1064  15934464     2  freebsd-ufs  (7.6G)
77  15935528    841648        - free -  (411M)
78
79# 将swap分区加回去 
80[root@freeBSD-template ~]# gpart add -t freebsd-swap -a 4k ada0
81ada0p3 added
82
83# 让文件系统知道分区大小已经变更
84[root@freeBSD-template ~]# growfs /dev/ada0p2
85Device is mounted read-write; resizing will result in temporary write suspension for /.
86It\'s strongly recommended to make a backup before growing the file system.
87OK to grow filesystem on /dev/ada0p2, mounted on /, from 7.6GB to 17GB? [yes/no] yes
88super-block backups (for fsck_ffs -b #) at:
89 16669312, 17951552, 19233792, 20516032, 21798272, 23080512, 24362752, 25644992, 26927232, 28209472, 29491712, 30773952, 32056192, 33338432, 34620672
90[root@freeBSD-template ~]# df -hT
91Filesystem   Type     Size    Used   Avail Capacity  Mounted on
92/dev/ada0p2  ufs       16G    7.2G    7.9G    48%    /
93devfs        devfs    1.0K    1.0K      0B   100%    /dev
94
95[root@freeBSD-template ~]# gpart show
96=>      40  37748656  ada0  GPT  (18G)
97        40      1024     1  freebsd-boot  (512K)
98      1064  35651584     2  freebsd-ufs  (17G)
99  35652648   2096048     3  freebsd-swap  (1.0G)

Error Install Python3.7.3 From Source
Go003 Golang Basics