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"} |
Richard Felkl | be962ed | 2018-06-14 17:28:08 +0200 | [diff] [blame] | 4 | VM_CTL_BRIDGE_NAME=${VM_CTL_BRIDGE_NAME:-"br-ctl"} |
Dennis Dmitriev | 1b2ccb2 | 2018-06-08 20:38:07 +0300 | [diff] [blame] | 5 | VM_MEM_KB=${VM_MEM_KB:-"8388608"} |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 6 | VM_CPUS=${VM_CPUS:-"4"} |
| 7 | |
| 8 | if [[ -z ${VM_NAME} ]]; then |
| 9 | echo "ERROR: \$VM_NAME not set!" |
| 10 | exit 1 |
| 11 | fi |
| 12 | if [[ ! -f ${VM_SOURCE_DISK} ]] || [[ -z ${VM_SOURCE_DISK} ]]; then |
| 13 | echo "ERROR: \$VM_SOURCE_DISK not set, or file does not exist!" |
| 14 | exit 1 |
| 15 | fi |
| 16 | if [[ ! -f ${VM_CONFIG_DISK} ]] || [[ -z ${VM_CONFIG_DISK} ]]; then |
| 17 | echo "ERROR: \$VM_CONFIG_DISK not set, or file does not exist!" |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | # Template definition |
| 22 | cat <<EOF > $(pwd)/${VM_NAME}-vm.xml |
| 23 | <domain type='kvm'> |
| 24 | <name>$VM_NAME</name> |
| 25 | <memory unit='KiB'>$VM_MEM_KB</memory> |
| 26 | <currentMemory unit='KiB'>$VM_MEM_KB</currentMemory> |
| 27 | <vcpu placement='static'>$VM_CPUS</vcpu> |
| 28 | <resource> |
| 29 | <partition>/machine</partition> |
| 30 | </resource> |
| 31 | <os> |
| 32 | <type >hvm</type> |
| 33 | <boot dev='hd'/> |
| 34 | </os> |
| 35 | <features> |
| 36 | <acpi/> |
| 37 | </features> |
| 38 | <clock offset='utc'> |
| 39 | <timer name='rtc' tickpolicy='catchup'/> |
| 40 | <timer name='pit' tickpolicy='delay'/> |
| 41 | <timer name='hpet' present='no'/> |
| 42 | </clock> |
| 43 | <pm> |
| 44 | <suspend-to-mem enabled='no'/> |
| 45 | <suspend-to-disk enabled='no'/> |
| 46 | </pm> |
| 47 | <devices> |
| 48 | <emulator>/usr/bin/kvm-spice</emulator> |
| 49 | <disk type='file' device='disk'> |
| 50 | <driver name='qemu' type='qcow2' cache='none'/> |
| 51 | <source file='$VM_SOURCE_DISK'/> |
| 52 | <target dev='vda' bus='virtio'/> |
| 53 | <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> |
| 54 | </disk> |
| 55 | <disk type='file' device='cdrom'> |
| 56 | <driver name='qemu' type='raw'/> |
| 57 | <source file='$VM_CONFIG_DISK'/> |
| 58 | <backingStore/> |
| 59 | <target dev='hda' bus='ide'/> |
| 60 | <readonly/> |
| 61 | <address type='drive' controller='0' bus='0' target='0' unit='0'/> |
| 62 | </disk> |
| 63 | <interface type='bridge'> |
| 64 | <source bridge='$VM_MGM_BRIDGE_NAME'/> |
| 65 | <model type='virtio'/> |
| 66 | <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> |
| 67 | </interface> |
| 68 | EOF |
Richard Felkl | be962ed | 2018-06-14 17:28:08 +0200 | [diff] [blame] | 69 | if [[ ! ${VM_CTL_BRIDGE_DISABLE} ]]; then |
alexz | 1c02cd6 | 2018-04-15 22:18:03 +0200 | [diff] [blame] | 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} |