| 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 |  | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 12 | CONSOLE_LOG = """ | 
 | 13 |     <serial type='file'> | 
 | 14 |       <source path='%(console_log)s'/> | 
 | 15 |       <target port='0'/> | 
 | 16 |       <alias name='serial0'/> | 
 | 17 |     </serial> | 
 | 18 |     <serial type='pty'> | 
 | 19 |       <source path='/dev/pts/49'/> | 
 | 20 |       <target port='1'/> | 
 | 21 |       <alias name='serial1'/> | 
 | 22 |     </serial> | 
 | 23 |     <console type='file'> | 
 | 24 |       <source path='%(console_log)s'/> | 
 | 25 |       <target type='serial' port='0'/> | 
 | 26 |       <alias name='serial0'/> | 
 | 27 |     </console> | 
 | 28 | """ | 
 | 29 |  | 
 | 30 |  | 
| Alexander Gordeev | 06fb29c | 2014-01-31 18:02:07 +0400 | [diff] [blame] | 31 | def main(): | 
 | 32 |     parser = argparse.ArgumentParser( | 
 | 33 |         description="Configure a kvm virtual machine for the seed image.") | 
 | 34 |     parser.add_argument('--name', default='seed', | 
 | 35 |                         help='the name to give the machine in libvirt.') | 
 | 36 |     parser.add_argument('--image', | 
 | 37 |                         help='Use a custom image file (must be qcow2).') | 
 | 38 |     parser.add_argument('--engine', default='qemu', | 
 | 39 |                         help='The virtualization engine to use') | 
 | 40 |     parser.add_argument('--arch', default='i686', | 
 | 41 |                         help='The architecture to use') | 
 | 42 |     parser.add_argument('--memory', default='2097152', | 
 | 43 |                         help="Maximum memory for the VM in KB.") | 
 | 44 |     parser.add_argument('--cpus', default='1', | 
 | 45 |                         help="CPU count for the VM.") | 
 | 46 |     parser.add_argument('--bootdev', default='hd', | 
 | 47 |                         help="What boot device to use (hd/network).") | 
 | 48 |     parser.add_argument('--network', default="brbm", | 
 | 49 |                         help='The libvirt network name to use') | 
 | 50 |     parser.add_argument('--libvirt-nic-driver', default='e1000', | 
 | 51 |                         help='The libvirt network driver to use') | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 52 |     parser.add_argument('--console-log', | 
 | 53 |                         help='File to log console') | 
| Alexander Gordeev | 06fb29c | 2014-01-31 18:02:07 +0400 | [diff] [blame] | 54 |     parser.add_argument('--emulator', default=None, | 
 | 55 |                         help='Path to emulator bin for vm template') | 
 | 56 |     args = parser.parse_args() | 
 | 57 |     with file(templatedir + '/vm.xml', 'rb') as f: | 
 | 58 |         source_template = f.read() | 
 | 59 |     params = { | 
 | 60 |         'name': args.name, | 
 | 61 |         'imagefile': args.image, | 
 | 62 |         'engine': args.engine, | 
 | 63 |         'arch': args.arch, | 
 | 64 |         'memory': args.memory, | 
 | 65 |         'cpus': args.cpus, | 
 | 66 |         'bootdev': args.bootdev, | 
 | 67 |         'network': args.network, | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 68 |         'nicdriver': args.libvirt_nic_driver, | 
| Alexander Gordeev | 06fb29c | 2014-01-31 18:02:07 +0400 | [diff] [blame] | 69 |         'emulator': args.emulator, | 
 | 70 |     } | 
 | 71 |  | 
 | 72 |     if args.emulator: | 
 | 73 |         params['emulator'] = args.emulator | 
 | 74 |     else: | 
 | 75 |         if os.path.exists("/usr/bin/kvm"):  # Debian | 
 | 76 |             params['emulator'] = "/usr/bin/kvm" | 
 | 77 |         elif os.path.exists("/usr/bin/qemu-kvm"):  # Redhat | 
 | 78 |             params['emulator'] = "/usr/bin/qemu-kvm" | 
 | 79 |  | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 80 |     if args.console_log: | 
| Adam Gandelman | ffd66ad | 2014-11-17 12:26:08 -0800 | [diff] [blame] | 81 |         params['bios_serial'] = "<bios useserial='yes'/>" | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 82 |         params['console_log'] = CONSOLE_LOG % {'console_log': args.console_log} | 
 | 83 |     else: | 
| Adam Gandelman | ffd66ad | 2014-11-17 12:26:08 -0800 | [diff] [blame] | 84 |         params['bios_serial'] = '' | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 85 |         params['console_log'] = '' | 
| Alexander Gordeev | 06fb29c | 2014-01-31 18:02:07 +0400 | [diff] [blame] | 86 |     libvirt_template = source_template % params | 
 | 87 |     conn = libvirt.open("qemu:///system") | 
| Adam Gandelman | 8af6fae | 2014-04-11 17:06:14 -0700 | [diff] [blame] | 88 |  | 
| Alexander Gordeev | 06fb29c | 2014-01-31 18:02:07 +0400 | [diff] [blame] | 89 |     a = conn.defineXML(libvirt_template) | 
 | 90 |     print ("Created machine %s with UUID %s" % (args.name, a.UUIDString())) | 
 | 91 |  | 
 | 92 | if __name__ == '__main__': | 
 | 93 |     main() |