启动时启动 VBoxHeadless VM

cap*_*gon 17 startup virtualbox

我似乎无法让我的 VM 在启动时运行。

我尝试了“启动应用程序”和 update-rc.d 没有运气。

sudo update-rc.d startvms defaults 99 10
Run Code Online (Sandbox Code Playgroud)

这为不同的运行级别创建了所有适当的符号链接,但 VM 仍未启动。

这是我的 startvms 脚本:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          startvms
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start my VMs at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
case "$1" in
  start)
        echo "Starting"
        nohup VBoxHeadless --startvm "UbuntuServer" &
        ;;
  stop)
        echo "Stopping $DESC"
        VBoxManage controlvm "UbuntuServer" poweroff
        ;;

  restart|force-reload)
        echo "Restarting $DESC"
        VBoxManage controlvm "UbuntuServer" poweroff
        nohup VBoxHeadless --startvm "UbuntuServer" &
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

cap*_*gon 27

就是最终奏效的方法!

1)创建启动脚本文件

在 /etc/init.d - 中sudo nano /etc/init.d/StartVM

将以下内容粘贴到文件中,并将“我的虚拟机名称”替换为您的虚拟机名称:

#! /bin/sh
# /etc/init.d/StartVM
#

#Edit these variables!
VMUSER=spode
VMNAME="My VM Name"

case "$1" in
  start)
    echo "Starting VirtualBox VM..."
    sudo -H -b -u $VMUSER /usr/bin/VBoxVRDP -s "$VMNAME"
    ;;
  stop)
    echo "Saving state of Virtualbox VM..."
    sudo -H -u  $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" savestate
    ;;
  *)
    echo "Usage: /etc/init.d/StartVM {start|stop}"
    exit 1
    ;;
esac

exit 0
Run Code Online (Sandbox Code Playgroud)

2)赋予脚本可执行权限

sudo chmod +x /etc/init.d/StartVM.

3)告诉脚本在启动时运行。

告诉脚本第一个关闭,最后一个启动。

sudo update-rc.d StartVM defaults 99 01

  • 在我的特定情况下,我希望 VM 正常关闭而不是保存状态。用“acpipowerbutton”替换“savestate”非常适合这个。 (3认同)
  • VRDP 不会增加额外的开销,`VBoxManage startvm --type headless` 会不会更好? (2认同)