Package VMBuilder :: Package plugins :: Package ubuntu :: Module distro
[frames] | no frames]

Source Code for Module VMBuilder.plugins.ubuntu.distro

  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  import VMBuilder 
 21  from   VMBuilder           import register_distro, Distro 
 22  from   VMBuilder.util      import run_cmd 
 23  from   VMBuilder.exception import VMBuilderUserError 
 24  import socket 
 25  import logging 
 26  import types 
 27  import os 
 28   
29 -class Ubuntu(Distro):
30 name = 'Ubuntu' 31 arg = 'ubuntu' 32 suites = ['dapper', 'gutsy', 'hardy', 'intrepid', 'jaunty'] 33 34 # Maps host arch to valid guest archs 35 valid_archs = { 'amd64' : ['amd64', 'i386', 'lpia' ], 36 'i386' : [ 'i386', 'lpia' ], 37 'lpia' : [ 'i386', 'lpia' ] } 38 39 xen_kernel = '' 40
41 - def register_options(self):
42 group = self.vm.setting_group('Package options') 43 group.add_option('--addpkg', action='append', metavar='PKG', help='Install PKG into the guest (can be specfied multiple times).') 44 group.add_option('--removepkg', action='append', metavar='PKG', help='Remove PKG from the guest (can be specfied multiple times)') 45 self.vm.register_setting_group(group) 46 47 group = self.vm.setting_group('General OS options') 48 self.host_arch = run_cmd('dpkg', '--print-architecture').rstrip() 49 group.add_option('-a', '--arch', default=self.host_arch, help='Specify the target architecture. Valid options: amd64 i386 lpia (defaults to host arch)') 50 group.add_option('--hostname', default='ubuntu', help='Set NAME as the hostname of the guest. Default: ubuntu. Also uses this name as the VM name.') 51 self.vm.register_setting_group(group) 52 53 group = self.vm.setting_group('Installation options') 54 group.add_option('--suite', default='jaunty', help='Suite to install. Valid options: %s [default: %%default]' % ' '.join(self.suites)) 55 group.add_option('--flavour', '--kernel-flavour', help='Kernel flavour to use. Default and valid options depend on architecture and suite') 56 group.add_option('--iso', metavar='PATH', help='Use an iso image as the source for installation of file. Full path to the iso must be provided. If --mirror is also provided, it will be used in the final sources.list of the vm. This requires suite and kernel parameter to match what is available on the iso, obviously.') 57 group.add_option('--mirror', metavar='URL', help='Use Ubuntu mirror at URL instead of the default, which is http://archive.ubuntu.com/ubuntu for official arches and http://ports.ubuntu.com/ubuntu-ports otherwise') 58 group.add_option('--install-mirror', metavar='URL', help='Use Ubuntu mirror at URL for the installation only. Apt\'s sources.list will still use default or URL set by --mirror') 59 group.add_option('--security-mirror', metavar='URL', help='Use Ubuntu security mirror at URL instead of the default, which is http://security.ubuntu.com/ubuntu for official arches and http://ports.ubuntu.com/ubuntu-ports otherwise.') 60 group.add_option('--install-security-mirror', metavar='URL', help='Use the security mirror at URL for installation only. Apt\'s sources.list will still use default or URL set by --security-mirror') 61 group.add_option('--components', metavar='COMPS', help='A comma seperated list of distro components to include (e.g. main,universe).') 62 group.add_option('--ppa', metavar='PPA', action='append', help='Add ppa belonging to PPA to the vm\'s sources.list.') 63 group.add_option('--lang', metavar='LANG', default=self.get_locale(), help='Set the locale to LANG [default: %default]') 64 self.vm.register_setting_group(group) 65 66 group = self.vm.setting_group('Settings for the initial user') 67 group.add_option('--user', default='ubuntu', help='Username of initial user [default: %default]') 68 group.add_option('--name', default='Ubuntu', help='Full name of initial user [default: %default]') 69 group.add_option('--pass', default='ubuntu', help='Password of initial user [default: %default]') 70 self.vm.register_setting_group(group) 71 72 group = self.vm.setting_group('Other options') 73 group.add_option('--ssh-key', metavar='PATH', help='Add PATH to root\'s ~/.ssh/authorized_keys (WARNING: this has strong security implications).') 74 group.add_option('--ssh-user-key', help='Add PATH to the user\'s ~/.ssh/authorized_keys.') 75 self.vm.register_setting_group(group)
76
77 - def set_defaults(self):
78 if not self.vm.mirror: 79 if self.vm.arch == 'lpia': 80 self.vm.mirror = 'http://ports.ubuntu.com/ubuntu-ports' 81 else: 82 self.vm.mirror = 'http://archive.ubuntu.com/ubuntu' 83 84 if not self.vm.security_mirror: 85 if self.vm.arch == 'lpia': 86 self.vm.security_mirror = 'http://ports.ubuntu.com/ubuntu-ports' 87 else: 88 self.vm.security_mirror = 'http://security.ubuntu.com/ubuntu' 89 90 if not self.vm.components: 91 self.vm.components = ['main', 'restricted', 'universe'] 92 else: 93 self.vm.components = self.vm.components.split(',')
94
95 - def get_locale(self):
96 try: 97 return os.environ['LANG'] 98 except: 99 return None
100
101 - def preflight_check(self):
102 """While not all of these are strictly checks, their failure would inevitably 103 lead to failure, and since we can check them before we start setting up disk 104 and whatnot, we might as well go ahead an do this now.""" 105 106 if not self.vm.suite in self.suites: 107 raise VMBuilderUserError('Invalid suite. Valid suites are: %s' % ' '.join(self.suites)) 108 109 modname = 'VMBuilder.plugins.ubuntu.%s' % (self.vm.suite, ) 110 mod = __import__(modname, fromlist=[self.vm.suite]) 111 self.suite = getattr(mod, self.vm.suite.capitalize())(self.vm) 112 113 if self.vm.arch not in self.valid_archs[self.host_arch] or \ 114 not self.suite.check_arch_validity(self.vm.arch): 115 raise VMBuilderUserError('%s is not a valid architecture. Valid architectures are: %s' % (self.vm.arch, 116 ' '.join(self.valid_archs[self.host_arch]))) 117 118 if not self.vm.components: 119 self.vm.components = ['main', 'restricted', 'universe'] 120 else: 121 if type(self.vm.components) is str: 122 self.vm.components = self.vm.components.split(',') 123 124 if self.vm.hypervisor.name == 'Xen': 125 logging.info('Xen kernel default: linux-image-%s %s', self.suite.xen_kernel_flavour, self.xen_kernel_version()) 126 127 self.vm.virtio_net = self.use_virtio_net()
128
129 - def install(self, destdir):
130 self.destdir = destdir 131 self.suite.install(destdir)
132
133 - def post_mount(self, fs):
134 self.suite.post_mount(fs)
135
136 - def use_virtio_net(self):
137 return self.suite.virtio_net
138
139 - def install_bootloader(self):
140 devmapfile = '%s/device.map' % self.vm.workdir 141 devmap = open(devmapfile, 'w') 142 for (disk, id) in zip(self.vm.disks, range(len(self.vm.disks))): 143 devmap.write("(hd%d) %s\n" % (id, disk.filename)) 144 devmap.close() 145 run_cmd('grub', '--device-map=%s' % devmapfile, '--batch', stdin='''root (hd0,0) 146 setup (hd0) 147 EOT''')
148
149 - def xen_kernel_version(self):
150 if self.suite.xen_kernel_flavour: 151 if not self.xen_kernel: 152 rmad = run_cmd('rmadison', 'linux-image-%s' % self.suite.xen_kernel_flavour) 153 version = ['0', '0','0', '0'] 154 155 for line in rmad.splitlines(): 156 sline = line.split('|') 157 158 if sline[2].strip().startswith(self.vm.suite): 159 vt = sline[1].strip().split('.') 160 for i in range(4): 161 if int(vt[i]) > int(version[i]): 162 version = vt 163 break 164 165 if version[0] == '0': 166 raise VMBuilderException('Something is wrong, no valid xen kernel for the suite %s found by rmadison' % self.vm.suite) 167 168 self.xen_kernel = '%s.%s.%s-%s' % (version[0],version[1],version[2],version[3]) 169 return self.xen_kernel 170 else: 171 raise VMBuilderUserError('There is no valid xen kernel for the suite selected.')
172
173 - def xen_kernel_path(self):
174 path = '/boot/vmlinuz-%s-%s' % (self.xen_kernel_version(), self.suite.xen_kernel_flavour) 175 return path
176
177 - def xen_ramdisk_path(self):
178 path = '/boot/initrd.img-%s-%s' % (self.xen_kernel_version(), self.suite.xen_kernel_flavour) 179 return path
180 181 182 register_distro(Ubuntu) 183