1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from VMBuilder import register_hypervisor_plugin, Plugin, VMBuilderUserError
20 import VMBuilder.util
21
23 name = 'libvirt integration'
24
26 group = self.setting_group('libvirt integration')
27 group.add_setting('libvirt', metavar='URI', help='Add VM to given URI')
28 group.add_setting('bridge', metavar="BRIDGE", help='Set up bridged network connected to BRIDGE.')
29 group.add_setting('network', metavar='NETWORK', default='default', help='Set up a network connection to virtual network NETWORK.')
30
31 - def all_domains(self):
32
33 return self.conn.listDefinedDomains() + [self.conn.lookupByID(id).name() for id in self.conn.listDomainsID()]
34
36 libvirt_uri = self.get_setting('libvirt')
37 if not libvirt_uri:
38 return True
39
40 if not self.context.name == 'KVM' and not self.context.name == 'QEMu':
41 raise VMBuilderUserError('The libvirt plugin is only equiped to work with KVM and QEMu at the moment.')
42
43 import libvirt
44 import xml.etree.ElementTree
45
46 self.conn = libvirt.open(libvirt_uri)
47
48 e = xml.etree.ElementTree.fromstring(self.conn.getCapabilities())
49
50 if not 'hvm' in [x.text for x in e.getiterator('os_type')]:
51 raise VMBuilderUserError('libvirt does not seem to want to accept hvm domains')
52
53 hostname = self.context.distro.get_setting('hostname')
54 if hostname in self.all_domains() and not self.context.overwrite:
55 raise VMBuilderUserError('Domain %s already exists at %s' % (hostname, libvirt_uri))
56
58 libvirt_uri = self.get_setting('libvirt')
59 if not libvirt_uri:
60
61 return False
62
63 hostname = self.context.distro.get_setting('hostname')
64 tmpl_ctxt = { 'mem': self.context.get_setting('mem'),
65 'cpus': self.context.get_setting('cpus'),
66 'bridge' : self.context.get_setting('bridge'),
67 'mac' : self.context.get_setting('mac'),
68 'network' : self.context.get_setting('network'),
69 'mac' : self.context.get_setting('mac'),
70 'virtio_net' : self.context.distro.use_virtio_net(),
71 'disks' : self.context.disks,
72 'filesystems' : self.context.filesystems,
73 'hostname' : hostname,
74 'domain_type' : self.context.libvirt_domain_type_name() }
75 if self.context.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE:
76 vmxml = VMBuilder.util.render_template('libvirt', self.context, 'libvirtxml_fsimage', tmpl_ctxt)
77 else:
78 vmxml = VMBuilder.util.render_template('libvirt', self.context, 'libvirtxml', tmpl_ctxt)
79
80 if hostname in self.all_domains() and not self.context.overwrite:
81 raise VMBuilderUserError('Domain %s already exists at %s' % (hostname, libvirt_uri))
82 else:
83 self.conn.defineXML(vmxml)
84
85 return True
86
87 register_hypervisor_plugin(Libvirt)
88