KVM创建虚拟机的改良版脚本

此创建虚拟机的脚本为使用虚拟机xml文件为模板创建,非virt-install命令。当然,用virt-install命令也可以,但没有xml文件灵活,胜在方便简洁。

centos底层镜像(minimal安装)

此底层镜像minimal安装,配置了阿里云的yum源,以及关闭了firewalld和selinux,自启动iptables,也安装了一些常用的软件包,也删除了所有的Mac地址和UUID信息防止虚拟机冲突。 也可以自己制作底层镜像。 网盘链接:https://pan.baidu.com/s/1uOwHN6E1Yhp3fsIkO7b66w 提取码:29mp MD5校验码:f327b2c07b7fcd445ea4e5350c4c3c89 默认存放路径:/var/lib/libvirt/images/CentOS7.qcow2

XML文件内容

默认存放路径: /etc/libvirt/qemu/CentOS7.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
<domain type='kvm'>
<name>NAME</name>
<memory unit='GiB'>MEM</memory>
<currentMemory unit='GiB'>MEM</currentMemory>
<vcpu placement='static'>VCPU</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
</cpu>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/NAME.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
</controller>
<controller type='virtio-serial' index='0'>
</controller>
<interface type='network'>
<source network='vbr1'/>
<model type='virtio'/>
</interface>
<interface type='network'>
<source network='vbr2'/>
<model type='virtio'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
</channel>
<input type='tablet' bus='usb'>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
</video>
<memballoon model='virtio'>
</memballoon>
</devices>
</domain>

#如果修改了虚拟机磁盘的存放路径,注意修改28行虚拟机磁盘存放的位置
# 如果需要在root下,请给root目录加r权限

网桥文件内容

默认存放路径 /etc/libvirt/qemu/networks/vbr1.xml

1
2
3
4
5
6
7
8
9
10
11
<network>
<name>vbr1</name>
<forward mode='nat'/>
<bridge name='vbr1' stp='on' delay='0'/>
<ip address='192.168.11.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.11.50' end='192.168.11.100'/>
</dhcp>
</ip>
</network>
#去除了uuid和mac地址,定义时可自动生成

变量声明

1
2
3
4
clone_img=用于cow的底层镜像(模板机)
clone_xml=用于创建虚拟机的xml配置文件
host_img=创建的虚拟机的磁盘存放路径
host_xml=创建的虚拟机的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
#!/bin/bash

#源镜像和源xml文件的路径
src_img="/var/lib/libvirt/images/virtualmachine/centos7.qcow2"
src_xml="/etc/libvirt/qemu/centos7.xml"

#克隆机镜像和xml文件存放的路径
dest_img_dir="/var/lib/libvirt/images/img/"
dest_xml_dir="/etc/libvirt/qemu/xml/"

#判断路径变量是否存在
if [ ! -f $src_img ];then
echo "Erroy: No found $src_img"
exit 3
fi
if [ ! -f $src_xml ];then
echo "Erroy: No found $src_xml"
exit 3
fi

[ -d $dest_img_dir ] mkdir $dest_img_dir
[ -d $dest_xml_dir ] mkdir $dest_xml_dir

#判断需要创建的虚拟机名字是否存在
read -p 'name : ' name
[ -z "$name" ] && echo -e && echo "name is empty" >&2 && exit 1
for i in virsh list --all awk '{print $2}' sed '1,2d;$d'
do
[ $name == $i ] && echo "name is conflict" >&2 && exit 2
done

#判断内存变量是否合理并不超过8
read -p 'mem(G) :[2] ' mem
mem=${mem:-2}
[ -z "$mem" ] && echo -e && echo "mem is empty" >&2 && exit 1
if [[ $mem = *[!0-9]* ]]; then
echo -e
echo "mem only is integer" >&2
exit 2
fi
[ $mem -gt 8 ] && echo -e && echo "mem is too large" >&2 && exit 3

#判断cpu变量是否合理
read -p 'cpu :[1] ' cpu
cpu=${cpu:-1}
[ -z "$cpu" ] && echo -e && echo "cpu is empty" >&2 && exit 1
if [[ $cpu = *[!0-9]* ]]; then
echo -e
echo "cpu only is integer" >&2
exit 2
fi
[ $cpu -gt 4 ] && echo -e && echo "cpu is too large" &>2 && exit 3

#创建克隆机模板
qemu-img create -b $src_img -f qcow2 ${dest_img_dir}${name}.img &> /dev/null
[ $? -eq 0 ] && echo -e "\033[1mqemu-img \033[32m[Done]\033[0m"
[ $? -ne 0 ] && echo -e "\033[1mqemu-img \033[31m[Failed]\033[0m"

#拷贝模板机的xml文件并修改参数
cp $src_xml ${dest_xml_dir}${name}.xml &> /dev/null
sed -i "s/NAME/${name}/" ${dest_xml_dir}${name}.xml &> /dev/null
sed -i "s/MEM/${mem}/" ${dest_xml_dir}${name}.xml &> /dev/null
sed -i "s/VCPU/${cpu}/" ${dest_xml_dir}${name}.xml &> /dev/null

#定义虚拟机
virsh define ${dest_xml_dir}${name}.xml &> /dev/null
[ $? -eq 0 ] && echo -e "\033[1mvirsh-define \033[32m[Done]\033[0m"
[ $? -ne 0 ] && echo -e "\033[1mvirsh-define \033[31m[Failed]\033[0m"

#开启虚拟机
virsh start $name &> /dev/null
[ $? -eq 0 ] && echo -e "\033[1mstart_host \033[32m[Done]\033[0m"
[ $? -ne 0 ] && echo -e "\033[1mstart_host \033[31m[Failed]\033[0m"

排错思路

脚本分三部分,那一部分出错,会报红,哪一部分报红,就重复运行报红部分的命令,把变量替换为实际路径

  • qemu-img,此命令为
1
2
qemu-img create -b $clone_img -f qcow2

此命令一般不会出错,出错原因可能是变量有问题,脚本路径不对
  • virsh-define,命令为
1
virsh define ${host_xml}${name}.xml

错误原因较多,可能目录没有libvirt的读取权限,可能配置文件错误

  • host_start,命令为
1
virsh start $name

此部分出错原因一般为虚拟机磁盘位置问题