2 # -*- coding: utf-8 -*-
7 This script installs a grml system (either a running system or ISO[s]) to a USB device
9 :copyright: (c) 2009 by Michael Prokop <mika@grml.org>
10 :license: GPL v2 or any later version
11 :bugreports: http://grml.org/bugs/
15 # from __future__ import with_statement
16 from optparse import OptionParser
17 from inspect import isroutine, isclass
18 import datetime, logging, os, re, subprocess, sys, tempfile, time, os.path
21 PROG_VERSION = "0.9.10"
22 MOUNTED = set() # register mountpoints
23 TMPFILES = set() # register tmpfiles
24 DATESTAMP = time.mktime(datetime.datetime.now().timetuple()) # unique identifier for syslinux.cfg
25 GRML_FLAVOURS = set() # which flavours are being installed?
29 USAGE = "Usage: %prog [options] <[ISO[s] | /live/image]> </dev/sdX#>\n\
31 %prog installs grml ISO[s] to an USB device to be able to boot from it.\n\
32 Make sure you have at least one grml ISO or a running grml system (/live/image),\n\
33 grub or syslinux and root access.\n\
35 Run %prog --help for usage hints, further information via: man grml2usb"
37 # pylint: disable-msg=C0103
38 parser = OptionParser(usage=USAGE)
39 parser.add_option("--bootoptions", dest="bootoptions",
40 action="store", type="string",
41 help="use specified bootoptions as default")
42 parser.add_option("--bootloader-only", dest="bootloaderonly", action="store_true",
43 help="do not copy files but just install a bootloader")
44 parser.add_option("--copy-only", dest="copyonly", action="store_true",
45 help="copy files only but do not install bootloader")
46 parser.add_option("--dry-run", dest="dryrun", action="store_true",
47 help="avoid executing commands")
48 parser.add_option("--fat16", dest="fat16", action="store_true",
49 help="format specified partition with FAT16")
50 parser.add_option("--force", dest="force", action="store_true",
51 help="force any actions requiring manual interaction")
52 #parser.add_option("--initrd", dest="initrd", action="store", type="string",
53 # help="install specified initrd instead of the default [TODO - not implemented yet]")
54 #parser.add_option("--kernel", dest="kernel", action="store", type="string",
55 # help="install specified kernel instead of the default [TODO - not implemented yet]")
56 parser.add_option("--lilo-binary", dest="lilobin", action="store", type="string",
57 help="lilo executable to be used for installing MBR")
58 parser.add_option("--mbr-menu", dest="mbrmenu", action="store_true",
59 help="enable interactive boot menu in MBR")
60 parser.add_option("--quiet", dest="quiet", action="store_true",
61 help="do not output anything but just errors on console")
62 parser.add_option("--skip-addons", dest="skipaddons", action="store_true",
63 help="do not install /boot/addons/ files")
64 parser.add_option("--skip-grub-config", dest="skipgrubconfig", action="store_true",
65 help="skip generation of grub configuration files")
66 parser.add_option("--skip-mbr", dest="skipmbr", action="store_true",
67 help="do not install a master boot record (MBR) on the device")
68 parser.add_option("--skip-syslinux-config", dest="skipsyslinuxconfig", action="store_true",
69 help="skip generation of syslinux configuration files")
70 parser.add_option("--syslinux", dest="syslinux", action="store_true",
71 help="install syslinux bootloader instead of grub")
72 parser.add_option("--syslinux-mbr", dest="syslinuxmbr", action="store_true",
73 help="install syslinux master boot record (MBR) instead of default")
74 #parser.add_option("--squashfs", dest="squashfs", action="store", type="string",
75 # help="install specified squashfs file instead of the default [TODO - not implemented yet]")
76 #parser.add_option("--uninstall", dest="uninstall", action="store_true",
77 # help="remove grml ISO files from specified device [TODO - not implemented yet]")
78 parser.add_option("--verbose", dest="verbose", action="store_true",
79 help="enable verbose mode")
80 parser.add_option("-v", "--version", dest="version", action="store_true",
81 help="display version and exit")
82 (options, args) = parser.parse_args()
84 GRML2USB_BASE = '/usr/share/grml2usb'
85 if not os.path.isdir(GRML2USB_BASE):
86 GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
89 class CriticalException(Exception):
90 """Throw critical exception if the exact error is not known but fatal."
92 @Exception: message"""
97 """Cleanup function to make sure there aren't any mounted devices left behind.
100 logging.info("Cleaning up before exiting...")
101 proc = subprocess.Popen(["sync"])
105 for device in MOUNTED:
107 # ignore: RuntimeError: Set changed size during iteration
109 logging.debug('caught expection RuntimeError, ignoring')
112 def register_tmpfile(path):
114 TODO - not implemented yet
120 def unregister_tmpfile(path):
122 TODO - not implemented yet
126 TMPFILES.remove(path)
129 def register_mountpoint(target):
130 """register specified target in a set() for handling clean exiting
132 @target: destination target of mountpoint
138 def unregister_mountpoint(target):
139 """unregister specified target in a set() for handling clean exiting
141 @target: destination target of mountpoint
144 if target in MOUNTED:
145 MOUNTED.remove(target)
148 def get_function_name(obj):
149 """Helper function for use in execute() to retrive name of a function
151 @obj: the function object
153 if not (isroutine(obj) or isclass(obj)):
155 return obj.__module__ + '.' + obj.__name__
158 def execute(f, *exec_arguments):
159 """Wrapper for executing a command. Either really executes
160 the command (default) or when using --dry-run commandline option
161 just displays what would be executed."""
162 # usage: execute(subprocess.Popen, (["ls", "-la"]))
163 # TODO: doesn't work for proc = execute(subprocess.Popen...() -> any ideas?
165 # pylint: disable-msg=W0141
166 logging.debug('dry-run only: %s(%s)' % (get_function_name(f), ', '.join(map(repr, exec_arguments))))
168 # pylint: disable-msg=W0142
169 return f(*exec_arguments)
173 """Check whether a given file can be executed
175 @fpath: full path to file
177 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
181 """Check whether a given program is available in PATH
183 @program: name of executable"""
184 fpath = os.path.split(program)[0]
189 for path in os.environ["PATH"].split(os.pathsep):
190 exe_file = os.path.join(path, program)
197 def search_file(filename, search_path='/bin' + os.pathsep + '/usr/bin'):
198 """Given a search path, find file
200 @filename: name of file to search for
201 @search_path: path where searching for the specified filename"""
203 paths = search_path.split(os.pathsep)
204 current_dir = '' # make pylint happy :)
206 # pylint: disable-msg=W0612
207 for current_dir, directories, files in os.walk(path):
208 if os.path.exists(os.path.join(current_dir, filename)):
212 return os.path.abspath(os.path.join(current_dir, filename))
217 def check_uid_root():
218 """Check for root permissions"""
219 if not os.geteuid()==0:
220 sys.exit("Error: please run this script with uid 0 (root).")
223 def mkfs_fat16(device):
224 """Format specified device with VFAT/FAT16 filesystem.
226 @device: partition that should be formated"""
228 # syslinux -d boot/isolinux /dev/sdb1
230 logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
233 logging.info("Formating partition with fat16 filesystem")
234 logging.debug("mkfs.vfat -F 16 %s" % device)
235 proc = subprocess.Popen(["mkfs.vfat", "-F", "16", device])
237 if proc.returncode != 0:
238 raise CriticalException("error executing mkfs.vfat")
241 def generate_main_grub2_config(grml_flavour, bootoptions):
242 """Generate grub2 configuration for use via grub.cfg
244 @grml_flavour: name of grml flavour the configuration should be generated for
245 @bootoptions: additional bootoptions that should be used by default"""
247 local_datestamp = DATESTAMP
250 ## main grub2 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
256 if loadfont /boot/grub/ascii.pf2 ; then
261 if terminal_output.gfxterm ; then true ; else
262 # For backward compatibility with versions of terminal.mod that don't
263 # understand terminal_output
268 if background_image /boot/grub/grml.png ; then
269 set color_normal=black/black
270 set color_highlight=red/black
272 set menu_color_normal=white/black
273 set menu_color_highlight=black/yellow
276 menuentry "%(grml_flavour)s (default)" {
277 set gfxpayload=1024x768x16,1024x768
278 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
279 initrd /boot/release/%(grml_flavour)s/initrd.gz
282 menuentry "Memory test (memtest86+)" {
283 linux16 /boot/addons/memtest
286 menuentry "Boot Grub (all in one image)" {
287 linux /boot/addons/memdisk
288 initrd /boot/addons/allinone.img
291 menuentry "Boot FreeDOS" {
292 linux /boot/addons/memdisk
293 initrd /boot/addons/balder10.imz
296 menuentry "Boot MirOS bsd4grml" {
297 multiboot /boot/addons/bsd4grml/ldbsd.com
298 module /boot/addons/bsd4grml/bsd.rd
299 module /boot/addons/bsd4grml/boot.1
300 module /boot/addons/bsd4grml/boot.2
301 module /boot/addons/bsd4grml/boot.3
302 module /boot/addons/bsd4grml/boot.4
303 module /boot/addons/bsd4grml/boot.5
304 module /boot/addons/bsd4grml/boot.6
305 module /boot/addons/bsd4grml/boot.cfg
308 menuentry "Boot OS of first partition on first disk" {
313 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
314 'bootoptions': bootoptions } )
317 def generate_flavour_specific_grub2_config(grml_flavour, bootoptions):
318 """Generate grub2 configuration for use via grub.cfg
320 @grml_flavour: name of grml flavour the configuration should be generated for
321 @bootoptions: additional bootoptions that should be used by default"""
323 local_datestamp = DATESTAMP
326 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
327 menuentry "%(grml_flavour)s - boot in default mode" {
328 set gfxpayload=1024x768x16,1024x768
329 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
330 initrd /boot/release/%(grml_flavour)s/initrd.gz
333 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
334 menuentry "%(grml_flavour)s-persistent - enable persistency feature" {
335 set gfxpayload=1024x768x16,1024x768
336 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
337 initrd /boot/release/%(grml_flavour)s/initrd.gz
340 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
341 menuentry "%(grml_flavour)s2ram - copy compressed grml file to RAM" {
342 set gfxpayload=1024x768x16,1024x768
343 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ toram=%(grml_flavour)s.squashfs %(bootoptions)s
344 initrd /boot/release/%(grml_flavour)s/initrd.gz
347 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
348 menuentry "%(grml_flavour)s-debug - enable debugging options" {
349 set gfxpayload=1024x768x16,1024x768
350 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ debug boot=live initcall_debug%(bootoptions)s
351 initrd /boot/release/%(grml_flavour)s/initrd.gz
354 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
355 menuentry "%(grml_flavour)s-x - start X Window System" {
356 set gfxpayload=1024x768x16,1024x768
357 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ startx=wm-ng %(bootoptions)s
358 initrd /boot/release/%(grml_flavour)s/initrd.gz
361 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
362 menuentry "%(grml_flavour)s-nofb - disable framebuffer" {
363 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=ofonly %(bootoptions)s
364 initrd /boot/release/%(grml_flavour)s/initrd.gz
367 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
368 menuentry "%(grml_flavour)s-failsafe - disable hardware detection" {
369 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal boot=live noautoconfig atapicd noapic noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia nosmp maxcpus=0 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm noresume selinux=0 edd=off %(bootoptions)s
370 initrd /boot/release/%(grml_flavour)s/initrd.gz
373 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
374 menuentry "%(grml_flavour)s-forensic - do not touch harddisks during hw recognition" {
375 set gfxpayload=1024x768x16,1024x768
376 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ nofstab noraid nolvm noautoconfig noswap raid=noautodetect forensic readonly %(bootoptions)s
377 initrd /boot/release/%(grml_flavour)s/initrd.gz
380 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
381 'bootoptions': bootoptions } )
384 def generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootoptions):
385 """Generate grub1 configuration for use via menu.lst
387 @grml_flavour: name of grml flavour the configuration should be generated for
388 @install_partition: partition number for use in (hd0,X)
389 @bootoptions: additional bootoptions that should be used by default"""
391 local_datestamp = DATESTAMP
394 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
395 title %(grml_flavour)s
396 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
397 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
399 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
400 title %(grml_flavour)s-persistent
401 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
402 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
404 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
405 title %(grml_flavour)s2ram
406 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ toram=%(grml_flavour)s.squashfs %(bootoptions)s
407 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
409 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
410 title %(grml_flavour)s-debug
411 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ debug boot=live initcall_debug%(bootoptions)s
412 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
414 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
415 title %(grml_flavour)s-x
416 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ startx=wm-ng %(bootoptions)s
417 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
419 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
420 title %(grml_flavour)s-nofb
421 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=ofonly %(bootoptions)s
422 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
424 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
425 title %(grml_flavour)s-failsafe
426 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal boot=live noautoconfig atapicd noapic noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia nosmp maxcpus=0 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm noresume selinux=0 edd=off %(bootoptions)s
427 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
429 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
430 title %(grml_flavour)s-forensic
431 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ nofstab noraid nolvm noautoconfig noswap raid=noautodetect forensic readonly %(bootoptions)s
432 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
434 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
435 title %(grml_flavour)s-serial
436 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=vesafb:off console=tty1 console=ttyS0,9600n8 %(bootoptions)s
437 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
439 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
440 'bootoptions': bootoptions, 'install_partition': install_partition } )
443 def generate_main_grub1_config(grml_flavour, install_partition, bootoptions):
444 """Generate grub1 configuration for use via menu.lst
446 @grml_flavour: name of grml flavour the configuration should be generated for"""
448 local_datestamp = DATESTAMP
451 ## main grub1 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
454 # color red/blue green/black
455 splashimage=(hd0,%(install_partition)s)/boot/grub/splash.xpm.gz
460 title %(grml_flavour)s - Default boot (using 1024x768 framebuffer)
461 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off vga=791 quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
462 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
464 title Memory test (memtest86+)
465 kernel (hd0,%(install_partition)s)/boot/addons/memtest
467 title Grub - all in one image
468 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
469 initrd (hd0,%(install_partition)s)/boot/addons/allinone.img
472 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
473 initrd (hd0,%(install_partition)s)/boot/addons/balder10.imz
476 kernel (hd0,%(install_partition)s)/boot/addons/bsd4grml/ldbsd.com
478 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
479 'bootoptions': bootoptions, 'install_partition': install_partition } )
482 def generate_isolinux_splash(grml_flavour):
483 """Generate bootsplash for isolinux/syslinux
485 @grml_flavour: name of grml flavour the configuration should be generated for"""
487 # TODO: adjust last bootsplash line (the one following the "Some information and boot ...")
489 grml_name = grml_flavour
492 \ f17
\f\18/boot/syslinux/logo.16
494 Some information and boot options available via keys F2 - F10. http://grml.org/
496 """ % {'grml_name': grml_name} )
499 def generate_main_syslinux_config(grml_flavour, bootoptions):
500 """Generate main configuration for use in syslinux.cfg
502 @grml_flavour: name of grml flavour the configuration should be generated for
503 @bootoptions: additional bootoptions that should be used by default"""
505 local_datestamp = DATESTAMP
508 ## main syslinux configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
509 # use this to control the bootup via a serial port
514 DISPLAY /boot/syslinux/boot.msg
515 F1 /boot/syslinux/boot.msg
524 F10 /boot/syslinux/f10
525 ## end of main configuration
527 ## global configuration
528 # the default option (using %(grml_flavour)s)
530 KERNEL /boot/release/%(grml_flavour)s/linux26
531 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
535 KERNEL /boot/addons/memtest
540 KERNEL /boot/addons/memdisk
541 APPEND initrd=/boot/addons/allinone.img
546 KERNEL /boot/addons/memdisk
547 APPEND initrd=/boot/addons/balder10.imz
552 KERNEL /boot/addons/bsd4grml/ldbsd.com
554 # hardware detection tool
556 KERNEL /boot/addons/hdt.c32
557 APPEND pciids=/boot/addons/pci.ids
559 ## end of global configuration
560 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp, 'bootoptions': bootoptions} )
563 def generate_flavour_specific_syslinux_config(grml_flavour, bootoptions):
564 """Generate flavour specific configuration for use in syslinux.cfg
566 @grml_flavour: name of grml flavour the configuration should be generated for
567 @bootoptions: bootoptions that should be used as a default"""
569 local_datestamp = DATESTAMP
573 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
574 LABEL %(grml_flavour)s
575 KERNEL /boot/release/%(grml_flavour)s/linux26
576 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
578 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
579 LABEL %(grml_flavour)s-persistent
580 KERNEL /boot/release/%(grml_flavour)s/linux26
581 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
583 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
584 LABEL %(grml_flavour)s2ram
585 KERNEL /boot/release/%(grml_flavour)s/linux26
586 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ toram=%(grml_flavour)s.squashfs %(bootoptions)s
588 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
589 LABEL %(grml_flavour)s-debug
590 KERNEL /boot/release/%(grml_flavour)s/linux26
591 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ debug boot=live initcall_debug%(bootoptions)s
593 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
594 LABEL %(grml_flavour)s-x
595 KERNEL /boot/release/%(grml_flavour)s/linux26
596 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ startx=wm-ng %(bootoptions)s
598 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
599 LABEL %(grml_flavour)s-nofb
600 KERNEL /boot/release/%(grml_flavour)s/linux26
601 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=ofonly %(bootoptions)s
603 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
604 LABEL %(grml_flavour)s-failsafe
605 KERNEL /boot/release/%(grml_flavour)s/linux26
606 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal boot=live noautoconfig atapicd noapic noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia nosmp maxcpus=0 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm noresume selinux=0 edd=off %(bootoptions)s
608 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
609 LABEL %(grml_flavour)s-forensic
610 KERNEL /boot/release/%(grml_flavour)s/linux26
611 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ nofstab noraid nolvm noautoconfig noswap raid=noautodetect forensic readonly %(bootoptions)s
613 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
614 LABEL %(grml_flavour)s-serial
615 KERNEL /boot/release/%(grml_flavour)s/linux26
616 APPEND initrd=/boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=vesafb:off console=tty1 console=ttyS0,9600n8 %(bootoptions)s
617 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp, 'bootoptions': bootoptions} )
620 def install_grub(device):
621 """Install grub on specified device.
623 @mntpoint: mountpoint of device where grub should install its files to
624 @device: partition where grub should be installed to"""
627 logging.info("Would execute grub-install [--root-directory=mount_point] %s now.", device)
629 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
630 register_tmpfile(device_mountpoint)
633 mount(device, device_mountpoint, "")
634 logging.debug("grub-install --recheck --no-floppy --root-directory=%s %s", device_mountpoint, device)
635 proc = subprocess.Popen(["grub-install", "--recheck", "--no-floppy",
636 "--root-directory=%s" % device_mountpoint, device], stdout=file(os.devnull, "r+"))
638 if proc.returncode != 0:
639 # raise Exception("error executing grub-install")
640 logging.critical("Fatal: error executing grub-install (please check the grml2usb FAQ)")
643 except CriticalException, error:
644 logging.critical("Fatal: %s" % error)
649 unmount(device_mountpoint, "")
650 os.rmdir(device_mountpoint)
651 unregister_tmpfile(device_mountpoint)
654 def install_syslinux(device):
655 """Install syslinux on specified device.
657 @device: partition where syslinux should be installed to"""
660 logging.info("Would install syslinux as bootloader on %s", device)
663 # syslinux -d boot/isolinux /dev/sdb1
664 logging.info("Installing syslinux as bootloader")
665 logging.debug("syslinux -d boot/syslinux %s" % device)
666 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
668 if proc.returncode != 0:
669 raise CriticalException("Error executing syslinux (either try --fat16 or --grub?)")
672 def install_bootloader(device):
673 """Install bootloader on specified device.
675 @device: partition where bootloader should be installed to"""
677 # by default we use grub, so install syslinux only on request
680 install_syslinux(device)
681 except CriticalException, error:
682 logging.critical("Fatal: %s" % error)
686 if not which("grub-install"):
687 logging.critical("Fatal: grub-install not available (please install the grub package or use the --syslinux option)")
693 except CriticalException, error:
694 logging.critical("Fatal: %s" % error)
699 def execute_lilo(lilo, device):
700 """execute lilo for activating the partitions in the MBR
702 @lilo: path of lilo executable
703 @device: device where lilo should be executed on"""
705 # to support -A for extended partitions:
706 logging.info("Activating partitions in MBR via lilo")
707 logging.debug("%s -S /dev/null -M %s ext" % (lilo, device))
708 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-M", device, "ext"])
710 if proc.returncode != 0:
711 raise Exception("error executing lilo")
713 # activate partition:
714 logging.debug("%s -S /dev/null -A %s 1" % (lilo, device))
715 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-A", device, "1"])
717 if proc.returncode != 0:
718 raise Exception("error executing lilo")
721 def install_syslinux_mbr(device):
722 """install syslinux's master boot record (MBR) on the specified device
724 @device: device where MBR of syslinux should be installed to"""
726 # make sure we have syslinux available
727 if not which("syslinux") and not options.copyonly:
728 raise Exception("syslinux not available (either install it or consider dropping the --syslinux option)")
730 # lilo's mbr is broken, use the one from syslinux instead:
731 if not os.path.isfile("/usr/lib/syslinux/mbr.bin"):
732 raise Exception("/usr/lib/syslinux/mbr.bin can not be read")
734 logging.info("Installing syslinux MBR")
735 logging.debug("cat /usr/lib/syslinux/mbr.bin > %s" % device)
737 # TODO -> use Popen instead?
738 retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True)
740 logging.critical("Error copying MBR to device (%s)" % retcode)
741 except OSError, error:
742 logging.critical("Execution failed:", error)
745 def install_mir_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
746 """install 'mbr' master boot record (MBR) on a device
748 Retrieve the partition table from "device", install an MBR from the
749 "mbrtemplate" file, set the "partition" (0..3) active, and install the
750 result back to "device".
752 @mbrtemplate: default MBR file
754 @device: name of a file assumed to be a hard disc (or USB stick) image, or
755 something like "/dev/sdb"
757 @partition: must be a number between 0 and 3, inclusive
759 @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
762 @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
763 specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
764 false then leave the mirbsdmbr specific flag set to FFh, set all
765 active flags to 0 and set the active flag of the partition to 80h. Note:
766 behaviour of mirbsdmbr: if flag = 0/1/2/3 then use it, otherwise search for
769 logging.info("Installing default MBR")
771 if not os.path.isfile(mbrtemplate):
772 logging.critical("Error: %s can not be read." % mbrtemplate)
773 raise CriticalException("Error installing MBR (either try --syslinux-mbr or install missing file?)")
775 if (partition < 0) or (partition > 3):
776 raise ValueError("partition must be between 0 and 3")
783 tmpf = tempfile.NamedTemporaryFile()
785 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1" % (device, tmpf.name))
786 proc = subprocess.Popen(["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"], stderr=file(os.devnull, "r+"))
788 if proc.returncode != 0:
789 raise Exception("error executing dd (first run)")
791 logging.debug("executing: dd if=%s of=%s bs=%s count=1 conv=notrunc" % (mbrtemplate,
792 tmpf.name, nmbrbytes))
793 proc = subprocess.Popen(["dd", "if=%s" % mbrtemplate, "of=%s" % tmpf.name, "bs=%s" % nmbrbytes,
794 "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+"))
796 if proc.returncode != 0:
797 raise Exception("error executing dd (second run)")
799 mbrcode = tmpf.file.read(512)
800 if len(mbrcode) < 512:
801 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
804 mbrcode = mbrcode[0:439] + chr(partition) + \
805 mbrcode[440:510] + "\x55\xAA"
807 actives = ["\x00", "\x00", "\x00", "\x00"]
808 actives[partition] = "\x80"
809 mbrcode = mbrcode[0:446] + actives[0] + \
810 mbrcode[447:462] + actives[1] + \
811 mbrcode[463:478] + actives[2] + \
812 mbrcode[479:494] + actives[3] + \
813 mbrcode[495:510] + "\x55\xAA"
817 tmpf.file.write(mbrcode)
820 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc" % (tmpf.name, device))
821 proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1",
822 "conv=notrunc"], stderr=file(os.devnull, "r+"))
824 if proc.returncode != 0:
825 raise Exception("error executing dd (third run)")
829 def handle_syslinux_mbr(device):
830 """Install syslinux master boot record on given device
832 @device: device where MBR should be installed to"""
834 if not is_writeable(device):
835 raise IOError("device not writeable for user")
837 # try to use system's lilo
841 # otherwise fall back to our static version
842 from platform import architecture
843 if architecture()[0] == '64bit':
844 lilo = GRML2USB_BASE + '/lilo/lilo.static.amd64'
846 lilo = GRML2USB_BASE + '/lilo/lilo.static.i386'
847 # finally prefer a specified lilo executable
849 lilo = options.lilobin
852 raise Exception("lilo executable can not be execute")
855 logging.info("Would install MBR running lilo and using syslinux.")
858 execute_lilo(lilo, device)
859 install_syslinux_mbr(device)
862 def is_writeable(device):
863 """Check if the device is writeable for the current user
865 @device: partition where bootloader should be installed to"""
869 #raise Exception("no device for checking write permissions")
871 if not os.path.exists(device):
874 return os.access(device, os.W_OK) and os.access(device, os.R_OK)
877 def mount(source, target, mount_options):
878 """Mount specified source on given target
880 @source: name of device/ISO that should be mounted
881 @target: directory where the ISO should be mounted to
882 @options: mount specific options"""
884 # note: options.dryrun does not work here, as we have to
885 # locate files and identify the grml flavour
887 for x in file('/proc/mounts').readlines():
888 if x.startswith(source):
889 raise CriticalException("Error executing mount: %s already mounted - please unmount before invoking grml2usb" % source)
891 if os.path.isdir(source):
892 logging.debug("Source %s is not a device, therefore not mounting." % source)
895 logging.debug("mount %s %s %s" % (mount_options, source, target))
896 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
898 if proc.returncode != 0:
899 raise CriticalException("Error executing mount (no filesystem on the partition?)")
901 logging.debug("register_mountpoint(%s)" % target)
902 register_mountpoint(target)
905 def unmount(target, unmount_options):
906 """Unmount specified target
908 @target: target where something is mounted on and which should be unmounted
909 @options: options for umount command"""
911 # make sure we unmount only already mounted targets
912 target_unmount = False
913 mounts = open('/proc/mounts').readlines()
914 mountstring = re.compile(".*%s.*" % re.escape(os.path.realpath(target)))
916 if re.match(mountstring, line):
917 target_unmount = True
919 if not target_unmount:
920 logging.debug("%s not mounted anymore" % target)
922 logging.debug("umount %s %s" % (list(unmount_options), target))
923 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
925 if proc.returncode != 0:
926 raise Exception("Error executing umount")
928 logging.debug("unregister_mountpoint(%s)" % target)
929 unregister_mountpoint(target)
932 def check_for_usbdevice(device):
933 """Check whether the specified device is a removable USB device
935 @device: device name, like /dev/sda1 or /dev/sda
938 usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
940 usbdev = os.path.realpath('/sys/class/block/' + usbdevice + '/removable')
941 if not os.path.isfile(usbdev):
942 # Ubuntu with kernel 2.6.24 for example:
943 usbdev = os.path.realpath('/sys/block/' + usbdevice + '/removable')
945 if os.path.isfile(usbdev):
946 is_usb = open(usbdev).readline()
953 def check_for_fat(partition):
954 """Check whether specified partition is a valid VFAT/FAT16 filesystem
956 @partition: device name of partition"""
959 udev_info = subprocess.Popen(["/sbin/blkid", "-s", "TYPE", "-o", "value", partition],
960 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
961 filesystem = udev_info.communicate()[0].rstrip()
963 if udev_info.returncode == 2:
964 raise CriticalException("Failed to read device %s"
965 " (wrong UID/permissions or device/directory not present?)" % partition)
967 if options.syslinux and filesystem != "vfat":
968 raise CriticalException("Partition %s does not contain a FAT16 filesystem. (Use --fat16 or run mkfs.vfat %s)" % (partition, partition))
971 raise CriticalException("Sorry, /sbin/blkid not available (install e2fsprogs?)")
974 def mkdir(directory):
975 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
977 # just silently pass as it's just fine it the directory exists
978 if not os.path.isdir(directory):
980 os.makedirs(directory)
981 # pylint: disable-msg=W0704
986 def copy_system_files(grml_flavour, iso_mount, target):
987 """copy grml's main files (like squashfs, kernel and initrd) to a given target
989 @grml_flavour: name of grml flavour the configuration should be generated for
990 @iso_mount: path where a grml ISO is mounted on
991 @target: path where grml's main files should be copied to"""
993 squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
995 logging.critical("Fatal: squashfs file not found")
996 raise CriticalException("error locating squashfs file")
998 squashfs_target = target + '/live/' + grml_flavour + '/'
999 execute(mkdir, squashfs_target)
1000 logging.debug("rsync -rlptDH --inplace %s %s" % (squashfs, squashfs_target + grml_flavour + '.squashfs'))
1001 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", squashfs, squashfs_target + grml_flavour + ".squashfs"])
1004 filesystem_module = search_file('filesystem.module', iso_mount)
1005 if filesystem_module is None:
1006 logging.critical("Fatal: filesystem.module not found")
1008 logging.debug("rsync -rlptDH --inplace %s %s" % (filesystem_module, squashfs_target + 'filesystem.module'))
1009 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", filesystem_module, squashfs_target + 'filesystem.module'])
1012 release_target = target + '/boot/release/' + grml_flavour
1013 execute(mkdir, release_target)
1015 kernel = search_file('linux26', iso_mount)
1017 logging.critical("Fatal kernel not found")
1019 logging.debug("rsync -rlptDH --inplace %s %s" % (kernel, release_target + '/linux26'))
1020 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", kernel, release_target + '/linux26'])
1023 initrd = search_file('initrd.gz', iso_mount)
1025 logging.critical("Fatal: initrd not found")
1027 logging.debug("rsync -rlptDH --inplace %s %s" % (initrd, release_target + '/initrd.gz'))
1028 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", initrd, release_target + '/initrd.gz'])
1032 def copy_grml_files(iso_mount, target):
1033 """copy some minor grml files to a given target
1035 @iso_mount: path where a grml ISO is mounted on
1036 @target: path where grml's main files should be copied to"""
1038 grml_target = target + '/grml/'
1039 execute(mkdir, grml_target)
1041 for myfile in 'grml-cheatcodes.txt', 'grml-version', 'LICENSE.txt', 'md5sums', 'README.txt':
1042 grml_file = search_file(myfile, iso_mount)
1043 if grml_file is None:
1044 logging.warn("Warning: myfile %s could not be found - can not install it", myfile)
1046 logging.debug("rsync -rlptDH --inplace %s %s" % (grml_file, grml_target + grml_file))
1047 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", grml_file, grml_target + myfile])
1050 grml_web_target = grml_target + '/web/'
1051 execute(mkdir, grml_web_target)
1053 for myfile in 'index.html', 'style.css':
1054 grml_file = search_file(myfile, iso_mount)
1055 if grml_file is None:
1056 logging.warn("Warning: myfile %s could not be found - can not install it")
1058 logging.debug("rsync -rlptDH --inplace %s %s" % (grml_file, grml_web_target + grml_file))
1059 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", grml_file, grml_web_target + myfile])
1062 grml_webimg_target = grml_web_target + '/images/'
1063 execute(mkdir, grml_webimg_target)
1065 for myfile in 'button.png', 'favicon.png', 'linux.jpg', 'logo.png':
1066 grml_file = search_file(myfile, iso_mount)
1067 if grml_file is None:
1068 logging.warn("Warning: myfile %s could not be found - can not install it")
1070 logging.debug("rsync -rlptDH --inplace %s %s" % (grml_file, grml_webimg_target + grml_file))
1071 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", grml_file, grml_webimg_target + myfile])
1075 def copy_addons(iso_mount, target):
1076 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1078 @iso_mount: path where a grml ISO is mounted on
1079 @target: path where grml's main files should be copied to"""
1081 addons = target + '/boot/addons/'
1082 execute(mkdir, addons)
1084 # grub all-in-one image
1085 allinoneimg = search_file('allinone.img', iso_mount)
1086 if allinoneimg is None:
1087 logging.warn("Warning: allinone.img not found (that's fine if you don't need it)")
1089 logging.debug("rsync -rlptDH --inplace %s %s" % (allinoneimg, addons + '/allinone.img'))
1090 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", allinoneimg, addons + 'allinone.img'])
1094 bsdimg = search_file('bsd4grml', iso_mount)
1096 logging.warn("Warning: bsd4grml not found (that's fine if you don't need it)")
1098 logging.debug("rsync -rlptDH --inplace %s %s" % (bsdimg, addons + '/'))
1099 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", bsdimg, addons + '/'])
1103 balderimg = search_file('balder10.imz', iso_mount)
1104 if balderimg is None:
1105 logging.warn("Warning: balder10.imz not found (that's fine if you don't need it)")
1107 logging.debug("rsync -rlptDH --inplace %s %s" % (balderimg, addons + '/balder10.imz'))
1108 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", balderimg, addons + 'balder10.imz'])
1111 # install hdt and pci.ids only when using syslinux (grub doesn't support it)
1112 if options.syslinux:
1113 # hdt (hardware detection tool) image
1114 hdtimg = search_file('hdt.c32', iso_mount)
1116 logging.debug("rsync -rlptDH --inplace %s %s" % (hdtimg, addons + '/hdt.c32'))
1117 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", hdtimg, addons + '/hdt.c32'])
1121 picids = search_file('pci.ids', iso_mount)
1123 logging.debug("rsync -rlptDH --inplace %s %s" % (picids, addons + '/pci.ids'))
1124 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", picids, addons + '/pci.ids'])
1128 memdiskimg = search_file('memdisk', iso_mount)
1129 if memdiskimg is None:
1130 logging.warn("Warning: memdisk not found (that's fine if you don't need it)")
1132 logging.debug("rsync -rlptDH --inplace %s %s" % (memdiskimg, addons + '/memdisk'))
1133 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", memdiskimg, addons + 'memdisk'])
1137 memtestimg = search_file('memtest', iso_mount)
1138 if memtestimg is None:
1139 logging.warn("Warning: memtest not found (that's fine if you don't need it)")
1141 logging.debug("rsync -rlptDH --inplace %s %s" % (memtestimg, addons + '/memtest'))
1142 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", memtestimg, addons + 'memtest'])
1146 def copy_bootloader_files(iso_mount, target):
1147 """copy grml's bootloader files to a given target
1149 @iso_mount: path where a grml ISO is mounted on
1150 @target: path where grml's main files should be copied to"""
1152 syslinux_target = target + '/boot/syslinux/'
1153 execute(mkdir, syslinux_target)
1155 logo = search_file('logo.16', iso_mount)
1156 logging.debug("rsync -rlptDH --inplace %s %s" % (logo, syslinux_target + 'logo.16'))
1157 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", logo, syslinux_target + 'logo.16'])
1160 for ffile in 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10':
1161 bootsplash = search_file(ffile, iso_mount)
1162 logging.debug("rsync -rlptDH --inplace %s %s" % (bootsplash, syslinux_target + ffile))
1163 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", bootsplash, syslinux_target + ffile])
1166 grub_target = target + '/boot/grub/'
1167 execute(mkdir, grub_target)
1169 if not os.path.isfile(GRML2USB_BASE + "/grub/splash.xpm.gz"):
1170 logging.critical("Error: %s/grub/splash.xpm.gz can not be read." % (GRML2USB_BASE, ))
1171 logging.critical("Please make sure you've the grml2usb Debian package installed!")
1174 logging.debug("rsync -rlptDH --inplace %s/grub/splash.xpm.gz %s%s" % (GRML2USB_BASE, grub_target, 'splash.xpm.gz'))
1175 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", GRML2USB_BASE + '/grub/splash.xpm.gz',
1176 grub_target + 'splash.xpm.gz'])
1179 # grml splash in grub
1180 if os.path.isfile(GRML2USB_BASE + "/grub/grml.png"):
1181 logging.debug("rsync -rlptDH --inplace %s/grub/grml.png to %s%s" % (GRML2USB_BASE, grub_target, 'grml.png'))
1182 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", GRML2USB_BASE + '/grub/grml.png',
1183 grub_target + 'grml.png'])
1186 # font file for graphical bootsplash in grub
1187 if os.path.isfile("/usr/share/grub/ascii.pf2"):
1188 logging.debug("rsync -rlptDH --inplace /usr/share/grub/ascii.pf2 to %s" % grub_target + 'ascii.pf2')
1189 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", '/usr/share/grub/ascii.pf2',
1190 grub_target + 'ascii.pf2'])
1194 def install_iso_files(grml_flavour, iso_mount, device, target):
1195 """Copy files from ISO to given target
1197 @grml_flavour: name of grml flavour the configuration should be generated for
1198 @iso_mount: path where a grml ISO is mounted on
1199 @device: device/partition where bootloader should be installed to
1200 @target: path where grml's main files should be copied to"""
1202 # TODO => several improvements:
1203 # * make sure grml_flavour, iso_mount, target are set when the function is called, otherwise raise exception
1204 # * provide alternative search_file() if file information is stored in a config.ini file?
1205 # * catch "install: .. No space left on device" & CO
1209 elif not options.bootloaderonly:
1210 logging.info("Copying files. This might take a while....")
1212 copy_system_files(grml_flavour, iso_mount, target)
1213 copy_grml_files(iso_mount, target)
1214 except CriticalException, error:
1215 logging.critical("Execution failed: %s", error)
1218 if not options.skipaddons:
1219 if grml_flavour.endswith('-small'):
1220 logging.info("Note: grml-small doesn't provide any addons, not installing them therefore.")
1222 copy_addons(iso_mount, target)
1224 if not options.copyonly:
1225 copy_bootloader_files(iso_mount, target)
1227 if not options.dryrun:
1228 handle_bootloader_config(grml_flavour, device, target)
1230 # make sure we sync filesystems before returning
1231 proc = subprocess.Popen(["sync"])
1235 def uninstall_files(device):
1236 """Get rid of all grml files on specified device
1238 @device: partition where grml2usb files should be removed from"""
1240 # TODO - not implemented yet
1241 logging.critical("TODO: uninstalling files from %s not yet implement, sorry." % device)
1244 def identify_grml_flavour(mountpath):
1245 """Get name of grml flavour
1247 @mountpath: path where the grml ISO is mounted to
1248 @return: name of grml-flavour"""
1250 version_file = search_file('grml-version', mountpath)
1252 if version_file == "":
1253 logging.critical("Error: could not find grml-version file.")
1257 tmpfile = open(version_file, 'r')
1258 grml_info = tmpfile.readline()
1259 grml_flavour = re.match(r'[\w-]*', grml_info).group()
1263 logging.critical("Unexpected error:", sys.exc_info()[0])
1269 def handle_grub1_config(grml_flavour, install_partition, grub_target, bootopt):
1270 """Main handler for generating grub1 configuration
1272 @grml_flavour: name of grml flavour the configuration should be generated for
1273 @install_partition: partition number for use in (hd0,X)
1274 @grub_target: path of grub's configuration files
1275 @bootoptions: additional bootoptions that should be used by default"""
1278 grub1_cfg = grub_target + 'menu.lst'
1279 logging.debug("Creating grub1 configuration file (menu.lst)")
1281 # install main configuration only *once*, no matter how many ISOs we have:
1282 if os.path.isfile(grub1_cfg):
1283 string = open(grub1_cfg).readline()
1284 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1285 if not re.match(main_identifier, string):
1286 grub1_config_file = open(grub1_cfg, 'w')
1287 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1288 grub1_config_file.close()
1290 grub1_config_file = open(grub1_cfg, 'w')
1291 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1292 grub1_config_file.close()
1294 grub_flavour_config = True
1295 if os.path.isfile(grub1_cfg):
1296 string = open(grub1_cfg).readlines()
1297 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1299 if flavour.match(line):
1300 grub_flavour_config = False
1302 if grub_flavour_config:
1303 grub1_config_file = open(grub1_cfg, 'a')
1304 grub1_config_file.write(generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootopt))
1305 grub1_config_file.close()
1308 def handle_grub2_config(grml_flavour, grub_target, bootopt):
1309 """Main handler for generating grub2 configuration
1311 @grml_flavour: name of grml flavour the configuration should be generated for
1312 @grub_target: path of grub's configuration files
1313 @bootoptions: additional bootoptions that should be used by default"""
1316 grub2_cfg = grub_target + 'grub.cfg'
1317 logging.debug("Creating grub2 configuration file (grub.lst)")
1321 # install main configuration only *once*, no matter how many ISOs we have:
1322 grub_flavour_is_default = False
1323 if os.path.isfile(grub2_cfg):
1324 string = open(grub2_cfg).readline()
1325 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1326 if not re.match(main_identifier, string):
1327 grub2_config_file = open(grub2_cfg, 'w')
1328 GRML_DEFAULT = grml_flavour
1329 grub_flavour_is_default = True
1330 grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1331 grub2_config_file.close()
1333 grub2_config_file = open(grub2_cfg, 'w')
1334 GRML_DEFAULT = grml_flavour
1335 grub_flavour_is_default = True
1336 grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1337 grub2_config_file.close()
1339 # install flavour specific configuration only *once* as well
1340 grub_flavour_config = True
1341 if os.path.isfile(grub2_cfg):
1342 string = open(grub2_cfg).readlines()
1343 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1345 if flavour.match(line):
1346 grub_flavour_config = False
1348 if grub_flavour_config:
1349 grub2_config_file = open(grub2_cfg, 'a')
1350 # display only if the grml flavour isn't the default
1351 if not grub_flavour_is_default:
1352 GRML_FLAVOURS.add(grml_flavour)
1353 grub2_config_file.write(generate_flavour_specific_grub2_config(grml_flavour, bootopt))
1354 grub2_config_file.close()
1357 def handle_grub_config(grml_flavour, device, target):
1358 """Main handler for generating grub (v1 and v2) configuration
1360 @grml_flavour: name of grml flavour the configuration should be generated for
1361 @device: device/partition where grub should be installed to
1362 @target: path of grub's configuration files"""
1364 logging.debug("Generating grub configuration")
1366 grub_target = target + '/boot/grub/'
1367 execute(mkdir, grub_target)
1369 if os.path.isdir(device):
1370 install_grub1_partition = None
1372 if device[-1:].isdigit():
1373 install_grub1_partition = int(device[-1:]) - 1
1375 raise CriticalException("error validating partition schema (raw device?)")
1377 # do NOT write "None" in kernel cmdline
1378 if options.bootoptions is None:
1381 bootopt = options.bootoptions
1384 handle_grub1_config(grml_flavour, install_grub1_partition, grub_target, bootopt)
1386 handle_grub2_config(grml_flavour, grub_target, bootopt)
1389 def handle_syslinux_config(grml_flavour, target):
1390 """Main handler for generating syslinux configuration
1392 @grml_flavour: name of grml flavour the configuration should be generated for
1393 @target: path of syslinux's configuration files"""
1395 # do NOT write "None" in kernel cmdline
1396 if options.bootoptions is None:
1399 bootopt = options.bootoptions
1401 logging.debug("Generating syslinux configuration")
1402 syslinux_target = target + '/boot/syslinux/'
1403 # should be present via copy_bootloader_files(), but make sure it exits:
1404 execute(mkdir, syslinux_target)
1405 syslinux_cfg = syslinux_target + 'syslinux.cfg'
1409 # install main configuration only *once*, no matter how many ISOs we have:
1410 syslinux_flavour_is_default = False
1411 if os.path.isfile(syslinux_cfg):
1412 string = open(syslinux_cfg).readline()
1413 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1414 if not re.match(main_identifier, string):
1415 syslinux_config_file = open(syslinux_cfg, 'w')
1416 GRML_DEFAULT = grml_flavour
1417 syslinux_flavour_is_default = True
1418 syslinux_config_file.write(generate_main_syslinux_config(grml_flavour, bootopt))
1419 syslinux_config_file.close()
1421 syslinux_config_file = open(syslinux_cfg, 'w')
1422 GRML_DEFAULT = grml_flavour
1423 syslinux_flavour_is_default = True
1424 syslinux_config_file.write(generate_main_syslinux_config(grml_flavour, bootopt))
1425 syslinux_config_file.close()
1427 # install flavour specific configuration only *once* as well
1428 syslinux_flavour_config = True
1429 if os.path.isfile(syslinux_cfg):
1430 string = open(syslinux_cfg).readlines()
1431 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1433 if flavour.match(line):
1434 syslinux_flavour_config = False
1436 if syslinux_flavour_config:
1437 syslinux_config_file = open(syslinux_cfg, 'a')
1438 # display only if the grml flavour isn't the default
1439 if not syslinux_flavour_is_default:
1440 GRML_FLAVOURS.add(grml_flavour)
1441 syslinux_config_file.write(generate_flavour_specific_syslinux_config(grml_flavour, bootopt))
1442 syslinux_config_file.close()
1444 logging.debug("Generating isolinux/syslinux splash %s" % syslinux_target + 'boot.msg')
1445 isolinux_splash = open(syslinux_target + 'boot.msg', 'w')
1446 isolinux_splash.write(generate_isolinux_splash(grml_flavour))
1447 isolinux_splash.close()
1450 def handle_bootloader_config(grml_flavour, device, target):
1451 """Main handler for generating bootloader's configuration
1453 @grml_flavour: name of grml flavour the configuration should be generated for
1454 @device: device/partition where bootloader should be installed to
1455 @target: path of bootloader's configuration files"""
1457 if options.skipsyslinuxconfig:
1458 logging.info("Skipping generation of syslinux configuration as requested.")
1461 handle_syslinux_config(grml_flavour, target)
1462 except CriticalException, error:
1463 logging.critical("Fatal: %s" % error)
1466 if options.skipgrubconfig:
1467 logging.info("Skipping generation of grub configuration as requested.")
1470 handle_grub_config(grml_flavour, device, target)
1471 except CriticalException, error:
1472 logging.critical("Fatal: %s" % error)
1475 def handle_dir(live_image, device):
1476 """Main logic for copying files of the currently running grml system.
1478 @live_image: directory where currently running live system resides (usually /live/image)
1479 @device: partition where the specified ISO should be installed to"""
1481 logging.info("Using %s as install base" % live_image)
1483 if os.path.isdir(device):
1484 logging.info("Specified target is a directory, therefore not mounting.")
1485 device_mountpoint = device
1486 remove_device_mountpoint = False
1488 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1489 register_tmpfile(device_mountpoint)
1490 remove_device_mountpoint = True
1492 mount(device, device_mountpoint, "")
1493 except CriticalException, error:
1494 logging.critical("Fatal: %s" % error)
1500 grml_flavour = identify_grml_flavour(live_image)
1501 logging.info("Identified grml flavour \"%s\"." % grml_flavour)
1502 install_iso_files(grml_flavour, live_image, device, device_mountpoint)
1504 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1507 if remove_device_mountpoint:
1509 unmount(device_mountpoint, "")
1510 if os.path.isdir(device_mountpoint):
1511 os.rmdir(device_mountpoint)
1512 unregister_tmpfile(device_mountpoint)
1513 except CriticalException, error:
1514 logging.critical("Fatal: %s" % error)
1518 def handle_iso(iso, device):
1519 """Main logic for mounting ISOs and copying files.
1521 @iso: full path to the ISO that should be installed to the specified device
1522 @device: partition where the specified ISO should be installed to"""
1524 logging.info("Using ISO %s" % iso)
1526 iso_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1527 register_tmpfile(iso_mountpoint)
1528 remove_iso_mountpoint = True
1530 if not os.path.isfile(iso):
1531 logging.critical("Fatal: specified ISO %s could not be read" % iso)
1536 mount(iso, iso_mountpoint, ["-o", "loop", "-t", "iso9660"])
1537 except CriticalException, error:
1538 logging.critical("Fatal: %s" % error)
1541 if os.path.isdir(device):
1542 logging.info("Specified target is a directory, therefore not mounting.")
1543 device_mountpoint = device
1544 remove_device_mountpoint = False
1547 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1548 register_tmpfile(device_mountpoint)
1549 remove_device_mountpoint = True
1551 mount(device, device_mountpoint, "")
1552 except CriticalException, error:
1553 logging.critical("Fatal: %s" % error)
1559 grml_flavour = identify_grml_flavour(iso_mountpoint)
1560 logging.info("Identified grml flavour \"%s\"." % grml_flavour)
1561 install_iso_files(grml_flavour, iso_mountpoint, device, device_mountpoint)
1563 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1566 if os.path.isdir(iso_mountpoint) and remove_iso_mountpoint:
1567 unmount(iso_mountpoint, "")
1568 os.rmdir(iso_mountpoint)
1569 unregister_tmpfile(iso_mountpoint)
1570 if remove_device_mountpoint:
1572 unmount(device_mountpoint, "")
1573 if os.path.isdir(device_mountpoint):
1574 os.rmdir(device_mountpoint)
1575 unregister_tmpfile(device_mountpoint)
1576 except CriticalException, error:
1577 logging.critical("Fatal: %s" % error)
1581 def handle_mbr(device):
1582 """Main handler for installing master boot record (MBR)
1584 @device: device where the MBR should be installed to"""
1587 logging.info("Would install MBR")
1590 if device[-1:].isdigit():
1591 mbr_device = re.match(r'(.*?)\d*$', device).group(1)
1592 partition_number = int(device[-1:]) - 1
1593 skip_install_mir_mbr = False
1595 # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1596 # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1597 if mbr_device == "/dev/loop":
1599 logging.info("Detected loop device - using %s as MBR device therefore" % mbr_device)
1600 skip_install_mir_mbr = True
1603 if options.syslinuxmbr:
1604 handle_syslinux_mbr(mbr_device)
1605 elif not skip_install_mir_mbr:
1607 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrmgr', mbr_device, partition_number, True)
1609 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrldr', mbr_device, partition_number, False)
1610 except IOError, error:
1611 logging.critical("Execution failed: %s", error)
1613 except Exception, error:
1614 logging.critical("Execution failed: %s", error)
1618 def handle_vfat(device):
1619 """Check for FAT specific settings and options
1621 @device: device that should checked / formated"""
1623 # make sure we have mkfs.vfat available
1624 if options.fat16 and not options.force:
1625 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1626 logging.critical('Sorry, mkfs.vfat not available. Exiting.')
1627 logging.critical('Please make sure to install dosfstools.')
1630 # make sure the user is aware of what he is doing
1631 f = raw_input("Are you sure you want to format the specified partition with fat16? y/N ")
1632 if f == "y" or f == "Y":
1633 logging.info("Note: you can skip this question using the option --force")
1636 except CriticalException, error:
1637 logging.critical("Execution failed: %s", error)
1642 # check for vfat filesystem
1643 if device is not None and not os.path.isdir(device):
1645 check_for_fat(device)
1646 except CriticalException, error:
1647 logging.critical("Execution failed: %s", error)
1650 if not os.path.isdir(device) and not check_for_usbdevice(device):
1651 print "Warning: the specified device %s does not look like a removable usb device." % device
1652 f = raw_input("Do you really want to continue? y/N ")
1653 if f == "y" or f == "Y":
1659 def handle_compat_warning(device):
1660 """Backwards compatible checks
1662 @device: device that should be checked"""
1664 # make sure we can replace old grml2usb script and warn user when using old way of life:
1665 if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
1666 print "Warning: the semantics of grml2usb has changed."
1667 print "Instead of using grml2usb /path/to/iso %s you might" % device
1668 print "want to use grml2usb /path/to/iso /dev/... instead."
1669 print "Please check out the grml2usb manpage for details."
1670 f = raw_input("Do you really want to continue? y/N ")
1671 if f == "y" or f == "Y":
1677 def handle_logging():
1678 """Log handling and configuration"""
1680 if options.verbose and options.quiet:
1681 parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
1684 FORMAT = "Debug: %(asctime)-15s %(message)s"
1685 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1687 FORMAT = "Critical: %(message)s"
1688 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1690 FORMAT = "%(message)s"
1691 logging.basicConfig(level=logging.INFO, format=FORMAT)
1694 def handle_bootloader(device):
1695 """wrapper for installing bootloader
1697 @device: device where bootloader should be installed to"""
1699 # Install bootloader only if not using the --copy-only option
1700 if options.copyonly:
1701 logging.info("Not installing bootloader and its files as requested via option copyonly.")
1702 elif os.path.isdir(device):
1703 logging.info("Not installing bootloader as %s is a directory." % device)
1705 install_bootloader(device)
1709 """Main function [make pylint happy :)]"""
1712 print os.path.basename(sys.argv[0]) + " " + PROG_VERSION
1716 parser.error("invalid usage")
1721 # make sure we have the appropriate permissions
1724 logging.info("Executing grml2usb version %s", PROG_VERSION)
1727 logging.info("Running in simulation mode as requested via option dry-run.")
1729 # specified arguments
1730 device = args[len(args) - 1]
1731 isos = args[0:len(args) - 1]
1733 if not os.path.isdir(device):
1734 if device[-1:].isdigit():
1735 if int(device[-1:]) > 4 or device[-2:].isdigit():
1736 logging.critical("Fatal: installation on partition number >4 not supported. (BIOS won't support it.)")
1739 if os.path.exists(device):
1740 logging.critical("Fatal: installation on raw device not supported. (BIOS won't support it.)")
1743 if not which("rsync"):
1744 logging.critical("Fatal: rsync not available, can not continue - sorry.")
1747 # provide upgrade path
1748 handle_compat_warning(device)
1750 # check for vfat partition
1753 # main operation (like installing files)
1755 if os.path.isdir(iso):
1756 handle_dir(iso, device)
1758 handle_iso(iso, device)
1761 if not os.path.isdir(device):
1762 if not options.skipmbr:
1765 handle_bootloader(device)
1767 logging.info("Note: grml flavour %s was installed as the default booting system." % GRML_DEFAULT)
1769 for flavour in GRML_FLAVOURS:
1770 logging.info("Note: you can boot flavour %s using '%s' on the commandline." % (flavour, flavour))
1772 # finally be politely :)
1773 logging.info("Finished execution of grml2usb (%s). Have fun with your grml system." % PROG_VERSION)
1776 if __name__ == "__main__":
1779 except KeyboardInterrupt:
1780 logging.info("Received KeyboardInterrupt")
1783 ## END OF FILE #################################################################
1784 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8