Openstack教程:使用virsh创建带有OS的image
在用virsh命令创建image之前,要保证你的kvm和libvirt已经安装且可以运行,如何安装kvm和libvirt可以参考安科网的其他文章。
root@u-node4:/var/run/libvirt# kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
root@u-node4:/var/run/libvirt# virsh list
Id Name State
----------------------------------------------------
root@u-node4:/home/zxl# mkdir /image
root@u-node4:/home/zxl# cd /image/
root@u-node4:/image# ll
total 8
drwxr-xr-x 2 root root 4096 Aug 22 10:04 ./
drwxr-xr-x 26 root root 4096 Aug 22 10:04 ../
下面是一个虚拟机配置文件的模板,对其进行特定的修改便可(注,此模板中没有定义cdrom,需要添加,一般情况下如果你有一个带有os的raw格式的image,且将此raw文件作文qcow2的backing_file进行大批量虚拟机创建时,则不需要ios镜像)
下面的为cdrom的定义,添加进模板中第33行便可
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/image/Ubuntu-12.04.5-alternate-amd64.iso'/>
<target dev='hdb' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' unit='0'/>
</disk>
root@u-node4:/image# vim ubuntu-12.04.xml
1 <domain type='kvm'>
2 <name>%VM_NAME%</name>
3 <uuid>%UUID%</uuid>
4 <memory>1048576</memory>
5 <currentMemory>1048576</currentMemory>
6 <vcpu>1</vcpu>
7 <os>
8 <type arch='x86_64' machine='pc-0.14'>hvm</type>
9 <boot dev='hd'/>
10 <bootmenu enable='yes'/>
11 </os>
12 <cpu match='exact'>
13 <model>core2duo</model>
14 <feature policy='require' name='vmx'/>
15 </cpu>
16 <features>
17 <acpi/>
18 <apic/>
19 <pae/>
20 </features>
21 <clock offset='localtime'/>
22 <on_poweroff>destroy</on_poweroff>
23 <on_reboot>restart</on_reboot>
24 <on_crash>restart</on_crash>
25 <devices>
26 <emulator>/usr/bin/kvm</emulator>
27 <disk type='file' device='disk'>
28 <driver name='qemu' type='qcow2' cache='none'/>
29 <source file='%IMAGE_PATH%'/>
30 <target dev='vda' bus='virtio'/>
31 <alias name='virtio-disk0'/>
32 </disk>
33
34 <disk type='file' device='disk'>
35 <driver name='qemu' type='raw' cache='none'/>
36 <source file='%RAW_DISK_PATH%'/>
37 <target dev='vdb' bus='virtio'/>
38 <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
39 </disk>
40
41
42 <controller type='ide' index='0'>
43 <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
44 </controller>
45 <controller type='fdc' index='0'/>
46
47 <interface type='bridge'>
48 <mac address='%MAC%'/>
49 <source bridge='br100'/>
50 <target dev='vnet0'/>
51 <alias name='net0'/>
52 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
53 </interface>
54
55 <interface type='network'>
56 <mac address='%MAC2%'/> <!-- 192.168.222.%IP1% -->
57 <source network='default'/>
58 <target dev='vnet1'/>
59 <alias name='net1'/>
60 <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
61 </interface>
62
63 <interface type='network'>
64 <mac address='%MAC3%'/> <!-- 192.168.111.%IP2% -->
65 <source network='default'/>
66 <target dev='vnet2'/>
67 <alias name='net2'/>
68 <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
69 </interface>
70
71
72
73 <serial type='pty'>
74 <target port='0'/>
75 </serial>
76 <console type='pty'>
77 <target type='serial' port='0'/>
78 </console>
79 <input type='tablet' bus='usb'/>
80 <input type='mouse' bus='ps2'/>
81 <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>
82 <listen type='address' address='0.0.0.0'/>
83 </graphics>
84 <sound model='ich6'>
85 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
86 </sound>
87 <video>
88 <model type='vga' vram='9216' heads='1'/>
89 <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
90 </video>
91 <memballoon model='virtio'>
92 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
93 </memballoon>
94 </devices>
95 </domain>
指定虚拟机名称
root@u-node4:/image# sed -i "s,%VM_NAME%,ubuntu-12.04,g" ubuntu-12.04.xml
指定UUID
root@u-node4:/image# uuidgen
f43b668f-f13c-4129-bee1-20673dcff5fb
root@u-node4:/image# sed -i "s,%UUID%,f43b668f-f13c-4129-bee1-20673dcff5fb,g" ubuntu-12.04.xml
创建一个raw格式的image(注:如果制作的image需要长期使用,尽量采用raw格式)
root@u-node4:/image# qemu-img create -f raw ubuntu-12.04.raw 30G
Formatting 'ubuntu-12.04.raw', fmt=raw size=32212254720
root@u-node4:/image# sed -i "s,%IMAGE_PATH%,/image/ubuntu-12.04.raw,g" ubuntu-12.04.xml
root@u-node4:/image#
设置虚拟光盘,用来安装操作系统 (此镜像需要你提前下载好)
root@u-node4:/image# sed -i "s,%ISO_PATH%,/image/ubuntu-12.04.5-alternate-amd64.iso" ubuntu-12.04.xml
root@u-node4:/image#
root@u-node4:/image# ll
total 779280
drwxr-xr-x 2 root root 4096 Aug 22 10:15 ./
drwxr-xr-x 26 root root 4096 Aug 22 10:04 ../
-rw-r--r-- 1 root root 797966336 Aug 13 06:17 ubuntu-12.04.5-alternate-amd64.iso
-rw-r--r-- 1 root root 32212254720 Aug 22 10:11 ubuntu-12.04.raw
-rw-r--r-- 1 root root 2894 Aug 22 10:12 ubuntu-12.04.xml
设置虚拟网卡,在此之前需要确保br100启用,用如下命令生成并替换MAC地址
root@u-node4:/image# sed -i "s,%ISO_PATH%,/image/ubuntu-12.04.5-alternate-amd64.iso,g" ubuntu-12.04.xml
root@u-node4:/image# MAC="fa:95:$(dd if=/dev/urandom count=1 2> /dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"
root@u-node4:/image# sed -i "s,%MAC%,$MAC,g" ubuntu-12.04.xml
root@u-node4:/image# MAC2="52:54:$(dd if=/dev/urandom count=1 2> /dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"
root@u-node4:/image# sed -i "s,%MAC2%,$MAC2,g" ubuntu-12.04.xml
这时ubuntu12.04.xml的文件就已经配置好了,下面就可以创建并且启动虚拟机了,在此之前先要进行define操作
root@u-node4:/image# virsh define ubuntu-12.04.xml
error: Failed to define domain from ubuntu-12.04.xml
error: XML error: unable to parse mac address '%MAC3%'
出现错误,删除ubuntu-12.04.xml中关于MAC3的定义后重新define
修改关于名称的定义为ubuntu-12.04
root@u-node4:/image# virsh define ubuntu-12.04.xml
Domain ubuntu-12.04 defined from ubuntu-12.04.xml
可以看到此虚拟机已经定义,处于关闭状态
root@u-node4:/image# virsh list --all
Id Name State
----------------------------------------------------
- ubuntu-12.04 shut off
root@u-node4:/image# virsh start ubuntu-12.04
error: Failed to start domain %ubuntu-12.04%
error: Cannot access storage file '%RAW_DISK_PATH%' (as uid:0, gid:0): No such file or directory
出现错误,删除ubuntu-12.04.xml中关于vdb raw磁盘的定义后启动
root@u-node4:~# virsh start ubuntu-12.04
error: Failed to start domain ubuntu-12.04
error: unsupported configuration: guest and host CPU are not compatible: Host CPU does not provide required features: monitor
出现错误删除下面内容
<cpu match='exact'>
<model>core2duo</model>
<feature policy='require' name='vmx'/>
</cpu>
root@u-node4:~# virsh start ubuntu-12.04
error: Failed to start domain ubuntu-12.04
error: Cannot get interface MTU on 'br100': No such device
出现错误删除,没有配置基于网桥的虚拟网卡
root@u-node4:~# vim /etc/network/interfaces
root@u-node4:/image# virsh start ubuntu-12.04
error: Failed to start domain ubuntu-12.04
error: internal error: Process exited while reading console log output: char device redirected to /dev/pts/1
Unsupported cluster size: 2^0kvm: -drive file=/image/ubuntu-12.04.raw,if=none,id=drive-virtio-disk0,format=qcow2,cache=none: could not open disk image /image/ubuntu-12.04.raw: Invalid argument
磁盘格式错误将<driver name='qemu' type='qcow2' cache='none'/>中的qcow2该为raw
root@u-node4:/image# virsh start ubuntu-12.04
Domain ubuntu-12.04 started
root@u-node4:/image# virsh list
Id Name State
----------------------------------------------------
11 ubuntu-12.04 running
已经可以用virt-manager看到虚拟机正在安装
但是我们一般用vnc查看虚拟机
root@u-node4:/image#apt-get install vncviewer
root@u-node4:/image#vncviewer 192.168.139.5:5900 (vncviewer 宿主机IP:xml中配置的端口)
也可以通过如下命令确定虚拟机vnc的端口,可以看到显示的是:0
这是最终虚拟机配置文件
root@u-node4:/image# vim ubuntu-12.04.xml
<domain type='kvm'>
<name>ubuntu-12.04</name>
<uuid>f43b668f-f13c-4129-bee1-20673dcff5fb</uuid>
<memory>1048576</memory>
<currentMemory>1048576</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch='x86_64' machine='pc-0.14'>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/image/ubuntu-12.04.raw'/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/image/ubuntu-12.04.5-alternate-amd64.iso'/>
<target dev='hdb' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' unit='0'/>
</disk>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='fdc' index='0'/>
<interface type='bridge'>
<mac address='fa:95:d9:d9:9a:ca'/>
<source bridge='br100'/>
<target dev='vnet0'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='network'>
<mac address='52:54:d8:c3:69:30'/> <!-- 192.168.222.%IP1% -->
<source network='default'/>
<target dev='vnet1'/>
<alias name='net1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='vga' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>