美文网首页
VirtualBox命令行VBoxManage创建与管理虚拟机教

VirtualBox命令行VBoxManage创建与管理虚拟机教

作者: 神农民 | 来源:发表于2017-12-20 22:55 被阅读283次

    VBoxManage is the command-line interface to VirtualBox.

    前言

    本文要操作的虚拟机信息如下:
        名称: UbuntuRDHome
        镜像名称: ubuntu-16.04.3-server-amd64.iso
        网络连接: 桥接
    
    主机环境:
        $ 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
    

    创建UbuntuRDHome虚拟机

    创建虚拟机(同时注册)

    VBoxManage createvm --name UbuntuRDHome --register
    
    注册虚拟机
    
        VBoxManage registervm <Absolute Full Path>/UbuntuRDHome.vbox
    
    仅注销虚拟机
    
        VBoxManage unregistervm UbuntuRDHome
    
    删除虚拟机(!!!会删除所有虚拟硬盘,谨慎操作!!!)
    
        VBoxManage unregistervm --delete UbuntuRDHome
    

    设置系统类型Ubuntu_64

    VBoxManage modifyvm UbuntuRDHome --ostype Ubuntu_64
    

    设置内存大小4G

    VBoxManage modifyvm UbuntuRDHome --memory 4096
    

    建立虚拟磁盘:系统盘100G

    VBoxManage createmedium --filename UbuntuRDHome_HDD_SYS_100G.vdi --size 100000
    

    建立虚拟磁盘:HOME盘100G

    VBoxManage createmedium --filename UbuntuRDHome_HDD_HOME_100G.vdi --size 100000
    

    创建存储控制器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
    

    关联虚拟机磁盘

    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
    
    解除关联:
        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
    

    关联镜像文件

    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
    
    解除关联:
        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
    

    设置网络为桥接(nictype和bridgeadapter要根据主机的实际情况选择)

    VBoxManage modifyvm UbuntuRDHome --nic1 bridged --nictype1 82545EM --cableconnected1 on --bridgeadapter1 enp5s0f0 
    

    设置远程桌面(可以使用VNC通过5540端口访问虚拟机桌面)

    VBoxManage modifyvm UbuntuRDHome --vrdeport 5540 --vrdeaddress ""
    

    打开远程桌面

    VBoxManage modifyvm UbuntuRDHome --vrde on
    

    关闭远程桌面

    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
    

    保持状态关闭虚拟机[推荐]

    VBoxManage controlvm UbuntuRDHome savestate
    
    放弃已保存的状态
        VBoxManage discardstate UbuntuRDHome
    

    断电关闭虚拟机

    VBoxManage controlvm UbuntuRDHome poweroff
    

    正常关机[不能彻底关闭,一直处于stopping状态]

    VBoxManage controlvm UbuntuRDHome acpipowerbutton
    

    管理虚拟机更多命令

    VBoxManage controlvm UbuntuRDHome XXX
    

    高级配置

    参照着VirtualBox GUI,凡是想要的功能都可以通过命令行执行。

    设置CPU数量(必须打开IOAPIC)

    VBoxManage modifyvm UbuntuRDHome  --ioapic on
    VBoxManage modifyvm UbuntuRDHome --cpus 8
    

    使能largepages选项

    VBoxManage modifyvm UbuntuRDHome  --largepages on
    

    设置CPU运行峰值

    VBoxManage modifyvm UbuntuRDHome --cpuexecutioncap 80
    

    虚拟硬盘的高级操作

    VirtualBox的媒体是统一管理的,可以使用list查看:

    VBoxManage list hdds
    

    克隆媒体(克隆已有的虚拟硬盘文件,之后可以添加到虚拟机)

    VBoxManage clonemedium hddfilename
    

    移动虚拟硬盘位置(不是简单的cp或mv)

    VBoxManage modifymedium hddfilename --move "new dir"
    

    快照的创建和恢复(十分重要)

    这里以创建快照 snap20171204-installed 为例,进行演示。

    快照列表:

    VBoxManage snapshot UbuntuRDHome list
    VBoxManage snapshot UbuntuRDHome showvminfo snap20171204-installed
    

    创建快照:

    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虚拟光驱

    sudo mount -t auto /dev/cdrom /mnt
    
    (这里的cdrom是cd的,有时是dvd,具体有什么光驱到 /dev目录下查看)
    

    安装

    cd /mnt
    sudo ./VBoxLinuxAdditions.run 
    

    重启

    sudo reboot
    

    阅读原文 | 作者官网 | 公众号 | Feed | 订阅 | 发私信

    相关文章

      网友评论

          本文标题:VirtualBox命令行VBoxManage创建与管理虚拟机教

          本文链接:https://www.haomeiwen.com/subject/wranwxtx.html