1
2

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を使った仮想マシンの自動起動

Last updated at Posted at 2020-03-22

TL;DRv

VirtualBoxの自動起動VboxAutostartを使用する

  • HostOSの設定
sudo mkdir -p -m 1777 /etc/vbox/autostart_db
echo "default_policy = allow" | sudo tee -a /etc/vbox/autostart.cfg
echo -e "VBOXAUTOSTART_DB=/etc/vbox/autostart_db\nVBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg" | sudo tee -a /etc/vbox/vbox.cfg
VBoxManage setproperty autostartdbpath /etc/vbox/autostart_db
  • GuestOSの設定 Vagrantfileに以下の行を追加
config.vm.provider "virtualbox" do |vb|
  vb.customize ["modifyvm", :id, "--autostart-enabled", "on"]
end

環境

  • HostOS: debian10(buster)
  • virtualbox-6.1
  • vagrant-2.2.7

動機

vagrantを使ってHostOS上に仮想マシンを立てている。
HostOS起動時に仮想マシンも立ち上げたい。
systemdのunit-fileを書けば実現はできるが、わざわざunit-fileを書くのは億劫。
virshやvSphereではUIやコマンドラインからボタン一つ押すだけ、1行書くだけで簡単に実現できるのと比較するとsystemdのunit-fileの面倒具合は敷居がだいぶ高い。
もっと簡単な方法があれば良いと考えていた。

HostOSの設定

仮想マシン毎の設定が保存されるDBを作る

sudo mkdir -p -m 1777 /etc/vbox/autostart_db

自動起動を許可する

echo "default_policy = allow" | sudo tee -a /etc/vbox/autostart.cfg

DBとautostart.cfgの場所をvirtualboxに教える

echo -e "VBOXAUTOSTART_DB=/etc/vbox/autostart_db\nVBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg" | sudo tee -a /etc/vbox/vbox.cfg

userに自動起動を有効にする

virtualboxを使って仮想マシンを操作するuserに自動起動を許可する

VBoxManage setproperty autostartdbpath /etc/vbox/autostart_db

GuestOSの設定

Vagrantfileに以下の行を追加

config.vm.provider "virtualbox" do |vb|
  vb.customize ["modifyvm", :id, "--autostart-enabled", "on"]
end

複数の仮想マシンの立ち上げ順番に依存がある場合、簡易的ではあるがdelayを設定すると実現できる

  vb.customize ["modifyvm", :id, "--autostart-delay", seconds ]

参考

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?