]>
git.siccegge.de Git - forks/vmdebootstrap.git/blob - vmdebootstrap
fafd78ab4c9ec6590a115026c763ac9a23e8bb05
2 # Copyright 2011-2013 Lars Wirzenius
3 # Copyright 2012 Codethink Limited
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class VmDebootstrap(cliapp
.Application
):
34 def add_settings(self
):
35 default_arch
= subprocess
.check_output(
36 ["dpkg", "--print-architecture"]).strip()
38 self
.settings
.boolean(['verbose'], 'report what is going on')
39 self
.settings
.string(['image'], 'put created disk image in FILE',
41 self
.settings
.bytesize(['size'],
42 'create a disk image of size SIZE (%default)',
45 self
.settings
.bytesize(['bootsize'],
46 'create boot partition of size SIZE (%default)',
49 self
.settings
.string(['boottype'],
50 'specify file system type for /boot/',
52 self
.settings
.string(['foreign'],
53 'set up foreign debootstrap environment using provided program (ie binfmt handler)')
54 self
.settings
.string(['variant'],
55 'select debootstrap variant it not using the default')
56 self
.settings
.boolean(
60 self
.settings
.string(['tarball'], "tar up the disk's contents in FILE",
62 self
.settings
.string(['mirror'],
63 'use MIRROR as package source (%default)',
65 default
='http://cdn.debian.net/debian/')
66 self
.settings
.string(['arch'], 'architecture to use (%default)',
69 self
.settings
.string(['distribution'],
70 'release to use (%default)',
73 self
.settings
.string_list(['package'], 'install PACKAGE onto system')
74 self
.settings
.string_list(['custom-package'],
75 'install package in DEB file onto system '
78 self
.settings
.boolean(['no-kernel'], 'do not install a linux package')
79 self
.settings
.boolean(['enable-dhcp'], 'enable DHCP on eth0')
80 self
.settings
.string(['root-password'], 'set root password',
82 self
.settings
.boolean(['lock-root-password'],
83 'lock root account so they cannot login?')
84 self
.settings
.string(['customize'],
85 'run SCRIPT after setting up system',
87 self
.settings
.string(['hostname'],
88 'set name to HOSTNAME (%default)',
91 self
.settings
.string_list(['user'],
92 'create USER with PASSWORD',
93 metavar
='USER/PASSWORD')
94 self
.settings
.boolean(['serial-console'],
95 'configure image to use a serial console')
96 self
.settings
.string(['serial-console-command'],
97 'command to manage the serial console, appended '
98 'to /etc/inittab (%default)',
100 default
='/sbin/getty -L ttyS0 115200 vt100')
101 self
.settings
.boolean(['sudo'],
102 'install sudo, and if user is created, add them '
104 self
.settings
.string(['owner'],
105 'the user who will own the image when the build '
107 self
.settings
.boolean(['squash'],
108 'use squashfs on the final image.')
109 self
.settings
.boolean(['configure-apt'],
110 'Create an apt source based on the distribution '
111 'and mirror selected.')
112 self
.settings
.boolean(['mbr'],
113 'Run install-mbr (no longer done by default)')
114 self
.settings
.boolean(['grub'],
115 'Install and configure grub2 - disables '
118 def process_args(self
, args
):
119 if not self
.settings
['image'] and not self
.settings
['tarball']:
120 raise cliapp
.AppException('You must give disk image filename, '
121 'or tarball filename')
122 if self
.settings
['image'] and not self
.settings
['size']:
123 raise cliapp
.AppException('If disk image is specified, '
124 'You must give image size.')
126 self
.remove_dirs
= []
127 self
.mount_points
= []
135 if self
.settings
['image']:
136 self
.create_empty_image()
137 self
.partition_image()
138 if self
.settings
['mbr']:
140 (rootdev
, bootdev
) = self
.setup_kpartx()
141 self
.mkfs(rootdev
, type=roottype
)
142 rootdir
= self
.mount(rootdev
)
144 if self
.settings
['boottype']:
145 boottype
= self
.settings
['boottype']
148 self
.mkfs(bootdev
, type=boottype
)
149 bootdir
= '%s/%s' % (rootdir
, 'boot/')
151 bootdir
= self
.mount(bootdev
, bootdir
)
153 rootdir
= self
.mkdtemp()
154 self
.debootstrap(rootdir
)
155 self
.set_hostname(rootdir
)
156 self
.create_fstab(rootdir
, rootdev
, roottype
, bootdev
, boottype
)
157 self
.install_debs(rootdir
)
158 self
.cleanup_apt_cache(rootdir
)
159 self
.set_root_password(rootdir
)
160 self
.create_users(rootdir
)
161 self
.remove_udev_persistent_rules(rootdir
)
162 self
.setup_networking(rootdir
)
163 if self
.settings
['configure-apt']:
164 self
.configure_apt(rootdir
)
165 self
.customize(rootdir
)
166 if self
.settings
['image']:
167 if self
.settings
['grub']:
168 self
.install_grub2(rootdev
, rootdir
)
169 elif self
.settings
['extlinux']:
170 self
.install_extlinux(rootdev
, rootdir
)
171 self
.append_serial_console(rootdir
)
172 self
.optimize_image(rootdir
)
173 if self
.settings
['squash']:
176 if self
.settings
['foreign']:
177 os
.unlink('%s/usr/bin/%s' %
178 (rootdir
, os
.path
.basename(self
.settings
['foreign'])))
180 if self
.settings
['tarball']:
181 self
.create_tarball(rootdir
)
183 if self
.settings
['owner']:
185 except BaseException
, e
:
186 self
.message('EEEK! Something bad happened...')
188 db_log
= os
.path
.join(rootdir
, 'debootstrap', 'debootstrap.log')
189 if os
.path
.exists(db_log
):
190 shutil
.copy(db_log
, os
.getcwd())
192 self
.cleanup_system()
195 self
.cleanup_system()
197 def message(self
, msg
):
199 if self
.settings
['verbose']:
202 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, env
=None, **kwargs
):
203 logging
.debug('runcmd: %s %s %s' % (argv
, env
, kwargs
))
204 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
205 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
207 out
, err
= p
.communicate(stdin
)
208 if p
.returncode
!= 0:
209 msg
= 'command failed: %s\n%s\n%s' % (argv
, out
, err
)
212 raise cliapp
.AppException(msg
)
216 dirname
= tempfile
.mkdtemp()
217 self
.remove_dirs
.append(dirname
)
218 logging
.debug('mkdir %s' % dirname
)
221 def mount(self
, device
, path
=None):
223 mount_point
= self
.mkdtemp()
226 self
.message('Mounting %s on %s' % (device
, mount_point
))
227 self
.runcmd(['mount', device
, mount_point
])
228 self
.mount_points
.append(mount_point
)
229 logging
.debug('mounted %s on %s' % (device
, mount_point
))
232 def create_empty_image(self
):
233 self
.message('Creating disk image')
234 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
235 self
.settings
['image'],
236 str(self
.settings
['size'])])
238 def partition_image(self
):
239 self
.message('Creating partitions')
240 self
.runcmd(['parted', '-s', self
.settings
['image'],
242 if self
.settings
['bootsize'] and self
.settings
['bootsize'] is not '0%':
243 bootsize
= str(self
.settings
['bootsize'] / (1024 * 1024))
244 self
.runcmd(['parted', '-s', self
.settings
['image'],
245 'mkpart', 'primary', 'fat16', '0', bootsize
])
248 self
.runcmd(['parted', '-s', self
.settings
['image'],
249 'mkpart', 'primary', bootsize
, '100%'])
250 self
.runcmd(['parted', '-s', self
.settings
['image'],
251 'set', '1', 'boot', 'on'])
253 def install_mbr(self
):
254 if os
.path
.exists("/sbin/install-mbr"):
255 self
.message('Installing MBR')
256 self
.runcmd(['install-mbr', self
.settings
['image']])
258 def setup_kpartx(self
):
259 out
= self
.runcmd(['kpartx', '-avs', self
.settings
['image']])
260 if self
.settings
['bootsize']:
268 devices
= [line
.split()[2]
269 for line
in out
.splitlines()
270 if line
.startswith('add map ')]
271 if len(devices
) != parts
:
272 raise cliapp
.AppException('Surprising number of partitions')
273 root
= '/dev/mapper/%s' % devices
[rootindex
]
274 if self
.settings
['bootsize']:
275 boot
= '/dev/mapper/%s' % devices
[bootindex
]
278 def mkfs(self
, device
, type):
279 self
.message('Creating filesystem %s' % type)
280 self
.runcmd(['mkfs', '-t', type, device
])
282 def debootstrap(self
, rootdir
):
283 self
.message('Debootstrapping')
285 if self
.settings
['foreign']:
286 necessary_packages
= []
288 necessary_packages
= ['acpid']
290 if self
.settings
['grub']:
291 necessary_packages
.append('grub2')
293 include
= self
.settings
['package']
295 if not self
.settings
['no-kernel']:
296 if self
.settings
['arch'] == 'i386':
299 kernel_arch
= self
.settings
['arch']
300 kernel_image
= 'linux-image-%s' % kernel_arch
301 include
.append(kernel_image
)
303 if self
.settings
['sudo'] and 'sudo' not in include
:
304 include
.append('sudo')
306 args
= ['debootstrap', '--arch=%s' % self
.settings
['arch']]
307 if self
.settings
['package'] and len(necessary_packages
) > 0:
309 '--include=%s' % ','.join(necessary_packages
+ include
))
310 if self
.settings
['foreign']:
311 args
.append('--foreign')
312 if self
.settings
['variant']:
313 args
.append('--variant')
314 args
.append(self
.settings
['variant'])
315 args
+= [self
.settings
['distribution'],
316 rootdir
, self
.settings
['mirror']]
317 logging
.debug(" ".join(args
))
319 if self
.settings
['foreign']:
320 # set a noninteractive debconf environment for secondstage
322 "DEBIAN_FRONTEND": "noninteractive",
323 "DEBCONF_NONINTERACTIVE_SEEN": "true",
326 # add the mapping to the complete environment.
327 env
.update(os
.environ
)
328 # First copy the binfmt handler over
329 self
.message('Setting up binfmt handler')
330 shutil
.copy(self
.settings
['foreign'], '%s/usr/bin/' % rootdir
)
331 # Next, run the package install scripts etc.
332 self
.message('Running debootstrap second stage')
333 self
.runcmd(['chroot', rootdir
,
334 '/debootstrap/debootstrap', '--second-stage'],
337 def set_hostname(self
, rootdir
):
338 hostname
= self
.settings
['hostname']
339 with
open(os
.path
.join(rootdir
, 'etc', 'hostname'), 'w') as f
:
340 f
.write('%s\n' % hostname
)
342 etc_hosts
= os
.path
.join(rootdir
, 'etc', 'hosts')
344 with
open(etc_hosts
, 'r') as f
:
346 with
open(etc_hosts
, 'w') as f
:
347 for line
in data
.splitlines():
348 if line
.startswith('127.0.0.1'):
349 line
+= ' %s' % hostname
350 f
.write('%s\n' % line
)
354 def create_fstab(self
, rootdir
, rootdev
, roottype
, bootdev
, boottype
):
356 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
357 '-s', 'UUID', device
])
358 return out
.splitlines()[0].strip()
361 rootdevstr
= 'UUID=%s' % fsuuid(rootdev
)
363 rootdevstr
= '/dev/sda1'
366 bootdevstr
= 'UUID=%s' % fsuuid(bootdev
)
370 fstab
= os
.path
.join(rootdir
, 'etc', 'fstab')
371 with
open(fstab
, 'w') as f
:
372 f
.write('proc /proc proc defaults 0 0\n')
373 f
.write('%s / %s errors=remount-ro 0 1\n' % (rootdevstr
, roottype
))
375 f
.write('%s /boot %s errors=remount-ro 0 2\n' % (bootdevstr
, boottype
))
377 def install_debs(self
, rootdir
):
378 if not self
.settings
['custom-package']:
380 self
.message('Installing custom packages')
381 tmp
= os
.path
.join(rootdir
, 'tmp', 'install_debs')
383 for deb
in self
.settings
['custom-package']:
384 shutil
.copy(deb
, tmp
)
385 filenames
= [os
.path
.join('/tmp/install_debs', os
.path
.basename(deb
))
386 for deb
in self
.settings
['custom-package']]
388 self
.runcmd_unchecked(['chroot', rootdir
, 'dpkg', '-i'] + filenames
)
389 logging
.debug('stdout:\n%s' % out
)
390 logging
.debug('stderr:\n%s' % err
)
391 out
= self
.runcmd(['chroot', rootdir
,
392 'apt-get', '-f', '--no-remove', 'install'])
393 logging
.debug('stdout:\n%s' % out
)
396 def cleanup_apt_cache(self
, rootdir
):
397 out
= self
.runcmd(['chroot', rootdir
, 'apt-get', 'clean'])
398 logging
.debug('stdout:\n%s' % out
)
400 def set_root_password(self
, rootdir
):
401 if self
.settings
['root-password']:
402 self
.message('Setting root password')
403 self
.set_password(rootdir
, 'root', self
.settings
['root-password'])
404 elif self
.settings
['lock-root-password']:
405 self
.message('Locking root password')
406 self
.runcmd(['chroot', rootdir
, 'passwd', '-l', 'root'])
408 self
.message('Give root an empty password')
409 self
.delete_password(rootdir
, 'root')
411 def create_users(self
, rootdir
):
412 def create_user(user
):
413 self
.runcmd(['chroot', rootdir
, 'adduser', '--gecos', user
,
414 '--disabled-password', user
])
415 if self
.settings
['sudo']:
416 self
.runcmd(['chroot', rootdir
, 'adduser', user
, 'sudo'])
418 for userpass
in self
.settings
['user']:
420 user
, password
= userpass
.split('/', 1)
422 self
.set_password(rootdir
, user
, password
)
424 create_user(userpass
)
425 self
.delete_password(rootdir
, userpass
)
427 def set_password(self
, rootdir
, user
, password
):
428 encrypted
= crypt
.crypt(password
, '..')
429 self
.runcmd(['chroot', rootdir
, 'usermod', '-p', encrypted
, user
])
431 def delete_password(self
, rootdir
, user
):
432 self
.runcmd(['chroot', rootdir
, 'passwd', '-d', user
])
434 def remove_udev_persistent_rules(self
, rootdir
):
435 self
.message('Removing udev persistent cd and net rules')
436 for x
in ['70-persistent-cd.rules', '70-persistent-net.rules']:
437 pathname
= os
.path
.join(rootdir
, 'etc', 'udev', 'rules.d', x
)
438 if os
.path
.exists(pathname
):
439 logging
.debug('rm %s' % pathname
)
442 logging
.debug('not removing non-existent %s' % pathname
)
444 def setup_networking(self
, rootdir
):
445 self
.message('Setting up networking')
447 f
= open(os
.path
.join(rootdir
, 'etc', 'network', 'interfaces'), 'w')
449 f
.write('iface lo inet loopback\n')
451 if self
.settings
['enable-dhcp']:
453 f
.write('auto eth0\n')
454 f
.write('iface eth0 inet dhcp\n')
458 def append_serial_console(self
, rootdir
):
459 if self
.settings
['serial-console']:
460 serial_command
= self
.settings
['serial-console-command']
461 logging
.debug('adding getty to serial console')
462 inittab
= os
.path
.join(rootdir
, 'etc/inittab')
463 with
open(inittab
, 'a') as f
:
464 f
.write('\nS0:23:respawn:%s\n' % serial_command
)
466 def install_grub2(self
, rootdev
, rootdir
):
467 self
.message("Configuring grub2")
468 # rely on kpartx using consistent naming to map loop0p1 to loop0
469 install_dev
= os
.path
.join('/dev', os
.path
.basename(rootdev
)[:-2])
470 self
.runcmd(['mount', '/dev', '-t', 'devfs', '-obind',
471 '%s' % os
.path
.join(rootdir
, 'dev')])
472 self
.runcmd(['mount', '/proc', '-t', 'proc', '-obind',
473 '%s' % os
.path
.join(rootdir
, 'proc')])
474 self
.runcmd(['mount', '/sys', '-t', 'sysfs', '-obind',
475 '%s' % os
.path
.join(rootdir
, 'sys')])
477 self
.runcmd(['chroot', rootdir
, 'update-grub'])
478 self
.runcmd(['chroot', rootdir
, 'grub-install', install_dev
])
479 except cliapp
.AppException
as e
:
480 self
.message("Failed to configure grub2. Using extlinux.")
481 self
.runcmd(['umount', os
.path
.join(rootdir
, 'sys')])
482 self
.runcmd(['umount', os
.path
.join(rootdir
, 'proc')])
483 self
.runcmd(['umount', os
.path
.join(rootdir
, 'dev')])
484 self
.install_extlinux(rootdev
, rootdir
)
486 def install_extlinux(self
, rootdev
, rootdir
):
487 if not os
.path
.exists("/usr/bin/extlinux"):
488 self
.message("extlinux not installed, skipping.")
490 self
.message('Installing extlinux')
493 dirname
= os
.path
.join(rootdir
, 'boot')
494 basenames
= os
.listdir(dirname
)
495 logging
.debug('find: %s' % basenames
)
496 for basename
in basenames
:
497 if re
.search(pattern
, basename
):
498 return os
.path
.join('boot', basename
)
499 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
502 kernel_image
= find('vmlinuz-.*')
503 initrd_image
= find('initrd.img-.*')
504 except cliapp
.AppException
as e
:
505 self
.message("Unable to find kernel. Not installing extlinux.")
506 logging
.debug("No kernel found. %s. Skipping install of extlinux." % e
)
509 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
510 '-s', 'UUID', rootdev
])
511 uuid
= out
.splitlines()[0].strip()
513 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
514 logging
.debug('configure extlinux %s' % conf
)
522 append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
525 'kernel': kernel_image
,
526 'initrd': initrd_image
,
529 'console=ttyS0,115200' if self
.settings
['serial-console'] else '',
530 'extserial': 'serial 0 115200' if self
.settings
['serial-console'] else '',
534 self
.runcmd(['extlinux', '--install', rootdir
])
535 self
.runcmd(['sync'])
538 def optimize_image(self
, rootdir
):
540 Filing up the image with zeros will increase its compression rate
542 zeros
= os
.path
.join(rootdir
, 'ZEROS')
543 self
.runcmd_unchecked(['dd', 'if=/dev/zero', 'of=' + zeros
, 'bs=1M'])
544 self
.runcmd(['rm', '-f', zeros
])
548 Run squashfs on the image.
550 if not os
.path
.exists('/usr/bin/mksquashfs'):
551 logging
.warning("Squash selected but mksquashfs not found!")
553 self
.message("Running mksquashfs")
554 suffixed
= "%s.squashfs" % self
.settings
['image']
555 self
.runcmd(['mksquashfs', self
.settings
['image'],
557 '-no-progress', '-comp', 'xz'], ignore_fail
=False)
558 os
.unlink(self
.settings
['image'])
559 self
.settings
['image'] = suffixed
561 def cleanup_system(self
):
562 # Clean up after any errors.
564 self
.message('Cleaning up')
566 # Umount in the reverse mount order
567 if self
.settings
['image']:
568 for i
in xrange(len(self
.mount_points
) - 1, -1, -1):
569 mount_point
= self
.mount_points
[i
]
571 self
.runcmd(['umount', mount_point
], ignore_fail
=False)
572 except cliapp
.AppException
:
573 logging
.debug("umount failed, sleeping and trying again")
575 self
.runcmd(['umount', mount_point
], ignore_fail
=False)
577 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
579 for dirname
in self
.remove_dirs
:
580 shutil
.rmtree(dirname
)
582 def customize(self
, rootdir
):
583 script
= self
.settings
['customize']
586 if not os
.path
.exists(script
):
587 example
= os
.path
.join("/usr/share/vmdebootstrap/examples/", script
)
588 if not os
.path
.exists(example
):
589 self
.message("Unable to find %s" % script
)
592 self
.message('Running customize script %s' % script
)
593 with
open('/dev/tty', 'w') as tty
:
594 cliapp
.runcmd([script
, rootdir
], stdout
=tty
, stderr
=tty
)
596 def create_tarball(self
, rootdir
):
597 # Create a tarball of the disk's contents
598 # shell out to runcmd since it more easily handles rootdir
599 self
.message('Creating tarball of disk contents')
600 self
.runcmd(['tar', '-cf', self
.settings
['tarball'], '-C', rootdir
, '.'])
602 def chown(self
, rootdir
):
603 # Change image owner after completed build
604 self
.message("Changing owner to %s" % self
.settings
["owner"])
605 subprocess
.call(["chown",
606 self
.settings
["owner"],
607 self
.settings
["image"]])
609 def configure_apt(self
, rootdir
):
610 # use the distribution and mirror to create an apt source
611 self
.message("Configuring apt to use distribution and mirror")
612 conf
= os
.path
.join(rootdir
, 'etc', 'apt', 'sources.list.d', 'base.list')
613 logging
.debug('configure apt %s' % conf
)
616 deb %(mirror)s %(distribution)s main
617 #deb-src %(mirror)s %(distribution)s main
619 'mirror': self
.settings
['mirror'],
620 'distribution': self
.settings
['distribution']
624 if __name__
== '__main__':
625 VmDebootstrap(version
=__version__
).run()