Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | envFile="$(pwd)/env_vars.sh" |
| 4 | if [[ ! -f ${envFile} ]]; then |
| 5 | echo "ERROR: Can not find 'env_vars' libfile (${envFile}), check your mcp/mcp-common-scripts repo." |
| 6 | exit 1 |
| 7 | else |
| 8 | source ${envFile} |
| 9 | fi |
| 10 | |
| 11 | function check_packages { |
| 12 | local slave=$1 |
| 13 | local packages="libvirt-bin qemu-kvm" |
| 14 | if [[ -n "${slave}" ]]; then |
Denis Egorenko | 28b8d04 | 2019-05-03 13:57:51 +0400 | [diff] [blame] | 15 | packages="${packages} qemu-utils python-ipaddress genisoimage" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 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 | |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 22 | function 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 | |
| 34 | function 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 | |
| 42 | function 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 Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 52 | function 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 | |
| 61 | function create_bridge_network { |
| 62 | local network=$1 |
| 63 | local bridge_name=$2 |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 64 | local createNetwork=$(do_create_new_network "${network}") |
| 65 | if [ "${createNetwork}" == 'true' ]; then |
| 66 | cat <<EOF > $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 67 | <network> |
| 68 | <name>${network}</name> |
| 69 | <forward mode="bridge"/> |
| 70 | <bridge name="${bridge_name}" /> |
| 71 | </network> |
| 72 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 73 | create_network ${network} |
| 74 | fi |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | function create_host_network { |
| 78 | local network=$1 |
| 79 | local gateway=$2 |
| 80 | local netmask=$3 |
| 81 | local nat=${4:-false} |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 82 | local createNetwork=$(do_create_new_network "${network}") |
| 83 | if [ "${createNetwork}" == 'true' ]; then |
| 84 | cat <<EOF > $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 85 | <network> |
| 86 | <name>${network}</name> |
| 87 | <bridge name="${network}" /> |
| 88 | <ip address="${gateway}" netmask="${netmask}"/> |
| 89 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 90 | if [[ "${nat}" =~ [Tt]rue ]]; then |
| 91 | cat <<EOF>> $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 92 | <forward mode="nat"/> |
| 93 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 94 | fi |
| 95 | cat <<EOF>> $(pwd)/${network}.xml |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 96 | </network> |
| 97 | EOF |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 98 | create_network ${network} |
| 99 | fi |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 100 | } |
| 101 | |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 102 | function 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 Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 108 | echo "${libvirtPath}/${basenameFile}" |
| 109 | } |
| 110 | |
| 111 | function render_config() { |
| 112 | local vmName=$1 |
| 113 | local vmMemKB=$2 |
| 114 | local vmCPUs=$3 |
| 115 | local vmSourceDisk=$4 |
| 116 | local vmConfigDisk=$5 |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 117 | # 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> |
| 151 | EOF |
| 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> |
| 162 | EOF |
| 163 | fi |
| 164 | |
| 165 | if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 166 | create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 167 | 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> |
| 173 | EOF |
| 174 | else |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 175 | create_host_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_NETWORK_GATEWAY}" "${VM_MGM_NETWORK_MASK}" true |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 176 | 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> |
| 182 | EOF |
| 183 | fi |
| 184 | |
| 185 | if [[ "${VM_MGM_BRIDGE_DISABLE}" =~ [Ff]alse ]]; then |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 186 | create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 187 | 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> |
| 193 | EOF |
| 194 | else |
Denis Egorenko | abe23c5 | 2019-05-30 16:53:55 +0400 | [diff] [blame^] | 195 | create_host_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_NETWORK_GATEWAY}" "${VM_CTL_NETWORK_MASK}" |
Denis Egorenko | c265640 | 2019-04-19 18:17:50 +0400 | [diff] [blame] | 196 | 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> |
| 202 | EOF |
| 203 | fi |
| 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> |
| 222 | EOF |
| 223 | |
| 224 | echo "INFO: rendered VM config:" |
| 225 | cat $(pwd)/${vmName}-vm.xml |
| 226 | } |