blob: ec51f6ddf1be12e3b0dd376db0f15760b696c572 [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
Denis Egorenko28b8d042019-05-03 13:57:51 +040015 packages="${packages} qemu-utils python-ipaddress genisoimage"
Denis Egorenkoc2656402019-04-19 18:17:50 +040016 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
Denis Egorenkoabe23c52019-05-30 16:53:55 +040022function check_bridge_exists {
23 local bridgeName=${1}
24 local optionName=${2}
25 local bridgeExists=$(brctl show | grep ${bridgeName})
26 if [ -z "${bridgeExists}" ]; then
27 echo "Option ${optionName} is set to False, which means using bridge ${bridgeName}, but it doesn't exist."
28 echo "Consider to switch to ${optionName}=True, which will lead to using local hosted networks."
29 echo "Or create bridge ${bridgeName} manually: https://docs.mirantis.com/mcp/q4-18/mcp-deployment-guide/deploy-mcp-drivetrain/prerequisites-dtrain.html"
30 exit 1
31 fi
32}
33
34function prereq_check {
35 local slave=${1}
36 check_packages "${slave}"
37 [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]] && check_bridge_exists "${VM_MGM_BRIDGE_NAME}" "VM_MGM_BRIDGE_DISABLE"
38 [[ "${VM_CTL_BRIDGE_DISABLE}" =~ [Ff]alse ]] && check_bridge_exists "${VM_CTL_BRIDGE_NAME}" "VM_CTL_BRIDGE_DISABLE"
39 [[ -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."
40}
41
42function do_create_new_network {
43 local netName=${1}
44 local netExists=$(virsh net-list | grep ${netName})
45 if [ -n "${netExists}" ] && [[ "${RECREATE_NETWORKS_IF_EXISTS}" =~ [Ff]alse ]]; then
46 echo 'false'
47 else
48 echo 'true'
49 fi
50}
51
Denis Egorenkoc2656402019-04-19 18:17:50 +040052function create_network {
53 local network=${1}
54 virsh net-destroy ${network} 2> /dev/null || true
55 virsh net-undefine ${network} 2> /dev/null || true
56 virsh net-define ${network}.xml
57 virsh net-autostart ${network}
58 virsh net-start ${network}
59}
60
61function create_bridge_network {
62 local network=$1
63 local bridge_name=$2
Denis Egorenkoabe23c52019-05-30 16:53:55 +040064 local createNetwork=$(do_create_new_network "${network}")
65 if [ "${createNetwork}" == 'true' ]; then
66 cat <<EOF > $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040067<network>
68 <name>${network}</name>
69 <forward mode="bridge"/>
70 <bridge name="${bridge_name}" />
71</network>
72EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040073 create_network ${network}
74 fi
Denis Egorenkoc2656402019-04-19 18:17:50 +040075}
76
77function create_host_network {
78 local network=$1
79 local gateway=$2
80 local netmask=$3
81 local nat=${4:-false}
Denis Egorenkoabe23c52019-05-30 16:53:55 +040082 local createNetwork=$(do_create_new_network "${network}")
83 if [ "${createNetwork}" == 'true' ]; then
84 cat <<EOF > $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040085<network>
86 <name>${network}</name>
87 <bridge name="${network}" />
88 <ip address="${gateway}" netmask="${netmask}"/>
89EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040090 if [[ "${nat}" =~ [Tt]rue ]]; then
91 cat <<EOF>> $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040092 <forward mode="nat"/>
93EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040094 fi
95 cat <<EOF>> $(pwd)/${network}.xml
Denis Egorenkoc2656402019-04-19 18:17:50 +040096</network>
97EOF
Denis Egorenkoabe23c52019-05-30 16:53:55 +040098 create_network ${network}
99 fi
Denis Egorenkoc2656402019-04-19 18:17:50 +0400100}
101
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400102function place_file_under_libvirt_owned_dir() {
103 local file=${1}
104 local libvirtPath=${2-'/var/lib/libvirt/images'}
105 local basenameFile=$(basename ${file})
106 cp "${file}" "${libvirtPath}/${basenameFile}"
107 chown libvirt-qemu:kvm "${libvirtPath}/${basenameFile}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400108 echo "${libvirtPath}/${basenameFile}"
109}
110
111function render_config() {
112 local vmName=$1
113 local vmMemKB=$2
114 local vmCPUs=$3
115 local vmSourceDisk=$4
116 local vmConfigDisk=$5
Denis Egorenkoc2656402019-04-19 18:17:50 +0400117 # Template definition
118 cat <<EOF > $(pwd)/${vmName}-vm.xml
119<domain type='kvm'>
120 <name>$vmName</name>
121 <memory unit='KiB'>$vmMemKB</memory>
122 <currentMemory unit='KiB'>$vmMemKB</currentMemory>
123 <vcpu placement='static'>$vmCPUs</vcpu>
124 <resource>
125 <partition>/machine</partition>
126 </resource>
127 <os>
128 <type >hvm</type>
129 <boot dev='hd'/>
130 </os>
131 <features>
132 <acpi/>
133 </features>
134 <clock offset='utc'>
135 <timer name='rtc' tickpolicy='catchup'/>
136 <timer name='pit' tickpolicy='delay'/>
137 <timer name='hpet' present='no'/>
138 </clock>
139 <pm>
140 <suspend-to-mem enabled='no'/>
141 <suspend-to-disk enabled='no'/>
142 </pm>
143 <devices>
144 <emulator>/usr/bin/kvm-spice</emulator>
145 <disk type='file' device='disk'>
146 <driver name='qemu' type='qcow2' cache='none'/>
147 <source file='$vmSourceDisk'/>
148 <target dev='vda' bus='virtio'/>
149 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
150 </disk>
151EOF
152 if [[ -n "${vmConfigDisk}" ]]; then
153 cat <<EOF >> $(pwd)/${vmName}-vm.xml
154 <disk type='file' device='cdrom'>
155 <driver name='qemu' type='raw'/>
156 <source file='$vmConfigDisk'/>
157 <backingStore/>
158 <target dev='hda' bus='ide'/>
159 <readonly/>
160 <address type='drive' controller='0' bus='0' target='0' unit='0'/>
161 </disk>
162EOF
163 fi
164
165 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400166 create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400167 cat <<EOF >> $(pwd)/${vmName}-vm.xml
168 <interface type='bridge'>
169 <source bridge='$VM_MGM_BRIDGE_NAME'/>
170 <model type='virtio'/>
171 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
172 </interface>
173EOF
174 else
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400175 create_host_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_NETWORK_GATEWAY}" "${VM_MGM_NETWORK_MASK}" true
Denis Egorenkoc2656402019-04-19 18:17:50 +0400176 cat <<EOF >> $(pwd)/${vmName}-vm.xml
177 <interface type='network'>
178 <source network='$VM_MGM_NETWORK_NAME'/>
179 <model type='virtio'/>
180 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
181 </interface>
182EOF
183fi
184
185 if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400186 create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400187 cat <<EOF >> $(pwd)/${vmName}-vm.xml
188 <interface type='bridge'>
189 <source bridge='$VM_CTL_BRIDGE_NAME'/>
190 <model type='virtio'/>
191 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
192 </interface>
193EOF
194 else
Denis Egorenkoabe23c52019-05-30 16:53:55 +0400195 create_host_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_NETWORK_GATEWAY}" "${VM_CTL_NETWORK_MASK}"
Denis Egorenkoc2656402019-04-19 18:17:50 +0400196 cat <<EOF >> $(pwd)/${vmName}-vm.xml
197 <interface type='network'>
198 <source network='$VM_CTL_NETWORK_NAME'/>
199 <model type='virtio'/>
200 <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
201 </interface>
202EOF
203fi
204
205 cat <<EOF >> $(pwd)/${vmName}-vm.xml
206 <serial type='pty'>
207 <source path='/dev/pts/1'/>
208 <target port='0'/>
209 </serial>
210 <console type='pty' tty='/dev/pts/1'>
211 <source path='/dev/pts/1'/>
212 <target type='serial' port='0'/>
213 </console>
214 <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'>
215 <listen type='address' address='127.0.0.1'/>
216 </graphics>
217 <rng model='virtio'>
218 <backend model='random'>/dev/random</backend>
219 </rng>
220 </devices>
221</domain>
222EOF
223
224 echo "INFO: rendered VM config:"
225 cat $(pwd)/${vmName}-vm.xml
226}