LoginSignup
3
3

More than 3 years have passed since last update.

virtualbox host OS 連動 自動起動/終了

Last updated at Posted at 2021-01-16

virtualbox host os 連動 自動起動/終了

ホスト OS (ubuntu20)の起動/終了にゲスト OS (VM)の起動/停止を連動させる
毎日やると意外とゲスト OS の起動/終了は面倒である

概要

ゲスト(vm) 起動/終了のサービスを作って systemd に登録する方法で実現する
systemd に登録することでホストOS起動、終了時に作成したサービスを読み込んでくれる
VM の起動/終了はVBoxManage コマンドを利用する
https://www.virtualbox.org/manual/ch08.html

VM 開始/終了するためにVM の UUID を調べる XXXX-XXXX-XXXX-XXX-XXXXX の部分

$ VBoxManage list vms
"VM_Name" {XXXX-XXXX-XXXX-XXX-XXXXX}

VM 開始
--type headless はウィンドウ(GUI)非表示で起動

VBoxManage startvm VM_UUID --type headless

VM 終了

VBoxManage controlvm VM_UUID acpipowerbutton

vm 起動/終了サービス用のディレクトリ作成

ディレクトリ名はなんでもいい。ここでは、ubuntuVMService とした

mkdir ~/ubuntuVMService
cd ~/ubuntuVMService

vm 起動サービス用のシェル

※sleep 10 念のため入れてるけどなくても大丈夫かもしれない

$ vim start.sh
#!/bin/sh
sleep 10
VMID="bcff1cd2-6410-4eaf-908d-b4cc5e93f34a"
VBoxManage startvm $VMID --type headless
exit

vm 終了サービス終了シェル

※echo ...stop.log の部分は動作確認後は消してもいい

$ vim stop.sh
#!/bin/sh
echo `date "+%Y/%m/%d %H:%M:%S" 'ubuntuVM stopping'` > ~/ubuntuVMService/stop.log

VMID="bcff1cd2-6410-4eaf-908d-b4cc5e93f34a"
VBoxManage controlvm $VMID acpipowerbutton

sleep 10
echo `date "+%Y/%m/%d %H:%M:%S" 'VBoxManage list runningvms'` >> ~/ubuntuVMService/stop.log
echo `VBoxManage list runningvms` >> ~/ubuntuVMService/stop.log

exit

シェル実行権限を与える

$ ll
合計 8
-rw-rw-r-- 1 user user 110  1月 16 15:20 start.sh
-rw-rw-r-- 1 user user 351  1月 16 15:20 stop.sh

$ chmod 755 *.sh

$ ll
合計 8
-rwxr-xr-x 1 user user 110  1月 16 15:20 start.sh*
-rwxr-xr-x 1 user user 351  1月 16 15:20 stop.sh*

vm サービス登録用定義ファイル

Ubuntu では、 systemd でサービスが管理されており、サービス定義ファイルは /etc/systemd/system 配下にある
*.service という定義ファイルを用意する

サービス登録のお作法としては
/lib/systemd/system/my-app.service に定義ファイルを配置して
/etc/systemd/systemn/multi-user.target.wants/my-app.service にシンボリックリンクを張る

ただいつも作ったファイルがどこのディレクトリか忘れるので
今回は、~/ubuntuVMService/ubuntuVM.service を作って
/lib/systemd/system/ubuntuVM.serviceシンボリックリンクを張り
さらに/etc/systemd/system/multi-user.target.wants/ubuntuVM.serviceにシンボリックリンクを張って構築する

$ vim ubuntuV.MService
[Unit]
Description=VirtualBox VM
After=network-online.target

[Service]
User=user
ExecStart=/home/user/ubuntuVMService/start.sh
ExecStop=/home/user/ubuntuVMService/stop.sh
Restart=no
Type=forking

[Install]
WantedBy=multi-user.target

サービス定義ファイルのシンボリックリンクを張る

sudo ln -s /home/user/ubuntuVMService/ubuntuVM.service /lib/systemd/system/ubuntuVM.service

sudo ln -s /lib/systemd/system/ubuntuVM.service /etc/systemd/system/multi-user.target.wants/ubuntuVM.service

systemctl コマンドでサービスを登録する

$ sudo systemctl enable ubuntuVM.service
Created symlink /etc/systemd/system/ubuntuVM.service → /home/user/ubuntuVMService/ubuntuVM.service.

サービスの起動確認

sudo systemctl start ubuntuVM

VM が起動しているか確認する

$ VBoxManage list runningvms
"vmub20_1" {bcff1cd2-6410-4eaf-908d-b4cc5e93f34a}

サービスの終了確認

sudo systemctl stop ubuntuVM

VM が終了しているか確認する

$ VBoxManage list runningvms
何も表示されなければVMは終了している

最後にホストOSを再起動してVM が起動しているか確認して起動していればOK

sudo reboot

OS再起動後

$ VBoxManage list runningvms
"vmub20_1" {bcff1cd2-6410-4eaf-908d-b4cc5e93f34a}

以上。

3
3
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
3
3