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
21 PROG_VERSION = "0.9.2"
22 MOUNTED = set() # register mountpoints
23 TMPFILES = set() # register tmpfiles
24 DATESTAMP = time.mktime(datetime.datetime.now().timetuple()) # unique identifier for syslinux.cfg
27 USAGE = "Usage: %prog [options] <[ISO[s] | /live/image]> </dev/sdX#>\n\
29 %prog installs grml ISO[s] to an USB device to be able to boot from it.\n\
30 Make sure you have at least one grml ISO or a running grml system (/live/image),\n\
31 grub or syslinux and root access.\n\
33 Execute %prog --help for usage hints, further information can be found in: man grml2usb"
35 # pylint: disable-msg=C0103
36 parser = OptionParser(usage=USAGE)
37 parser.add_option("--bootoptions", dest="bootoptions",
38 action="store", type="string",
39 help="use specified bootoptions as default")
40 parser.add_option("--bootloader-only", dest="bootloaderonly", action="store_true",
41 help="do not copy files but just install a bootloader")
42 parser.add_option("--copy-only", dest="copyonly", action="store_true",
43 help="copy files only but do not install bootloader")
44 parser.add_option("--dry-run", dest="dryrun", action="store_true",
45 help="avoid executing commands")
46 parser.add_option("--fat16", dest="fat16", action="store_true",
47 help="format specified partition with FAT16")
48 parser.add_option("--force", dest="force", action="store_true",
49 help="force any actions requiring manual interaction")
50 #parser.add_option("--initrd", dest="initrd", action="store", type="string",
51 # help="install specified initrd instead of the default [TODO - not implemented yet]")
52 #parser.add_option("--kernel", dest="kernel", action="store", type="string",
53 # help="install specified kernel instead of the default [TODO - not implemented yet]")
54 parser.add_option("--lilo-binary", dest="lilobin", action="store", type="string",
55 help="lilo executable to be used for installing MBR")
56 parser.add_option("--mbr-manager", dest="mbrmgr", action="store_true",
57 help="enable interactive boot manager menu in MBR")
58 parser.add_option("--quiet", dest="quiet", action="store_true",
59 help="do not output anything but just errors on console")
60 parser.add_option("--skip-addons", dest="skipaddons", action="store_true",
61 help="do not install /boot/addons/ files")
62 parser.add_option("--skip-mbr", dest="skipmbr", action="store_true",
63 help="do not install a master boot record (MBR) on the device")
64 parser.add_option("--syslinux", dest="syslinux", action="store_true",
65 help="install syslinux bootloader instead of grub")
66 parser.add_option("--syslinux-mbr", dest="syslinuxmbr", action="store_true",
67 help="install syslinux master boot record (MBR) instead of default")
68 #parser.add_option("--squashfs", dest="squashfs", action="store", type="string",
69 # help="install specified squashfs file instead of the default [TODO - not implemented yet]")
70 #parser.add_option("--uninstall", dest="uninstall", action="store_true",
71 # help="remove grml ISO files from specified device [TODO - not implemented yet]")
72 parser.add_option("--verbose", dest="verbose", action="store_true",
73 help="enable verbose mode")
74 parser.add_option("-v", "--version", dest="version", action="store_true",
75 help="display version and exit")
76 (options, args) = parser.parse_args()
79 class CriticalException(Exception):
80 """Throw critical exception if the exact error is not known but fatal."
82 @Exception: message"""
87 """Cleanup function to make sure there aren't any mounted devices left behind.
90 logging.info("Cleaning up before exiting...")
91 proc = subprocess.Popen(["sync"])
95 for device in MOUNTED:
97 # ignore: RuntimeError: Set changed size during iteration
99 logging.debug('caught expection RuntimeError, ignoring')
102 def register_tmpfile(path):
104 TODO - not implemented yet
110 def unregister_tmpfile(path):
112 TODO - not implemented yet
116 TMPFILES.remove(path)
119 def register_mountpoint(target):
120 """register specified target in a set() for handling clean exiting
122 @target: destination target of mountpoint
128 def unregister_mountpoint(target):
129 """unregister specified target in a set() for handling clean exiting
131 @target: destination target of mountpoint
134 if target in MOUNTED:
135 MOUNTED.remove(target)
138 def get_function_name(obj):
139 """Helper function for use in execute() to retrive name of a function
141 @obj: the function object
143 if not (isroutine(obj) or isclass(obj)):
145 return obj.__module__ + '.' + obj.__name__
148 def execute(f, *exec_arguments):
149 """Wrapper for executing a command. Either really executes
150 the command (default) or when using --dry-run commandline option
151 just displays what would be executed."""
152 # usage: execute(subprocess.Popen, (["ls", "-la"]))
153 # TODO: doesn't work for proc = execute(subprocess.Popen...() -> any ideas?
155 # pylint: disable-msg=W0141
156 logging.debug('dry-run only: %s(%s)' % (get_function_name(f), ', '.join(map(repr, exec_arguments))))
158 # pylint: disable-msg=W0142
159 return f(*exec_arguments)
163 """Check whether a given file can be executed
165 @fpath: full path to file
167 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
171 """Check whether a given program is available in PATH
173 @program: name of executable"""
174 fpath = os.path.split(program)[0]
179 for path in os.environ["PATH"].split(os.pathsep):
180 exe_file = os.path.join(path, program)
187 def search_file(filename, search_path='/bin' + os.pathsep + '/usr/bin'):
188 """Given a search path, find file
190 @filename: name of file to search for
191 @search_path: path where searching for the specified filename"""
193 paths = search_path.split(os.pathsep)
194 current_dir = '' # make pylint happy :)
196 # pylint: disable-msg=W0612
197 for current_dir, directories, files in os.walk(path):
198 if os.path.exists(os.path.join(current_dir, filename)):
202 return os.path.abspath(os.path.join(current_dir, filename))
207 def check_uid_root():
208 """Check for root permissions"""
209 if not os.geteuid()==0:
210 sys.exit("Error: please run this script with uid 0 (root).")
213 def mkfs_fat16(device):
214 """Format specified device with VFAT/FAT16 filesystem.
216 @device: partition that should be formated"""
218 # syslinux -d boot/isolinux /dev/sdb1
219 logging.info("Formating partition with fat16 filesystem")
220 logging.debug("mkfs.vfat -F 16 %s" % device)
221 proc = subprocess.Popen(["mkfs.vfat", "-F", "16", device])
223 if proc.returncode != 0:
224 raise CriticalException("error executing mkfs.vfat")
227 def generate_main_grub2_config(grml_flavour, install_partition, bootoptions):
228 """Generate grub2 configuration for use via grub.cfg
230 @grml_flavour: name of grml flavour the configuration should be generated for
231 @install_partition: partition number for use in (hd0,X)
232 @bootoptions: additional bootoptions that should be used by default"""
234 local_datestamp = DATESTAMP
237 ## main grub2 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
243 if font (hd0,%(install_partition)s)/boot/grub/ascii.pff ; then
251 if background_image (hd0,%(install_partition)s)/boot/grub/grml.png ; then
252 set color_normal=black/black
253 set color_highlight=red/black
255 set menu_color_normal=white/black
256 set menu_color_highlight=black/yellow
259 menuentry "%(grml_flavour)s (default)" {
260 set root=(hd0,%(install_partition)s)
261 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off lang=us vga=791 quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
262 initrd /boot/release/%(grml_flavour)s/initrd.gz
265 menuentry "Memory test (memtest86+)" {
266 set root=(hd0,%(install_partition)s)
267 linux /boot/addons/memtest
270 menuentry "Grub - all in one image" {
271 set root=(hd0,%(install_partition)s)
272 linux /boot/addons/memdisk
273 initrd /boot/addons/allinone.img
276 menuentry "FreeDOS" {
277 set root=(hd0,%(install_partition)s)
278 linux /boot/addons/memdisk
279 initrd /boot/addons/balder10.imz
283 set root=(hd0,%(install_partition)s)
284 linux /boot/addons/bsd4grml/ldbsd.com
287 menuentry "Boot OS of first partition on first disk" {
292 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
293 'bootoptions': bootoptions, 'install_partition': install_partition } )
296 def generate_flavour_specific_grub2_config(grml_flavour, install_partition, bootoptions):
297 """Generate grub2 configuration for use via grub.cfg
299 @grml_flavour: name of grml flavour the configuration should be generated for
300 @install_partition: partition number for use in (hd0,X)
301 @bootoptions: additional bootoptions that should be used by default"""
303 local_datestamp = DATESTAMP
306 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
307 menuentry "%(grml_flavour)s" {
308 set root=(hd0,%(install_partition)s)
309 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
310 initrd /boot/release/%(grml_flavour)s/initrd.gz
313 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
314 menuentry "%(grml_flavour)s2ram" {
315 set root=(hd0,%(install_partition)s)
316 linux /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 %(bootoptions)s
317 initrd /boot/release/%(grml_flavour)s/initrd.gz
320 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
321 menuentry "%(grml_flavour)s-debug" {
322 set root=(hd0,%(install_partition)s)
323 linux /boot/release/%(grml_flavour)s/linux26
324 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
327 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
328 menuentry "%(grml_flavour)s-x" {
329 set root=(hd0,%(install_partition)s)
330 linux /boot/release/%(grml_flavour)s/linux26
331 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
334 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
335 menuentry "%(grml_flavour)s-nofb" {
336 set root=(hd0,%(install_partition)s)
337 linux /boot/release/%(grml_flavour)s/linux26
338 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
341 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
342 menuentry "%(grml_flavour)s-failsafe" {
343 set root=(hd0,%(install_partition)s)
344 linux /boot/release/%(grml_flavour)s/linux26
345 initrd /boot/release/%(grml_flavour)s/initrd.gz apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal lang=us boot=live noautoconfig atapicd noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia maxcpus=1 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm %(bootoptions)s
348 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
349 menuentry "%(grml_flavour)s-forensic" {
350 set root=(hd0,%(install_partition)s)
351 linux /boot/release/%(grml_flavour)s/linux26
352 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 %(bootoptions)s
355 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
356 menuentry "%(grml_flavour)s-serial" {
357 set root=(hd0,%(install_partition)s)
358 linux /boot/release/%(grml_flavour)s/linux26
359 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
362 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
363 'bootoptions': bootoptions, 'install_partition': install_partition } )
366 def generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootoptions):
367 """Generate grub1 configuration for use via menu.lst
369 @grml_flavour: name of grml flavour the configuration should be generated for
370 @install_partition: partition number for use in (hd0,X)
371 @bootoptions: additional bootoptions that should be used by default"""
373 local_datestamp = DATESTAMP
376 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
377 title %(grml_flavour)s
378 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
379 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
381 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
382 title %(grml_flavour)s2ram
383 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 %(bootoptions)s
384 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
386 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
387 title %(grml_flavour)s-debug
388 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
389 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
391 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
392 title %(grml_flavour)s-x
393 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
394 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
396 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
397 title %(grml_flavour)s-nofb
398 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
399 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
401 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
402 title %(grml_flavour)s-failsafe
403 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 lang=us boot=live noautoconfig atapicd noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia maxcpus=1 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm %(bootoptions)s
404 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
406 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
407 title %(grml_flavour)s-forensic
408 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 %(bootoptions)s
409 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
411 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
412 title %(grml_flavour)s-serial
413 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
414 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
416 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
417 'bootoptions': bootoptions, 'install_partition': install_partition } )
420 def generate_main_grub1_config(grml_flavour, install_partition, bootoptions):
421 """Generate grub1 configuration for use via menu.lst
423 @grml_flavour: name of grml flavour the configuration should be generated for"""
425 local_datestamp = DATESTAMP
428 ## main grub1 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
431 # color red/blue green/black
432 splashimage=(hd0,%(install_partition)s)/boot/grub/splash.xpm.gz
437 title %(grml_flavour)s - Default boot (using 1024x768 framebuffer)
438 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off lang=us vga=791 quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/
439 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
441 title Memory test (memtest86+)
442 kernel (hd0,%(install_partition)s)/boot/addons/memtest
444 title Grub - all in one image
445 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
446 initrd (hd0,%(install_partition)s)/boot/addons/allinone.img
449 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
450 initrd (hd0,%(install_partition)s)/boot/addons/balder10.imz
453 kernel (hd0,%(install_partition)s)/boot/addons/bsd4grml/ldbsd.com
455 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
456 'bootoptions': bootoptions, 'install_partition': install_partition } )
459 def generate_isolinux_splash(grml_flavour):
460 """Generate bootsplash for isolinux/syslinux
462 @grml_flavour: name of grml flavour the configuration should be generated for"""
464 # TODO: adjust last bootsplash line (the one following the "Some information and boot ...")
466 grml_name = grml_flavour
469 \ f17
\f\18/boot/syslinux/logo.16
471 Some information and boot options available via keys F2 - F10. http://grml.org/
473 """ % {'grml_name': grml_name} )
476 def generate_main_syslinux_config(grml_flavour, bootoptions):
477 """Generate main configuration for use in syslinux.cfg
479 @grml_flavour: name of grml flavour the configuration should be generated for
480 @bootoptions: additional bootoptions that should be used by default"""
482 local_datestamp = DATESTAMP
485 ## main syslinux configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
486 # use this to control the bootup via a serial port
491 DISPLAY /boot/syslinux/boot.msg
492 F1 /boot/syslinux/boot.msg
501 F10 /boot/syslinux/f10
502 ## end of main configuration
504 ## global configuration
505 # the default option (using %(grml_flavour)s)
507 KERNEL /boot/release/%(grml_flavour)s/linux26
508 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
512 KERNEL /boot/addons/memtest
517 KERNEL /boot/addons/memdisk
518 APPEND initrd=/boot/addons/allinone.img
523 KERNEL /boot/addons/memdisk
524 APPEND initrd=/boot/addons/balder10.imz
529 KERNEL /boot/addons/bsd4grml/ldbsd.com
531 ## end of global configuration
532 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp, 'bootoptions': bootoptions} )
535 def generate_flavour_specific_syslinux_config(grml_flavour, bootoptions):
536 """Generate flavour specific configuration for use in syslinux.cfg
538 @grml_flavour: name of grml flavour the configuration should be generated for
539 @bootoptions: bootoptions that should be used as a default"""
541 local_datestamp = DATESTAMP
545 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
546 LABEL %(grml_flavour)s
547 KERNEL /boot/release/%(grml_flavour)s/linux26
548 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
550 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
551 LABEL %(grml_flavour)s2ram
552 KERNEL /boot/release/%(grml_flavour)s/linux26
553 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 %(bootoptions)s
555 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
556 LABEL %(grml_flavour)s-debug
557 KERNEL /boot/release/%(grml_flavour)s/linux26
558 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
560 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
561 LABEL %(grml_flavour)s-x
562 KERNEL /boot/release/%(grml_flavour)s/linux26
563 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
565 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
566 LABEL %(grml_flavour)s-nofb
567 KERNEL /boot/release/%(grml_flavour)s/linux26
568 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
570 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
571 LABEL %(grml_flavour)s-failsafe
572 KERNEL /boot/release/%(grml_flavour)s/linux26
573 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 lang=us boot=live noautoconfig atapicd noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia maxcpus=1 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm %(bootoptions)s
575 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
576 LABEL %(grml_flavour)s-forensic
577 KERNEL /boot/release/%(grml_flavour)s/linux26
578 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 %(bootoptions)s
580 # flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
581 LABEL %(grml_flavour)s-serial
582 KERNEL /boot/release/%(grml_flavour)s/linux26
583 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
584 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp, 'bootoptions': bootoptions} )
587 def install_grub(device):
588 """Install grub on specified device.
590 @mntpoint: mountpoint of device where grub should install its files to
591 @device: partition where grub should be installed to"""
594 logging.info("Would execute grub-install [--root-directory=mount_point] %s now.", device)
596 device_mountpoint = tempfile.mkdtemp()
597 register_tmpfile(device_mountpoint)
599 mount(device, device_mountpoint, "")
600 logging.debug("grub-install --recheck --no-floppy --root-directory=%s %s", device_mountpoint, device)
601 proc = subprocess.Popen(["grub-install", "--recheck", "--no-floppy",
602 "--root-directory=%s" % device_mountpoint, device], stdout=file(os.devnull, "r+"))
604 if proc.returncode != 0:
605 raise Exception("error executing grub-install")
606 except CriticalException, error:
607 logging.critical("Fatal: %s" % error)
612 unmount(device_mountpoint, "")
613 os.rmdir(device_mountpoint)
614 unregister_tmpfile(device_mountpoint)
617 def install_syslinux(device):
618 """Install syslinux on specified device.
620 @device: partition where syslinux should be installed to"""
623 logging.info("Would install syslinux as bootloader on %s", device)
626 # syslinux -d boot/isolinux /dev/sdb1
627 logging.info("Installing syslinux as bootloader")
628 logging.debug("syslinux -d boot/syslinux %s" % device)
629 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
631 if proc.returncode != 0:
632 raise CriticalException("Error executing syslinux (either try --fat16 or --grub?)")
635 def install_bootloader(device):
636 """Install bootloader on specified device.
638 @device: partition where bootloader should be installed to"""
640 # by default we use grub, so install syslinux only on request
643 install_syslinux(device)
644 except CriticalException, error:
645 logging.critical("Fatal: %s" % error)
651 except CriticalException, error:
652 logging.critical("Fatal: %s" % error)
657 def execute_lilo(lilo, device):
658 """execute lilo for activating the partitions in the MBR
660 @lilo: path of lilo executable
661 @device: device where lilo should be executed on"""
663 # to support -A for extended partitions:
664 logging.info("Activating partitions in MBR via lilo")
665 logging.debug("%s -S /dev/null -M %s ext" % (lilo, device))
666 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-M", device, "ext"])
668 if proc.returncode != 0:
669 raise Exception("error executing lilo")
671 # activate partition:
672 logging.debug("%s -S /dev/null -A %s 1" % (lilo, device))
673 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-A", device, "1"])
675 if proc.returncode != 0:
676 raise Exception("error executing lilo")
679 def install_syslinux_mbr(device):
680 """install syslinux's master boot record (MBR) on the specified device
682 @device: device where MBR of syslinux should be installed to"""
684 # lilo's mbr is broken, use the one from syslinux instead:
685 if not os.path.isfile("/usr/lib/syslinux/mbr.bin"):
686 raise Exception("/usr/lib/syslinux/mbr.bin can not be read")
688 logging.info("Installing syslinux MBR")
689 logging.debug("cat /usr/lib/syslinux/mbr.bin > %s" % device)
691 # TODO -> use Popen instead?
692 retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True)
694 logging.critical("Error copying MBR to device (%s)" % retcode)
695 except OSError, error:
696 logging.critical("Execution failed:", error)
699 def install_mir_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
700 """install 'mbr' master boot record (MBR) on a device
702 Retrieve the partition table from "device", install an MBR from
703 the "mbrtemplate" file, set the "partition" (0..3) active, and
704 install the result back to "device".
706 @mbrtemplate: default MBR file
708 @device: name of a file assumed to be a hard disc (or USB stick) image,
709 or something like "/dev/sdb"
710 @partition: must be a number between 0 and 3, inclusive
712 @mbrtemplate: must be a valid MBR file of at least 440 (or 439
713 if ismirbsdmbr) bytes.
715 @ismirbsdmbr: if set the partitions' active flags are not changed.
716 Instead, the MBR's default value is set accordingly."""
718 logging.info("Installing default MBR")
720 if not os.path.isfile(mbrtemplate):
721 logging.critical("Error: %s can not be read." % mbrtemplate)
722 raise CriticalException("Error installing MBR (either try --syslinux-mbr or install missing file?)")
724 if (partition < 0) or (partition > 3):
725 raise ValueError("partition must be between 0 and 3")
732 tmpf = tempfile.NamedTemporaryFile()
734 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1" % (device, tmpf.name))
735 proc = subprocess.Popen(["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"], stderr=file(os.devnull, "r+"))
737 if proc.returncode != 0:
738 raise Exception("error executing dd (first run)")
740 logging.debug("executing: dd if=%s of=%s bs=%s count=1 conv=notrunc" % (mbrtemplate,
741 tmpf.name, nmbrbytes))
742 proc = subprocess.Popen(["dd", "if=%s" % mbrtemplate, "of=%s" % tmpf.name, "bs=%s" % nmbrbytes,
743 "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+"))
745 if proc.returncode != 0:
746 raise Exception("error executing dd (second run)")
748 mbrcode = tmpf.file.read(512)
749 if len(mbrcode) < 512:
750 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
753 mbrcode = mbrcode[0:439] + chr(partition) + \
754 mbrcode[440:510] + "\x55\xAA"
756 actives = ["\x00", "\x00", "\x00", "\x00"]
757 actives[partition] = "\x80"
758 mbrcode = mbrcode[0:446] + actives[0] + \
759 mbrcode[447:462] + actives[1] + \
760 mbrcode[463:478] + actives[2] + \
761 mbrcode[479:494] + actives[3] + \
762 mbrcode[495:510] + "\x55\xAA"
766 tmpf.file.write(mbrcode)
769 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc" % (tmpf.name, device))
770 proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1",
771 "conv=notrunc"], stderr=file(os.devnull, "r+"))
773 if proc.returncode != 0:
774 raise Exception("error executing dd (third run)")
778 def handle_syslinux_mbr(device):
779 """Install syslinux master boot record on given device
781 @device: device where MBR should be installed to"""
783 if not is_writeable(device):
784 raise IOError("device not writeable for user")
786 # try to use system's lilo
790 # otherwise fall back to our static version
791 from platform import architecture
792 if architecture()[0] == '64bit':
793 lilo = '/usr/share/grml2usb/lilo/lilo.static.amd64'
795 lilo = '/usr/share/grml2usb/lilo/lilo.static.i386'
796 # finally prefer a specified lilo executable
798 lilo = options.lilobin
801 raise Exception("lilo executable can not be execute")
804 logging.info("Would install MBR running lilo and using syslinux.")
807 execute_lilo(lilo, device)
808 install_syslinux_mbr(device)
811 def is_writeable(device):
812 """Check if the device is writeable for the current user
814 @device: partition where bootloader should be installed to"""
818 #raise Exception("no device for checking write permissions")
820 if not os.path.exists(device):
823 return os.access(device, os.W_OK) and os.access(device, os.R_OK)
826 def mount(source, target, mount_options):
827 """Mount specified source on given target
829 @source: name of device/ISO that should be mounted
830 @target: directory where the ISO should be mounted to
831 @options: mount specific options"""
833 # note: options.dryrun does not work here, as we have to
834 # locate files and identify the grml flavour
836 for x in file('/proc/mounts').readlines():
837 if x.startswith(source):
838 raise CriticalException("Error executing mount: %s already mounted - please unmount before invoking grml2usb" % source)
840 if os.path.isdir(source):
841 logging.debug("Source %s is not a device, not mounting therefor." % source)
844 logging.debug("mount %s %s %s" % (mount_options, source, target))
845 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
847 if proc.returncode != 0:
848 raise CriticalException("Error executing mount (no filesystem on the partition?)")
850 logging.debug("register_mountpoint(%s)" % target)
851 register_mountpoint(target)
854 def unmount(target, unmount_options):
855 """Unmount specified target
857 @target: target where something is mounted on and which should be unmounted
858 @options: options for umount command"""
860 # make sure we unmount only already mounted targets
861 target_unmount = False
862 mounts = open('/proc/mounts').readlines()
863 mountstring = re.compile(".*%s.*" % re.escape(target))
865 if re.match(mountstring, line):
866 target_unmount = True
868 if not target_unmount:
869 logging.debug("%s not mounted anymore" % target)
871 logging.debug("umount %s %s" % (list(unmount_options), target))
872 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
874 if proc.returncode != 0:
875 raise Exception("Error executing umount")
877 logging.debug("unregister_mountpoint(%s)" % target)
878 unregister_mountpoint(target)
881 def check_for_usbdevice(device):
882 """Check whether the specified device is a removable USB device
884 @device: device name, like /dev/sda1 or /dev/sda
887 usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
888 usbdevice = os.path.realpath('/sys/class/block/' + usbdevice + '/removable')
889 if os.path.isfile(usbdevice):
890 is_usb = open(usbdevice).readline()
897 def check_for_fat(partition):
898 """Check whether specified partition is a valid VFAT/FAT16 filesystem
900 @partition: device name of partition"""
903 udev_info = subprocess.Popen(["/lib/udev/vol_id", "-t", partition],
904 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
905 filesystem = udev_info.communicate()[0].rstrip()
907 if udev_info.returncode == 2:
908 raise CriticalException("Failed to read device %s"
909 " (wrong UID/permissions or device/directory not present?)" % partition)
911 if options.syslinux and filesystem != "vfat":
912 raise CriticalException("Partition %s does not contain a FAT16 filesystem. (Use --fat16 or run mkfs.vfat %s)" % (partition, partition))
915 raise CriticalException("Sorry, /lib/udev/vol_id not available.")
918 def mkdir(directory):
919 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
921 # just silently pass as it's just fine it the directory exists
922 if not os.path.isdir(directory):
924 os.makedirs(directory)
925 # pylint: disable-msg=W0704
930 def copy_system_files(grml_flavour, iso_mount, target):
931 """copy grml's main files (like squashfs, kernel and initrd) to a given target
933 @grml_flavour: name of grml flavour the configuration should be generated for
934 @iso_mount: path where a grml ISO is mounted on
935 @target: path where grml's main files should be copied to"""
937 squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
939 logging.critical("Fatal: squashfs file not found")
941 squashfs_target = target + '/live/' + grml_flavour + '/'
942 execute(mkdir, squashfs_target)
943 logging.debug("cp %s %s" % (squashfs, squashfs_target + grml_flavour + '.squashfs'))
944 proc = subprocess.Popen(["cp", squashfs, squashfs_target + grml_flavour + ".squashfs"])
947 filesystem_module = search_file('filesystem.module', iso_mount)
948 if filesystem_module is None:
949 logging.critical("Fatal: filesystem.module not found")
951 logging.debug("cp %s %s" % (filesystem_module, squashfs_target + 'filesystem.module'))
952 proc = subprocess.Popen(["cp", filesystem_module, squashfs_target + 'filesystem.module'])
955 release_target = target + '/boot/release/' + grml_flavour
956 execute(mkdir, release_target)
958 kernel = search_file('linux26', iso_mount)
960 logging.critical("Fatal kernel not found")
962 logging.debug("cp %s %s" % (kernel, release_target + '/linux26'))
963 proc = subprocess.Popen(["cp", kernel, release_target + '/linux26'])
966 initrd = search_file('initrd.gz', iso_mount)
968 logging.critical("Fatal: initrd not found")
970 logging.debug("cp %s %s" % (initrd, release_target + '/initrd.gz'))
971 proc = subprocess.Popen(["cp", initrd, release_target + '/initrd.gz'])
975 def copy_grml_files(iso_mount, target):
976 """copy some minor grml files to a given target
978 @iso_mount: path where a grml ISO is mounted on
979 @target: path where grml's main files should be copied to"""
981 grml_target = target + '/grml/'
982 execute(mkdir, grml_target)
984 for myfile in 'grml-cheatcodes.txt', 'grml-version', 'LICENSE.txt', 'md5sums', 'README.txt':
985 grml_file = search_file(myfile, iso_mount)
986 if grml_file is None:
987 logging.warn("Warning: myfile %s could not be found - can not install it", myfile)
989 logging.debug("cp %s %s" % (grml_file, grml_target + grml_file))
990 proc = subprocess.Popen(["cp", grml_file, grml_target + myfile])
993 grml_web_target = grml_target + '/web/'
994 execute(mkdir, grml_web_target)
996 for myfile in 'index.html', 'style.css':
997 grml_file = search_file(myfile, iso_mount)
998 if grml_file is None:
999 logging.warn("Warning: myfile %s could not be found - can not install it")
1001 logging.debug("cp %s %s" % (grml_file, grml_web_target + grml_file))
1002 proc = subprocess.Popen(["cp", grml_file, grml_web_target + myfile])
1005 grml_webimg_target = grml_web_target + '/images/'
1006 execute(mkdir, grml_webimg_target)
1008 for myfile in 'button.png', 'favicon.png', 'linux.jpg', 'logo.png':
1009 grml_file = search_file(myfile, iso_mount)
1010 if grml_file is None:
1011 logging.warn("Warning: myfile %s could not be found - can not install it")
1013 logging.debug("cp %s %s" % (grml_file, grml_webimg_target + grml_file))
1014 proc = subprocess.Popen(["cp", grml_file, grml_webimg_target + myfile])
1018 def copy_addons(iso_mount, target):
1019 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1021 @iso_mount: path where a grml ISO is mounted on
1022 @target: path where grml's main files should be copied to"""
1024 addons = target + '/boot/addons/'
1025 execute(mkdir, addons)
1027 # grub all-in-one image
1028 allinoneimg = search_file('allinone.img', iso_mount)
1029 if allinoneimg is None:
1030 logging.warn("Warning: allinone.img not found - can not install it")
1032 logging.debug("cp %s %s" % (allinoneimg, addons + '/allinone.img'))
1033 proc = subprocess.Popen(["cp", allinoneimg, addons + 'allinone.img'])
1037 bsdimg = search_file('bsd4grml', iso_mount)
1039 logging.warn("Warning: bsd4grml not found - can not install it")
1041 logging.debug("cp -a %s %s" % (bsdimg, addons + '/'))
1042 proc = subprocess.Popen(["cp", "-a", bsdimg, addons + '/'])
1046 balderimg = search_file('balder10.imz', iso_mount)
1047 if balderimg is None:
1048 logging.warn("Warning: balder10.imz not found - can not install it")
1050 logging.debug("cp %s %s" % (balderimg, addons + '/balder10.imz'))
1051 proc = subprocess.Popen(["cp", balderimg, addons + 'balder10.imz'])
1055 memdiskimg = search_file('memdisk', iso_mount)
1056 if memdiskimg is None:
1057 logging.warn("Warning: memdisk not found - can not install it")
1059 logging.debug("cp %s %s" % (memdiskimg, addons + '/memdisk'))
1060 proc = subprocess.Popen(["cp", memdiskimg, addons + 'memdisk'])
1064 memtestimg = search_file('memtest', iso_mount)
1065 if memtestimg is None:
1066 logging.warn("Warning: memtest not found - can not install it")
1068 logging.debug("cp %s %s" % (memtestimg, addons + '/memtest'))
1069 proc = subprocess.Popen(["cp", memtestimg, addons + 'memtest'])
1073 def copy_bootloader_files(iso_mount, target):
1074 """copy grml's bootloader files to a given target
1076 @iso_mount: path where a grml ISO is mounted on
1077 @target: path where grml's main files should be copied to"""
1079 syslinux_target = target + '/boot/syslinux/'
1080 execute(mkdir, syslinux_target)
1082 logo = search_file('logo.16', iso_mount)
1083 logging.debug("cp %s %s" % (logo, syslinux_target + 'logo.16'))
1084 proc = subprocess.Popen(["cp", logo, syslinux_target + 'logo.16'])
1087 for ffile in 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10':
1088 bootsplash = search_file(ffile, iso_mount)
1089 logging.debug("cp %s %s" % (bootsplash, syslinux_target + ffile))
1090 proc = subprocess.Popen(["cp", bootsplash, syslinux_target + ffile])
1093 grub_target = target + '/boot/grub/'
1094 execute(mkdir, grub_target)
1096 if not os.path.isfile("/usr/share/grml2usb/grub/splash.xpm.gz"):
1097 logging.critical("Error: /usr/share/grml2usb/grub/splash.xpm.gz can not be read.")
1100 logging.debug("cp /usr/share/grml2usb/grub/splash.xpm.gz %s" % grub_target + 'splash.xpm.gz')
1101 proc = subprocess.Popen(["cp", '/usr/share/grml2usb/grub/splash.xpm.gz',
1102 grub_target + 'splash.xpm.gz'])
1105 # grml splash in grub
1106 if os.path.isfile("/usr/share/grml2usb/grub/grml.png"):
1107 logging.debug("cp /usr/share/grml2usb/grub/grml.png to %s" % grub_target + 'grml.png')
1108 proc = subprocess.Popen(["cp", '/usr/share/grml2usb/grub/grml.png',
1109 grub_target + 'grml.png'])
1112 # font file for graphical bootsplash in grub
1113 if os.path.isfile("/usr/share/grub/ascii.pff"):
1114 logging.debug("cp /usr/share/grub/ascii.pff to %s" % grub_target + 'ascii.pff')
1115 proc = subprocess.Popen(["cp", '/usr/share/grub/ascii.pff',
1116 grub_target + 'ascii.pff'])
1120 def install_iso_files(grml_flavour, iso_mount, device, target):
1121 """Copy files from ISO to given target
1123 @grml_flavour: name of grml flavour the configuration should be generated for
1124 @iso_mount: path where a grml ISO is mounted on
1125 @device: device/partition where bootloader should be installed to
1126 @target: path where grml's main files should be copied to"""
1128 # TODO => several improvements:
1129 # * make sure grml_flavour, iso_mount, target are set when the function is called, otherwise raise exception
1130 # * provide alternative search_file() if file information is stored in a config.ini file?
1131 # * catch "install: .. No space left on device" & CO
1134 logging.info("Would copy files to %s", iso_mount)
1136 elif not options.bootloaderonly:
1137 logging.info("Copying files. This might take a while....")
1138 copy_system_files(grml_flavour, iso_mount, target)
1139 copy_grml_files(iso_mount, target)
1141 if not options.skipaddons:
1142 copy_addons(iso_mount, target)
1144 if not options.copyonly:
1145 copy_bootloader_files(iso_mount, target)
1147 if not options.dryrun and not os.path.isdir(device):
1148 handle_bootloader_config(grml_flavour, device, target)
1150 # make sure we sync filesystems before returning
1151 proc = subprocess.Popen(["sync"])
1155 def uninstall_files(device):
1156 """Get rid of all grml files on specified device
1158 @device: partition where grml2usb files should be removed from"""
1160 # TODO - not implemented yet
1161 logging.critical("TODO: uninstalling files from %s not yet implement, sorry." % device)
1164 def identify_grml_flavour(mountpath):
1165 """Get name of grml flavour
1167 @mountpath: path where the grml ISO is mounted to
1168 @return: name of grml-flavour"""
1170 version_file = search_file('grml-version', mountpath)
1172 if version_file == "":
1173 logging.critical("Error: could not find grml-version file.")
1177 tmpfile = open(version_file, 'r')
1178 grml_info = tmpfile.readline()
1179 grml_flavour = re.match(r'[\w-]*', grml_info).group()
1183 logging.critical("Unexpected error:", sys.exc_info()[0])
1189 def handle_grub1_config(grml_flavour, install_partition, grub_target, bootopt):
1190 """Main handler for generating grub1 configuration
1192 @grml_flavour: name of grml flavour the configuration should be generated for
1193 @install_partition: partition number for use in (hd0,X)
1194 @grub_target: path of grub's configuration files
1195 @bootoptions: additional bootoptions that should be used by default"""
1198 grub1_cfg = grub_target + 'menu.lst'
1199 logging.debug("Creating grub1 configuration file (menu.lst)")
1201 # install main configuration only *once*, no matter how many ISOs we have:
1202 if os.path.isfile(grub1_cfg):
1203 string = open(grub1_cfg).readline()
1204 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1205 if not re.match(main_identifier, string):
1206 grub1_config_file = open(grub1_cfg, 'w')
1207 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1208 grub1_config_file.close()
1210 grub1_config_file = open(grub1_cfg, 'w')
1211 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1212 grub1_config_file.close()
1214 grub_flavour_config = True
1215 if os.path.isfile(grub1_cfg):
1216 string = open(grub1_cfg).readlines()
1217 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1219 if flavour.match(line):
1220 grub_flavour_config = False
1222 if grub_flavour_config:
1223 grub1_config_file = open(grub1_cfg, 'a')
1224 grub1_config_file.write(generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootopt))
1225 grub1_config_file.close( )
1228 def handle_grub2_config(grml_flavour, install_partition, grub_target, bootopt):
1229 """Main handler for generating grub2 configuration
1231 @grml_flavour: name of grml flavour the configuration should be generated for
1232 @install_partition: partition number for use in (hd0,X)
1233 @grub_target: path of grub's configuration files
1234 @bootoptions: additional bootoptions that should be used by default"""
1237 grub2_cfg = grub_target + 'grub.cfg'
1238 logging.debug("Creating grub2 configuration file (grub.lst)")
1240 # install main configuration only *once*, no matter how many ISOs we have:
1241 if os.path.isfile(grub2_cfg):
1242 string = open(grub2_cfg).readline()
1243 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1244 if not re.match(main_identifier, string):
1245 grub2_config_file = open(grub2_cfg, 'w')
1246 logging.info("Note: grml flavour %s is being installed as the default booting system." % grml_flavour)
1247 grub2_config_file.write(generate_main_grub2_config(grml_flavour, install_partition, bootopt))
1248 grub2_config_file.close()
1250 grub2_config_file = open(grub2_cfg, 'w')
1251 logging.info("Note: grml flavour %s is being installed as the default booting system." % grml_flavour)
1252 grub2_config_file.write(generate_main_grub2_config(grml_flavour, install_partition, bootopt))
1253 grub2_config_file.close()
1255 grub_flavour_config = True
1256 if os.path.isfile(grub2_cfg):
1257 string = open(grub2_cfg).readlines()
1258 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1260 if flavour.match(line):
1261 grub_flavour_config = False
1263 if grub_flavour_config:
1264 grub2_config_file = open(grub2_cfg, 'a')
1265 logging.info("Note: you can boot flavour %s using '%s' on the commandline." % (grml_flavour, grml_flavour))
1266 grub2_config_file.write(generate_flavour_specific_grub2_config(grml_flavour, install_partition, bootopt))
1267 grub2_config_file.close( )
1270 def handle_grub_config(grml_flavour, device, target):
1271 """Main handler for generating grub (v1 and v2) configuration
1273 @grml_flavour: name of grml flavour the configuration should be generated for
1274 @device: device/partition where grub should be installed to
1275 @target: path of grub's configuration files"""
1277 logging.debug("Generating grub configuration")
1279 grub_target = target + '/boot/grub/'
1280 execute(mkdir, grub_target)
1282 # we have to adjust root() inside grub configuration
1283 if device[-1:].isdigit():
1284 install_grub1_partition = int(device[-1:]) - 1
1285 install_grub2_partition = device[-1:]
1287 raise CriticalException("error validating partition schema (raw device?)")
1289 # do NOT write "None" in kernel cmdline
1290 if options.bootoptions is None:
1293 bootopt = options.bootoptions
1296 handle_grub1_config(grml_flavour, install_grub1_partition, grub_target, bootopt)
1298 handle_grub2_config(grml_flavour, install_grub2_partition, grub_target, bootopt)
1301 def handle_syslinux_config(grml_flavour, target):
1302 """Main handler for generating syslinux configuration
1304 @grml_flavour: name of grml flavour the configuration should be generated for
1305 @target: path of syslinux's configuration files"""
1307 # do NOT write "None" in kernel cmdline
1308 if options.bootoptions is None:
1311 bootopt = options.bootoptions
1313 logging.info("Generating syslinux configuration")
1314 syslinux_target = target + '/boot/syslinux/'
1315 # should be present via copy_bootloader_files(), but make sure it exits:
1316 execute(mkdir, syslinux_target)
1317 syslinux_cfg = syslinux_target + 'syslinux.cfg'
1319 # install main configuration only *once*, no matter how many ISOs we have:
1320 if os.path.isfile(syslinux_cfg):
1321 string = open(syslinux_cfg).readline()
1322 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1323 if not re.match(main_identifier, string):
1324 syslinux_config_file = open(syslinux_cfg, 'w')
1325 logging.info("Note: grml flavour %s is being installed as the default booting system." % grml_flavour)
1326 syslinux_config_file.write(generate_main_syslinux_config(grml_flavour, bootopt))
1327 syslinux_config_file.close()
1329 syslinux_config_file = open(syslinux_cfg, 'w')
1330 syslinux_config_file.write(generate_main_syslinux_config(grml_flavour, bootopt))
1331 syslinux_config_file.close()
1333 # install flavour specific configuration only *once* as well
1334 # kind of ugly - I'm pretty sure this could be smoother...
1335 syslinux_flavour_config = True
1336 if os.path.isfile(syslinux_cfg):
1337 string = open(syslinux_cfg).readlines()
1338 logging.info("Note: you can boot flavour %s using '%s' on the commandline." % (grml_flavour, grml_flavour))
1339 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1341 if flavour.match(line):
1342 syslinux_flavour_config = False
1344 if syslinux_flavour_config:
1345 syslinux_config_file = open(syslinux_cfg, 'a')
1346 syslinux_config_file.write(generate_flavour_specific_syslinux_config(grml_flavour, bootopt))
1347 syslinux_config_file.close( )
1349 logging.debug("Generating isolinux/syslinux splash %s" % syslinux_target + 'boot.msg')
1350 isolinux_splash = open(syslinux_target + 'boot.msg', 'w')
1351 isolinux_splash.write(generate_isolinux_splash(grml_flavour))
1352 isolinux_splash.close( )
1355 def handle_bootloader_config(grml_flavour, device, target):
1356 """Main handler for generating bootloader's configuration
1358 @grml_flavour: name of grml flavour the configuration should be generated for
1359 @device: device/partition where bootloader should be installed to
1360 @target: path of bootloader's configuration files"""
1362 if options.syslinux:
1363 handle_syslinux_config(grml_flavour, target)
1366 handle_grub_config(grml_flavour, device, target)
1367 except CriticalException, error:
1368 logging.critical("Fatal: %s" % error)
1372 def handle_iso(iso, device):
1373 """Main logic for mounting ISOs and copying files.
1375 @iso: full path to the ISO that should be installed to the specified device
1376 @device: partition where the specified ISO should be installed to"""
1378 logging.info("Using ISO %s" % iso)
1380 if os.path.isdir(iso):
1381 logging.critical("TODO: /live/image handling not yet implemented - sorry")
1384 iso_mountpoint = tempfile.mkdtemp()
1385 register_tmpfile(iso_mountpoint)
1386 remove_iso_mountpoint = True
1388 if not os.path.isfile(iso):
1389 logging.critical("Fatal: specified ISO %s could not be read" % iso)
1394 mount(iso, iso_mountpoint, ["-o", "loop", "-t", "iso9660"])
1395 except CriticalException, error:
1396 logging.critical("Fatal: %s" % error)
1399 if os.path.isdir(device):
1400 logging.info("Specified target is a directory, not mounting therefor.")
1401 device_mountpoint = device
1402 remove_device_mountpoint = False
1405 device_mountpoint = tempfile.mkdtemp()
1406 register_tmpfile(device_mountpoint)
1407 remove_device_mountpoint = True
1409 mount(device, device_mountpoint, "")
1410 except CriticalException, error:
1411 logging.critical("Fatal: %s" % error)
1416 grml_flavour = identify_grml_flavour(iso_mountpoint)
1417 logging.info("Identified grml flavour \"%s\"." % grml_flavour)
1418 install_iso_files(grml_flavour, iso_mountpoint, device, device_mountpoint)
1420 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1423 if os.path.isdir(iso_mountpoint) and remove_iso_mountpoint:
1424 unmount(iso_mountpoint, "")
1425 os.rmdir(iso_mountpoint)
1426 unregister_tmpfile(iso_mountpoint)
1427 if remove_device_mountpoint:
1428 unmount(device_mountpoint, "")
1429 if os.path.isdir(device_mountpoint):
1430 os.rmdir(device_mountpoint)
1431 unregister_tmpfile(device_mountpoint)
1434 def handle_mbr(device):
1435 """Main handler for installing master boot record (MBR)
1437 @device: device where the MBR should be installed to"""
1439 # make sure we have syslinux available
1440 if not options.skipmbr:
1441 if not which("syslinux") and not options.copyonly and not options.dryrun:
1442 logging.critical('Sorry, syslinux not available. Exiting.')
1443 logging.critical('Please install syslinux or consider using the --grub option.')
1446 if not options.skipmbr:
1447 if device[-1:].isdigit():
1448 mbr_device = re.match(r'(.*?)\d*$', device).group(1)
1449 partition_number = int(device[-1:]) - 1
1452 if options.syslinuxmbr:
1453 handle_syslinux_mbr(mbr_device)
1456 install_mir_mbr('/usr/share/grml2usb/mbr/mbrmgr', mbr_device, partition_number, True)
1458 install_mir_mbr('/usr/share/grml2usb/mbr/mbrldr', mbr_device, partition_number, True)
1459 except IOError, error:
1460 logging.critical("Execution failed: %s", error)
1462 except Exception, error:
1463 logging.critical("Execution failed: %s", error)
1467 def handle_vfat(device):
1468 """Check for FAT specific settings and options
1470 @device: device that should checked / formated"""
1472 # make sure we have mkfs.vfat available
1473 if options.fat16 and not options.force:
1474 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1475 logging.critical('Sorry, mkfs.vfat not available. Exiting.')
1476 logging.critical('Please make sure to install dosfstools.')
1479 # make sure the user is aware of what he is doing
1480 f = raw_input("Are you sure you want to format the specified partition with fat16? y/N ")
1481 if f == "y" or f == "Y":
1482 logging.info("Note: you can skip this question using the option --force")
1485 except CriticalException, error:
1486 logging.critical("Execution failed: %s", error)
1491 # check for vfat filesystem
1492 if device is not None and not os.path.isdir(device):
1494 check_for_fat(device)
1495 except CriticalException, error:
1496 logging.critical("Execution failed: %s", error)
1499 if not check_for_usbdevice(device) and not os.path.isdir(device):
1500 print "Warning: the specified device %s does not look like a removable usb device." % device
1501 f = raw_input("Do you really want to continue? y/N ")
1502 if f == "y" or f == "Y":
1508 def handle_compat_warning(device):
1509 """Backwards compatible checks
1511 @device: device that should be checked"""
1513 # make sure we can replace old grml2usb script and warn user when using old way of life:
1514 if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
1515 print "Warning: the semantics of grml2usb has changed."
1516 print "Instead of using grml2usb /path/to/iso %s you might" % device
1517 print "want to use grml2usb /path/to/iso /dev/... instead."
1518 print "Please check out the grml2usb manpage for details."
1519 f = raw_input("Do you really want to continue? y/N ")
1520 if f == "y" or f == "Y":
1526 def handle_logging():
1527 """Log handling and configuration"""
1530 FORMAT = "Debug: %(asctime)-15s %(message)s"
1531 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1533 FORMAT = "Critical: %(message)s"
1534 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1536 FORMAT = "%(message)s"
1537 logging.basicConfig(level=logging.INFO, format=FORMAT)
1540 def handle_bootloader(device):
1541 """wrapper for installing bootloader
1543 @device: device where bootloader should be installed to"""
1545 # Install bootloader only if not using the --copy-only option
1546 if options.copyonly:
1547 logging.info("Not installing bootloader and its files as requested via option copyonly.")
1548 elif os.path.isdir(device):
1549 logging.info("Not installing bootloader as %s is a directory." % device)
1551 install_bootloader(device)
1555 """Main function [make pylint happy :)]"""
1558 print os.path.basename(sys.argv[0]) + " " + PROG_VERSION
1562 parser.error("invalid usage")
1567 # make sure we have the appropriate permissions
1571 logging.info("Running in simulation mode as requested via option dry-run.")
1573 # specified arguments
1574 device = args[len(args) - 1]
1575 isos = args[0:len(args) - 1]
1577 if not os.path.isdir(device):
1578 if device[-1:].isdigit():
1579 if int(device[-1:]) > 4:
1580 logging.critical("Fatal: installation on partition number >4 not supported. (BIOS won't support it.)")
1583 if os.path.exists(device):
1584 logging.critical("Fatal: installation on raw device not supported. (BIOS won't support it.)")
1587 # provide upgrade path
1588 handle_compat_warning(device)
1590 # check for vfat partition
1593 # main operation (like installing files)
1595 handle_iso(iso, device)
1598 if not os.path.isdir(device):
1601 handle_bootloader(device)
1603 # finally be politely :)
1604 logging.info("Finished execution of grml2usb (%s). Have fun with your grml system." % PROG_VERSION)
1607 if __name__ == "__main__":
1610 except KeyboardInterrupt:
1611 logging.info("Received KeyboardInterrupt")
1614 ## END OF FILE #################################################################
1615 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8