8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrant で VirtualBox の guestproperty を使って cloud-init を使ってみる

Last updated at Posted at 2015-07-13

Vagrant で cloud-init を、どうにかこうにかして使ってみました。

  • ホスト
    • Windows 7
    • Vagrant 1.7.2
    • VirtualBox 4.3.26
  • ゲスト
    • CentOS 7.1.1503
    • cloud-init 0.7.5

Box の作成

Vagrant で適当な CentOS 7 の環境を作って SSH でログインします。

vagrant up
vagrant ssh

cloud-init をインストールします。

yum install -y cloud-init

cloud-init の設定ファイルを作ります。
cloud-init を None データソースで動作確認する のように None データソースを使います。

cat <<EOS> /etc/cloud/cloud.cfg.d/vbox-cloud-init.cfg
datasource_list: [None]
datasource:
  None:
    metadata:
    userdata_raw: |
      #include
      /etc/cloud/user-data
EOS

VirtualBox の guestproperty を使うとホスト/ゲスト間で値を共有することができます。
これを利用して↑の cloud-init の設定ファイルで指定した位置に user-data を配置するためのスクリプトを作ります。

cat <<EOS> /usr/local/libexec/vbox-cloud-init.sh
# !/bin/bash
VBoxControl --nologo guestproperty get user-data | sed '1s/^Value: *//' > /etc/cloud/user-data
EOS
chmod +x /usr/local/libexec/vbox-cloud-init.sh

このスクリプトをシステム起動時に実行するために systemd のユニットを作ります。
cloud-init 関連のサービスよりも先に実行する必要があります。

cat <<EOS> /etc/systemd/system/vbox-cloud-init.service
[Unit]
Description=vbox-cloud-init
Before=cloud-init.service cloud-init-local.service cloud-config.target cloud-config.service cloud-final.service
After=local-fs.target systemd-journald.socket basic.target system.slice

[Service]
Type=oneshot
ExecStart=/usr/local/libexec/vbox-cloud-init.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOS

systemd の設定ファイルを読み直して有効にします。

systemctl daemon-reload
systemctl enable vbox-cloud-init
systemctl status vbox-cloud-init

ホスト側のコンソールに戻って box を作ります。

vagrant package --output vbox-cloud-init.box

Box を追加します。

vagrant box add vbox-cloud-init vbox-cloud-init.box --force

Vagrant の環境の起動

別のディレクトリで Vagrantfile を作ります。
ホストのファイルから guestproperty を設定しているのがポイントです。

Vagrant.configure(2) do |config|
  config.vm.box = "vbox-cloud-init"

  config.vm.provider :virtualbox do |vb|
    vb.customize ['guestproperty', 'set', :id, 'user-data', File.read('user-data')]
  end
end

user-data も適当に作ります。

# cloud-config
user: oreore
hostname: oreore.example.com
fqdn: oreore.example.com
timezone: "Asia/Tokyo"

起動して接続します。

vagrant up
vagrant ssh

cloud-init によりホスト名の設定やらユーザーの作成やらが行われています。

$ uname -n
oreore.example.com

$ id oreore
uid=1000(oreore) gid=1000(oreore) groups=1000(oreore),4(adm),10(wheel),190(systemd-journal)

Vagrant の provision でいいじゃん! って感じですが、Vagrant の provision だと sshd が起動した後になるので、例えばホスト名とかはもっと前の段階で設定させたいこともあると思うし、vagrant ssh のための公開鍵を設置したりもできるので、もうとっと作りこめばそれなりに有用かもしれないです。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?