Alexander Gordeev | 06fb29c | 2014-01-31 18:02:07 +0400 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import os.path |
| 5 | |
| 6 | import libvirt |
| 7 | |
| 8 | templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)), |
| 9 | 'templates') |
| 10 | |
| 11 | |
| 12 | def main(): |
| 13 | parser = argparse.ArgumentParser( |
| 14 | description="Configure a kvm virtual machine for the seed image.") |
| 15 | parser.add_argument('--name', default='seed', |
| 16 | help='the name to give the machine in libvirt.') |
| 17 | parser.add_argument('--image', |
| 18 | help='Use a custom image file (must be qcow2).') |
| 19 | parser.add_argument('--engine', default='qemu', |
| 20 | help='The virtualization engine to use') |
| 21 | parser.add_argument('--arch', default='i686', |
| 22 | help='The architecture to use') |
| 23 | parser.add_argument('--memory', default='2097152', |
| 24 | help="Maximum memory for the VM in KB.") |
| 25 | parser.add_argument('--cpus', default='1', |
| 26 | help="CPU count for the VM.") |
| 27 | parser.add_argument('--bootdev', default='hd', |
| 28 | help="What boot device to use (hd/network).") |
| 29 | parser.add_argument('--network', default="brbm", |
| 30 | help='The libvirt network name to use') |
| 31 | parser.add_argument('--libvirt-nic-driver', default='e1000', |
| 32 | help='The libvirt network driver to use') |
| 33 | parser.add_argument('--emulator', default=None, |
| 34 | help='Path to emulator bin for vm template') |
| 35 | args = parser.parse_args() |
| 36 | with file(templatedir + '/vm.xml', 'rb') as f: |
| 37 | source_template = f.read() |
| 38 | params = { |
| 39 | 'name': args.name, |
| 40 | 'imagefile': args.image, |
| 41 | 'engine': args.engine, |
| 42 | 'arch': args.arch, |
| 43 | 'memory': args.memory, |
| 44 | 'cpus': args.cpus, |
| 45 | 'bootdev': args.bootdev, |
| 46 | 'network': args.network, |
| 47 | 'emulator': args.emulator, |
| 48 | } |
| 49 | |
| 50 | if args.emulator: |
| 51 | params['emulator'] = args.emulator |
| 52 | else: |
| 53 | if os.path.exists("/usr/bin/kvm"): # Debian |
| 54 | params['emulator'] = "/usr/bin/kvm" |
| 55 | elif os.path.exists("/usr/bin/qemu-kvm"): # Redhat |
| 56 | params['emulator'] = "/usr/bin/qemu-kvm" |
| 57 | |
| 58 | nicparams = { |
| 59 | 'nicdriver': args.libvirt_nic_driver, |
| 60 | 'network': args.network, |
| 61 | } |
| 62 | |
| 63 | params['bm_network'] = """ |
| 64 | <!-- neutron friendly 'bare metal' network --> |
| 65 | <interface type='network'> |
| 66 | <source network='%(network)s'/> |
| 67 | <virtualport type='openvswitch'/> |
| 68 | <model type='%(nicdriver)s'/> |
| 69 | <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> |
| 70 | </interface>""" % nicparams |
| 71 | |
| 72 | libvirt_template = source_template % params |
| 73 | conn = libvirt.open("qemu:///system") |
| 74 | a = conn.defineXML(libvirt_template) |
| 75 | print ("Created machine %s with UUID %s" % (args.name, a.UUIDString())) |
| 76 | |
| 77 | if __name__ == '__main__': |
| 78 | main() |