发布于 1年前

从已有的vagrant虚拟机中打包镜像

使用 vagrant 的好一个好处就是可以想协作中分享我们的 Box

打包

下面就介绍一下打包方式

首先确定需要打包哪台虚拟机,进入安装 VirtualBox 的安装目录

默认在这个路径 C:\Program Files\Oracle\VirtualBox

cd "C:\Program Files\Oracle\VirtualBox"

查看所有的虚拟机,可以看到有显示格式如下: "vmname" {vmid} ,我们这里需要的是 vmname

> ./VBoxManage.exe list vms
"django-twitter_default_1619620682201_12310" {b3c74405-5d50-4f05-9f0e-98c9563f1fa5}

将目标切换到你需要的路径中,然后执行以下的目录

vagrant package --base django-twitter_default_1619620682201_12310 --output demo_ubuntu18.box

好了,这个时候你的路径下就会生成一个 demo_ubuntu18.box 的文件

使用打包的好的镜像

光有打包是不够的,因为打包的目的是为了使用 box 镜像。使用的时候有两个注意点,那就是因为 ssh 密钥的缘故,你 add ,然后 up 之后会发现进不去,这是就需要去修改 vagrantfile 这个文件,将默认的密钥登录改为账号密码登录。

ssh 密钥错误如下

> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/bionic64_demo_ubuntu18'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: twitter-term-1_default_1623336326135_46711
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
    default: 8000 (guest) => 80 (host) (adapter 1)
    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
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
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.

但是账号密码登录不如密钥登录安全,所以第一次(账号+密码)登录之后,建议改为密钥登陆

初始化

如果你还没有 vagrantfile 文件,才需要初始化操作

你可以复用老虚拟机的 vagrantfile 文件,或者执行初始化操作新建一个 vagrantfile 文件

vagrant init [box_name]

添加 box

vagrant box add {box_name} {box_file_path}

参考文章:

利用vagrant打包系统--制作自己的box
Vagrant+Miniconda+PyCharm
Vagrant ssh authentication failure
"Warning: Authentication failure. Retrying... " after packaging box #5186

©2020 edoou.com   京ICP备16001874号-3