blob: 378fcb85ad2eed94b3df4db9fef101d0331761d8 [file] [log] [blame]
Alexander Gordeev06fb29c2014-01-31 18:02:07 +04001#!/usr/bin/env python
2
3import argparse
4import os.path
5
6import libvirt
7
8templatedir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
9 'templates')
10
11
Adam Gandelman8af6fae2014-04-11 17:06:14 -070012CONSOLE_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 Gordeev06fb29c2014-01-31 18:02:07 +040031def 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 Gandelman8af6fae2014-04-11 17:06:14 -070052 parser.add_argument('--console-log',
53 help='File to log console')
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040054 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 Gandelman8af6fae2014-04-11 17:06:14 -070068 'nicdriver': args.libvirt_nic_driver,
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040069 '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 Gandelman8af6fae2014-04-11 17:06:14 -070080 if args.console_log:
Adam Gandelmanffd66ad2014-11-17 12:26:08 -080081 params['bios_serial'] = "<bios useserial='yes'/>"
Adam Gandelman8af6fae2014-04-11 17:06:14 -070082 params['console_log'] = CONSOLE_LOG % {'console_log': args.console_log}
83 else:
Adam Gandelmanffd66ad2014-11-17 12:26:08 -080084 params['bios_serial'] = ''
Adam Gandelman8af6fae2014-04-11 17:06:14 -070085 params['console_log'] = ''
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040086 libvirt_template = source_template % params
87 conn = libvirt.open("qemu:///system")
Adam Gandelman8af6fae2014-04-11 17:06:14 -070088
Alexander Gordeev06fb29c2014-01-31 18:02:07 +040089 a = conn.defineXML(libvirt_template)
90 print ("Created machine %s with UUID %s" % (args.name, a.UUIDString()))
91
92if __name__ == '__main__':
93 main()