systemd 如何自动启动并控制 VirtualBox 来宾?

Dan*_*ler 2 virtualbox systemd

在我的特殊情况下,在安装了 Ubuntu 18.04 Bionic 和 VirtualBox-5.2.20 的新服务器后,自动启动 guest 虚拟机的旧方法似乎不再可用。并不是说它有那么美妙——但它确实有效。似乎没有任何干净的方法可以解决这个问题 - systemd 和 VirtualBox 如何协同工作实现智能启动、控制和关闭?

小智 6

与您的解决方案类似,但更简单一些:

  1. 运行systemctl edit vbox@.service --full --force并粘贴以下内容,将用户和组更新为您的用户名。
[Unit]
Description=Virtual Box Guest %I
After=network.target vboxdrv.service
Before=runlevel2.target shutdown.target
 
[Service]
User=USERNAME
Group=GROUPNAME
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
 
ExecStart=/usr/bin/VBoxManage startvm %i --type headless
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton
 
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
  1. 重新加载系统:systemctl daemon-reload

  2. 获取您的虚拟机列表VBoxManage list vms

$ VBoxManage list vms
"Ubuntu" {1ba32309-d4c4-420a-a9c8-a38177f00bc4}
"Windows" {573df054-0e33-4389-896a-1234f10e25ad}
Run Code Online (Sandbox Code Playgroud)
  1. 使用步骤 3 中返回的名称通过 systemd 管理虚拟机。例如,要管理“Ubuntu”VM,您将运行:
sudo systemctl start vbox@Ubuntu     # Start the VM
sudo systemctl enable vbox@Ubuntu    # Start the VM on boot
Run Code Online (Sandbox Code Playgroud)