Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Denis Egorenko | 0d045ae | 2019-06-04 18:23:11 +0400 | [diff] [blame^] | 3 | set -e |
| 4 | |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 5 | envFile="$(pwd)/env_vars.sh" |
| 6 | if [[ ! -f ${envFile} ]]; then |
| 7 | echo "ERROR: Can not find 'env_vars' libfile (${envFile}), check your mcp/mcp-common-scripts repo." |
| 8 | exit 1 |
| 9 | else |
| 10 | source ${envFile} |
| 11 | fi |
| 12 | |
| 13 | function check_packages { |
| 14 | local slave=$1 |
| 15 | local packages="libvirt-bin qemu-kvm" |
| 16 | if [[ -n "${slave}" ]]; then |
Denis Egorenko | 28b8d04 | 2019-05-03 13:57:51 +0400 | [diff] [blame] | 17 | packages="${packages} qemu-utils python-ipaddress genisoimage" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 18 | 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 Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 24 | function 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 | |
| 36 | function 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 | |
| 44 | function 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 Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 54 | function 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 | |
| 63 | function create_bridge_network { |
| 64 | local network=$1 |
| 65 | local bridge_name=$2 |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 66 | local createNetwork=$(do_create_new_network "${network}") |
| 67 | if [ "${createNetwork}" == 'true' ]; then |
| 68 | cat <<EOF > $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 69 | <network> |
| 70 | <name>${network}</name> |
| 71 | <forward mode="bridge"/> |
| 72 | <bridge name="${bridge_name}" /> |
| 73 | </network> |
| 74 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 75 | create_network ${network} |
| 76 | fi |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | function create_host_network { |
| 80 | local network=$1 |
| 81 | local gateway=$2 |
| 82 | local netmask=$3 |
| 83 | local nat=${4:-false} |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 84 | local createNetwork=$(do_create_new_network "${network}") |
| 85 | if [ "${createNetwork}" == 'true' ]; then |
| 86 | cat <<EOF > $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 87 | <network> |
| 88 | <name>${network}</name> |
| 89 | <bridge name="${network}" /> |
| 90 | <ip address="${gateway}" netmask="${netmask}"/> |
| 91 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 92 | if [[ "${nat}" =~ [Tt]rue ]]; then |
| 93 | cat <<EOF>> $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 94 | <forward mode="nat"/> |
| 95 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 96 | fi |
| 97 | cat <<EOF>> $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 98 | </network> |
| 99 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 100 | create_network ${network} |
| 101 | fi |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 102 | } |
| 103 | |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 104 | function 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 Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 110 | echo "${libvirtPath}/${basenameFile}" |
| 111 | } |
| 112 | |
| 113 | function render_config() { |
| 114 | local vmName=$1 |
| 115 | local vmMemKB=$2 |
| 116 | local vmCPUs=$3 |
| 117 | local vmSourceDisk=$4 |
| 118 | local vmConfigDisk=$5 |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 119 | # 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> |
| 153 | EOF |
| 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> |
| 164 | EOF |
| 165 | fi |
| 166 | |
Denis Egorenko | 0d045ae | 2019-06-04 18:23:11 +0400 | [diff] [blame^] | 167 | local allocationDataFile=$(isoinfo -i ${VM_CONFIG_DISK} -J -f | grep -w "allocation_data.yml") |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 168 | if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 169 | create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 170 | 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> |
| 176 | EOF |
| 177 | else |
Denis Egorenko | 0d045ae | 2019-06-04 18:23:11 +0400 | [diff] [blame^] | 178 | 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 Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 181 | 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> |
| 187 | EOF |
| 188 | fi |
| 189 | |
| 190 | if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame] | 191 | create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 192 | 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> |
| 198 | EOF |
| 199 | else |
Denis Egorenko | 0d045ae | 2019-06-04 18:23:11 +0400 | [diff] [blame^] | 200 | 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 Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 203 | 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> |
| 209 | EOF |
| 210 | fi |
| 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> |
| 229 | EOF |
| 230 | |
| 231 | echo "INFO: rendered VM config:" |
| 232 | cat $(pwd)/${vmName}-vm.xml |
| 233 | } |