[VirtualBox]Ubuntu 開機自動啟動VirtualBox

參考網址:https://www.itread01.com/content/1548466591.html

第一種:使用 VRDP 方式通過命令列啟動虛擬機器:
 

VBoxManage startvm U1104

 

第二種 :使用 Headless 啟動無前端圖形介面方式:
 

VBoxHeadless -startvm "U1104"

 

從命令列關閉虛擬機器
 

VBoxManage controlvm U1104 poweroff

 

要在開機時就啟動VirtualBox,必需用無前端圖形介面的方式
把VBoxHeadless -startvm "U1104" 寫入/etc/rc.local
 

#!/bin/sh
#/bin/VBoxManage startvm U1104-mini > /var/log/vbox.log
date > /var/log/vbox.log
/bin/VBoxHeadless -startvm "U1104" &
#sleep 10s
wait
ps aux | grep box >> /var/log/vbox.log
echo "看到这行字,说明添加自启动脚本成功。" >> /var/log/vbox.log

#vbox list
#VBoxManage list runningvms

#vbox poweroff
#VBoxManage controlvm U1104 poweroff

exit 0

 

如果出現

WARNING: The character device /dev/vboxdrv does not exist. Please install the virtualbox-dkms package and the appropriate headers, most likely linux-headers-generic.

You will not be able to start VMs until this problem is fixed.

請參考StackOverFlow
 

sudo apt-get install virtualbox-dkms

#Once you install those packages you also need to do the reconfiguration:
sudo dpkg-reconfigure virtualbox-dkms 
sudo dpkg-reconfigure virtualbox

#If your problem is still not fixed try installing the headers:
sudo apt-get install linux-headers-generic

 

另一種方式
在/etc/init.d 中建立 vm-start
 

#!/bin/bash
/bin/sleep 1
/bin/su root -c "/usr/bin/vboxmanage startvm U1104 --type headless"
/bin/sleep 1

chmod +x /etc/init.d/vm-start

在/etc/rc.local中新增
 

#!/bin/sh
#/bin/VBoxManage startvm U1104 > /var/log/vbox.log
date > /var/log/vbox.log
/etc/init.d/vm-start
#/bin/VBoxHeadless -startvm "U1104"
#/bin/VBoxHeadless -startvm "U1104" &
sleep 10s
#wait
ps aux | grep box >> /var/log/vbox.log
echo "看到这行字,说明添加自启动脚本成功。" >> /var/log/vbox.log
 
#vbox list
#VBoxManage list runningvms
 
#vbox poweroff
#VBoxManage controlvm U1104 poweroff
 
exit 0

 

本篇發表於 Linux系統, 軟體使用。將永久鏈結加入書籤。