1 , XML文件
1 <domain type='kvm'> //如果是Xen,則type=‘xen’ 2 <name>ubuntu</name> //虛擬機名稱,同一物理機唯一 3 <uuid>fd3535db-2558-43e9-b067-314f48211343</uuid> //同一物理機唯一,可用uuidgen生成 4 <memory>524288</memory> 5 <currentMemory>524288</currentMemory> //memory這兩個值最好設成一樣 6 <vcpu>2</vcpu> //虛擬機可使用的cpu個數,查看物理機可用CPU個數:cat /proc/cpuinfo |grep processor | wc -l 7 <os> 8 <type arch='x86_64' machine='pc-i440fx-vivid'>hvm</type> //arch指出系統架構類型,machine 則是機器類型,查看機器類型:qemu-system-x86_64 -M ? 9 <boot dev='hd'/> //啟動介質 10 <bootmenu enable='yes'/> //表示啟動按F12進入啟動菜單 11 </os> 12 <features> 13 <acpi/> //Advanced Configuration and Power Interface,高級配置與電源接口 14 <apic/> //Advanced Programmable Interrupt Controller,高級可編程中斷控制器 15 <pae/> //Physical Address Extension,物理地址擴展 16 </features> 17 <clock offset='localtime'/> //虛擬機時鍾設置,這里表示本地本機時間 18 <on_poweroff>destroy</on_poweroff> //突發事件動作 19 <on_reboot>restart</on_reboot> 20 <on_crash>restart</on_crash> 21 <devices> //設備配置 22 <emulator>/usr/bin/kvm</emulator> //如果是Xen則是/usr/lib/xen/binqemu-dm 23 <disk type='file' device='disk'> 24 <driver name='qemu' type='raw'/> 25 <source file='/opt/vm/vmdev/ubuntu-15.10.raw' /> 26 <target dev='vda' bus='virtio'/> 27 <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> //slot值同一虛擬機上唯一 28 </disk> 29 <disk type='file' device='disk'> 30 <driver name='qemu' type='raw'/> 31 <source file='/opt/vm/vmdev/ubuntu-15.10_2.raw' /> 32 <target dev='vdb' bus='virtio'/> 33 </disk> 34 <disk type='file' device='cdrom'> 35 <driver name='qemu' type='raw'/> 36 <source file='/opt/vm/vmiso/ubuntu-15.10-server-amd64.iso'/> 37 <target dev='hdc' bus='ide'/> 38 <readonly/> 39 </disk> 40 <interface type='bridge'> //利用網橋連接網絡 41 <mac address='fa:92:01:33:d4:fa'/> //可用命令生成:見下面的補充 42 <source bridge='br100'/> //配置的網橋網卡名稱 43 <target dev='vnet0'/> //同一網橋下相同 44 <alias name='net0'/> //同一網橋下相同 45 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> //注意slot值唯一 46 </interface> 47 <interface type='network'> //基於虛擬局域網的網絡 48 <mac address='52:54:4a:e1:1c:84'/> //可用命令生成,見下面的補充 49 <source network='default'/> //默認 50 <target dev='vnet1'/> //同一虛擬局域網的值相同 51 <alias name='net1'/> 52 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> //注意slot值 53 </interface> 54 <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us' //vnc配置,基本不用改 55 /> 56 <listen type='address' address='0.0.0.0'/> 57 </graphics> 58 </devices> 59 </domain>
基於網橋的MAC地址創建方法:
root@cy-ubuntu# MACADDR="fa:92:$(dd if=/dev/urandom count=1 2>/dev/null |md5sum |sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; root@cy-ubuntu# echo $MACADDR fa:92:01:33:d4:fa
基於虛擬局域網MAC地址創建方法:
root@cy-ubuntu# MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null |md5sum |sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; root@cy-ubuntu# echo $MACADDR 52:54:4a:e1:1c:84
創建虛擬磁盤:
raw格式:
root@cy-computer:~# qemu-img create -f raw /opt/vmdev/ubuntu-15.10.raw 20G
qcow2格式:
root@cy-computer:~# qemu-img create -f qcow2 -o cluster_size=2M,backing_file=/opt/vmdev/ubuntu-15.10.raw /opt/vmdev/ubuntu-nova.qcow2 40G
//backing_file的使用可以節省磁盤空間和批量創建虛擬機的時間。此時以raw中的系統為基礎,qcow2只記錄改變的數據
2 , 虛擬網路
創建網橋br100
修改網絡配置文件:/etc/network/interfaces
基於DHCP方式:
# interfaces(5) file used by ifup(8) and ifdown(8) auto lo iface lo inet loopback #新增 auto enp7s0 iface enp7s0 inet dhcp auto br100 iface br100 inet dhcp bridge_port enp7s0 bridge_stp off bridge_maxwait 0 bridge_fd 0
注;我的機器配置后發現無法訪問網頁,刪除了兩條網關配置才可以,原因是給網卡分配了IP。
root@cy-ubuntu:~# route del -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.1.1
基於靜態地址:
# interfaces(5) file used by ifup(8) and ifdown(8) auto lo iface lo inet loopback #xin zeng auto enp7s0 #iface enp7s0 inet dhcp iface enp7s0 inet static address 192.168.1.110 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.1 auto br100 #iface br100 inet dhcp iface br100 inet static bridge_ports enp7s0 bridge_stp off bridge_maxwait 0 bridge_fd 0 address 192.168.1.110 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.1
重啟生效:/etc/init.d/networking restart
