VirtualBox

动态分配的虚拟硬盘只是逐渐占用物理空间(直到达到分配的大小),不过当其内部空间不用时不会自动缩减占用的物理硬盘空间。

固定大小的虚拟硬盘文件可能在某些系统中要花很长时间来创建,但它往往使用起来较快。

VirtualBox 用户界面中没有转换的选项,但是可以通过它的 vboxmanage 工具进行转换。

1. 准备 vboxmanage 工具

如何你使用的是 macos 系统,vboxmanage 工具在你安装完 VirtualBox 后就已经可用。你可以在终端中验证:

1
vboxmanage -v

如果你使用的是 windows 系统,VBoxManage.exe 可执行程序可在你 VirtualBox 安装目录中找到,例如:

1
D:\VirtualBox\

我们可以将这个路径添加到系统用户环境变量 Path 中,然后直接在 cmd 中使用它。

2. 转换虚拟硬盘

查询当前的所有虚拟硬盘:

1
vboxmanage list hdds

操作如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
PS C:\Users\rahn\Desktop> cd D:\VirtualBox\
PS D:\VirtualBox> vboxmanage -v
6.1.38r153438
PS D:\VirtualBox> vboxmanage list hdds
UUID: 7d3ecb5c-de7c-4071-a6cb-d928115498e1
Parent UUID: base
State: created
Type: normal (base)
Location: D:\Virtual\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019-disk001.vdi
Storage format: vdi
Capacity: 61440 MBytes
Encryption: disabled

UUID: 888a6678-6419-4605-b27c-3eb367e7af6f
Parent UUID: base
State: locked write
Type: normal (base)
Location: D:\Virtual\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019-disk002.vdi
Storage format: VDI
Capacity: 61440 MBytes
Encryption: disabled

UUID: 0ef8e73e-46df-42d2-93a1-8317df36c27c
Parent UUID: base
State: locked read
Type: normal (base)
Location: D:\Virtual\Ubuntu Server 20.04\Ubuntu Server 20.04\Ubuntu Server822-disk002.vdi
Storage format: vdi
Capacity: 102400 MBytes
Encryption: disabled

UUID: 3047a7c4-b3f0-4dfc-a416-73a5fd772407
Parent UUID: 0ef8e73e-46df-42d2-93a1-8317df36c27c
State: locked write
Type: normal (differencing)
Location: D:\Virtual\Ubuntu Server 20.04\Ubuntu Server 20.04\Snapshots/{3047a7c4-b3f0-4dfc-a416-73a5fd772407}.vdi
Storage format: vdi
Capacity: 102400 MBytes
Encryption: disabled

该命令可以看到虚拟硬盘文件的 Location 及 UUID 信息。

再通过以下命令,可以查看具体虚拟硬盘的详细信息,其中包括 Format variant:

1
vboxmanage showhdinfo <UUID>

从固定大小转换为动态分配,可以这样:

1
vboxmanage clonemedium disk "C:\path\to\source.vdi" "C:\path\to\destination.vdi" --variant Fixed
1
2
3
vboxmanage clonemedium disk "D:\Virtual\Ubuntu Server 20.04 830\Ubuntu Server 20.04 830\Ubuntu Server 20.04 830.vdi" "D:\Virtual\Ubuntu Server 20.04 830\Ubuntu Server 20.04 830\Ubuntu Server 20.04 fixed.vdi" --variant Fixed

vboxmanage clonemedium disk "D:\Virtual\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019-disk001.vdi" "D:\Virtual\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019\Windows 10 Enterprise LTSC 2019-disk002.vdi" --variant Fixed

从固定大小转换为动态分配,可以这样:

1
vboxmanage clonemedium disk "C:\path\to\source.vdi" "C:\path\to\destination.vdi" --variant Standard

转换前,请确保虚拟主机是关闭电源状态。转换完成后,我们在 VirtualBox 用户界面中选中对应虚拟主机 –> 设置 –> 存储,然后移除原来的虚拟硬盘并添加转换后的虚拟硬盘。最后,启动虚拟机即可。

3. 删除旧的虚拟硬盘

在确保虚拟主机运行正常之后,我们可以通过以下命令删除旧的虚拟硬盘:

1
vboxmanage closemedium <UUID> --delete