Package VMBuilder :: Package plugins :: Package xen :: Module vm
[frames] | no frames]

Source Code for Module VMBuilder.plugins.xen.vm

 1  # 
 2  #    Uncomplicated VM Builder 
 3  #    Copyright (C) 2007-2009 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, Hypervisor 
20  from   VMBuilder.util import run_cmd 
21  import VMBuilder 
22  import VMBuilder.hypervisor 
23  import logging 
24  import os.path 
25  import stat 
26   
27 -class Xen(Hypervisor):
28 name = 'Xen' 29 arg = 'xen' 30 preferred_storage = VMBuilder.hypervisor.STORAGE_FS_IMAGE 31 needs_bootloader = False 32
33 - def register_options(self):
34 group = self.vm.setting_group('Xen option') 35 group.add_option('--xen-kernel', metavar='PATH', help='Path to the kernel to use (e.g.: /boot/vmlinux-2.6.27-7-server). Default depends on distribution and suite') 36 group.add_option('--xen-ramdisk', metavar='PATH', help='Path to the ramdisk to use (e.g.: /boot/initrd.img-2.6.27-7-server). Default depends on distribution and suite.') 37 self.vm.register_setting_group(group)
38
39 - def finalize(self):
40 destimages = [] 41 for filesystem in self.vm.filesystems: 42 if not filesystem.preallocated: 43 destfile = '%s/%s' % (self.vm.destdir, os.path.basename(filesystem.filename)) 44 logging.info('Moving %s to %s' % (filesystem.filename, destfile)) 45 self.vm.result_files.append(destfile) 46 run_cmd('cp', '--sparse=always', filesystem.filename, destfile) 47 os.unlink(filesystem.filename) 48 filesystem.filename = os.path.abspath(destfile) 49 destimages.append(destfile) 50 51 if not self.vm.xen_kernel: 52 self.vm.xen_kernel = self.vm.distro.xen_kernel_path() 53 if not self.vm.xen_ramdisk: 54 self.vm.xen_ramdisk = self.vm.distro.xen_ramdisk_path() 55 56 xenconf = '%s/xen.conf' % self.vm.destdir 57 fp = open(xenconf, 'w') 58 fp.write(""" 59 # Configuration file for the Xen instance %s, created 60 # by VMBuilder 61 kernel = '%s' 62 ramdisk = '%s' 63 memory = %d 64 65 root = '/dev/xvda1 ro' 66 disk = [ 67 %s 68 ] 69 70 name = '%s' 71 72 dhcp = 'dhcp' 73 vif = [''] 74 75 on_poweroff = 'destroy' 76 on_reboot = 'restart' 77 on_crash = 'restart' 78 79 extra = 'xencons=tty console=tty1 console=hvc0' 80 81 """ % (self.vm.name, 82 self.vm.xen_kernel, 83 self.vm.xen_ramdisk, 84 self.vm.mem, 85 ',\n'.join(["'tap:aio:%s,xvda%d,w'" % (os.path.abspath(img), id+1) for (img, id) in zip(destimages, range(len(destimages)))]), 86 self.vm.name)) 87 fp.close() 88 self.vm.result_files.append(xenconf)
89 90 register_hypervisor(Xen) 91