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
38 - def check_kernel_flavour(self, arch, flavour):
39 return flavour in self.valid_flavours[arch]
40
41 - def check_arch_validity(self, arch):
42 return arch in self.valid_flavours.keys()
43
44 - def install(self, destdir):
45 self.destdir = destdir 46 47 logging.debug("debootstrapping") 48 self.debootstrap() 49 50 logging.debug("Setting up sources.list") 51 self.install_sources_list() 52 53 logging.debug("Installing fstab") 54 self.install_fstab() 55 56 logging.debug("Creating devices") 57 self.create_devices() 58 59 if self.vm.hypervisor.needs_bootloader: 60 logging.debug("Installing grub") 61 self.install_grub() 62 63 logging.debug("Configuring guest networking") 64 self.config_network() 65 66 logging.debug("Preventing daemons from starting") 67 self.prevent_daemons_starting() 68 69 if self.vm.hypervisor.needs_bootloader: 70 logging.debug("Installing menu.list") 71 self.install_menu_lst() 72 73 logging.debug("Installing kernel") 74 self.install_kernel() 75 76 logging.debug("Creating device.map") 77 self.install_device_map() 78 79 logging.debug("Installing extra packages") 80 self.install_extras() 81 82 logging.debug("Creating initial user") 83 self.create_initial_user() 84 85 logging.debug("Installing ssh keys") 86 self.install_authorized_keys() 87 88 logging.debug("Making sure system is up-to-date") 89 self.update() 90 91 logging.debug("Unmounting volatile lrm filesystems") 92 self.unmount_volatile() 93 94 logging.debug("Unpreventing daemons from starting") 95 self.unprevent_daemons_starting()
96
97 - def update(self):
98 self.run_in_target('apt-get', '-y', '--force-yes', 'dist-upgrade')
99
100 - def install_authorized_keys(self):
101 if self.vm.ssh_key: 102 os.mkdir('%s/root/.ssh' % self.destdir, 0700) 103 shutil.copy(self.vm.ssh_key, '%s/root/.ssh/authorized_keys' % self.destdir) 104 os.chmod('%s/root/.ssh/authorized_keys' % self.destdir, 0644) 105 if self.vm.ssh_user_key: 106 os.mkdir('%s/home/%s/.ssh' % (self.destdir, self.vm.user), 0700) 107 shutil.copy(self.vm.ssh_user_key, '%s/home/%s/.ssh/authorized_keys' % (self.destdir, self.vm.user)) 108 os.chmod('%s/home/%s/.ssh/authorized_keys' % (self.destdir, self.vm.user), 0644)
109
110 - def create_initial_user(self):
111 self.run_in_target('adduser', '--disabled-password', '--gecos', self.vm.name, self.vm.user) 112 self.run_in_target('chpasswd', stdin=('%s:%s\n' % (self.vm.user, getattr(self.vm, 'pass')))) 113 self.run_in_target('addgroup', '--system', 'admin') 114 self.run_in_target('adduser', self.vm.user, 'admin') 115 116 self.install_from_template('/etc/sudoers', 'sudoers') 117 for group in ['adm', 'audio', 'cdrom', 'dialout', 'floppy', 'video', 'plugdev', 'dip', 'netdev', 'powerdev', 'lpadmin', 'scanner']: 118 self.run_in_target('adduser', self.vm.user, group, ignore_fail=True) 119 120 # Lock root account 121 self.run_in_target('chpasswd', '-e', stdin='root:!\n')
122
123 - def kernel_name(self):
124 return 'linux-image-%s' % (self.vm.flavour or self.default_flavour[self.vm.arch],)
125
126 - def config_network(self):
127 self.vm.install_file('/etc/hostname', self.vm.hostname) 128 self.install_from_template('/etc/hosts', 'etc_hosts', { 'hostname' : self.vm.hostname, 'domain' : self.vm.domain }) 129 self.install_from_template('/etc/network/interfaces', 'interfaces')
130
132 os.unlink('%s/usr/sbin/policy-rc.d' % self.destdir)
133
134 - def prevent_daemons_starting(self):
135 os.chmod(self.install_from_template('/usr/sbin/policy-rc.d', 'nostart-policy-rc.d'), 0755)
136
137 - def install_extras(self):
138 if not self.vm.addpkg and not self.vm.removepkg: 139 return 140 cmd = ['apt-get', 'install', '-y', '--force-yes'] 141 cmd += self.vm.addpkg or [] 142 cmd += ['%s-' % pkg for pkg in self.vm.removepkg or []] 143 self.run_in_target(env={ 'DEBIAN_FRONTEND' : 'noninteractive' }, *cmd)
144
145 - def unmount_volatile(self):
146 for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.destdir): 147 logging.debug("Unmounting %s" % mntpnt) 148 run_cmd('umount', mntpnt)
149
150 - def install_menu_lst(self):
151 run_cmd('mount', '--bind', '/dev', '%s/dev' % self.destdir) 152 self.vm.add_clean_cmd('umount', '%s/dev' % self.destdir, ignore_fail=True) 153 154 self.run_in_target('mount', '-t', 'proc', 'proc', '/proc') 155 self.vm.add_clean_cmd('umount', '%s/proc' % self.destdir, ignore_fail=True) 156 157 self.run_in_target(self.updategrub, '-y') 158 self.mangle_grub_menu_lst() 159 self.run_in_target(self.updategrub) 160 self.run_in_target('grub-set-default', '0') 161 162 run_cmd('umount', '%s/dev' % self.destdir) 163 run_cmd('umount', '%s/proc' % self.destdir)
164
165 - def mangle_grub_menu_lst(self):
166 bootdev = disk.bootpart(self.vm.disks) 167 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) 168 run_cmd('sed', '-ie', 's/^# groot.*/# groot %s/g' % bootdev.get_grub_id(), '%s/boot/grub/menu.lst' % self.destdir) 169 run_cmd('sed', '-ie', '/^# kopt_2_6/ d', '%s/boot/grub/menu.lst' % self.destdir)
170
171 - def install_sources_list(self):
172 self.install_from_template('/etc/apt/sources.list', 'sources.list') 173 self.run_in_target('apt-get', 'update')
174
175 - def install_fstab(self):
176 if self.vm.hypervisor.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE: 177 self.install_from_template('/etc/fstab', 'dapper_fstab_fsimage', { 'fss' : disk.get_ordered_filesystems(self.vm), 'prefix' : self.disk_prefix }) 178 else: 179 self.install_from_template('/etc/fstab', 'dapper_fstab', { 'parts' : disk.get_ordered_partitions(self.vm.disks), 'prefix' : self.disk_prefix })
180
181 - def install_device_map(self):
182 self.install_from_template('/boot/grub/device.map', 'devicemap', { 'prefix' : self.disk_prefix })
183
184 - def debootstrap(self):
185 cmd = ['/usr/sbin/debootstrap', '--arch=%s' % self.vm.arch, self.vm.suite, self.destdir ] 186 if self.vm.mirror: 187 cmd += [self.vm.mirror] 188 run_cmd(*cmd)
189
190 - def install_kernel(self):
191 self.install_from_template('/etc/kernel-img.conf', 'kernelimg', { 'updategrub' : self.updategrub }) 192 run_cmd('chroot', self.destdir, 'apt-get', '--force-yes', '-y', 'install', self.kernel_name(), 'grub')
193
194 - def install_grub(self):
195 self.run_in_target('apt-get', '--force-yes', '-y', 'install', 'grub') 196 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)
197
198 - def create_devices(self):
199 import VMBuilder.plugins.xen 200 201 if isinstance(self.vm.hypervisor, VMBuilder.plugins.xen.Xen): 202 self.run_in_target('mknod', '/dev/xvda', 'b', '202', '0') 203 self.run_in_target('mknod', '/dev/xvda1', 'b', '202', '1') 204 self.run_in_target('mknod', '/dev/xvda2', 'b', '202', '2') 205 self.run_in_target('mknod', '/dev/xvda3', 'b', '202', '3') 206 self.run_in_target('mknod', '/dev/xvc0', 'c', '204', '191')
207
208 - def install_from_template(self, *args, **kwargs):
209 return self.vm.distro.install_from_template(*args, **kwargs)
210
211 - def run_in_target(self, *args, **kwargs):
212 return run_cmd('chroot', self.destdir, *args, **kwargs)
213
214 - def post_mount(self, fs):
215 if fs.mntpnt == '/': 216 logging.debug("Creating /var/run in root filesystem") 217 os.makedirs('%s/var/run' % fs.mntpath) 218 logging.debug("Creating /var/lock in root filesystem") 219 os.makedirs('%s/var/lock' % fs.mntpath)
220