无终端显示器 Debian服务器上的VirtualBox 4.1

时间:2020-03-21 11:47:30  来源:igfitidea点击:

在无终端显示器 Debian服务器上安装VirtualBox并设置Ubuntu 12.04来宾操作系统。

安装

安装内核头文件:

# apt-get update && apt-get install linux-headers-`uname -r`

安装VirtualBox:

# apt-get install virtualbox

默认情况下,应安装三个软件包:virtualbox,virtualbox-dkms和virtualbox-qt。
如果需要VirtualBox来宾添加,请安装它们:

# apt-get install virtualbox-guest-additions virtualbox-guest-additions-iso

检查是否已加载VirtualBox内核模块:

# lsmod | egrep 'Module|vbox'
Module         Size Used by
vboxpci       18742 0 
vboxnetadp    25431 0 
vboxnetflt    23260 0 
vboxdrv      165027 3 vboxnetflt,vboxnetadp,vboxpci

如果未加载这些文件,请执行以下操作:

# modprobe vboxdrv 
# modprobe vboxnetflt
# modprobe vboxnetadp
# modprobe vboxpci

安装VirtualBox扩展

下载VirtualBox扩展包(v4.1.18),其中包含VBoxHeadless所需的VRDE模块:

# wget http://download.virtualbox.org/virtualbox/4.1.18/Oracle_VM_VirtualBox_Extension_Pack-4.1.18.vbox-extpack

安装扩展包:

# VBoxManage extpack install ./Oracle_VM_VirtualBox_Extension_Pack-4.1.18.vbox-extpack
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully installed "Oracle VM VirtualBox Extension Pack".

验证安装:

# VBoxManage list extpacks
Extension Packs: 1
Pack no. 0:   Oracle VM VirtualBox Extension Pack
Version:      4.1.18
Revision:     78361
Description:  USB 2.0 Host Controller, VirtualBox RDP, PXE ROM with E1000 support.
VRDE Module:  VBoxVRDP
Usable:       true 
Why unusable:

如果我们需要删除扩展包,则可以通过以下方式进行操作:

# VBoxManage extpack uninstall "Oracle VM VirtualBox Extension Pack"

配置

我们将使用单独的30GB'/dev/sdb1'磁盘存储VirtualBox镜像:

# df -h|egrep "Size|vb"
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        30G  172M   28G   1% /vb

我们将使用常规的“桑迪”用户来运行VirtualBox,将sandy添加到vboxusers组:

# usermod -aG vboxusers sandy

检查确认:

# grep vbox /etc/group
vboxusers:x:110:sandy

将VirtualBox目录的所有权更改为sandy:

# chown -R sandy /vb/

切换到普通的沙用户:

# su -l sandy

创建VirtualBox Ubuntu镜像

可以使用vbox-script.sh完成以下所有配置:

#!/bin/bash
# written by igi (www.theitroad.com)
# 12/04/2014 (dd/mm/yy)
# copyleft free software
# below are the variables to pass to VBoxManage, modify appropriately
VM="Ubuntu1204";
ISO="/path/to/iso/file.iso";
VBROOT="/vb";
OSTYPE="Ubuntu";
DISKSIZE=10240; #in MB
内存=512; #in MB
CPU=1;
CPUCAP=100;
PAE="off";
HWVIRT="off";
NESTPAGING="off";
VRAM=8;
USB="off";
echo "Creating the "$VM" VM."
cd ~;
VBoxManage createhd --filename "$VBROOT"/"$VM"/"$VM".vdi --size "$DISKSIZE";
VBoxManage createvm --register --name "$VM" --basefolder "$VBROOT" --ostype "$OSTYPE";
VBoxManage storagectl "$VM" --name "SATA Controller" --add sata  --controller IntelAHCI;
VBoxManage storageattach "$VM" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "$VBROOT"/"$VM"/"$VM".vdi;
VBoxManage storagectl "$VM" --name "IDE Controller" --add ide;
VBoxManage storageattach "$VM" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium "$ISO";
VBoxManage modifyvm "$VM" --memory "$内存";
VBoxManage modifyvm "$VM" --boot1 dvd --boot2 disk --boot3 none --boot4 none;
VBoxManage modifyvm "$VM" --chipset piix3;
VBoxManage modifyvm "$VM" --ioapic off;
VBoxManage modifyvm "$VM" --mouse ps2;
VBoxManage modifyvm "$VM" --cpus "$CPU" --cpuexecutioncap "$CPUCAP" --pae "$PAE";
VBoxManage modifyvm "$VM" --hwvirtex off --nestedpaging off;
VBoxManage modifyvm "$VM" --nic1 bridged --bridgeadapter1 eth0;
VBoxManage modifyvm "$VM" --vram "$VRAM";
VBoxManage modifyvm "$VM" --monitorcount 1;
VBoxManage modifyvm "$VM" --accelerate2dvideo off --accelerate3d off;
VBoxManage modifyvm "$VM" --audio none;
VBoxManage modifyvm "$VM" --snapshotfolder "$VBROOT"/"$VM"/Snapshots;
VBoxManage modifyvm "$VM" --clipboard bidirectional;
VBoxManage modifyvm "$VM" --usb "$USB";
echo "Run 'vboxmanage list -l vms | less' to check configuration."
exit 0

设置要使用的虚拟机名称:

$VM=Ubuntu1204

下载Ubuntu服务器的镜像文件:

$cd ~
$wget http://releases.ubuntu.com/12.04/ubuntu-12.04.5-server-i386.iso

创建10GB虚拟硬盘'/vb/Ubuntu1204/Ubuntu1204.vdi':

