blob: d5b1cec063089a5aa1372094394364af39567158 [file] [log] [blame]
Denis Egorenkoc2656402019-04-19 18:17:50 +04001#!/bin/bash
2
Denis Egorenko0d045ae2019-06-04 18:23:11 +04003set -e
4
Denis Egorenkoc2656402019-04-19 18:17:50 +04005envFile="$(pwd)/env_vars.sh"
6if [[ ! -f ${envFile} ]]; then
7 echo "ERROR: Can not find 'env_vars' libfile (${envFile}), check your mcp/mcp-common-scripts repo."
8 exit 1
9else
10 source ${envFile}
11fi
12
13function check_packages {
14 local slave=$1
15 local packages="libvirt-bin qemu-kvm"
16 if [[ -n "${slave}" ]]; then
Denis Egorenko28b8d042019-05-03 13:57:51 +040017 packages="${packages} qemu-utils python-ipaddress genisoimage"
Denis Egorenkoc2656402019-04-19 18:17:50 +040018 fi
19 for i in $packages; do
20 dpkg -s $i &> /dev/null || { echo "Package $i is not installed!"; exit 1; }
21 done
22}
23
Denis Egorenkoabe23c52019-05-30 16:53:55 +040024function check_bridge_exists {
25 local bridgeName=${1}
26 local optionName=${2}
27 local bridgeExists=$(brctl show | grep ${bridgeName})
28 if [ -z "${bridgeExists}" ]; then
29 echo "Option ${optionName} is set to False, which means using bridge ${bridgeName}, but it doesn't exist."
30 echo "Consider to switch to ${optionName}=True, which will lead to using local hosted networks."
31 echo "Or create bridge ${bridgeName} manually: https://docs.mirantis.com/mcp/q4-18/mcp-deployment-guide/deploy-mcp-drivetrain/prerequisites-dtrain.html"
32 exit 1
33 fi
34}
35
36function prereq_check {
37 local slave=${1}
38 check_packages "${slave}"
Denis Egorenko6c9aef32019-06-07 19:19:02 +040039 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
40 check_bridge_exists "${VM_MGM_BRIDGE_NAME}" "VM_MGM_BRIDGE_DISABLE"
41 fi
42 if [[ "${VM_CTL_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
43 check_bridge_exists "${VM_CTL_BRIDGE_NAME}" "VM_CTL_BRIDGE_DISABLE"
44 fi
45 if [[ -n "${NON_DEFAULT_LIBVIRT_DIR}" ]]; then
46 echo "All files will be saved under ${NON_DEFAULT_LIBVIRT_DIR} directory. Make sure that libvirt-qemu:kvm has access rights to that path."
47 fi
Denis Egorenkoabe23c52019-05-30 16:53:55 +040048}
49
50function do_create_new_network {
51 local netName=${1}
52 local netExists=$(virsh net-list | grep ${netName})
53 if [ -n "${netExists}" ] && [[ "${RECREATE_NETWORKS_IF_EXISTS}" =~ [Ff]alse ]]; then
54 echo 'false'
55 else
56 echo 'true'
57 fi
58}
59
Denis Egorenkoc2656402019-04-19 18:17:50 +040060function create_network {
61 local network=${1}
62 virsh net-destroy ${network} 2> /dev/null || true
63 virsh net-undefine ${network} 2> /dev/null || true
64 virsh net-define ${network}.xml
65 virsh net-autostart ${network}
66 virsh net-start ${network}
67}
68
69function create_bridge_network {
70 local network=$1
71 local bridge_name=$2
Denis Egorenkoabe23c52019-05-30 16:53:55 +040072 local createNetwork=$(do_create_new_network "${network}")
73 if [ "${createNetwork}" == 'true' ]; then
74 cat <<EOF > $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040075<network>
76 <name>${network}</name>
77 <forward mode="bridge"/>
78 <bridge name="${bridge_name}" />
79</network>
80EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040081 create_network ${network}
82 fi
Denis Egorenkoc2656402019-04-19 18:17:50 +040083}
84
85function create_host_network {
86 local network=$1
87 local gateway=$2
88 local netmask=$3
89 local nat=${4:-false}
Denis Egorenkoabe23c52019-05-30 16:53:55 +040090 local createNetwork=$(do_create_new_network "${network}")
91 if [ "${createNetwork}" == 'true' ]; then
92 cat <<EOF > $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040093<network>
94 <name>${network}</name>
95 <bridge name="${network}" />
96 <ip address="${gateway}" netmask="${netmask}"/>
97EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040098 if [[ "${nat}" =~ [Tt]rue ]]; then
99 cat <<EOF>> $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +0400100 <forward mode="nat"/>
101EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400102 fi
103 cat <<EOF>> $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +0400104</network>
105EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400106 create_network ${network}
107 fi
Denis Egorenkoc2656402019-04-19 18:17:50 +0400108}
109
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400110function place_file_under_libvirt_owned_dir() {
111 local file=${1}
112 local libvirtPath=${2-'/var/lib/libvirt/images'}
113 local basenameFile=$(basename ${file})
114 cp "${file}" "${libvirtPath}/${basenameFile}"
115 chown libvirt-qemu:kvm "${libvirtPath}/${basenameFile}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400116 echo "${libvirtPath}/${basenameFile}"
117}
118
119function render_config() {
120 local vmName=$1
121 local vmMemKB=$2
122 local vmCPUs=$3
123 local vmSourceDisk=$4
124 local vmConfigDisk=$5
Denis Egorenkoc2656402019-04-19 18:17:50 +0400125 # Template definition
126 cat <<EOF > $(pwd)/${vmName}-vm.xml
127<domain type='kvm'>
128 <name>$vmName</name>
129 <memory unit='KiB'>$vmMemKB</memory>
130 <currentMemory unit='KiB'>$vmMemKB</currentMemory>
131 <vcpu placement='static'>$vmCPUs</vcpu>
132 <resource>
133 <partition>/machine</partition>
134 </resource>
135 <os>
136 <type >hvm</type>
137 <boot dev='hd'/>
138 </os>
139 <features>
140 <acpi/>
141 </features>
142 <clock offset='utc'>
143 <timer name='rtc' tickpolicy='catchup'/>
144 <timer name='pit' tickpolicy='delay'/>
145 <timer name='hpet' present='no'/>
146 </clock>
147 <pm>
148 <suspend-to-mem enabled='no'/>
149 <suspend-to-disk enabled='no'/>
150 </pm>
151 <devices>
152 <emulator>/usr/bin/kvm-spice</emulator>
153 <disk type='file' device='disk'>
154 <driver name='qemu' type='qcow2' cache='none'/>
155 <source file='$vmSourceDisk'/>
156 <target dev='vda' bus='virtio'/>
157 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
158 </disk>
159EOF
160 if [[ -n "${vmConfigDisk}" ]]; then
161 cat <<EOF >> $(pwd)/${vmName}-vm.xml
162 <disk type='file' device='cdrom'>
163 <driver name='qemu' type='raw'/>
164 <source file='$vmConfigDisk'/>
165 <backingStore/>
166 <target dev='hda' bus='ide'/>
167 <readonly/>
168 <address type='drive' controller='0' bus='0' target='0' unit='0'/>
169 </disk>
170EOF
171 fi
172
Denis Egorenko0d045ae2019-06-04 18:23:11 +0400173 local allocationDataFile=$(isoinfo -i ${VM_CONFIG_DISK} -J -f | grep -w "allocation_data.yml")
Denis Egorenkoc2656402019-04-19 18:17:50 +0400174 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400175 create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400176 cat <<EOF >> $(pwd)/${vmName}-vm.xml
177 <interface type='bridge'>
178 <source bridge='$VM_MGM_BRIDGE_NAME'/>
179 <model type='virtio'/>
180 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
181 </interface>
182EOF
183 else
Denis Egorenko0d045ae2019-06-04 18:23:11 +0400184 local vmMgmNetworkGateway=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'deploy_network_gateway' | cut -f 2 -d ':' | tr -d ' ')
185 local vmMgmNetworkMask=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'deploy_network_netmask' | cut -f 2 -d ':' | tr -d ' ')
186 create_host_network "${VM_MGM_NETWORK_NAME}" "${vmMgmNetworkGateway}" "${vmMgmNetworkMask}" true
Denis Egorenkoc2656402019-04-19 18:17:50 +0400187 cat <<EOF >> $(pwd)/${vmName}-vm.xml
188 <interface type='network'>
189 <source network='$VM_MGM_NETWORK_NAME'/>
190 <model type='virtio'/>
191 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
192 </interface>
193EOF
194fi
195
196 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400197 create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400198 cat <<EOF >> $(pwd)/${vmName}-vm.xml
199 <interface type='bridge'>
200 <source bridge='$VM_CTL_BRIDGE_NAME'/>
201 <model type='virtio'/>
202 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
203 </interface>
204EOF
205 else
Denis Egorenko0d045ae2019-06-04 18:23:11 +0400206 local vmCtlNetworkGateway=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'infra_config_address' | cut -f 2 -d ':' | tr -d ' ' | sed -r "s/\.[[:digit:]]+$/\.1/g")
207 local vmCtlNetworkMask=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'control_network_netmask' | cut -f 2 -d ':' | tr -d ' ')
208 create_host_network "${VM_CTL_NETWORK_NAME}" "${vmCtlNetworkGateway}" "${vmCtlNetworkMask}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400209 cat <<EOF >> $(pwd)/${vmName}-vm.xml
210 <interface type='network'>
211 <source network='$VM_CTL_NETWORK_NAME'/>
212 <model type='virtio'/>
213 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
214 </interface>
215EOF
216fi
217
218 cat <<EOF >> $(pwd)/${vmName}-vm.xml
219 <serial type='pty'>
220 <source path='/dev/pts/1'/>
221 <target port='0'/>
222 </serial>
223 <console type='pty' tty='/dev/pts/1'>
224 <source path='/dev/pts/1'/>
225 <target type='serial' port='0'/>
226 </console>
227 <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
228 <listen type='address' address='127.0.0.1'/>
229 </graphics>
230 <rng model='virtio'>
231 <backend model='random'>/dev/random</backend>
232 </rng>
233 </devices>
234</domain>
235EOF
236
237 echo "INFO: rendered VM config:"
238 cat $(pwd)/${vmName}-vm.xml
239}