blob: eb8b73379387fee4197b7ca0f22324f0c01cfd01 [file] [log] [blame]
Denis Egorenkoc2656402019-04-19 18:17:50 +04001#!/bin/bash
2
3envFile="$(pwd)/env_vars.sh"
4if [[ ! -f ${envFile} ]]; then
5 echo "ERROR: Can not find 'env_vars' libfile (${envFile}), check your mcp/mcp-common-scripts repo."
6 exit 1
7else
8 source ${envFile}
9fi
10
11function check_packages {
12 local slave=$1
13 local packages="libvirt-bin qemu-kvm"
14 if [[ -n "${slave}" ]]; then
15 packages="${packages} qemu-utils python-ipaddress mkisofs"
16 fi
17 for i in $packages; do
18 dpkg -s $i &> /dev/null || { echo "Package $i is not installed!"; exit 1; }
19 done
20}
21
22function create_network {
23 local network=${1}
24 virsh net-destroy ${network} 2> /dev/null || true
25 virsh net-undefine ${network} 2> /dev/null || true
26 virsh net-define ${network}.xml
27 virsh net-autostart ${network}
28 virsh net-start ${network}
29}
30
31function create_bridge_network {
32 local network=$1
33 local bridge_name=$2
34 cat <<EOF > $(pwd)/${network}.xml
35<network>
36 <name>${network}</name>
37 <forward mode="bridge"/>
38 <bridge name="${bridge_name}" />
39</network>
40EOF
41 create_network ${network}
42}
43
44function create_host_network {
45 local network=$1
46 local gateway=$2
47 local netmask=$3
48 local nat=${4:-false}
49 cat <<EOF > $(pwd)/${network}.xml
50<network>
51 <name>${network}</name>
52 <bridge name="${network}" />
53 <ip address="${gateway}" netmask="${netmask}"/>
54EOF
55 if [[ "${nat}" =~ [Tt]rue ]]; then
56 cat <<EOF>> $(pwd)/${network}.xml
57 <forward mode="nat"/>
58EOF
59 fi
60 cat <<EOF>> $(pwd)/${network}.xml
61</network>
62EOF
63 create_network ${network}
64}
65
66function place_file_under_libvirt() {
67 local libvirtPath="/var/lib/libvirt/images"
68 local image=${1}
69 local basenameFile=$(basename ${image})
70 cp "${image}" "${libvirtPath}/${basenameFile}"
71 chown -R libvirt-qemu:kvm "${libvirtPath}"
72 echo "${libvirtPath}/${basenameFile}"
73}
74
75function render_config() {
76 local vmName=$1
77 local vmMemKB=$2
78 local vmCPUs=$3
79 local vmSourceDisk=$4
80 local vmConfigDisk=$5
81 local createNetworks=${6:-true}
82 # Template definition
83 cat <<EOF > $(pwd)/${vmName}-vm.xml
84<domain type='kvm'>
85 <name>$vmName</name>
86 <memory unit='KiB'>$vmMemKB</memory>
87 <currentMemory unit='KiB'>$vmMemKB</currentMemory>
88 <vcpu placement='static'>$vmCPUs</vcpu>
89 <resource>
90 <partition>/machine</partition>
91 </resource>
92 <os>
93 <type >hvm</type>
94 <boot dev='hd'/>
95 </os>
96 <features>
97 <acpi/>
98 </features>
99 <clock offset='utc'>
100 <timer name='rtc' tickpolicy='catchup'/>
101 <timer name='pit' tickpolicy='delay'/>
102 <timer name='hpet' present='no'/>
103 </clock>
104 <pm>
105 <suspend-to-mem enabled='no'/>
106 <suspend-to-disk enabled='no'/>
107 </pm>
108 <devices>
109 <emulator>/usr/bin/kvm-spice</emulator>
110 <disk type='file' device='disk'>
111 <driver name='qemu' type='qcow2' cache='none'/>
112 <source file='$vmSourceDisk'/>
113 <target dev='vda' bus='virtio'/>
114 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
115 </disk>
116EOF
117 if [[ -n "${vmConfigDisk}" ]]; then
118 cat <<EOF >> $(pwd)/${vmName}-vm.xml
119 <disk type='file' device='cdrom'>
120 <driver name='qemu' type='raw'/>
121 <source file='$vmConfigDisk'/>
122 <backingStore/>
123 <target dev='hda' bus='ide'/>
124 <readonly/>
125 <address type='drive' controller='0' bus='0' target='0' unit='0'/>
126 </disk>
127EOF
128 fi
129
130 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
131 [[ "${createNetworks}" =~ [Tt]rue ]] && create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}"
132 cat <<EOF >> $(pwd)/${vmName}-vm.xml
133 <interface type='bridge'>
134 <source bridge='$VM_MGM_BRIDGE_NAME'/>
135 <model type='virtio'/>
136 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
137 </interface>
138EOF
139 else
140 [[ "${createNetworks}" =~ [Tt]rue ]] && create_host_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_NETWORK_GATEWAY}" "${VM_MGM_NETWORK_MASK}" true
141 cat <<EOF >> $(pwd)/${vmName}-vm.xml
142 <interface type='network'>
143 <source network='$VM_MGM_NETWORK_NAME'/>
144 <model type='virtio'/>
145 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
146 </interface>
147EOF
148fi
149
150 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
151 [[ "${createNetworks}" =~ [Tt]rue ]] && create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}"
152 cat <<EOF >> $(pwd)/${vmName}-vm.xml
153 <interface type='bridge'>
154 <source bridge='$VM_CTL_BRIDGE_NAME'/>
155 <model type='virtio'/>
156 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
157 </interface>
158EOF
159 else
160 [[ "${createNetworks}" =~ [Tt]rue ]] && create_host_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_NETWORK_GATEWAY}" "${VM_CTL_NETWORK_MASK}"
161 cat <<EOF >> $(pwd)/${vmName}-vm.xml
162 <interface type='network'>
163 <source network='$VM_CTL_NETWORK_NAME'/>
164 <model type='virtio'/>
165 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
166 </interface>
167EOF
168fi
169
170 cat <<EOF >> $(pwd)/${vmName}-vm.xml
171 <serial type='pty'>
172 <source path='/dev/pts/1'/>
173 <target port='0'/>
174 </serial>
175 <console type='pty' tty='/dev/pts/1'>
176 <source path='/dev/pts/1'/>
177 <target type='serial' port='0'/>
178 </console>
179 <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
180 <listen type='address' address='127.0.0.1'/>
181 </graphics>
182 <rng model='virtio'>
183 <backend model='random'>/dev/random</backend>
184 </rng>
185 </devices>
186</domain>
187EOF
188
189 echo "INFO: rendered VM config:"
190 cat $(pwd)/${vmName}-vm.xml
191}