blob: 86bd5666b0d938ab45bb262cf85d1522e02161db [file] [log] [blame]
alexz1c02cd62018-04-15 22:18:03 +02001#!/bin/bash -xe
2
Denis Egorenkodcc233d2019-03-06 18:09:24 +04003VM_MGM_BRIDGE_DISABLE=${VM_MGM_BRIDGE_DISABLE:-false}
4VM_CTL_BRIDGE_DISABLE=${VM_CTL_BRIDGE_DISABLE:-false}
alexz1c02cd62018-04-15 22:18:03 +02005VM_MGM_BRIDGE_NAME=${VM_MGM_BRIDGE_NAME:-"br-mgm"}
Richard Felklbe962ed2018-06-14 17:28:08 +02006VM_CTL_BRIDGE_NAME=${VM_CTL_BRIDGE_NAME:-"br-ctl"}
Denis Egorenkodcc233d2019-03-06 18:09:24 +04007VM_MGM_NETWORK_NAME=${VM_MGM_NETWORK_NAME:-"mgm_network"}
8VM_CTL_NETWORK_NAME=${VM_CTL_NETWORK_NAME:-"ctl_network"}
azvyagintsev4e851ee2018-12-21 13:49:12 +02009VM_MEM_KB=${VM_MEM_KB:-"12589056"}
alexz1c02cd62018-04-15 22:18:03 +020010VM_CPUS=${VM_CPUS:-"4"}
Denis Egorenkodcc233d2019-03-06 18:09:24 +040011# optional params if you won't use bridge on host
12VM_MGM_NETWORK_GATEWAY=${VM_MGM_NETWORK_GATEWAY:-"192.168.56.1"}
13VM_MGM_NETWORK_MASK=${VM_MGM_NETWORK_MASK:-"255.255.255.0"}
14VM_CTL_NETWORK_GATEWAY=${VM_CTL_NETWORK_GATEWAY:-"192.168.57.1"}
15VM_CTL_NETWORK_MASK=${VM_CTL_NETWORK_MASK:-"255.255.255.0"}
alexz1c02cd62018-04-15 22:18:03 +020016
17if [[ -z ${VM_NAME} ]]; then
18 echo "ERROR: \$VM_NAME not set!"
19 exit 1
20fi
21if [[ ! -f ${VM_SOURCE_DISK} ]] || [[ -z ${VM_SOURCE_DISK} ]]; then
22 echo "ERROR: \$VM_SOURCE_DISK not set, or file does not exist!"
23 exit 1
24fi
25if [[ ! -f ${VM_CONFIG_DISK} ]] || [[ -z ${VM_CONFIG_DISK} ]]; then
26 echo "ERROR: \$VM_CONFIG_DISK not set, or file does not exist!"
27 exit 1
28fi
29
Denis Egorenkodcc233d2019-03-06 18:09:24 +040030function check_packages {
31 PACKAGES="qemu-utils libvirt-bin qemu-kvm"
32 for i in $PACKAGES; do
33 dpkg -s $i &> /dev/null || { echo "Package $i is not installed!"; exit 1; }
34 done
35}
36
37function create_network {
38 local network=${1}
39 virsh net-destroy ${network} 2> /dev/null || true
40 virsh net-undefine ${network} 2> /dev/null || true
41 virsh net-define ${network}.xml
42 virsh net-autostart ${network}
43 virsh net-start ${network}
44}
45
46function create_bridge_network {
47 local network=$1
48 local bridge_name=$2
49 cat <<EOF > $(pwd)/${network}.xml
50<network>
51 <name>${network}</name>
52 <forward mode="bridge"/>
53 <bridge name="${bridge_name}" />
54</network>
55EOF
56 create_network ${network}
57}
58
59function create_host_network {
60 local network=$1
61 local gateway=$2
62 local netmask=$3
63 local nat=${4:-false}
64 cat <<EOF > $(pwd)/${network}.xml
65<network>
66 <name>${network}</name>
67 <bridge name="${network}" />
68 <ip address="${gateway}" netmask="${netmask}"/>
69EOF
70 if [[ ${nat} ]]; then
71 cat <<EOF>> $(pwd)/${network}.xml
72 <forward mode="nat"/>
73EOF
74 fi
75 cat <<EOF>> $(pwd)/${network}.xml
76</network>
77EOF
78 create_network ${network}
79}
80
81check_packages
82
alexz1c02cd62018-04-15 22:18:03 +020083# Template definition
84cat <<EOF > $(pwd)/${VM_NAME}-vm.xml
85<domain type='kvm'>
86 <name>$VM_NAME</name>
87 <memory unit='KiB'>$VM_MEM_KB</memory>
88 <currentMemory unit='KiB'>$VM_MEM_KB</currentMemory>
89 <vcpu placement='static'>$VM_CPUS</vcpu>
90 <resource>
91 <partition>/machine</partition>
92 </resource>
93 <os>
94 <type >hvm</type>
95 <boot dev='hd'/>
96 </os>
97 <features>
98 <acpi/>
99 </features>
100 <clock offset='utc'>
101 <timer name='rtc' tickpolicy='catchup'/>
102 <timer name='pit' tickpolicy='delay'/>
103 <timer name='hpet' present='no'/>
104 </clock>
105 <pm>
106 <suspend-to-mem enabled='no'/>
107 <suspend-to-disk enabled='no'/>
108 </pm>
109 <devices>
110 <emulator>/usr/bin/kvm-spice</emulator>
111 <disk type='file' device='disk'>
112 <driver name='qemu' type='qcow2' cache='none'/>
113 <source file='$VM_SOURCE_DISK'/>
114 <target dev='vda' bus='virtio'/>
115 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
116 </disk>
117 <disk type='file' device='cdrom'>
118 <driver name='qemu' type='raw'/>
119 <source file='$VM_CONFIG_DISK'/>
120 <backingStore/>
121 <target dev='hda' bus='ide'/>
122 <readonly/>
123 <address type='drive' controller='0' bus='0' target='0' unit='0'/>
124 </disk>
Denis Egorenkodcc233d2019-03-06 18:09:24 +0400125EOF
126if [[ ! ${VM_MGM_BRIDGE_DISABLE} ]]; then
127 create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}"
128 cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
alexz1c02cd62018-04-15 22:18:03 +0200129 <interface type='bridge'>
130 <source bridge='$VM_MGM_BRIDGE_NAME'/>
131 <model type='virtio'/>
132 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
133 </interface>
134EOF
Denis Egorenkodcc233d2019-03-06 18:09:24 +0400135else
136 create_host_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_NETWORK_GATEWAY}" "${VM_MGM_NETWORK_MASK}" true
137 cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
138 <interface type='network'>
139 <source network='$VM_MGM_NETWORK_NAME'/>
140 <model type='virtio'/>
141 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
142 </interface>
143EOF
144fi
145
Richard Felklbe962ed2018-06-14 17:28:08 +0200146if [[ ! ${VM_CTL_BRIDGE_DISABLE} ]]; then
Denis Egorenkodcc233d2019-03-06 18:09:24 +0400147 create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}"
148 cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
alexz1c02cd62018-04-15 22:18:03 +0200149 <interface type='bridge'>
150 <source bridge='$VM_CTL_BRIDGE_NAME'/>
151 <model type='virtio'/>
152 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
153 </interface>
154EOF
Denis Egorenkodcc233d2019-03-06 18:09:24 +0400155else
156 create_host_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_NETWORK_GATEWAY}" "${VM_CTL_NETWORK_MASK}"
157 cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
158 <interface type='network'>
159 <source network='$VM_CTL_NETWORK_NAME'/>
160 <model type='virtio'/>
161 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
162 </interface>
163EOF
alexz1c02cd62018-04-15 22:18:03 +0200164fi
Denis Egorenkodcc233d2019-03-06 18:09:24 +0400165
alexz1c02cd62018-04-15 22:18:03 +0200166cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml
167 <serial type='pty'>
168 <source path='/dev/pts/1'/>
169 <target port='0'/>
170 </serial>
171 <console type='pty' tty='/dev/pts/1'>
172 <source path='/dev/pts/1'/>
173 <target type='serial' port='0'/>
174 </console>
175 <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
176 <listen type='address' address='127.0.0.1'/>
177 </graphics>
azvyagintsev0022afe2018-10-29 15:09:15 +0200178 <rng model='virtio'>
179 <backend model='random'>/dev/random</backend>
180 </rng>
alexz1c02cd62018-04-15 22:18:03 +0200181 </devices>
182</domain>
183EOF
184
185echo "INFO: rendered VM config:"
186cat $(pwd)/${VM_NAME}-vm.xml
187
188virsh define $(pwd)/${VM_NAME}-vm.xml
189virsh autostart ${VM_NAME}