VBoxManage is the command-line interface to VirtualBox.
前言
本文要操作的虛擬機信息如下:
名稱: UbuntuRDHome
鏡像名稱: ubuntu-16.04.3-server-amd64.iso
網(wǎng)絡(luò)連接: 橋接
主機環(huán)境:
$ uname -a命令輸出:
Linux UbuntuServer 4.10.0-37-generic #41-Ubuntu SMP Fri Oct 6 20:20:37 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -a命令輸出:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.04
Release: 17.04
Codename: zesty
$ ifconfig命令輸出:
enp5s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.8.3 netmask 255.255.255.0 broadcast 172.16.8.255
inet6 fe80::dad3:85ff:fee3:8a56 prefixlen 64 scopeid 0x20<link>
ether d8:d3:85:e3:8a:56 txqueuelen 1000 (Ethernet)
RX packets 12193671 bytes 5948053357 (5.9 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 19703213 bytes 21537731944 (21.5 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0xc0400000-c041ffff
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 12358 bytes 750596 (750.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12358 bytes 750596 (750.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
創(chuàng)建UbuntuRDHome虛擬機
創(chuàng)建虛擬機(同時注冊)
VBoxManage createvm --name UbuntuRDHome --register
注冊虛擬機
VBoxManage registervm <Absolute Full Path>/UbuntuRDHome.vbox
僅注銷虛擬機
VBoxManage unregistervm UbuntuRDHome
刪除虛擬機(?。?!會刪除所有虛擬硬盤,謹慎操作?。。。?
VBoxManage unregistervm --delete UbuntuRDHome
設(shè)置系統(tǒng)類型Ubuntu_64
VBoxManage modifyvm UbuntuRDHome --ostype Ubuntu_64
設(shè)置內(nèi)存大小4G
VBoxManage modifyvm UbuntuRDHome --memory 4096
建立虛擬磁盤:系統(tǒng)盤100G
VBoxManage createmedium --filename UbuntuRDHome_HDD_SYS_100G.vdi --size 100000
建立虛擬磁盤:HOME盤100G
VBoxManage createmedium --filename UbuntuRDHome_HDD_HOME_100G.vdi --size 100000
創(chuàng)建存儲控制器IDE、SATA
VBoxManage storagectl UbuntuRDHome --name IDE --add ide --controller PIIX4 --bootable on
VBoxManage storagectl UbuntuRDHome --name SATA --add sata --controller IntelAhci --bootable on
移除控制器:
VBoxManage storagectl UbuntuRDHome --name IDE --remove
VBoxManage storagectl UbuntuRDHome --name SATA --remove
關(guān)聯(lián)虛擬機磁盤
VBoxManage storageattach UbuntuRDHome --storagectl SATA --port 0 --device 0 --type hdd --medium UbuntuRDHome_HDD_SYS_100G.vdi
VBoxManage storageattach UbuntuRDHome --storagectl SATA --port 1 --device 0 --type hdd --medium UbuntuRDHome_HDD_HOME_100G.vdi
解除關(guān)聯(lián):
VBoxManage storageattach UbuntuRDHome --storagectl SATA --port 0 --device 0 --type hdd --medium none
VBoxManage storageattach UbuntuRDHome --storagectl SATA --port 1 --device 0 --type hdd --medium none
關(guān)聯(lián)鏡像文件
VBoxManage storageattach UbuntuRDHome --storagectl IDE --port 0 --device 0 --type dvddrive --medium ubuntu-16.04.3-server-amd64.iso
VBoxManage storageattach UbuntuRDHome --storagectl IDE --port 1 --device 0 --type dvddrive --medium VBoxGuestAdditions_5.1.22.iso
解除關(guān)聯(lián):
VBoxManage storageattach UbuntuRDHome --storagectl IDE --port 0 --device 0 --type dvddrive --medium none
VBoxManage storageattach UbuntuRDHome --storagectl IDE --port 1 --device 0 --type dvddrive --medium none
設(shè)置網(wǎng)絡(luò)為橋接(nictype和bridgeadapter要根據(jù)主機的實際情況選擇)
VBoxManage modifyvm UbuntuRDHome --nic1 bridged --nictype1 82545EM --cableconnected1 on --bridgeadapter1 enp5s0f0
設(shè)置遠程桌面(可以使用VNC通過5540端口訪問虛擬機桌面)
VBoxManage modifyvm UbuntuRDHome --vrdeport 5540 --vrdeaddress ""
打開遠程桌面
VBoxManage modifyvm UbuntuRDHome --vrde on
關(guān)閉遠程桌面
VBoxManage modifyvm UbuntuRDHome --vrde off
查看虛擬機信息
VBoxManage -v
VBoxManage list vms
VBoxManage list runningvms
VBoxManage showvminfo UbuntuRDHome
VBoxManage list hdds
VBoxManage list dvds
啟動與管理虛擬機
啟動虛擬機
VBoxManage startvm UbuntuRDHome --type headless
保持狀態(tài)關(guān)閉虛擬機[推薦]
VBoxManage controlvm UbuntuRDHome savestate
放棄已保存的狀態(tài)
VBoxManage discardstate UbuntuRDHome
斷電關(guān)閉虛擬機
VBoxManage controlvm UbuntuRDHome poweroff
正常關(guān)機[不能徹底關(guān)閉,一直處于stopping狀態(tài)]
VBoxManage controlvm UbuntuRDHome acpipowerbutton
管理虛擬機更多命令
VBoxManage controlvm UbuntuRDHome XXX
高級配置
參照著VirtualBox GUI,凡是想要的功能都可以通過命令行執(zhí)行。
設(shè)置CPU數(shù)量(必須打開IOAPIC)
VBoxManage modifyvm UbuntuRDHome --ioapic on
VBoxManage modifyvm UbuntuRDHome --cpus 8
使能largepages選項
VBoxManage modifyvm UbuntuRDHome --largepages on
設(shè)置CPU運行峰值
VBoxManage modifyvm UbuntuRDHome --cpuexecutioncap 80
虛擬硬盤的高級操作
VirtualBox的媒體是統(tǒng)一管理的,可以使用list查看:
VBoxManage list hdds
克隆媒體(克隆已有的虛擬硬盤文件,之后可以添加到虛擬機)
VBoxManage clonemedium hddfilename
移動虛擬硬盤位置(不是簡單的cp或mv)
VBoxManage modifymedium hddfilename --move "new dir"
快照的創(chuàng)建和恢復(十分重要)
這里以創(chuàng)建快照 snap20171204-installed 為例,進行演示。
快照列表:
VBoxManage snapshot UbuntuRDHome list
VBoxManage snapshot UbuntuRDHome showvminfo snap20171204-installed
創(chuàng)建快照:
VBoxManage snapshot UbuntuRDHome take snap20171204-installed
刪除快照:
VBoxManage snapshot UbuntuRDHome delete snap20171204-installed
恢復快照:
VBoxManage snapshot UbuntuRDHome restore snap20171204-installed
恢復當前快照:
VBoxManage snapshot UbuntuRDHome restorecurrent
安裝 Guest Additions(可選)
掛載CD/DVD虛擬光驅(qū)
sudo mount -t auto /dev/cdrom /mnt
(這里的cdrom是cd的,有時是dvd,具體有什么光驅(qū)到 /dev目錄下查看)
安裝
cd /mnt
sudo ./VBoxLinuxAdditions.run
重啟
sudo reboot