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-2008 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 as published by 
 9  #    the Free Software Foundation, either version 3 of the License, or 
10  #    (at your option) any later version. 
11  # 
12  #    This program is distributed in the hope that it will be useful, 
13  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15  #    GNU General Public License for more details. 
16  # 
17  #    You should have received a copy of the GNU General Public License 
18  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
19  # 
20  from   VMBuilder      import register_hypervisor, Hypervisor 
21  from   VMBuilder.util import run_cmd 
22  import VMBuilder 
23  import VMBuilder.hypervisor 
24  import logging 
25  import os.path 
26  import stat 
27   
28 -class Xen(Hypervisor):
29 name = 'Xen' 30 arg = 'xen' 31 preferred_storage = VMBuilder.hypervisor.STORAGE_FS_IMAGE 32 needs_bootloader = False 33
34 - def register_options(self):
35 group = self.vm.setting_group('Xen option') 36 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') 37 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.') 38 self.vm.register_setting_group(group)
39
40 - def finalize(self):
41 destimages = [] 42 for filesystem in self.vm.filesystems: 43 if not filesystem.preallocated: 44 destfile = '%s/%s' % (self.vm.destdir, os.path.basename(filesystem.filename)) 45 logging.info('Moving %s to %s' % (filesystem.filename, destfile)) 46 self.vm.result_files.append(destfile) 47 run_cmd('cp', '--sparse=always', filesystem.filename, destfile) 48 os.unlink(filesystem.filename) 49 filesystem.filename = os.path.abspath(destfile) 50 destimages.append(destfile) 51 52 if not self.vm.xen_kernel: 53 self.vm.xen_kernel = self.vm.distro.xen_kernel_path() 54 if not self.vm.xen_ramdisk: 55 self.vm.xen_ramdisk = self.vm.distro.xen_ramdisk_path() 56 57 xenconf = '%s/xen.conf' % self.vm.destdir 58 fp = open(xenconf, 'w') 59 fp.write(""" 60 # Configuration file for the Xen instance %s, created 61 # by VMBuilder 62 kernel = '%s' 63 ramdisk = '%s' 64 memory = %d 65 66 root = '/dev/xvda1 ro' 67 disk = [ 68 %s 69 ] 70 71 name = '%s' 72 73 dhcp = 'dhcp' 74 vif = [''] 75 76 on_poweroff = 'destroy' 77 on_reboot = 'restart' 78 on_crash = 'restart' 79 80 extra = 'xencons=tty console=tty1 console=hvc0' 81 82 """ % (self.vm.name, 83 self.vm.xen_kernel, 84 self.vm.xen_ramdisk, 85 self.vm.mem, 86 ',\n'.join(["'tap:aio:%s,xvda%d,w'" % (os.path.abspath(img), id+1) for (img, id) in zip(destimages, range(len(destimages)))]), 87 self.vm.name)) 88 fp.close() 89 self.vm.result_files.append(xenconf)
90 91 register_hypervisor(Xen) 92