105
61
logging.debug("Setting timezone")
106
62
self.set_timezone()
108
logging.debug("Making sure system is up-to-date")
111
logging.debug("Setting up final sources.list")
112
self.install_sources_list(final=True)
114
logging.debug("cleaning apt")
115
self.run_in_target('apt-get', 'clean');
117
logging.debug("Unmounting volatile lrm filesystems")
118
self.unmount_volatile()
120
logging.debug('Unbinding /dev and /proc filesystems')
121
self.unmount_dev_proc()
123
if hasattr(self.vm, 'ec2') and self.vm.ec2:
64
if hasattr(self.context, 'ec2') and self.context.ec2:
124
65
logging.debug("Configuring for ec2")
125
66
self.install_ec2()
127
logging.debug("Unpreventing daemons from starting")
128
self.unprevent_daemons_starting()
68
def create_manifest(self):
69
manifest = self.context.get_setting('manifest')
131
71
logging.debug("Creating manifest")
132
72
manifest_contents = self.run_in_target('dpkg-query', '-W', '--showformat=${Package} ${Version}\n')
133
fp = open(self.vm.manifest, 'w')
73
fp = open(manifest, 'w')
134
74
fp.write(manifest_contents)
76
self.call_hook('fix_ownership', manifest)
138
79
self.run_in_target('apt-get', '-y', '--force-yes', 'dist-upgrade',
139
80
env={ 'DEBIAN_FRONTEND' : 'noninteractive' })
141
82
def install_authorized_keys(self):
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))
152
if self.vm.ssh_user_key or self.vm.ssh_key:
153
if not self.vm.addpkg:
155
self.vm.addpkg += ['openssh-server']
83
ssh_key = self.context.get_setting('ssh-key')
85
os.mkdir('%s/root/.ssh' % self.context.chroot_dir, 0700)
86
shutil.copy(ssh_key, '%s/root/.ssh/authorized_keys' % self.context.chroot_dir)
87
os.chmod('%s/root/.ssh/authorized_keys' % self.context.chroot_dir, 0644)
89
user = self.context.get_setting('user')
90
ssh_user_key = self.context.get_setting('ssh-user-key')
92
os.mkdir('%s/home/%s/.ssh' % (self.context.chroot_dir, user), 0700)
93
shutil.copy(ssh_user_key, '%s/home/%s/.ssh/authorized_keys' % (self.context.chroot_dir, user))
94
os.chmod('%s/home/%s/.ssh/authorized_keys' % (self.context.chroot_dir, user), 0644)
95
self.run_in_target('chown', '-R', '%s:%s' % ((user,)*2), '/home/%s/.ssh/' % (user))
97
if ssh_user_key or ssh_key:
98
addpkg = self.context.get_setting('addpkg')
99
addpkg += ['openssh-server']
100
self.context.set_setting('addpkg', addpkg)
157
102
def mount_dev_proc(self):
158
run_cmd('mount', '--bind', '/dev', '%s/dev' % self.destdir)
159
self.vm.add_clean_cmd('umount', '%s/dev' % self.destdir, ignore_fail=True)
103
run_cmd('mount', '--bind', '/dev', '%s/dev' % self.context.chroot_dir)
104
self.context.add_clean_cb(self.unmount_dev)
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)
106
run_cmd('mount', '--bind', '/dev/pts', '%s/dev/pts' % self.context.chroot_dir)
107
self.context.add_clean_cb(self.unmount_dev_pts)
164
109
self.run_in_target('mount', '-t', 'proc', 'proc', '/proc')
165
self.vm.add_clean_cmd('umount', '%s/proc' % self.destdir, ignore_fail=True)
167
def unmount_dev_proc(self):
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)
110
self.context.add_clean_cb(self.unmount_proc)
112
def unmount_proc(self):
113
self.context.cancel_cleanup(self.unmount_proc)
114
run_cmd('umount', '%s/proc' % self.context.chroot_dir)
116
def unmount_dev_pts(self):
117
self.context.cancel_cleanup(self.unmount_dev_pts)
118
run_cmd('umount', '%s/dev/pts' % self.context.chroot_dir)
120
def unmount_dev(self):
121
self.context.cancel_cleanup(self.unmount_dev)
122
run_cmd('umount', '%s/dev' % self.context.chroot_dir)
172
124
def update_passwords(self):
173
125
# Set the user password, using md5
174
self.run_in_target(stdin=('%s:%s\n' % (self.vm.user, getattr(self.vm, 'pass'))), *self.chpasswd_cmd)
126
user = self.context.get_setting('user')
127
passwd = self.context.get_setting('pass')
128
self.run_in_target(stdin=('%s:%s\n' % (user, passwd)), *self.chpasswd_cmd)
176
130
# Lock root account only if we didn't set the root password
178
self.run_in_target(stdin=('%s:%s\n' % ('root', self.vm.rootpass)), *self.chpasswd_cmd)
131
rootpass = self.context.get_setting('rootpass')
133
self.run_in_target(stdin=('%s:%s\n' % ('root', rootpass)), *self.chpasswd_cmd)
180
135
self.run_in_target('usermod', '-L', 'root')
182
if self.vm.lock_user:
183
logging.info('Locking %s' %(self.vm.user))
184
self.run_in_target('usermod', '-L', self.vm.user)
137
lock_user = self.context.get_setting('lock-user')
139
logging.info('Locking %s' % (user, ))
140
self.run_in_target('usermod', '-L', user)
186
142
def create_initial_user(self):
188
self.run_in_target('adduser', '--disabled-password', '--uid', self.vm.uid, '--gecos', self.vm.name, self.vm.user)
143
uid = self.context.get_setting('uid')
144
name = self.context.get_setting('name')
145
user = self.context.get_setting('user')
147
self.run_in_target('adduser', '--disabled-password', '--uid', uid, '--gecos', name, user)
190
self.run_in_target('adduser', '--disabled-password', '--gecos', self.vm.name, self.vm.user)
149
self.run_in_target('adduser', '--disabled-password', '--gecos', name, user)
191
151
self.run_in_target('addgroup', '--system', 'admin')
192
self.run_in_target('adduser', self.vm.user, 'admin')
152
self.run_in_target('adduser', user, 'admin')
194
154
self.install_from_template('/etc/sudoers', 'sudoers')
195
155
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)
156
self.run_in_target('adduser', user, group, ignore_fail=True)
198
158
self.update_passwords()
200
160
def kernel_name(self):
201
return 'linux-image-%s' % (self.vm.flavour or self.default_flavour[self.vm.arch],)
203
def config_network(self):
204
self.vm.install_file('/etc/hostname', self.vm.hostname)
205
self.install_from_template('/etc/hosts', 'etc_hosts', { 'hostname' : self.vm.hostname, 'domain' : self.vm.domain })
206
self.install_from_template('/etc/network/interfaces', 'interfaces')
161
flavour = self.context.get_setting('flavour')
162
arch = self.context.get_setting('arch')
163
return 'linux-image-%s' % (flavour or self.default_flavour[arch],)
165
def config_host_and_domainname(self):
166
hostname = self.context.get_setting('hostname')
167
domain = self.context.get_setting('domain')
168
self.context.install_file('/etc/hostname', hostname)
169
self.install_from_template('/etc/hosts', 'etc_hosts', { 'hostname' : hostname, 'domain' : domain })
171
def config_interfaces(self, nics):
172
self.install_from_template('/etc/network/interfaces', 'interfaces', { 'ip' : nics[0].type == 'dhcp' and 'dhcp' or nics[0].ip })
208
174
def unprevent_daemons_starting(self):
209
os.unlink('%s/usr/sbin/policy-rc.d' % self.destdir)
175
os.unlink('%s/usr/sbin/policy-rc.d' % self.context.chroot_dir)
211
177
def prevent_daemons_starting(self):
212
178
os.chmod(self.install_from_template('/usr/sbin/policy-rc.d', 'nostart-policy-rc.d'), 0755)
214
180
def install_extras(self):
215
if not self.vm.addpkg and not self.vm.removepkg:
181
addpkg = self.context.get_setting('addpkg')
182
removepkg = self.context.get_setting('removepkg')
183
if not addpkg and not removepkg:
217
185
cmd = ['apt-get', 'install', '-y', '--force-yes']
218
cmd += self.vm.addpkg or []
219
cmd += ['%s-' % pkg for pkg in self.vm.removepkg or []]
187
cmd += ['%s-' % pkg for pkg in removepkg or []]
220
188
self.run_in_target(env={ 'DEBIAN_FRONTEND' : 'noninteractive' }, *cmd)
222
190
def unmount_volatile(self):
223
for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.destdir):
191
for mntpnt in glob.glob('%s/lib/modules/*/volatile' % self.context.chroot_dir):
224
192
logging.debug("Unmounting %s" % mntpnt)
225
193
run_cmd('umount', mntpnt)
227
def install_menu_lst(self):
195
def install_menu_lst(self, disks):
228
196
self.run_in_target(self.updategrub, '-y')
229
self.mangle_grub_menu_lst()
197
self.mangle_grub_menu_lst(disks)
230
198
self.run_in_target(self.updategrub)
231
199
self.run_in_target('grub-set-default', '0')
233
def mangle_grub_menu_lst(self):
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)
201
def mangle_grub_menu_lst(self, disks):
202
bootdev = disk.bootpart(disks)
203
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.context.chroot_dir)
204
run_cmd('sed', '-ie', 's/^# groot.*/# groot %s/g' % bootdev.get_grub_id(), '%s/boot/grub/menu.lst' % self.context.chroot_dir)
205
run_cmd('sed', '-ie', '/^# kopt_2_6/ d', '%s/boot/grub/menu.lst' % self.context.chroot_dir)
239
207
def install_sources_list(self, final=False):
241
mirror, updates_mirror, security_mirror = self.vm.mirror, self.vm.mirror, self.vm.security_mirror
209
mirror = updates_mirror = self.context.get_setting('mirror')
210
security_mirror = self.context.get_setting('security-mirror')
243
212
mirror, updates_mirror, security_mirror = self.install_mirrors()
245
self.install_from_template('/etc/apt/sources.list', 'sources.list', { 'mirror' : mirror, 'security_mirror' : security_mirror, 'updates_mirror' : updates_mirror })
214
components = self.context.get_setting('components')
215
ppa = self.context.get_setting('ppa')
216
suite = self.context.get_setting('suite')
217
self.install_from_template('/etc/apt/sources.list', 'sources.list', { 'mirror' : mirror,
218
'security_mirror' : security_mirror,
219
'updates_mirror' : updates_mirror,
220
'components' : components,
247
224
# If setting up the final mirror, allow apt-get update to fail
248
225
# (since we might be on a complete different network than the
250
227
self.run_in_target('apt-get', 'update', ignore_fail=final)
252
229
def install_apt_proxy(self):
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)
230
proxy = self.context.get_setting('proxy')
231
if proxy is not None:
232
self.context.install_file('/etc/apt/apt.conf', '// Proxy added by vmbuilder\nAcquire::http { Proxy "%s"; };' % proxy)
256
def install_fstab(self):
257
if self.vm.hypervisor.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE:
258
self.install_from_template('/etc/fstab', 'dapper_fstab_fsimage', { 'fss' : disk.get_ordered_filesystems(self.vm), 'prefix' : self.disk_prefix })
260
self.install_from_template('/etc/fstab', 'dapper_fstab', { 'parts' : disk.get_ordered_partitions(self.vm.disks), 'prefix' : self.disk_prefix })
234
def install_fstab(self, disks, filesystems):
235
self.install_from_template('/etc/fstab', 'dapper_fstab', { 'parts' : disk.get_ordered_partitions(disks), 'prefix' : self.disk_prefix })
262
237
def install_device_map(self):
263
238
self.install_from_template('/boot/grub/device.map', 'devicemap', { 'prefix' : self.disk_prefix })
265
240
def debootstrap(self):
266
cmd = ['/usr/sbin/debootstrap', '--arch=%s' % self.vm.arch]
268
cmd += ['--variant=%s' % self.vm.variant]
269
cmd += [self.vm.suite, self.destdir, self.debootstrap_mirror()]
241
arch = self.context.get_setting('arch')
242
cmd = ['/usr/sbin/debootstrap', '--arch=%s' % arch]
244
variant = self.context.get_setting('variant')
246
cmd += ['--variant=%s' % variant]
248
suite = self.context.get_setting('suite')
249
cmd += [suite, self.context.chroot_dir, self.debootstrap_mirror()]
270
250
kwargs = { 'env' : { 'DEBIAN_FRONTEND' : 'noninteractive' } }
272
kwargs['env']['http_proxy'] = self.vm.proxy
252
proxy = self.context.get_setting('proxy')
254
kwargs['env']['http_proxy'] = proxy
273
255
run_cmd(*cmd, **kwargs)
275
257
def debootstrap_mirror(self):
258
iso = self.context.get_setting('iso')
277
260
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)
261
self.context.add_clean_cb(lambda:os.rmdir(isodir))
262
run_cmd('mount', '-o', 'loop', '-t', 'iso9660', iso, isodir)
263
self.context.add_clean_cmd('umount', isodir)
281
264
self.iso_mounted = True
283
266
return 'file://%s' % isodir
288
271
def install_mirrors(self):
289
if self.vm.install_mirror:
290
mirror = self.vm.install_mirror
292
mirror = self.vm.mirror
294
if self.vm.install_mirror:
295
updates_mirror = self.vm.install_mirror
297
updates_mirror = self.vm.mirror
299
if self.vm.install_security_mirror:
300
security_mirror = self.vm.install_security_mirror
302
security_mirror = self.vm.security_mirror
272
install_mirror = self.context.get_setting('install-mirror')
274
mirror = install_mirror
276
mirror = self.context.get_setting('mirror')
278
updates_mirror = mirror
280
install_security_mirror = self.context.get_setting('install-security-mirror')
281
if install_security_mirror:
282
security_mirror = install_security_mirror
284
security_mirror = self.context.get_setting('security-mirror')
304
286
return (mirror, updates_mirror, security_mirror)
306
def install_kernel(self):
288
def install_kernel(self, destdir):
307
289
self.install_from_template('/etc/kernel-img.conf', 'kernelimg', { 'updategrub' : self.updategrub })
308
run_cmd('chroot', self.destdir, 'apt-get', '--force-yes', '-y', 'install', self.kernel_name(), 'grub')
290
run_cmd('chroot', destdir, 'apt-get', '--force-yes', '-y', 'install', self.kernel_name(), 'grub')
310
def install_grub(self):
292
def install_grub(self, chroot_dir):
293
arch = self.context.get_setting('arch')
311
294
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)
295
run_cmd('rsync', '-a', '%s%s/%s/' % (chroot_dir, self.grubroot, arch == 'amd64' and 'x86_64-pc' or 'i386-pc'), '%s/boot/grub/' % chroot_dir)
314
297
def create_devices(self):
315
import VMBuilder.plugins.xen
300
# import VMBuilder.plugins.xen
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')
302
# if isinstance(self.context.hypervisor, VMBuilder.plugins.xen.Xen):
303
# self.run_in_target('mknod', '/dev/xvda', 'b', '202', '0')
304
# self.run_in_target('mknod', '/dev/xvda1', 'b', '202', '1')
305
# self.run_in_target('mknod', '/dev/xvda2', 'b', '202', '2')
306
# self.run_in_target('mknod', '/dev/xvda3', 'b', '202', '3')
307
# self.run_in_target('mknod', '/dev/xvc0', 'c', '204', '191')
324
309
def install_from_template(self, *args, **kwargs):
325
return self.vm.distro.install_from_template(*args, **kwargs)
310
return self.context.install_from_template(*args, **kwargs)
327
312
def run_in_target(self, *args, **kwargs):
328
return self.vm.distro.run_in_target(*args, **kwargs)
313
return self.context.run_in_target(*args, **kwargs)
330
315
def copy_to_target(self, infile, destpath):
331
316
logging.debug("Copying %s on host to %s in guest" % (infile, destpath))