1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
40
43
96
98 self.run_in_target('apt-get', '-y', '--force-yes', 'dist-upgrade')
99
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
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
121 self.run_in_target('chpasswd', stdin='root:!\n')
122
125
130
132 os.unlink('%s/usr/sbin/policy-rc.d' % self.destdir)
133
136
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
146 for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.destdir):
147 logging.debug("Unmounting %s" % mntpnt)
148 run_cmd('umount', mntpnt)
149
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
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
174
180
183
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
193
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
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
210
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