VirtualBox安装使用过程中的问题集
2013-12-21 15:35:06

本文收集了我在安装使用VirtualBox的过程中所碰到的问题,以及对应的解决办法。

开启宿主机的vt支持
缺少内核包时初始出错的处理
ssh开启X11转发
关闭hostonlyif接口中的dhcp服务器
让VBox虚拟机随宿主机系统的启动而启动

------------------------
开启宿主机的vt支持

这种情况多出现在一些较前生产的机器上,它们的cpu本身支持64位运算,需要bios开启支持,但默认的情况下并不开启,就会出现下面的情况。
VirtualBox中安装虚拟机时出错:
VT-x features locked or unavailable in MSR,VERR_VMX_MSR_LOCKED_OR_DISABLED

VT-x features locked or unavailable in MSR

(VERR_VMX_MSR_LOCKED_OR_DISABLED)

返回代码:
E_FAIL (0×80004005)
组件:Console
界面:IConsole

先要确保主机本身的虚拟化技术开启了,再确保虚拟机配置中,选中了Vt-x了。

2.对于你的主机,关于如何去开启虚拟机技术,VT-x的话,简单说就是:
进入你的BIOS,去打开对应的Intel Virtual Technology。

虚拟机的配置,是在:
系统->硬件加速,中的 硬件虚拟,记得选上:

启用VT-x/AMD-V

错误:VT-x features locked or unavailable in MSR.

这是因为CPU不支持VT-X技术或者VT-X技术被锁定。如果不打开虚拟化支持,是不能在虚拟机装64位操作系统或者安装操作系统设置多个CPU,如果安装32位操作系统只设置一个CPU,则不会出现上述错误。

VT-X是Intel CPU虚拟化技术,查看CPU是否支持VT-x技术: cat /proc/cpuinfo|grep flags  

如果存在 vmx 选项,则说明支持。

设置CPU开启VT-x技术
开机进入BIOS选项 ,依次选Config->CPU->Intel Virtualization Technology,里面有个Intel VT-d Feature ,改成Enabled ,保存退出,关机,然后启动机器。

VT-x features locked or unavailable in MSR

When the virtual machine boots the live image disk, I get an error:
Failed to open a session for the virtual machine <guest name>

VT-x features locked or unavailable in MSR.
(VERR_VMX_MSR_LOCKED_OR_DISABLED)

The details are:
Result Code:
E_FAIL (0x80004005)
Component:
Console
Interface:
IConsole {1968b7d3-e3bf-4ceb-99e0-cb7c913317bb}

笔者的thinkpad t500上安装的debian 7中的vbox安装64系统时,也出现了类似的情况

------------------------
缺少内核包时初始出错的处理

这个出现在安装好virtualbox后,对其进行初始操作时,由于缺少了内核有关的包,导致其内核加载程序不能正常编译加载而出错。
# /etc/init.d/vboxdrv setup
Stopping VirtualBox kernel modules                         [  OK  ]
Uninstalling old VirtualBox DKMS kernel modules            [  OK  ]
Trying to register the VirtualBox kernel modules using DKMSError! echo
Your kernel headers for kernel 2.6.18-238.el5 cannot be found at
/lib/modules/2.6.18-238.el5/build or /lib/modules/2.6.18-238.el5/source.
                                                           [FAILED]
  (Failed, trying without DKMS)
Recompiling VirtualBox kernel modules                      [FAILED]
  (Look at /var/log/vbox-install.log to find out what went wrong)

Whats the output of 'rpm -qa | grep kernel'

Try as recommended in the error specifying the directory manually
/etc/init.d/vboxdrv setup KERN_DIR=/usr/src/kernels/2.6.18-194.8.1.v5-i686

/etc/init.d/vboxdrv setup
Stopping VirtualBox kernel module                          [  OK  ]
Recompiling VirtualBox kernel module                       [FAILED]
  (Look at /var/log/vbox-install.log to find out what went wrong)
[root@system ~]# /etc/init.d/vboxdrv setup KERN_DIR=/usr/src/kernels/2.6.18-194.8.1.v5-i686
Stopping VirtualBox kernel module                          [  OK  ]
Recompiling VirtualBox kernel module                       [FAILED]
  (Look at /var/log/vbox-install.log to find out what went wrong)
[root@system ~]# rpm -qa | grep kernel
kernel-2.6.18-194.8.1.v5
kernel-headers-2.6.18-308.1.1.v5
kernel-devel-2.6.18-308.1.1.v5

Also the vbox-install.log:
Makefile:159: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again.  Stop.

