1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import glob
20 import logging
21 import os
22 import suite
23 import shutil
24 import socket
25 import tempfile
26 import VMBuilder
27 import VMBuilder.disk as disk
28 from VMBuilder.util import run_cmd
29
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 chpasswd_cmd = [ 'chpasswd', '--md5' ]
40
43
46
49
136
138 self.run_in_target('apt-get', '-y', '--force-yes', 'dist-upgrade',
139 env={ 'DEBIAN_FRONTEND' : 'noninteractive' })
140
142 if self.vm.ssh_key:
143 os.mkdir('%s/root/.ssh' % self.destdir, 0700)
144 shutil.copy(self.vm.ssh_key, '%s/root/.ssh/authorized_keys' % self.destdir)
145 os.chmod('%s/root/.ssh/authorized_keys' % self.destdir, 0644)
146 if self.vm.ssh_user_key:
147 os.mkdir('%s/home/%s/.ssh' % (self.destdir, self.vm.user), 0700)
148 shutil.copy(self.vm.ssh_user_key, '%s/home/%s/.ssh/authorized_keys' % (self.destdir, self.vm.user))
149 os.chmod('%s/home/%s/.ssh/authorized_keys' % (self.destdir, self.vm.user), 0644)
150 self.run_in_target('chown', '-R', '%s:%s' % (self.vm.user,)*2, '/home/%s/.ssh/' % (self.vm.user))
151
152 if self.vm.ssh_user_key or self.vm.ssh_key:
153 if not self.vm.addpkg:
154 self.vm.addpkg = []
155 self.vm.addpkg += ['openssh-server']
156
158 run_cmd('mount', '--bind', '/dev', '%s/dev' % self.destdir)
159 self.vm.add_clean_cmd('umount', '%s/dev' % self.destdir, ignore_fail=True)
160
161 run_cmd('mount', '--bind', '/dev/pts', '%s/dev/pts' % self.destdir)
162 self.vm.add_clean_cmd('umount', '%s/dev/pts' % self.destdir, ignore_fail=True)
163
164 self.run_in_target('mount', '-t', 'proc', 'proc', '/proc')
165 self.vm.add_clean_cmd('umount', '%s/proc' % self.destdir, ignore_fail=True)
166
168 run_cmd('umount', '%s/dev/pts' % self.destdir)
169 run_cmd('umount', '%s/dev' % self.destdir)
170 run_cmd('umount', '%s/proc' % self.destdir)
171
185
187 if self.vm.uid:
188 self.run_in_target('adduser', '--disabled-password', '--uid', self.vm.uid, '--gecos', self.vm.name, self.vm.user)
189 else:
190 self.run_in_target('adduser', '--disabled-password', '--gecos', self.vm.name, self.vm.user)
191 self.run_in_target('addgroup', '--system', 'admin')
192 self.run_in_target('adduser', self.vm.user, 'admin')
193
194 self.install_from_template('/etc/sudoers', 'sudoers')
195 for group in ['adm', 'audio', 'cdrom', 'dialout', 'floppy', 'video', 'plugdev', 'dip', 'netdev', 'powerdev', 'lpadmin', 'scanner']:
196 self.run_in_target('adduser', self.vm.user, group, ignore_fail=True)
197
198 self.update_passwords()
199
202
207
209 os.unlink('%s/usr/sbin/policy-rc.d' % self.destdir)
210
213
215 if not self.vm.addpkg and not self.vm.removepkg:
216 return
217 cmd = ['apt-get', 'install', '-y', '--force-yes']
218 cmd += self.vm.addpkg or []
219 cmd += ['%s-' % pkg for pkg in self.vm.removepkg or []]
220 self.run_in_target(env={ 'DEBIAN_FRONTEND' : 'noninteractive' }, *cmd)
221
223 for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.destdir):
224 logging.debug("Unmounting %s" % mntpnt)
225 run_cmd('umount', mntpnt)
226
232
234 bootdev = disk.bootpart(self.vm.disks)
235 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)
236 run_cmd('sed', '-ie', 's/^# groot.*/# groot %s/g' % bootdev.get_grub_id(), '%s/boot/grub/menu.lst' % self.destdir)
237 run_cmd('sed', '-ie', '/^# kopt_2_6/ d', '%s/boot/grub/menu.lst' % self.destdir)
238
240 if final:
241 mirror, updates_mirror, security_mirror = self.vm.mirror, self.vm.mirror, self.vm.security_mirror
242 else:
243 mirror, updates_mirror, security_mirror = self.install_mirrors()
244
245 self.install_from_template('/etc/apt/sources.list', 'sources.list', { 'mirror' : mirror, 'security_mirror' : security_mirror, 'updates_mirror' : updates_mirror })
246
247
248
249
250 self.run_in_target('apt-get', 'update', ignore_fail=final)
251
253 if self.vm.proxy is not None:
254 self.vm.install_file('/etc/apt/apt.conf', '// Proxy added by vmbuilder\nAcquire::http { Proxy "%s"; };' % self.vm.proxy)
255
261
264
266 cmd = ['/usr/sbin/debootstrap', '--arch=%s' % self.vm.arch]
267 if self.vm.variant:
268 cmd += ['--variant=%s' % self.vm.variant]
269 cmd += [self.vm.suite, self.destdir, self.debootstrap_mirror()]
270 kwargs = { 'env' : { 'DEBIAN_FRONTEND' : 'noninteractive' } }
271 if self.vm.proxy:
272 kwargs['env']['http_proxy'] = self.vm.proxy
273 run_cmd(*cmd, **kwargs)
274
276 if self.vm.iso:
277 isodir = tempfile.mkdtemp()
278 self.vm.add_clean_cb(lambda:os.rmdir(isodir))
279 run_cmd('mount', '-o', 'loop', '-t', 'iso9660', self.vm.iso, isodir)
280 self.vm.add_clean_cmd('umount', isodir)
281 self.iso_mounted = True
282
283 return 'file://%s' % isodir
284 else:
285 return self.install_mirrors()[0]
286
287
289 if self.vm.install_mirror:
290 mirror = self.vm.install_mirror
291 else:
292 mirror = self.vm.mirror
293
294 if self.vm.install_mirror:
295 updates_mirror = self.vm.install_mirror
296 else:
297 updates_mirror = self.vm.mirror
298
299 if self.vm.install_security_mirror:
300 security_mirror = self.vm.install_security_mirror
301 else:
302 security_mirror = self.vm.security_mirror
303
304 return (mirror, updates_mirror, security_mirror)
305
309
311 self.run_in_target('apt-get', '--force-yes', '-y', 'install', 'grub')
312 run_cmd('rsync', '-a', '%s%s/%s/' % (self.destdir, self.grubroot, self.vm.arch == 'amd64' and 'x86_64-pc' or 'i386-pc'), '%s/boot/grub/' % self.destdir)
313
315 import VMBuilder.plugins.xen
316
317 if isinstance(self.vm.hypervisor, VMBuilder.plugins.xen.Xen):
318 self.run_in_target('mknod', '/dev/xvda', 'b', '202', '0')
319 self.run_in_target('mknod', '/dev/xvda1', 'b', '202', '1')
320 self.run_in_target('mknod', '/dev/xvda2', 'b', '202', '2')
321 self.run_in_target('mknod', '/dev/xvda3', 'b', '202', '3')
322 self.run_in_target('mknod', '/dev/xvc0', 'c', '204', '191')
323
326
329
331 logging.debug("Copying %s on host to %s in guest" % (infile, destpath))
332 dir = '%s/%s' % (self.destdir, os.path.dirname(destpath))
333 if not os.path.isdir(dir):
334 os.makedirs(dir)
335 if os.path.isdir(infile):
336 shutil.copytree(infile, '%s/%s' % (self.destdir, destpath))
337 else:
338 shutil.copy(infile, '%s/%s' % (self.destdir, destpath))
339
340 - def post_mount(self, fs):
341 if fs.mntpnt == '/':
342 logging.debug("Creating /var/run in root filesystem")
343 os.makedirs('%s/var/run' % fs.mntpath)
344 logging.debug("Creating /var/lock in root filesystem")
345 os.makedirs('%s/var/lock' % fs.mntpath)
346
348 if os.path.exists('/etc/default/locale'):
349 self.copy_to_target('/etc/default/locale', '/etc/default/locale')
350 self.run_in_target('dpkg-reconfigure', '-fnoninteractive', '-pcritical', 'libc6')
351 self.run_in_target('locale-gen', 'en_US')
352 if self.vm.lang:
353 self.run_in_target('locale-gen', self.vm.lang)
354 self.install_from_template('/etc/default/locale', 'locale', { 'lang' : self.vm.lang })
355 self.run_in_target('dpkg-reconfigure', '-fnoninteractive', '-pcritical', 'locales')
356 self.run_in_target('dpkg-reconfigure', '-pcritical', 'locales')
357
359 shutil.copy(logfile, '%s/var/log/vmbuilder-install.log' % (rootdir,))
360
362 if self.vm.timezone:
363 os.unlink('%s/etc/localtime' % self.destdir)
364 shutil.copy('%s/usr/share/zoneinfo/%s' % (self.destdir, self.vm.timezone), '%s/etc/localtime' % (self.destdir,))
365
367 if self.vm.ec2:
368 logging.debug('This suite does not support ec2')
369
371 fp = open('%s/etc/default/rcS' % self.destdir, 'a')
372 fp.write('HWCLOCKACCESS=no')
373 fp.close()
374