blob: ed2ad0fa1344ebfeed7fabea3f52a58f11bcdef8 [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}"
39 [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]] && check_bridge_exists "${VM_MGM_BRIDGE_NAME}" "VM_MGM_BRIDGE_DISABLE"
40 [[ "${VM_CTL_BRIDGE_DISABLE}" =~ [Ff]alse ]] && check_bridge_exists "${VM_CTL_BRIDGE_NAME}" "VM_CTL_BRIDGE_DISABLE"
41 [[ -n "${NON_DEFAULT_LIBVIRT_DIR}" ]] && echo "All files will be saved under ${NON_DEFAULT_LIBVIRT_DIR} directory. Make sure that libvirt-qemu:kvm has access rights to that path."
42}
43
44function do_create_new_network {
45 local netName=${1}
46 local netExists=$(virsh net-list | grep ${netName})
47 if [ -n "${netExists}" ] && [[ "${RECREATE_NETWORKS_IF_EXISTS}" =~ [Ff]alse ]]; then
48 echo 'false'
49 else
50 echo 'true'
51 fi
52}
53
Denis Egorenkoc2656402019-04-19 18:17:50 +040054function create_network {
55 local network=${1}
56 virsh net-destroy ${network} 2> /dev/null || true
57 virsh net-undefine ${network} 2> /dev/null || true
58 virsh net-define ${network}.xml
59 virsh net-autostart ${network}
60 virsh net-start ${network}
61}
62
63function create_bridge_network {
64 local network=$1
65 local bridge_name=$2
Denis Egorenkoabe23c52019-05-30 16:53:55 +040066 local createNetwork=$(do_create_new_network "${network}")
67 if [ "${createNetwork}" == 'true' ]; then
68 cat <<EOF > $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040069<network>
70 <name>${network}</name>
71 <forward mode="bridge"/>
72 <bridge name="${bridge_name}" />
73</network>
74EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040075 create_network ${network}
76 fi
Denis Egorenkoc2656402019-04-19 18:17:50 +040077}
78
79function create_host_network {
80 local network=$1
81 local gateway=$2
82 local netmask=$3
83 local nat=${4:-false}
Denis Egorenkoabe23c52019-05-30 16:53:55 +040084 local createNetwork=$(do_create_new_network "${network}")
85 if [ "${createNetwork}" == 'true' ]; then
86 cat <<EOF > $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040087<network>
88 <name>${network}</name>
89 <bridge name="${network}" />
90 <ip address="${gateway}" netmask="${netmask}"/>
91EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040092 if [[ "${nat}" =~ [Tt]rue ]]; then
93 cat <<EOF>> $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040094 <forward mode="nat"/>
95EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040096 fi
97 cat <<EOF>> $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040098</network>
99EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400100 create_network ${network}
101 fi
Denis Egorenkoc2656402019-04-19 18:17:50 +0400102}
103
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400104function place_file_under_libvirt_owned_dir() {
105 local file=${1}
106 local libvirtPath=${2-'/var/lib/libvirt/images'}
107 local basenameFile=$(basename ${file})
108 cp "${file}" "${libvirtPath}/${basenameFile}"
109 chown libvirt-qemu:kvm "${libvirtPath}/${basenameFile}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400110 echo "${libvirtPath}/${basenameFile}"
111}
112
113function render_config() {
114 local vmName=$1
115 local vmMemKB=$2
116 local vmCPUs=$3
117 local vmSourceDisk=$4
118 local vmConfigDisk=$5
Denis Egorenkoc2656402019-04-19 18:17:50 +0400119 # Template definition
120 cat <<EOF > $(pwd)/${vmName}-vm.xml
121<domain type='kvm'>
122 <name>$vmName</name>
123 <memory unit='KiB'>$vmMemKB</memory>
124 <currentMemory unit='KiB'>$vmMemKB</currentMemory>
125 <vcpu placement='static'>$vmCPUs</vcpu>
126 <resource>
127 <partition>/machine</partition>
128 </resource>
129 <os>
130 <type >hvm</type>
131 <boot dev='hd'/>
132 </os>
133 <features>
134 <acpi/>
135 </features>
136 <clock offset='utc'>
137 <timer name='rtc' tickpolicy='catchup'/>
138 <timer name='pit' tickpolicy='delay'/>
139 <timer name='hpet' present='no'/>
140 </clock>
141 <pm>
142 <suspend-to-mem enabled='no'/>
143 <suspend-to-disk enabled='no'/>
144 </pm>
145 <devices>
146 <emulator>/usr/bin/kvm-spice</emulator>
147 <disk type='file' device='disk'>
148 <driver name='qemu' type='qcow2' cache='none'/>
149 <source file='$vmSourceDisk'/>
150 <target dev='vda' bus='virtio'/>
151 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
152 </disk>
153EOF
154 if [[ -n "${vmConfigDisk}" ]]; then
155 cat <<EOF >> $(pwd)/${vmName}-vm.xml
156 <disk type='file' device='cdrom'>
157 <driver name='qemu' type='raw'/>
158 <source file='$vmConfigDisk'/>
159 <backingStore/>
160 <target dev='hda' bus='ide'/>
161 <readonly/>
162 <address type='drive' controller='0' bus='0' target='0' unit='0'/>
163 </disk>
164EOF
165 fi
166
Denis Egorenko0d045ae2019-06-04 18:23:11 +0400167 local allocationDataFile=$(isoinfo -i ${VM_CONFIG_DISK} -J -f | grep -w "allocation_data.yml")
Denis Egorenkoc2656402019-04-19 18:17:50 +0400168 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400169 create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400170 cat <<EOF >> $(pwd)/${vmName}-vm.xml
171 <interface type='bridge'>
172 <source bridge='$VM_MGM_BRIDGE_NAME'/>
173 <model type='virtio'/>
174 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
175 </interface>
176EOF
177 else
Denis Egorenko0d045ae2019-06-04 18:23:11 +0400178 local vmMgmNetworkGateway=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'deploy_network_gateway' | cut -f 2 -d ':' | tr -d ' ')
179 local vmMgmNetworkMask=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'deploy_network_netmask' | cut -f 2 -d ':' | tr -d ' ')
180 create_host_network "${VM_MGM_NETWORK_NAME}" "${vmMgmNetworkGateway}" "${vmMgmNetworkMask}" true
Denis Egorenkoc2656402019-04-19 18:17:50 +0400181 cat <<EOF >> $(pwd)/${vmName}-vm.xml
182 <interface type='network'>
183 <source network='$VM_MGM_NETWORK_NAME'/>
184 <model type='virtio'/>
185 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
186 </interface>
187EOF
188fi
189
190 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400191 create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400192 cat <<EOF >> $(pwd)/${vmName}-vm.xml
193 <interface type='bridge'>
194 <source bridge='$VM_CTL_BRIDGE_NAME'/>
195 <model type='virtio'/>
196 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
197 </interface>
198EOF
199 else
Denis Egorenko0d045ae2019-06-04 18:23:11 +0400200 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")
201 local vmCtlNetworkMask=$(isoinfo -i ${VM_CONFIG_DISK} -J -x ${allocationDataFile} | grep -w 'control_network_netmask' | cut -f 2 -d ':' | tr -d ' ')
202 create_host_network "${VM_CTL_NETWORK_NAME}" "${vmCtlNetworkGateway}" "${vmCtlNetworkMask}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400203 cat <<EOF >> $(pwd)/${vmName}-vm.xml
204 <interface type='network'>
205 <source network='$VM_CTL_NETWORK_NAME'/>
206 <model type='virtio'/>
207 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
208 </interface>
209EOF
210fi
211
212 cat <<EOF >> $(pwd)/${vmName}-vm.xml
213 <serial type='pty'>
214 <source path='/dev/pts/1'/>
215 <target port='0'/>
216 </serial>
217 <console type='pty' tty='/dev/pts/1'>
218 <source path='/dev/pts/1'/>
219 <target type='serial' port='0'/>
220 </console>
221 <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
222 <listen type='address' address='127.0.0.1'/>
223 </graphics>
224 <rng model='virtio'>
225 <backend model='random'>/dev/random</backend>
226 </rng>
227 </devices>
228</domain>
229EOF
230
231 echo "INFO: rendered VM config:"
232 cat $(pwd)/${vmName}-vm.xml
233}