$VBoxManage createhd --filename /vb/$VM/$VM.vdi --size 10240
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Disk image created. UUID: e9071338-8ec2-4517-bfc8-24e7c24753d0

查找要使用的Ubuntu镜像的操作系统类型:

$VBoxManage list ostypes | grep -i ubuntu
ID:          Ubuntu
Description: Ubuntu
ID:          Ubuntu_64
Description: Ubuntu (64 bit)

我们使用32位Ubuntu镜像,因此“ Ubuntu”就可以了,创建并注册虚拟机:

$VBoxManage createvm --register --name $VM --basefolder /vb --ostype Ubuntu
Virtual machine 'Ubuntu1204' is created and registered.
UUID: bf4911f5-f1d1-46d6-ab89-d7e0d2cc7407
Settings file: '/vb/Ubuntu1204/Ubuntu1204.vbox'

配置存储

添加SATA控制器:

$VBoxManage storagectl $VM --name "SATA Controller" --add sata  --controller IntelAHCI

将虚拟硬盘驱动器连接到SATA端口:

$VBoxManage storageattach $VM --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium /vb/$VM/$VM.vdi

添加IDE控制器:

$VBoxManage storagectl $VM --name "IDE Controller" --add ide

将Ubuntu DVD镜像文件添加到IDE端口:

$VBoxManage storageattach $VM --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium ~/ubuntu-12.04.4-server-i386.iso

配置系统主板

分配512MB 内存:

$VBoxManage modifyvm $VM --memory 512

设置引导优先级,首先设置DVD-ROM,然后设置硬盘驱动器:

$VBoxManage modifyvm $VM --boot1 dvd --boot2 disk --boot3 none --boot4 none

除非有理由更改为ich9,否则将芯片组保留为piix3:

$VBoxManage modifyvm $VM --chipset piix3

禁用I/O APIC,因为我们的客户操作系统是32位的,并且我们将为虚拟机使用单个虚拟CPU:

$VBoxManage modifyvm $VM --ioapic off

如果需要绝对清洗设备,请更改为usbtablet。

$VBoxManage modifyvm $VM --mouse ps2

配置系统处理器

提供一个具有100%执行上限的虚拟CPU。
由于我们的CPU支持,因此也启用了物理地址扩展。

$VBoxManage modifyvm $VM --cpus 1 --cpuexecutioncap 100 --pae on

配置系统加速

由于我们的CPU不支持hwvirtex,因此不支持Intel VT-x硬件虚拟化:

$VBoxManage modifyvm $VM --hwvirtex off --nestedpaging off

配置网络适配器

添加桥接的eth0(这是我们的适配器)网卡:

$VBoxManage modifyvm $VM --nic1 bridged --bridgeadapter1 eth0

配置显示和音频

设置提供给虚拟机的视频内存量:

$VBoxManage modifyvm $VM --vram 8
$VBoxManage modifyvm $VM --monitorcount 1

不需要视频加速,因为我们不会在来宾计算机上使用GUI:

$VBoxManage modifyvm $VM --accelerate2dvideo off --accelerate3d off

禁用音频,因为它也是不需要的:

$VBoxManage modifyvm $VM --audio none

配置常规高级功能和USB

将“/vb/Ubuntu1204/Snapshots”路径设置为快照文件夹:

$VBoxManage modifyvm $VM --snapshotfolder /vb/$VM/Snapshots

启用双向剪贴板共享:

$VBoxManage modifyvm $VM --clipboard bidirectional

禁用USB支持:

$VBoxManage modifyvm $VM --usb off

查看来宾虚拟机配置

可以通过以下方式检查配置:

$vboxmanage list -l vms | less

安装虚拟机

在我们的无头Linux服务器上,启动VM并将VirtualBox远程桌面扩展服务器绑定到TCP 8888端口:

$VBoxHeadless --startvm $VM -e "TCP/Ports=8888" &
Oracle VM VirtualBox Headless Interface 4.1.18_Debian
(C) 2008-2014 Oracle Corporation
All rights reserved.
VRDE server is listening on port 8888.

请注意,由于无鼠键显示器服务器没有其他输出方式,因此无论我们是否在VM的设置中启用了VRDP服务器,都将始终启用VRDP服务器。

我们可以使用netstat检查VRDP是否正在侦听TCP端口8888.
确保防火墙上的端口处于打开状态,以便能够连接。

# netstat -ntlp | grep -i vbox
tcp   0  0 0.0.0.0:8888    0.0.0.0:*      LISTEN   24318/VBoxHeadless

要继续安装Ubuntu,只需从另一台具有GUI的计算机上连接rdesktop(或者xfreerdp),然后按照屏幕上的说明进行操作:

$rdesktop <virtualbox_server_ip>:8888

安装完成后,可以在不使用VRDP服务器的情况下启动Ubuntu VM,因为我们将使用SSH:

$VBoxHeadless --startvm $VM --vrde off &

获取虚拟机的专用IP(用于SSH)

我们可以通过发出以下命令来查找VM的专用IP:

$VBoxManage guestproperty enumerate $VM | grep IP
Name: /VirtualBox/GuestInfo/Net/0/V4/IP, value: 10.1.2.3, timestamp: 1234, flags:

或者:

$VBoxManage guestproperty get $VM "/VirtualBox/GuestInfo/Net/0/V4/IP"
Value: 10.1.2.3

创建快照

完成安装来宾操作系统后,请创建快照,以便我们随时可以根据需要还原更改:

$VBoxManage snapshot $VM take fresh-install
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

从快照还原非常简单:

$VBoxManage snapshot $VM restore fresh-install
Restoring snapshot d437e014-0bcf-4d4b-9314-4ff050162627
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%