alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 1 | #!/bin/bash -xe |
| 2 | |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 3 | VM_MGM_BRIDGE_DISABLE=${VM_MGM_BRIDGE_DISABLE:-false} |
| 4 | VM_CTL_BRIDGE_DISABLE=${VM_CTL_BRIDGE_DISABLE:-false} |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 5 | VM_MGM_BRIDGE_NAME=${VM_MGM_BRIDGE_NAME:-"br-mgm"} |
Richard Felkl | be962ed | 2018-06-14 17:28:08 +0200 | [diff] [blame] | 6 | VM_CTL_BRIDGE_NAME=${VM_CTL_BRIDGE_NAME:-"br-ctl"} |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 7 | VM_MGM_NETWORK_NAME=${VM_MGM_NETWORK_NAME:-"mgm_network"} |
| 8 | VM_CTL_NETWORK_NAME=${VM_CTL_NETWORK_NAME:-"ctl_network"} |
azvyagintsev | 4e851ee | 2018-12-21 13:49:12 +0200 | [diff] [blame] | 9 | VM_MEM_KB=${VM_MEM_KB:-"12589056"} |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 10 | VM_CPUS=${VM_CPUS:-"4"} |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 11 | # optional params if you won't use bridge on host |
| 12 | VM_MGM_NETWORK_GATEWAY=${VM_MGM_NETWORK_GATEWAY:-"192.168.56.1"} |
| 13 | VM_MGM_NETWORK_MASK=${VM_MGM_NETWORK_MASK:-"255.255.255.0"} |
| 14 | VM_CTL_NETWORK_GATEWAY=${VM_CTL_NETWORK_GATEWAY:-"192.168.57.1"} |
| 15 | VM_CTL_NETWORK_MASK=${VM_CTL_NETWORK_MASK:-"255.255.255.0"} |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 16 | |
| 17 | if [[ -z ${VM_NAME} ]]; then |
| 18 | echo "ERROR: \$VM_NAME not set!" |
| 19 | exit 1 |
| 20 | fi |
| 21 | if [[ ! -f ${VM_SOURCE_DISK} ]] || [[ -z ${VM_SOURCE_DISK} ]]; then |
| 22 | echo "ERROR: \$VM_SOURCE_DISK not set, or file does not exist!" |
| 23 | exit 1 |
| 24 | fi |
| 25 | if [[ ! -f ${VM_CONFIG_DISK} ]] || [[ -z ${VM_CONFIG_DISK} ]]; then |
| 26 | echo "ERROR: \$VM_CONFIG_DISK not set, or file does not exist!" |
| 27 | exit 1 |
| 28 | fi |
| 29 | |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 30 | function check_packages { |
| 31 | PACKAGES="qemu-utils libvirt-bin qemu-kvm" |
| 32 | for i in $PACKAGES; do |
| 33 | dpkg -s $i &> /dev/null || { echo "Package $i is not installed!"; exit 1; } |
| 34 | done |
| 35 | } |
| 36 | |
| 37 | function create_network { |
| 38 | local network=${1} |
| 39 | virsh net-destroy ${network} 2> /dev/null || true |
| 40 | virsh net-undefine ${network} 2> /dev/null || true |
| 41 | virsh net-define ${network}.xml |
| 42 | virsh net-autostart ${network} |
| 43 | virsh net-start ${network} |
| 44 | } |
| 45 | |
| 46 | function create_bridge_network { |
| 47 | local network=$1 |
| 48 | local bridge_name=$2 |
| 49 | cat <<EOF > $(pwd)/${network}.xml |
| 50 | <network> |
| 51 | <name>${network}</name> |
| 52 | <forward mode="bridge"/> |
| 53 | <bridge name="${bridge_name}" /> |
| 54 | </network> |
| 55 | EOF |
| 56 | create_network ${network} |
| 57 | } |
| 58 | |
| 59 | function create_host_network { |
| 60 | local network=$1 |
| 61 | local gateway=$2 |
| 62 | local netmask=$3 |
| 63 | local nat=${4:-false} |
| 64 | cat <<EOF > $(pwd)/${network}.xml |
| 65 | <network> |
| 66 | <name>${network}</name> |
| 67 | <bridge name="${network}" /> |
| 68 | <ip address="${gateway}" netmask="${netmask}"/> |
| 69 | EOF |
| 70 | if [[ ${nat} ]]; then |
| 71 | cat <<EOF>> $(pwd)/${network}.xml |
| 72 | <forward mode="nat"/> |
| 73 | EOF |
| 74 | fi |
| 75 | cat <<EOF>> $(pwd)/${network}.xml |
| 76 | </network> |
| 77 | EOF |
| 78 | create_network ${network} |
| 79 | } |
| 80 | |
| 81 | check_packages |
| 82 | |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 83 | # Template definition |
| 84 | cat <<EOF > $(pwd)/${VM_NAME}-vm.xml |
| 85 | <domain type='kvm'> |
| 86 | <name>$VM_NAME</name> |
| 87 | <memory unit='KiB'>$VM_MEM_KB</memory> |
| 88 | <currentMemory unit='KiB'>$VM_MEM_KB</currentMemory> |
| 89 | <vcpu placement='static'>$VM_CPUS</vcpu> |
| 90 | <resource> |
| 91 | <partition>/machine</partition> |
| 92 | </resource> |
| 93 | <os> |
| 94 | <type >hvm</type> |
| 95 | <boot dev='hd'/> |
| 96 | </os> |
| 97 | <features> |
| 98 | <acpi/> |
| 99 | </features> |
| 100 | <clock offset='utc'> |
| 101 | <timer name='rtc' tickpolicy='catchup'/> |
| 102 | <timer name='pit' tickpolicy='delay'/> |
| 103 | <timer name='hpet' present='no'/> |
| 104 | </clock> |
| 105 | <pm> |
| 106 | <suspend-to-mem enabled='no'/> |
| 107 | <suspend-to-disk enabled='no'/> |
| 108 | </pm> |
| 109 | <devices> |
| 110 | <emulator>/usr/bin/kvm-spice</emulator> |
| 111 | <disk type='file' device='disk'> |
| 112 | <driver name='qemu' type='qcow2' cache='none'/> |
| 113 | <source file='$VM_SOURCE_DISK'/> |
| 114 | <target dev='vda' bus='virtio'/> |
| 115 | <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> |
| 116 | </disk> |
| 117 | <disk type='file' device='cdrom'> |
| 118 | <driver name='qemu' type='raw'/> |
| 119 | <source file='$VM_CONFIG_DISK'/> |
| 120 | <backingStore/> |
| 121 | <target dev='hda' bus='ide'/> |
| 122 | <readonly/> |
| 123 | <address type='drive' controller='0' bus='0' target='0' unit='0'/> |
| 124 | </disk> |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 125 | EOF |
| 126 | if [[ ! ${VM_MGM_BRIDGE_DISABLE} ]]; then |
| 127 | create_bridge_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_BRIDGE_NAME}" |
| 128 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 129 | <interface type='bridge'> |
| 130 | <source bridge='$VM_MGM_BRIDGE_NAME'/> |
| 131 | <model type='virtio'/> |
| 132 | <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> |
| 133 | </interface> |
| 134 | EOF |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 135 | else |
| 136 | create_host_network "${VM_MGM_NETWORK_NAME}" "${VM_MGM_NETWORK_GATEWAY}" "${VM_MGM_NETWORK_MASK}" true |
| 137 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml |
| 138 | <interface type='network'> |
| 139 | <source network='$VM_MGM_NETWORK_NAME'/> |
| 140 | <model type='virtio'/> |
| 141 | <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> |
| 142 | </interface> |
| 143 | EOF |
| 144 | fi |
| 145 | |
Richard Felkl | be962ed | 2018-06-14 17:28:08 +0200 | [diff] [blame] | 146 | if [[ ! ${VM_CTL_BRIDGE_DISABLE} ]]; then |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 147 | create_bridge_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_BRIDGE_NAME}" |
| 148 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 149 | <interface type='bridge'> |
| 150 | <source bridge='$VM_CTL_BRIDGE_NAME'/> |
| 151 | <model type='virtio'/> |
| 152 | <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> |
| 153 | </interface> |
| 154 | EOF |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 155 | else |
| 156 | create_host_network "${VM_CTL_NETWORK_NAME}" "${VM_CTL_NETWORK_GATEWAY}" "${VM_CTL_NETWORK_MASK}" |
| 157 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml |
| 158 | <interface type='network'> |
| 159 | <source network='$VM_CTL_NETWORK_NAME'/> |
| 160 | <model type='virtio'/> |
| 161 | <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> |
| 162 | </interface> |
| 163 | EOF |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 164 | fi |
Denis Egorenko | dcc233d | 2019-03-06 18:09:24 +0400 | [diff] [blame^] | 165 | |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 166 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml |
| 167 | <serial type='pty'> |
| 168 | <source path='/dev/pts/1'/> |
| 169 | <target port='0'/> |
| 170 | </serial> |
| 171 | <console type='pty' tty='/dev/pts/1'> |
| 172 | <source path='/dev/pts/1'/> |
| 173 | <target type='serial' port='0'/> |
| 174 | </console> |
| 175 | <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'> |
| 176 | <listen type='address' address='127.0.0.1'/> |
| 177 | </graphics> |
azvyagintsev | 0022afe | 2018-10-29 15:09:15 +0200 | [diff] [blame] | 178 | <rng model='virtio'> |
| 179 | <backend model='random'>/dev/random</backend> |
| 180 | </rng> |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 181 | </devices> |
| 182 | </domain> |
| 183 | EOF |
| 184 | |
| 185 | echo "INFO: rendered VM config:" |
| 186 | cat $(pwd)/${VM_NAME}-vm.xml |
| 187 | |
| 188 | virsh define $(pwd)/${VM_NAME}-vm.xml |
| 189 | virsh autostart ${VM_NAME} |