vagrant是用于管理虚拟机生命周期的命令行实用程序。将依赖关系及其配置隔离在一个一次性和一致的环境中。参见:Vagrant虚拟机方案:docker的开源跨平台替代Vagrant_pycharm +vagrant + virutalbox-CSDN博客
以前尝试过vagrant导入以前的Virtualbox虚拟机镜像,但是没有成功,见:vagrant怎么在宿主机管理虚拟机镜像box(先搁置)-CSDN博客
原来不能直接导入,需要转换格式,而且要转换两次格式。
以前的失败尝试
添加本地的vbox镜像
G:\vm\ubuntu22>vagrant box add ubuntu22.vbox --name ubuntu22
还是有报错:
G:\vm\ubuntu22>vagrant box add ubuntu22.vbox --name ubuntu22
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu22' (v0) for provider: (amd64)
box: Unpacking necessary files from: file://G:/vm/ubuntu22/ubuntu22.vbox
box:
The box failed to unpackage properly. Please verify that the box
file you're trying to add is not corrupted and that enough disk space
is available and then try again.
The output from attempting to unpackage (if any):
bsdtar.EXE: Error opening archive: Unrecognized archive format
首先不知道是不是需要用到临时文件,因为这个镜像太大了,接近100G。
成功实践
这次使用了一个很小的镜像来做测试,这样格式转换起来快!这个镜像是汇编手写的,见:好玩的汇编编译器NASM:一款基于x86架构的汇编与反汇编软件(手写可以放入一张磁盘的操作系统)_nasm编译器-CSDN博客
导出vbox成开放式虚拟化格式2.0
直接在VirtualBox的图形界面下转换即可:
输出格式设为开放式虚拟化格式2.0 ,点击完成即可。
看看转换成的ova文件:
2025/09/05 10:46 77,824 newos.ova
当然格式转换也可以使用VBoxManage export命令来完成:
VBoxManage export <虚拟机名称> --output <输出路径>.ova
比如实际转换命令:
"e:\Program Files\Oracle\VirtualBox\VBoxManage.exe" export newos --output test.ova
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully exported 1 machine(s).
转换开放式虚拟化格式2.0到box格式
使用命令:
vagrant package --base newos --output newos.box
注意,开放式虚拟化格式2.0的存盘文件为:newos.ova ,但是命令里不能带ova后缀,带了后缀是失败:
G:\vm\newos>vagrant package --base newos.ova --output newos1.box
==> newos.ova: VM not created. Moving on...
转换完成
G:\vm\ova>vagrant package --base newos --output newos.box
==> newos: Exporting VM...
==> newos: Compressing package to: G:/vm/ova/newos.box
box格式导入到vagrant box资源中
vagrant box add newos.box --name newos
G:\vm\ova>vagrant box add newos.box --name newos
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'newos' (v0) for provider: (amd64)
box: Unpacking necessary files from: file://G:/vm/ova/newos.box
box:
==> box: Successfully added box 'newos' (v0) for '(amd64)'!
vagrant初始化并启动镜像
创建一个目录,到目录里初始化,并启动
G:\vm>mkdir vagrantnewos
G:\vm>cd vagrantnewos
G:\vm\vagrantnewos>vagrant init newos
初始化完成,启动镜像
G:\vm\vagrantnewos>Vagrant up
启动的输出
G:\vm\vagrantnewos>Vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'newos'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrantnewos_default_1757041607065_34065
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
https://www.virtualbox.org/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
因为这个镜像是一个只有几个字节的镜像,没有操作系统,所以最后提示Timed out 。但是至少这样一个流程跑通了。
总结
VirtualBox的存盘文件vbox,无法被vagrant直接导入。如果需要使用以前的vbox虚拟镜像系统,那么就要先转为开放式虚拟化格式(ova格式),这步可以在VirtualBox的图形界面里操作,也可以用VBoxManage export命令。
然后通过下面命令将ova格式转为box格式。
vagrant package --base newos.ova --output newos.box
转换成box格式后,通过vagrant box add命令加入到box资源中:
vagrant box add newos.box --name newos
后面就是vagrant的常规用法了:创建一个目录,然后vagrant init初始化、vagrant up启动虚拟系统。
G:\vm>mkdir vagrantnewos
G:\vm>cd vagrantnewos
G:\vm\vagrantnewos>vagrant init newos
G:\vm\vagrantnewos>Vagrant up
这里初始化的时候直接跟newos参数,就是因为我们通过多次转换,将newos镜像注册到vagrant box资源中的缘故。