大小孩

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

每次重启VirtualBox宿主机之后,都需要手动启动其上的虚拟机,这是一件非常烦心的事。在VirtualBox 4.2中添加的一个新特性是autostart,该特性仅在Linux、Solaris和MacOSX主机中可用。假设每次启动系统时都会启动一个虚拟机,为什么不采用这种方式呢?

Oracle VM VirtualBox 用户手册

#查看Linux版本
# cat /proc/version 
Linux version 3.10.0-693.11.6.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Thu Jan 4 01:06:37 UTC 2018

#查看VirtualBox版本
# vboxmanage --version
5.0.40r115130

(1) 首先你需要创建文件 /etc/default/virtualbox 并且添加几个参数,配置文件内容如下:

# virtualbox defaults file
VBOXAUTOSTART_DB=/etc/vbox       #Vbox自动启动(文件)数据库目录
VBOXAUTOSTART_CONFIG=/etc/vbox/vbox.cfg

#Vbox自动启动配置文件,用于存储自动启动策略。

(2) 现在需要创建文件 /etc/vbox/vbox.cfg 并且添加如下内容:

# Default policy is to deny starting a VM, the other option is "allow".
default_policy = deny
# Create an entry for each user allowed to run autostart(eg. allow 'vbox' user to run autostart)
vbox = {
allow = true
}
注意:如果上边的文件名vbox.cfg不起作用,请尝试命名它为autostart.cfg。如果你是唯一的用户,那么可以将default_policy = allow直接添加到vbox.cfg文件中(允许所有)。

(3) 将目录所属组设置为vboxusers,并确保用户对目录有写权限。

sudo chgrp vboxusers /etc/vbox
sudo chmod 1775 /etc/vbox

(4) 将用户添加到vboxusers

sudo usermod -a -G vboxusers USERNAME

#把 USERNAME 替换成你要设置的用户名,例如上边用的vbox 
注意:如果你改变了当前登录用户的所属组,请注销并重新登录。

(5) 设置要自动启动的VM

设置自动启动数据库目录(Every user who wants to enable autostart for individual machines has to set the path to the autostart database directory with)

VBoxManage setproperty autostartdbpath /etc/vbox

为一个VM开启自动启动(and enable autostart for an individual VM with)

VBoxManage list vms  #显示已经注册的虚拟机名称和UUID
VBoxManage showvminfo <uuid|vmname> |grep start  #查看开机启动配置

VBoxManage modifyvm <uuid|vmname> --autostart-enabled on  #开启自动启动
VBoxManage modifyvm <uuid|vmname> --autostart-enabled off  #关闭自动启动

# 如果VBoxManage list vms不显示任何虚拟机,需要先注册
VBoxManage registervm <filename>
这将在 $VBOXAUTOSTART_DB 目录(即/etc/vbox)创建一个 myuserid.start (在本示例中即vbox.start)文件

(6) 重启vboxautostart使配置生效

sudo service vboxautostart-service restart

(7) 重启系统然后VM就可以自动启动了

可以将过程写成一个脚本

#---vm_autostart.sh------
#!/bin/bash # Set VM autostart function update_autostart() { host=$1 state=$2 VBoxManage modifyvm "$host" --autostart-enabled "$state" } # List all VM function list_vms() { VBoxManage list vms|awk -F\" '{print $3,$2}'| while read vm_id vm_name do echo -n "$vm_name : " VBoxManage showvminfo $vm_id |grep -i 'Autostart Enabled' done } # Option choice case $1 in list) list_vms ;; enable) update_autostart $2 on ;; disable) update_autostart $2 off ;; *) echo "Usage: $0 [enable|disable|list] " echo "eg:" echo " $0 enable <uuid|vmname>" echo " $0 disable <uuid|vmname>" echo " $0 list" esac

原文:

http://lifeofageekadmin.com/how-to-set-your-virtualbox-vm-to-automatically-startup/
posted on 2018-06-20 14:36  大小孩  阅读(560)  评论(0)    收藏  举报