Package VMBuilder :: Package plugins :: Package libvirt
[frames] | no frames]

Source Code for Package VMBuilder.plugins.libvirt

 1  # 
 2  #    Uncomplicated VM Builder 
 3  #    Copyright (C) 2007-2010 Canonical Ltd. 
 4  # 
 5  #    See AUTHORS for list of contributors 
 6  # 
 7  #    This program is free software: you can redistribute it and/or modify 
 8  #    it under the terms of the GNU General Public License version 3, as 
 9  #    published by the Free Software Foundation. 
10  # 
11  #    This program is distributed in the hope that it will be useful, 
12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  #    GNU General Public License for more details. 
15  # 
16  #    You should have received a copy of the GNU General Public License 
17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  # 
19  from   VMBuilder import register_hypervisor_plugin, Plugin, VMBuilderUserError 
20  import VMBuilder.util 
21   
22 -class Libvirt(Plugin):
23 name = 'libvirt integration' 24
25 - def register_options(self):
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 # This does not seem to work when any domain is already running 33 return self.conn.listDefinedDomains() + [self.conn.lookupByID(id).name() for id in self.conn.listDomainsID()]
34
35 - def preflight_check(self):
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
57 - def deploy(self, destdir):
58 libvirt_uri = self.get_setting('libvirt') 59 if not libvirt_uri: 60 # Not for us 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