在用virsh命令创建p_w_picpath之前,要保证你的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 /p_w_picpath
root@u-node4:/home/zxl# cd /p_w_picpath/
root@u-node4:/p_w_picpath# 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格式的p_w_picpath,且将此raw文件作文qcow2的backing_file进行大批量虚拟机创建时,则不需要ios镜像)
下面的为cdrom的定义,添加进模板中第33行便可
root@u-node4:/p_w_picpath# vim ubuntu-12.04.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
指定虚拟机名称
root@u-node4:/p_w_picpath# sed -i "s,%VM_NAME%,ubuntu-12.04,g" ubuntu-12.04.xml
指定UUID
root@u-node4:/p_w_picpath# uuidgen
f43b668f-f13c-4129-bee1-20673dcff5fb
root@u-node4:/p_w_picpath# sed -i "s,%UUID%,f43b668f-f13c-4129-bee1-20673dcff5fb,g" ubuntu-12.04.xml
创建一个raw格式的p_w_picpath(注:如果制作的p_w_picpath需要长期使用,尽量采用raw格式)
root@u-node4:/p_w_picpath# qemu-img create -f raw ubuntu-12.04.raw 30G
Formatting 'ubuntu-12.04.raw', fmt=raw size=32212254720
root@u-node4:/p_w_picpath# sed -i "s,%IMAGE_PATH%,/p_w_picpath/ubuntu-12.04.raw,g" ubuntu-12.04.xml
root@u-node4:/p_w_picpath#
设置虚拟光盘,用来安装操作系统 (此镜像需要你提前下载好)
root@u-node4:/p_w_picpath# sed -i "s,%ISO_PATH%,/p_w_picpath/ubuntu-12.04.5-alternate-amd64.iso" ubuntu-12.04.xml
root@u-node4:/p_w_picpath#
root@u-node4:/p_w_picpath# 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:/p_w_picpath# sed -i "s,%ISO_PATH%,/p_w_picpath/ubuntu-12.04.5-alternate-amd64.iso,g" ubuntu-12.04.xml
root@u-node4:/p_w_picpath# MAC="fa:95:$(dd if=/dev/urandom count=1 2> /dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"
root@u-node4:/p_w_picpath# sed -i "s,%MAC%,$MAC,g" ubuntu-12.04.xml
root@u-node4:/p_w_picpath# MAC2="52:54:$(dd if=/dev/urandom count=1 2> /dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"
root@u-node4:/p_w_picpath# sed -i "s,%MAC2%,$MAC2,g" ubuntu-12.04.xml
这时ubuntu12.04.xml的文件就已经配置好了,下面就可以创建并且启动虚拟机了,在此之前先要进行define操作
root@u-node4:/p_w_picpath# 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:/p_w_picpath# virsh define ubuntu-12.04.xml
Domain ubuntu-12.04 defined from ubuntu-12.04.xml
可以看到此虚拟机已经定义,处于关闭状态
root@u-node4:/p_w_picpath# virsh list --all
Id Name State
----------------------------------------------------
- ubuntu-12.04 shut off
root@u-node4:/p_w_picpath# 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
出现错误删除下面内容
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:/p_w_picpath# 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=/p_w_picpath/ubuntu-12.04.raw,if=none,id=drive-virtio-disk0,format=qcow2,cache=none: could not open disk p_w_picpath /p_w_picpath/ubuntu-12.04.raw: Invalid argument
磁盘格式错误将
root@u-node4:/p_w_picpath# virsh start ubuntu-12.04
Domain ubuntu-12.04 started
root@u-node4:/p_w_picpath# virsh list
Id Name State
----------------------------------------------------
11 ubuntu-12.04 running
已经可以用virt-manager看到虚拟机正在安装
但是我们一般用vnc查看虚拟机
root@u-node4:/p_w_picpath#apt-get install vncviewer
root@u-node4:/p_w_picpath#vncviewer 192.168.139.5:5900 (vncviewer 宿主机IP:xml中配置的端口)
也可以通过如下命令确定虚拟机vnc的端口,可以看到显示的是:0
这是最终虚拟机配置文件
root@u-node4:/p_w_picpath# vim ubuntu-12.04.xml