本文教大家怎么用libvirt自帶的virsh命令行工具創建虛擬機。
安裝相關軟件包:
sudo apt-get install qemu-kvm libvirt
創建虛擬機配置文件ubuntu.xml:
<domain type='kvm'>
<name>test1_ubuntu</name> //虛擬機名稱
<memory>1048576</memory> //最大內存,單位k
<currentMemory>1048576</currentMemory> //可用內存,單位k
<vcpu>1</vcpu> //虛擬cpu個數
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='cdrom'/> //光盤啟動
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/home/xuriwuyun/.kvm/image/ubuntu.qcow2'/> //目的鏡像路徑
<target dev='hda' bus='ide'/>
</disk>
<disk type='file' device='cdrom'>
<source file='/home/xuriwuyun/Downloads/ubuntu-12.04.1-server-amd64.iso'/> //光盤鏡像路徑
<target dev='hdb' bus='ide'/>
</disk>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' listen = '0.0.0.0' keymap='en-us'/>//vnc方式登錄,端口號自動分配,自動加1,可以通過virsh vncdisplay來查詢
</devices>
</domain>
制作虛擬機硬盤鏡像文件:
qemu create ubuntu.raw 10G
下載系統鏡像文件,修改配置文件中的相關參數。
啟動虛擬機:
virsh create ubuntu.xml
登錄虛擬機:
vncviewer localhost:5900
此處可使用命令:
netstat -lnpt
查看虛擬機的vnc監聽端口。