解决的方法是安装好对应的内核软件包,在运行其setup过程,即可解决此问题。内核的安装过程请看此处

------------------------
ssh开启X11转发

这种情况见于vbox安装于没有像xfce、gnome、kde等这种桌面gui情况时,且控制端也不在本机的情况。其下的所有虚拟机将运行在'headless'无头模式下,但虚拟机的系统的安装却要在gui下进行(关于远程操作虚拟机,请参考《VirtualBox下从命令行创建虚拟机》的“如何远程连接管理虚拟机”节),在你没有安装'vbox-extpack'工具包以使用vrdp协议来控制时。那么通过ssh来登录宿主机来控制虚拟机就是这里要说的,这种方法也多用在虚拟机的安装上。这也就需要开启ssh的x转发功能,让远端的虚拟界面显示在控制端的桌面上。当然,我的控制端是debian 7 xfce,windows下将linux桌面转过来好像要复杂一些吧。

X forwarding does not work with ssh -X or ssh -Y. When starting a program, it says:

E233: cannot open display

Error: Can't open display:
SSH does not indicate that X11 forwarding failed, but it gives these messages in debug mode:
debug1: Requesting X11 forwarding with authentication spoofing.
debug2: channel 0: request x11-req confirm 0

远端使用调试模式
Try starting sshd on a different port under debug mode.
/usr/sbin/sshd -d -D -p 2222

This enables debug mode, sshd does not detach and run as daemon and sshd listens on port 2222. This allows you to debug the problem while leaving the normal sshd server running. In my case it gave this:

Failed to allocate internet-domain X11 display socket.
debug1: x11_create_display_inet failed.

1)、在被控端的sshd服务的配置文件(sshd_config)中开启转发
X11Forwarding yes
下面是可选的
X11DisplayOffset 10
X11UseLocalhost yes

2)、安装对应的x11桌面环境
xorg-x11-server-Xorg
xorg-x11-xauth

如果要中文支持的话,还需要安装相关的字体:xorg-x11-fonts-*

ssh -X|-Y Hostip

Use -Y. From the man page on ssh:
-Y Enables trusted X11 forwarding. Trusted X11 forwardings are not subjected to the X11 SECURITY extension controls.

If you're still having problems though, you can try setting $DISPLAY to your local box's IP:
$ export DISPLAY='<ip_address>:11.0'
It may work to set it as
$ export DISPLAY='127.0.0.1:11.0'

因此'-X'与'-Y'都是在控制端通过ssh连接远程宿主机的必要选项,如果想得到更多的调试信息还是跟'-v'或'-vv'等选项。

------------------------
关闭hostonlyif接口中的dhcp服务器


当创建hostonlyif好接口后,其中它就有一默认的dhcp服务器,但我不打算用它,如果它与我的软路由产生了冲突后,那么只有关闭它。
VBoxManage hostonlyif create
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Interface 'vboxnet0' was successfully created

VBoxManage hostonlyif ipconfig vboxnet0 --ip 10.0.0.2 --netmask 255.255.0.0

当使用了软路由来实现的dhcp后,网络模式为hostonly时,vbox自有的dhcp会先取代软件路由的,因此有必要禁用vbox的dhcp服务器。

查看vbox中的dhcp server
VBoxManage list dhcpservers
NetworkName:    HostInterfaceNetworking-vboxnet0
IP:             192.168.56.100
NetworkMask:    255.255.255.0
lowerIPAddress: 192.168.56.101
upperIPAddress: 192.168.56.254
Enabled:        Yes

VBoxManage dhcpserver modify --netname HostInterfaceNetworking-vboxnet0  --disable

VBoxManage dhcpserver modify --ifname vboxnet0  --disable

------------------------
清理(删除)虚拟镜像磁盘文件


列出本机的vbox上的磁盘文件
$ vbm list hdds

清理(删除)磁盘文件的语法
$ vbm closemedium disk <uuid> --delete

UUID:           f29964b7-2aec-4a47-b9a9-03da39f72457
Parent UUID:    base
State:          inaccessible
Type:           normal (base)
Location:       /home/hto/pwork/vmos/disk/xpext.vdi
Storage format: VDI
Capacity:       0 MBytes

