| alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame^] | 1 | #!/bin/bash -xe | 
|  | 2 |  | 
|  | 3 | VM_MGM_BRIDGE_NAME=${VM_MGM_BRIDGE_NAME:-"br-mgm"} | 
|  | 4 | VM_MEM_KB=${VM_MEM_KB:-"4194304"} | 
|  | 5 | VM_CPUS=${VM_CPUS:-"4"} | 
|  | 6 |  | 
|  | 7 | if [[ -z ${VM_NAME} ]]; then | 
|  | 8 | echo "ERROR: \$VM_NAME not set!" | 
|  | 9 | exit 1 | 
|  | 10 | fi | 
|  | 11 | if [[ ! -f ${VM_SOURCE_DISK} ]] || [[ -z ${VM_SOURCE_DISK} ]]; then | 
|  | 12 | echo "ERROR: \$VM_SOURCE_DISK not set, or file does not exist!" | 
|  | 13 | exit 1 | 
|  | 14 | fi | 
|  | 15 | if [[ ! -f ${VM_CONFIG_DISK} ]] || [[ -z ${VM_CONFIG_DISK} ]]; then | 
|  | 16 | echo "ERROR: \$VM_CONFIG_DISK not set, or file does not exist!" | 
|  | 17 | exit 1 | 
|  | 18 | fi | 
|  | 19 |  | 
|  | 20 | # Template definition | 
|  | 21 | cat <<EOF > $(pwd)/${VM_NAME}-vm.xml | 
|  | 22 | <domain type='kvm'> | 
|  | 23 | <name>$VM_NAME</name> | 
|  | 24 | <memory unit='KiB'>$VM_MEM_KB</memory> | 
|  | 25 | <currentMemory unit='KiB'>$VM_MEM_KB</currentMemory> | 
|  | 26 | <vcpu placement='static'>$VM_CPUS</vcpu> | 
|  | 27 | <resource> | 
|  | 28 | <partition>/machine</partition> | 
|  | 29 | </resource> | 
|  | 30 | <os> | 
|  | 31 | <type >hvm</type> | 
|  | 32 | <boot dev='hd'/> | 
|  | 33 | </os> | 
|  | 34 | <features> | 
|  | 35 | <acpi/> | 
|  | 36 | </features> | 
|  | 37 | <clock offset='utc'> | 
|  | 38 | <timer name='rtc' tickpolicy='catchup'/> | 
|  | 39 | <timer name='pit' tickpolicy='delay'/> | 
|  | 40 | <timer name='hpet' present='no'/> | 
|  | 41 | </clock> | 
|  | 42 | <pm> | 
|  | 43 | <suspend-to-mem enabled='no'/> | 
|  | 44 | <suspend-to-disk enabled='no'/> | 
|  | 45 | </pm> | 
|  | 46 | <devices> | 
|  | 47 | <emulator>/usr/bin/kvm-spice</emulator> | 
|  | 48 | <disk type='file' device='disk'> | 
|  | 49 | <driver name='qemu' type='qcow2' cache='none'/> | 
|  | 50 | <source file='$VM_SOURCE_DISK'/> | 
|  | 51 | <target dev='vda' bus='virtio'/> | 
|  | 52 | <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> | 
|  | 53 | </disk> | 
|  | 54 | <disk type='file' device='cdrom'> | 
|  | 55 | <driver name='qemu' type='raw'/> | 
|  | 56 | <source file='$VM_CONFIG_DISK'/> | 
|  | 57 | <backingStore/> | 
|  | 58 | <target dev='hda' bus='ide'/> | 
|  | 59 | <readonly/> | 
|  | 60 | <address type='drive' controller='0' bus='0' target='0' unit='0'/> | 
|  | 61 | </disk> | 
|  | 62 | <interface type='bridge'> | 
|  | 63 | <source bridge='$VM_MGM_BRIDGE_NAME'/> | 
|  | 64 | <model type='virtio'/> | 
|  | 65 | <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> | 
|  | 66 | </interface> | 
|  | 67 | EOF | 
|  | 68 | if [[ -n ${VM_CTL_BRIDGE_NAME} ]]; then | 
|  | 69 | echo "\$VM_CTL_BRIDGE_NAME detected, adding one more nic to VM" | 
|  | 70 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml | 
|  | 71 | <interface type='bridge'> | 
|  | 72 | <source bridge='$VM_CTL_BRIDGE_NAME'/> | 
|  | 73 | <model type='virtio'/> | 
|  | 74 | <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> | 
|  | 75 | </interface> | 
|  | 76 | EOF | 
|  | 77 | fi | 
|  | 78 | cat <<EOF >> $(pwd)/${VM_NAME}-vm.xml | 
|  | 79 | <serial type='pty'> | 
|  | 80 | <source path='/dev/pts/1'/> | 
|  | 81 | <target port='0'/> | 
|  | 82 | </serial> | 
|  | 83 | <console type='pty' tty='/dev/pts/1'> | 
|  | 84 | <source path='/dev/pts/1'/> | 
|  | 85 | <target type='serial' port='0'/> | 
|  | 86 | </console> | 
|  | 87 | <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'> | 
|  | 88 | <listen type='address' address='127.0.0.1'/> | 
|  | 89 | </graphics> | 
|  | 90 | </devices> | 
|  | 91 | </domain> | 
|  | 92 | EOF | 
|  | 93 |  | 
|  | 94 | echo "INFO: rendered VM config:" | 
|  | 95 | cat $(pwd)/${VM_NAME}-vm.xml | 
|  | 96 |  | 
|  | 97 | virsh define $(pwd)/${VM_NAME}-vm.xml | 
|  | 98 | virsh autostart ${VM_NAME} |