尝试创建 CentOS 8.1911 的 vagrant box

CentOS 8.1911已发布,但截至2020年2月1日,官方的Vagrant Cloud镜像尚未发布。

当然,如果再稍微等待一下,不久之后就会发布了。但由于种种原因,我希望立刻使用,所以我也把自己的 CentOS 8.1911 box 制作了一份,顺便学习了一下自定义该 box 的方法。

制作自定义的 CentOS 8.1911 虚拟盒子。

    カスタマイズ用ディレクトリを準備する。
$ mkdir centos8custom
$ cd centos8custom/
    CentOS 8 v1905.1 の公式 box を起動する。
$ vagrant init centos/8 --box-version 1905.1
$ vagrant up
    CentOS 8.1911 へアップデートする。
$ vagrant ssh
[vagrant@localhost ~]$ sudo -s
[root@localhost vagrant]# cat /etc/centos-release
CentOS Linux release 8.0.1905 (Core) 
[root@localhost vagrant]# 
[root@localhost vagrant]# dnf update
(...)
[root@localhost vagrant]# cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core) 
[root@localhost vagrant]# 
[root@localhost vagrant]# exit
[vagrant@localhost ~]$ logout
Connection to 127.0.0.1 closed.
$ 
    CentOS 8.1911 の box を作成する。
$ vagrant package
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /Users/demo/proj/centos8custom/package.box
$ 
    作成した box を登録する。
$ vagrant box add centos8.1911 package.box 
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos8.1911' (v0) for provider: 
    box: Unpacking necessary files from: file:///Users/demo/proj/centos8custom/package.box
==> box: Successfully added box 'centos8.1911' (v0) for 'virtualbox'!
$ vagrant box list
centos/8           (virtualbox, 1905.1)
centos8.1911       (virtualbox, 0)
$ 

我以为这样就可以了,但是…

在自定义的盒子上启动用自己制作的定制盒子尝试启动后,出现了一些错误。

    テスト用ディレクトリを準備する。
$ cd ..
$ mkdir vagrant-test
$ cd vagrant-test/
    カスタム box で起動する。
$ vagrant init centos8.1911
(...)
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos8.1911'...
(...)
==> default: Mounting shared folders...
    default: /vagrant => /Users/demo/proj/vagrant-test
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

mount: /vagrant: unknown filesystem type 'vboxsf'.

$ 

据说没有vboxsf。要使用vboxsf需要vagrant guest additions,但是原本的1905.1镜像本来就没有安装这个东西吧?

我会尝试查找错误的原因。原始的1905.1的启动消息仔细看一下,显示如下。这表示/vagrant已经通过rsync进行了挂载。

==> default: Rsyncing folder: /Users/demo/proj/centos8custom/ => /vagrant

为什么自定义的盒子不默认使用这个功能?因为 rsync 只在盒子启动时同步文件,但不需要特殊的插件等即可使用。官方的 CentOS 盒子默认使用此功能,这是一个很好的机制。

因此,我决定去看看盒子里面的东西。

首先是官方的 CentOS 8 盒子。实际上,它存在于以下目录中。

$ cd ~/.vagrant.d/boxes/centos-VAGRANTSLASH-8/1905.1/virtualbox/
$ ls
CentOS-8-Vagrant-8.0.1905-1.x86_64.vmdk box.ovf                 metadata.json
Vagrantfile             box_update_check
$

在这个 Vagrantfile 上看到了,确认了。这就是默认的模板。

Vagrant.configure("2") do |config|
  config.vm.base_mac = "52540072fe6e"
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end

接下来让我们来看一下自定义的盒子。

$ cd ~/.vagrant.d/boxes/centos8.1911/0/virtualbox/
$ ls
Vagrantfile     box-disk001.vmdk    box.ovf         metadata.json       vagrant_private_key
$

当我查看Vagrantfile时,我发现它的内容完全不同,确实没有写任何关于synced_folder的设置。

Vagrant::Config.run do |config|
  # This Vagrantfile is auto-generated by `vagrant package` to contain
  # the MAC address of the box. Custom configuration should be placed in
  # the actual `Vagrantfile` in this box.
  config.vm.base_mac = "52540072FE6E"
end

# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

Vagrant.configure("2") do |config|
  config.ssh.private_key_path = File.expand_path("../vagrant_private_key", __FILE__)
end

这个 Vagrantfile 怎么才能定制化呢?

我做了各种调查后的结果

    • このファイルは vagrant package で作られる

vagrant package コマンドの –vagrantfile でオプションでカスタマイズできる

我了解到好像是那样的。

就这样重新创建一个定制的盒子吧。

创建定制的 CentOS 8.1911 Box(再次创建)。

    カスタマイズ用ディレクトリへ戻り、box を起動する。
$ cd centos8custom/
$ vagrant up
    box 起動時にrsync同期されたファイルを削除しておく。
$ vagrant ssh
[vagrant@localhost ~]$ rm -rf /vagrant/*
[vagrant@localhost ~]$ logout
Connection to 127.0.0.1 closed.
$ 
    古いパッケージを削除する。
$ rm package.box 
    カスタマイズ用 Vagrantfile.custom を作成する。
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos8.1911"
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end
    カスタマイズ box を作成する。
$ vagrant package --vagrantfile Vagrantfile.custom 
    古い box を削除し、再作成した box で置き換える。
$ vagrant box remove centos8.1911
$ vagrant box add centos8.1911 package.box 
$ vagrant box list
centos/8            (virtualbox, 1905.1)
centos8.1911        (virtualbox, 0)
$ 

让我们来看看已注册的盒子里面有什么内容。

$ cd ~/.vagrant.d/boxes/centos8.1911/0/virtualbox/
$ ls
Vagrantfile     box.ovf         metadata.json
box-disk001.vmdk    include         vagrant_private_key
$ ls include/
_Vagrantfile
$ 
Vagrant::Config.run do |config|
  # This Vagrantfile is auto-generated by `vagrant package` to contain
  # the MAC address of the box. Custom configuration should be placed in
  # the actual `Vagrantfile` in this box.
  config.vm.base_mac = "52540072FE6E"
end

# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

Vagrant.configure("2") do |config|
  config.ssh.private_key_path = File.expand_path("../vagrant_private_key", __FILE__)
end

这个文件和以前没有变化,但有包含的文件。

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos8.1911"
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end

这样一来,设置就肯定是有效的了。

使用重新制作的定制盒子进行启动。

    テスト用ディレクトリへ移動する。
$ cd ../vagrant-test/
    古い box の VM が稼働している場合は破棄する。
$ vagrant destroy
$ rm Vagrantfile
    再作成した カスタム box で起動する。
$ vagrant init centos8.1911
(...)
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos8.1911'...
(...)
==> default: Rsyncing folder: /Users/demo/proj/vagrant-test/ => /vagrant
$ 

搞定了!现在它跟官方的 CentOS8 box 有着相同的表现。

这个箱子中的 Vagrantfile 配置并没有完全一样,但可能是因为官方的 centos8 箱子使用了另一种打包方式。

bannerAds