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 xen_kernel_flavour = None
38 virtio_net = False
39
42
45
104
106 self.run_in_target('apt-get', '-y', '--force-yes', 'dist-upgrade')
107
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
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
133 self.run_in_target('chpasswd', '-e', stdin='root:!\n')
134
137
142
144 os.unlink('%s/usr/sbin/policy-rc.d' % self.destdir)
145
148
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
158 for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.destdir):
159 logging.debug("Unmounting %s" % mntpnt)
160 run_cmd('umount', mntpnt)
161
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
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
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
192
193
194 self.run_in_target('apt-get', 'update', ignore_fail=final)
195
201
204
208
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
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
245
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
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
262
265
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
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