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

Source Code for Module VMBuilder.plugins.ubuntu.dapper

  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 glob 
 21  import logging 
 22  import os 
 23  import suite 
 24  import shutil 
 25  import socket 
 26  import VMBuilder 
 27  import VMBuilder.disk as disk 
 28  from   VMBuilder.util import run_cmd 
 29   
30 -class Dapper(suite.Suite):
31 updategrub = "/sbin/update-grub" 32 grubroot = "/lib/grub" 33 valid_flavours = { 'i386' : ['386', '686', '686-smp', 'k7', 'k7-smp', 'server', 'server-bigiron'], 34 'amd64' : ['amd64-generic', 'amd64-k8', 'amd64-k8-smp', 'amd64-server', 'amd64-xeon']} 35 default_flavour = { 'i386' : 'server', 'amd64' : 'amd64-server' } 36 disk_prefix = 'hd' 37 xen_kernel_flavour = None 38 virtio_net = False 39
40 - def check_kernel_flavour(self, arch, flavour):
41 return flavour in self.valid_flavours[arch]
42
43 - def check_arch_validity(self, arch):
44 return arch in self.valid_flavours.keys()
45
46 - def install(self, destdir):
47 self.destdir = destdir 48 49 logging.debug("debootstrapping") 50 self.debootstrap() 51 52 logging.debug("Setting up sources.list") 53 self.install_sources_list() 54 55 logging.debug("Installing fstab") 56 self.install_fstab() 57 58 logging.debug("Creating devices") 59 self.create_devices() 60 61 if self.vm.hypervisor.needs_bootloader: 62 logging.debug("Installing grub") 63 self.install_grub() 64 65 logging.debug("Configuring guest networking") 66 self.config_network() 67 68 logging.debug("Preventing daemons from starting") 69 self.prevent_daemons_starting() 70 71 if self.vm.hypervisor.needs_bootloader: 72 logging.debug("Installing menu.list") 73 self.install_menu_lst() 74 75 logging.debug("Installing kernel") 76 self.install_kernel() 77 78 logging.debug("Creating device.map") 79 self.install_device_map() 80 81 logging.debug("Installing ssh keys") 82 self.install_authorized_keys() 83 84 logging.debug("Installing extra packages") 85 self.install_extras() 86 87 logging.debug("Creating initial user") 88 self.create_initial_user() 89 90 logging.debug("Copy host settings") 91 self.copy_settings() 92 93 logging.debug("Making sure system is up-to-date") 94 self.update() 95 96 logging.debug("Setting up final sources.list") 97 self.install_sources_list(final=True) 98 99 logging.debug("Unmounting volatile lrm filesystems") 100 self.unmount_volatile() 101 102 logging.debug("Unpreventing daemons from starting") 103 self.unprevent_daemons_starting()
104
105 - def update(self):
106 self.run_in_target('apt-get', '-y', '--force-yes', 'dist-upgrade')
107
108 - def install_authorized_keys(self):
109 if self.vm.ssh_key: 110 os.mkdir('%s/root/.ssh' % self.destdir, 0700) 111 shutil.copy(self.vm.ssh_key, '%s/root/.ssh/authorized_keys' % self.destdir) 112 os.chmod('%s/root/.ssh/authorized_keys' % self.destdir, 0644) 113 if self.vm.ssh_user_key: 114 os.mkdir('%s/home/%s/.ssh' % (self.destdir, self.vm.user), 0700) 115 shutil.copy(self.vm.ssh_user_key, '%s/home/%s/.ssh/authorized_keys' % (self.destdir, self.vm.user)) 116 os.chmod('%s/home/%s/.ssh/authorized_keys' % (self.destdir, self.vm.user), 0644) 117 if self.vm.ssh_user_key or self.vm.ssh_key: 118 if not self.vm.addpkg: 119 self.vm.addpkg = [] 120 self.vm.addpkg += ['openssh-server']
121
122 - def create_initial_user(self):
123 self.run_in_target('adduser', '--disabled-password', '--gecos', self.vm.name, self.vm.user) 124 self.run_in_target('chpasswd', stdin=('%s:%s\n' % (self.vm.user, getattr(self.vm, 'pass')))) 125 self.run_in_target('addgroup', '--system', 'admin') 126 self.run_in_target('adduser', self.vm.user, 'admin') 127 128 self.install_from_template('/etc/sudoers', 'sudoers') 129 for group in ['adm', 'audio', 'cdrom', 'dialout', 'floppy', 'video', 'plugdev', 'dip', 'netdev', 'powerdev', 'lpadmin', 'scanner']: 130 self.run_in_target('adduser', self.vm.user, group, ignore_fail=True) 131 132 # Lock root account 133 self.run_in_target('chpasswd', '-e', stdin='root:!\n')
134
135 - def kernel_name(self):
136 return 'linux-image-%s' % (self.vm.flavour or self.default_flavour[self.vm.arch],)
137
138 - def config_network(self):
139 self.vm.install_file('/etc/hostname', self.vm.hostname) 140 self.install_from_template('/etc/hosts', 'etc_hosts', { 'hostname' : self.vm.hostname, 'domain' : self.vm.domain }) 141 self.install_from_template('/etc/network/interfaces', 'interfaces')
142
144 os.unlink('%s/usr/sbin/policy-rc.d' % self.destdir)
145
146 - def prevent_daemons_starting(self):
147 os.chmod(self.install_from_template('/usr/sbin/policy-rc.d', 'nostart-policy-rc.d'), 0755)
148
149 - def install_extras(self):
150 if not self.vm.addpkg and not self.vm.removepkg: 151 return 152 cmd = ['apt-get', 'install', '-y', '--force-yes'] 153 cmd += self.vm.addpkg or [] 154 cmd += ['%s-' % pkg for pkg in self.vm.removepkg or []] 155 self.run_in_target(env={ 'DEBIAN_FRONTEND' : 'noninteractive' }, *cmd)
156
157 - def unmount_volatile(self):
158 for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.destdir): 159 logging.debug("Unmounting %s" % mntpnt) 160 run_cmd('umount', mntpnt)
161
162 - def install_menu_lst(self):
163 run_cmd('mount', '--bind', '/dev', '%s/dev' % self.destdir) 164 self.vm.add_clean_cmd('umount', '%s/dev' % self.destdir, ignore_fail=True) 165 166 self.run_in_target('mount', '-t', 'proc', 'proc', '/proc') 167 self.vm.add_clean_cmd('umount', '%s/proc' % self.destdir, ignore_fail=True) 168 169 self.run_in_target(self.updategrub, '-y') 170 self.mangle_grub_menu_lst() 171 self.run_in_target(self.updategrub) 172 self.run_in_target('grub-set-default', '0') 173 174 run_cmd('umount', '%s/dev' % self.destdir) 175 run_cmd('umount', '%s/proc' % self.destdir)
176
177 - def mangle_grub_menu_lst(self):
178 bootdev = disk.bootpart(self.vm.disks) 179 run_cmd('sed', '-ie', 's/^# kopt=root=\([^ ]*\)\(.*\)/# kopt=root=\/dev\/hd%s%d\\2/g' % (bootdev.disk.devletters(), bootdev.get_index()+1), '%s/boot/grub/menu.lst' % self.destdir) 180 run_cmd('sed', '-ie', 's/^# groot.*/# groot %s/g' % bootdev.get_grub_id(), '%s/boot/grub/menu.lst' % self.destdir) 181 run_cmd('sed', '-ie', '/^# kopt_2_6/ d', '%s/boot/grub/menu.lst' % self.destdir)
182
183 - def install_sources_list(self, final=False):
184 if final: 185 mirror, updates_mirror, security_mirror = self.vm.mirror, self.vm.mirror, self.vm.security_mirror 186 else: 187 mirror, updates_mirror, security_mirror = self.install_mirrors() 188 189 self.install_from_template('/etc/apt/sources.list', 'sources.list', { 'mirror' : mirror, 'security_mirror' : security_mirror, 'updates_mirror' : updates_mirror }) 190 191 # If setting up the final mirror, allow apt-get update to fail 192 # (since we might be on a complete different network than the 193 # final vm is going to be on). 194 self.run_in_target('apt-get', 'update', ignore_fail=final)
195
196 - def install_fstab(self):
197 if self.vm.hypervisor.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE: 198 self.install_from_template('/etc/fstab', 'dapper_fstab_fsimage', { 'fss' : disk.get_ordered_filesystems(self.vm), 'prefix' : self.disk_prefix }) 199 else: 200 self.install_from_template('/etc/fstab', 'dapper_fstab', { 'parts' : disk.get_ordered_partitions(self.vm.disks), 'prefix' : self.disk_prefix })
201
202 - def install_device_map(self):
203 self.install_from_template('/boot/grub/device.map', 'devicemap', { 'prefix' : self.disk_prefix })
204
205 - def debootstrap(self):
206 cmd = ['/usr/sbin/debootstrap', '--arch=%s' % self.vm.arch, self.vm.suite, self.destdir, self.debootstrap_mirror()] 207 run_cmd(*cmd)
208
209 - def debootstrap_mirror(self):
210 if self.vm.iso: 211 os.mkdir(isodir) 212 self.vm.add_clean_cb(lambda:os.rmdir(isodir)) 213 run_cmd('mount', '-o', 'loop', '-t', 'iso9660', self.vm.iso, isodir) 214 self.vm.add_clean_cmd('umount', isodir) 215 self.iso_mounted = True 216 217 return 'file://%s' % isodir 218 else: 219 return self.install_mirrors()[0]
220 221
222 - def install_mirrors(self):
223 if self.vm.iso: 224 mirror = "file:///isomnt" 225 elif self.vm.install_mirror: 226 mirror = self.vm.install_mirror 227 else: 228 mirror = self.vm.mirror 229 230 if self.vm.install_mirror: 231 updates_mirror = self.vm.install_mirror 232 else: 233 updates_mirror = self.vm.mirror 234 235 if self.vm.install_security_mirror: 236 security_mirror = self.vm.install_security_mirror 237 else: 238 security_mirror = self.vm.security_mirror 239 240 return (mirror, updates_mirror, security_mirror)
241
242 - def install_kernel(self):
243 self.install_from_template('/etc/kernel-img.conf', 'kernelimg', { 'updategrub' : self.updategrub }) 244 run_cmd('chroot', self.destdir, 'apt-get', '--force-yes', '-y', 'install', self.kernel_name(), 'grub')
245
246 - def install_grub(self):
247 self.run_in_target('apt-get', '--force-yes', '-y', 'install', 'grub') 248 run_cmd('cp', '-a', '%s%s/%s/' % (self.destdir, self.grubroot, self.vm.arch == 'amd64' and 'x86_64-pc' or 'i386-pc'), '%s/boot/grub' % self.destdir)
249
250 - def create_devices(self):
251 import VMBuilder.plugins.xen 252 253 if isinstance(self.vm.hypervisor, VMBuilder.plugins.xen.Xen): 254 self.run_in_target('mknod', '/dev/xvda', 'b', '202', '0') 255 self.run_in_target('mknod', '/dev/xvda1', 'b', '202', '1') 256 self.run_in_target('mknod', '/dev/xvda2', 'b', '202', '2') 257 self.run_in_target('mknod', '/dev/xvda3', 'b', '202', '3') 258 self.run_in_target('mknod', '/dev/xvc0', 'c', '204', '191')
259
260 - def install_from_template(self, *args, **kwargs):
261 return self.vm.distro.install_from_template(*args, **kwargs)
262
263 - def run_in_target(self, *args, **kwargs):
264 self.vm.distro.run_in_target(*args, **kwargs)
265
266 - def copy_to_target(self, infile, destpath):
267 dir = '%s/%s' % (self.destdir, os.path.dirname(destpath)) 268 if not os.path.isdir(dir): 269 os.makedirs(dir) 270 if os.path.isdir(infile): 271 shutil.copytree(infile, '%s/%s' % (self.destdir, destpath)) 272 else: 273 shutil.copy(infile, '%s/%s' % (self.destdir, destpath))
274
275 - def post_mount(self, fs):
276 if fs.mntpnt == '/': 277 logging.debug("Creating /var/run in root filesystem") 278 os.makedirs('%s/var/run' % fs.mntpath) 279 logging.debug("Creating /var/lock in root filesystem") 280 os.makedirs('%s/var/lock' % fs.mntpath)
281
282 - def copy_settings(self):
283 self.copy_to_target('/etc/default/locale', '/etc/default/locale') 284 self.copy_to_target('/etc/timezone', '/etc/timezone') 285 self.run_in_target('dpkg-reconfigure', '-pcritical', 'libc6') 286 self.run_in_target('locale-gen', 'en_US') 287 if self.vm.lang: 288 self.run_in_target('locale-gen', self.vm.lang) 289 self.install_from_template('/etc/default/locale', 'locale', { 'lang' : self.vm.lang }) 290 self.run_in_target('dpkg-reconfigure', '-pcritical', 'locales')
291