移除那些被意外删除掉的但依然在vbox中存在的虚拟磁盘,如上列:
vbm closemedium disk f29964b7-2aec-4a47-b9a9-03da39f72457 --delete
0%...
Progress state: VBOX_E_FILE_ERROR
VBoxManage: error: Failed to delete medium
VBoxManage: error: Could not delete the medium storage unit '/home/hto/pwork/vmos/disk/xpext.vdi'.
VBoxManage: error: VD: error VERR_FILE_NOT_FOUND opening image file '/home/hto/pwork/vmos/disk/xpext.vdi' (VERR_FILE_NOT_FOUND)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component Medium, interface IMedium
VBoxManage: error: Context: "int handleCloseMedium(HandlerArg*)" at line 1344 of file VBoxManageDisk.cpp

虽然出错了,但该虚拟磁盘的信息还是清理掉了。

UUID:           53cdeefd-0f57-4f4f-b360-508554bf2ff3
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /mnt/pc/vmos/disk/xd0.ext.vdi
Storage format: VDI
Capacity:       40960 MBytes

vbm closemedium disk /mnt/pc/vmos/disk/xd0.ext.vdi --delete

------------------------
让VBox虚拟机随宿主机系统的启动而启动
Make VBox VM Autostart


几种方案:
1.Using VirtualBox Autostart Service
2.Using System  start-script(rc-local、su、sudo)
3.Using Systemd service unit

On Ubuntu 20.04:
VMNAME: Virtual Manchine Name that is registered with VirtualBox
User: Account to run as

编辑/etc/default/virtualbox中的这两项(没有需自加)
VBOXAUTOSTART_DB which contains an absolute path to the autostart database directory and
VBOXAUTOSTART_CONFIG which contains the location of the autostart config settings.

内容为:
#AutoStarting VMs
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg

编辑/etc/vbox/autostart.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
myuserid = {
    allow = true
}

如果是唯一的用户,则可以添加default_policy=allow行。

将目录组权限设置为vboxuser组,并确保用户可以写入该目录以及粘性位:
chgrp vboxusers /etc/vbox
chmod 1775 /etc/vbox

并将相关的用户加入到vboxusers组。
usermod -a -G vboxusers USERNAME

如果是为当前用户设置权限组,请登出后再登录使用设定生效。

每个想要为该计算机启用自动启动的用户都必须使用设置自动启动数据库目录的路径:
VBoxManage setproperty autostartdbpath /etc/vbox

并启用单个VM的自动启动
VBoxManage modifyvm <uuid|vmname> --autostart-enabled on

这将在/etc/vbox目录中创建一个myuserid.start文件。现在重新启动vboxautostart服务以使应用更改生效。
service vboxautostart-service restart

至此可以重启主机来测试上述的设定是否生效。在一台centos7的主机相关配置如下:
# ls /etc/vbox
autostartvm.cfg  root.start

# more autostartvm.cfg
default_policy = deny

root = {
    allow = true
    startup_delay = 10
}

# more root.start
1


如果执行:VBoxManage modifyvm <uuid|vmname> --autostart-enabled on
时报错,可能需要为该用户创建标志文件(文件属主为该用户):
touch "username".start
touch "username".stop


使用systemd服务来控制虚拟机的启动
Create /etc/systemd/system/VMNAME.service

[Unit]
Description=vm1
After=network.target virtualbox.service
Before=runlevel2.target shutdown.target
[Service]
User=user
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/usr/bin/VBoxManage startvm VMNAME --type headless
ExecStop=/usr/bin/VBoxManage controlvm VMNAME acpipowerbutton
[Install]
WantedBy=multi-user.target

Reload Daemon
sudo systemctl daemon-reload

Test
systemctl status VMNAME

Start
sudo systemctl start VMNAME

Stop
sudo systemctl stop VMNAME

Enable
sudo systemctl enable VMNAME

Disable
sudo systemctl disable VMNAME


或自写脚本并将其做成启动服务:
#!/bin/bash
VMUSER="freeoa"
VMNAME="FreeOAVM" #also you can put UUID in this variable

case "$1" in
    start)
        echo "===Starting VirtualBox VM==="
        sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
        ;;
    stop)
        echo "===Saving state of Virtualbox VM==="
    sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" savestate
    sleep 9
        ;;
    shutdown)
        echo "===Shutting down Virtualbox VM==="
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
    sleep 9
        ;;
    reset)
        echo "===Resetting VirtualBox VM==="
         sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" reset
        ;;
    status)
         echo -n "VMNAME->";sudo -H -u $VMUSER VBoxManage showvminfo "$VMNAME" --machinereadable |grep "VMState="| cut -d "=" -f2
        ;;
        echo "Usage: /etc/init.d/VMscript {start|stop|shutdown|reset|status}"
        exit 1
        ;;
    esac

exit 0