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
23 PROG_VERSION = "0.9.18"
24 MOUNTED = set() # register mountpoints
25 TMPFILES = set() # register tmpfiles
26 DATESTAMP = time.mktime(datetime.datetime.now().timetuple()) # unique identifier for syslinux.cfg
27 GRML_FLAVOURS = set() # which flavours are being installed?
30 def syslinux_warning(option, opt, value, parser):
31 sys.stderr.write("Note: the --syslinux option is deprecated as syslinux " +
32 "is grml2usb's default. Continuing anyway.\n")
33 setattr(parser.values, option.dest, True)
36 USAGE = "Usage: %prog [options] <[ISO[s] | /live/image]> </dev/sdX#>\n\
38 %prog installs grml ISO[s] to an USB device to be able to boot from it.\n\
39 Make sure you have at least one grml ISO or a running grml system (/live/image),\n\
40 grub or syslinux and root access.\n\
42 Run %prog --help for usage hints, further information via: man grml2usb"
44 # pylint: disable-msg=C0103
45 parser = OptionParser(usage=USAGE)
46 parser.add_option("--bootoptions", dest="bootoptions",
47 action="store", type="string",
48 help="use specified bootoptions as default")
49 parser.add_option("--bootloader-only", dest="bootloaderonly", action="store_true",
50 help="do not copy files but just install a bootloader")
51 parser.add_option("--copy-only", dest="copyonly", action="store_true",
52 help="copy files only but do not install bootloader")
53 parser.add_option("--dry-run", dest="dryrun", action="store_true",
54 help="avoid executing commands")
55 parser.add_option("--fat16", dest="fat16", action="store_true",
56 help="format specified partition with FAT16")
57 parser.add_option("--force", dest="force", action="store_true",
58 help="force any actions requiring manual interaction")
59 parser.add_option("--grub", dest="grub", action="store_true",
60 help="install grub bootloader instead of (default) syslinux")
61 parser.add_option("--grub-mbr", dest="grubmbr", action="store_true",
62 help="install grub into MBR instead of (default) PBR")
63 parser.add_option("--lilo-binary", dest="lilobin", action="store", type="string",
64 help="lilo executable to be used for installing MBR")
65 parser.add_option("--mbr-menu", dest="mbrmenu", action="store_true",
66 help="enable interactive boot menu in MBR")
67 parser.add_option("--quiet", dest="quiet", action="store_true",
68 help="do not output anything but just errors on console")
69 parser.add_option("--remove-bootoption", dest="removeoption", action="append",
70 help="regex for removing existing bootoptions")
71 parser.add_option("--skip-addons", dest="skipaddons", action="store_true",
72 help="do not install /boot/addons/ files")
73 parser.add_option("--skip-grub-config", dest="skipgrubconfig", action="store_true",
74 help="skip generation of grub configuration files")
75 parser.add_option("--skip-mbr", dest="skipmbr", action="store_true",
76 help="do not install a master boot record (MBR) on the device")
77 parser.add_option("--skip-syslinux-config", dest="skipsyslinuxconfig", action="store_true",
78 help="skip generation of syslinux configuration files")
79 parser.add_option("--syslinux", dest="syslinux", action="callback", default=True,
80 callback=syslinux_warning,
81 help="install syslinux bootloader (deprecated as it's the default)")
82 parser.add_option("--syslinux-mbr", dest="syslinuxmbr", action="store_true",
83 help="install syslinux master boot record (MBR) instead of default")
84 parser.add_option("--verbose", dest="verbose", action="store_true",
85 help="enable verbose mode")
86 parser.add_option("-v", "--version", dest="version", action="store_true",
87 help="display version and exit")
88 (options, args) = parser.parse_args()
91 GRML2USB_BASE = '/usr/share/grml2usb'
92 if not os.path.isdir(GRML2USB_BASE):
93 GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
96 class CriticalException(Exception):
97 """Throw critical exception if the exact error is not known but fatal."
99 @Exception: message"""
103 # The following two functions help to operate on strings as
104 # array (list) of bytes (octets). In Python 3000, the bytes
105 # datatype will need to be used. This is intended for using
106 # with manipulation of files on the octet level, like shell
107 # arrays, e.g. in MBR creation.
111 """Convert a list of integers [0;255] to a string."""
112 return struct.pack("%sB" % len(a), *a)
116 """Convert a (bytes) string into a list of integers."""
117 return struct.unpack("%sB" % len(s), s)
121 """Cleanup function to make sure there aren't any mounted devices left behind.
124 logging.info("Cleaning up before exiting...")
125 proc = subprocess.Popen(["sync"])
129 for device in MOUNTED:
131 # ignore: RuntimeError: Set changed size during iteration
133 logging.debug('caught expection RuntimeError, ignoring')
136 def register_tmpfile(path):
138 TODO - not implemented yet
144 def unregister_tmpfile(path):
146 TODO - not implemented yet
150 TMPFILES.remove(path)
153 def register_mountpoint(target):
154 """register specified target in a set() for handling clean exiting
156 @target: destination target of mountpoint
162 def unregister_mountpoint(target):
163 """unregister specified target in a set() for handling clean exiting
165 @target: destination target of mountpoint
168 if target in MOUNTED:
169 MOUNTED.remove(target)
172 def get_function_name(obj):
173 """Helper function for use in execute() to retrive name of a function
175 @obj: the function object
177 if not (isroutine(obj) or isclass(obj)):
179 return obj.__module__ + '.' + obj.__name__
182 def execute(f, *exec_arguments):
183 """Wrapper for executing a command. Either really executes
184 the command (default) or when using --dry-run commandline option
185 just displays what would be executed."""
186 # usage: execute(subprocess.Popen, (["ls", "-la"]))
187 # TODO: doesn't work for proc = execute(subprocess.Popen...() -> any ideas?
189 # pylint: disable-msg=W0141
190 logging.debug('dry-run only: %s(%s)', get_function_name(f), ', '.join(map(repr, exec_arguments)))
192 # pylint: disable-msg=W0142
193 return f(*exec_arguments)
197 """Check whether a given file can be executed
199 @fpath: full path to file
201 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
205 """Check whether a given program is available in PATH
207 @program: name of executable"""
208 fpath = os.path.split(program)[0]
213 for path in os.environ["PATH"].split(os.pathsep):
214 exe_file = os.path.join(path, program)
221 def search_file(filename, search_path='/bin' + os.pathsep + '/usr/bin'):
222 """Given a search path, find file
224 @filename: name of file to search for
225 @search_path: path where searching for the specified filename"""
227 paths = search_path.split(os.pathsep)
228 current_dir = '' # make pylint happy :)
230 # pylint: disable-msg=W0612
231 for current_dir, directories, files in os.walk(path):
232 if os.path.exists(os.path.join(current_dir, filename)):
236 return os.path.abspath(os.path.join(current_dir, filename))
241 def check_uid_root():
242 """Check for root permissions"""
243 if not os.geteuid()==0:
244 sys.exit("Error: please run this script with uid 0 (root).")
247 def mkfs_fat16(device):
248 """Format specified device with VFAT/FAT16 filesystem.
250 @device: partition that should be formated"""
253 logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
256 logging.info("Formating partition with fat16 filesystem")
257 logging.debug("mkfs.vfat -F 16 %s", device)
258 proc = subprocess.Popen(["mkfs.vfat", "-F", "16", device])
260 if proc.returncode != 0:
261 raise CriticalException("error executing mkfs.vfat")
264 def generate_main_grub2_config(grml_flavour, bootoptions):
265 """Generate grub2 configuration for use via grub.cfg
267 @grml_flavour: name of grml flavour the configuration should be generated for
268 @bootoptions: additional bootoptions that should be used by default"""
270 local_datestamp = DATESTAMP
273 ## main grub2 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
279 if loadfont /boot/grub/ascii.pf2 ; then
284 if terminal_output.gfxterm ; then true ; else
285 # For backward compatibility with versions of terminal.mod that don't
286 # understand terminal_output
291 if background_image /boot/grub/grml.png ; then
292 set color_normal=black/black
293 set color_highlight=red/black
295 set menu_color_normal=white/black
296 set menu_color_highlight=black/yellow
299 menuentry "%(grml_flavour)s (default)" {
300 set gfxpayload=1024x768x16,1024x768
301 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
302 initrd /boot/release/%(flavour_filename)s/initrd.gz
305 menuentry "Memory test (memtest86+)" {
306 linux16 /boot/addons/memtest
309 menuentry "Boot Grub (all in one image)" {
310 linux /boot/addons/memdisk
311 initrd /boot/addons/allinone.img
314 menuentry "Boot FreeDOS" {
315 linux /boot/addons/memdisk
316 initrd /boot/addons/balder10.imz
319 menuentry "Boot MirOS bsd4grml" {
320 multiboot /boot/addons/bsd4grml/ldbsd.com
321 module /boot/addons/bsd4grml/bsd.rd
322 module /boot/addons/bsd4grml/boot.1
323 module /boot/addons/bsd4grml/boot.2
324 module /boot/addons/bsd4grml/boot.3
325 module /boot/addons/bsd4grml/boot.4
326 module /boot/addons/bsd4grml/boot.5
327 module /boot/addons/bsd4grml/boot.6
328 module /boot/addons/bsd4grml/boot.cfg
331 menuentry "Boot OS of first partition on first disk" {
336 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
337 'flavour_filename': grml_flavour.replace('-', ''),
338 'bootoptions': bootoptions } )
341 def generate_flavour_specific_grub2_config(grml_flavour, bootoptions):
342 """Generate grub2 configuration for use via grub.cfg
344 @grml_flavour: name of grml flavour the configuration should be generated for
345 @bootoptions: additional bootoptions that should be used by default"""
347 local_datestamp = DATESTAMP
350 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
351 menuentry "%(grml_flavour)s - boot in default mode" {
352 set gfxpayload=1024x768x16,1024x768
353 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
354 initrd /boot/release/%(flavour_filename)s/initrd.gz
357 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
358 menuentry "%(grml_flavour)s-persistent - enable persistency feature" {
359 set gfxpayload=1024x768x16,1024x768
360 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
361 initrd /boot/release/%(flavour_filename)s/initrd.gz
364 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
365 menuentry "%(grml_flavour)s2ram - copy compressed grml file to RAM" {
366 set gfxpayload=1024x768x16,1024x768
367 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ toram=%(grml_flavour)s.squashfs %(bootoptions)s
368 initrd /boot/release/%(flavour_filename)s/initrd.gz
371 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
372 menuentry "%(grml_flavour)s-debug - enable debugging options" {
373 set gfxpayload=1024x768x16,1024x768
374 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ debug initcall_debug%(bootoptions)s
375 initrd /boot/release/%(flavour_filename)s/initrd.gz
378 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
379 menuentry "%(grml_flavour)s-x - start X Window System" {
380 set gfxpayload=1024x768x16,1024x768
381 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ startx=wm-ng %(bootoptions)s
382 initrd /boot/release/%(flavour_filename)s/initrd.gz
385 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
386 menuentry "%(grml_flavour)s-nofb - disable framebuffer" {
387 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=ofonly %(bootoptions)s
388 initrd /boot/release/%(flavour_filename)s/initrd.gz
391 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
392 menuentry "%(grml_flavour)s-failsafe - disable hardware detection" {
393 linux /boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal 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 pci=nomsi %(bootoptions)s
394 initrd /boot/release/%(flavour_filename)s/initrd.gz
397 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
398 menuentry "%(grml_flavour)s-forensic - do not touch harddisks during hw recognition" {
399 set gfxpayload=1024x768x16,1024x768
400 linux /boot/release/%(flavour_filename)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
401 initrd /boot/release/%(flavour_filename)s/initrd.gz
404 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
405 'flavour_filename': grml_flavour.replace('-', ''),
406 'bootoptions': bootoptions } )
409 def generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootoptions):
410 """Generate grub1 configuration for use via menu.lst
412 @grml_flavour: name of grml flavour the configuration should be generated for
413 @install_partition: partition number for use in (hd0,X)
414 @bootoptions: additional bootoptions that should be used by default"""
416 local_datestamp = DATESTAMP
419 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
420 title %(grml_flavour)s
421 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
422 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
424 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
425 title %(grml_flavour)s-persistent
426 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce vga=791 quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
427 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
429 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
430 title %(grml_flavour)s2ram
431 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)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
432 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
434 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
435 title %(grml_flavour)s-debug
436 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ debug initcall_debug%(bootoptions)s
437 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
439 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
440 title %(grml_flavour)s-x
441 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ startx=wm-ng %(bootoptions)s
442 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
444 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
445 title %(grml_flavour)s-nofb
446 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=ofonly %(bootoptions)s
447 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
449 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
450 title %(grml_flavour)s-failsafe
451 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal 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 pci=nomsi %(bootoptions)s
452 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
454 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
455 title %(grml_flavour)s-forensic
456 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)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
457 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
459 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
460 title %(grml_flavour)s-serial
461 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)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
462 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
464 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
465 'flavour_filename': grml_flavour.replace('-', ''),
466 'bootoptions': bootoptions, 'install_partition': install_partition } )
469 def generate_main_grub1_config(grml_flavour, install_partition, bootoptions):
470 """Generate grub1 configuration for use via menu.lst
472 @grml_flavour: name of grml flavour the configuration should be generated for"""
474 local_datestamp = DATESTAMP
477 ## main grub1 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
480 # color red/blue green/black
481 splashimage=(hd0,%(install_partition)s)/boot/grub/splash.xpm.gz
486 title %(grml_flavour)s - Default boot (using 1024x768 framebuffer)
487 kernel (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/linux26 apm=power-off vga=791 quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
488 initrd (hd0,%(install_partition)s)/boot/release/%(flavour_filename)s/initrd.gz
490 title Memory test (memtest86+)
491 kernel (hd0,%(install_partition)s)/boot/addons/memtest
493 title Grub - all in one image
494 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
495 initrd (hd0,%(install_partition)s)/boot/addons/allinone.img
498 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
499 initrd (hd0,%(install_partition)s)/boot/addons/balder10.imz
502 kernel (hd0,%(install_partition)s)/boot/addons/bsd4grml/ldbsd.com
504 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
505 'flavour_filename': grml_flavour.replace('-', ''),
506 'bootoptions': bootoptions, 'install_partition': install_partition } )
509 def generate_isolinux_splash(grml_flavour):
510 """Generate bootsplash for isolinux/syslinux
512 @grml_flavour: name of grml flavour the configuration should be generated for"""
514 # TODO: adjust last bootsplash line (the one following the "Some information and boot ...")
516 grml_name = grml_flavour
519 \ f17
\f\18/boot/syslinux/logo.16
521 Some information and boot options available via keys F2 - F10. http://grml.org/
523 """ % {'grml_name': grml_name} )
526 def generate_main_syslinux_config(*args):
527 """Generate main configuration for use in syslinux.cfg
529 @*args: just for backward compatibility"""
531 local_datestamp = DATESTAMP
535 menu label Default boot modes:
544 menu label Additional boot entries for:
546 include additional.cfg
553 include promptname.cfg
556 Jump to old style isolinux prompt
557 featuring further information
558 regarding available boot options.
566 def generate_flavour_specific_syslinux_config(grml_flavour):
567 """Generate flavour specific configuration for use in syslinux.cfg
569 @grml_flavour: name of grml flavour the configuration should be generated for"""
573 menu begin grml %(grml_flavour)s
574 menu title %(display_name)s
576 menu label ^Back to main menu...
579 # include config for boot parameters from disk
580 include %(grml_flavour)s_grml.cfg
583 """ % {'grml_flavour': grml_flavour, 'display_name' : grml_flavour.replace('_', '-') } )
586 def install_grub(device):
587 """Install grub on specified device.
589 @mntpoint: mountpoint of device where grub should install its files to
590 @device: partition where grub should be installed to"""
593 logging.info("Would execute grub-install [--root-directory=mount_point] %s now.", device)
595 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
596 register_tmpfile(device_mountpoint)
599 mount(device, device_mountpoint, "")
601 # If using --grub-mbr then make sure we install grub in MBR instead of PBR
602 # Thanks to grub2. NOT.
604 logging.debug("Using option --grub-mbr ...")
605 if device[-1:].isdigit():
606 grub_device = re.match(r'(.*?)\d*$', device).group(1)
612 logging.info("Installing grub as bootloader")
613 logging.debug("grub-install --recheck --no-floppy --root-directory=%s %s",
614 device_mountpoint, grub_device)
615 proc = subprocess.Popen(["grub-install", "--recheck", "--no-floppy",
616 "--root-directory=%s" % device_mountpoint, grub_device], stdout=file(os.devnull, "r+"))
618 if proc.returncode != 0:
619 # raise Exception("error executing grub-install")
620 logging.critical("Fatal: error executing grub-install (please check the grml2usb FAQ or drop the --grub option)")
621 logging.critical("Note: if using grub2 consider using the --grub-mbr option because grub2's PBR feature is broken.")
624 except CriticalException, error:
625 logging.critical("Fatal: %s", error)
630 unmount(device_mountpoint, "")
631 os.rmdir(device_mountpoint)
632 unregister_tmpfile(device_mountpoint)
635 def install_syslinux(device):
636 """Install syslinux on specified device.
638 @device: partition where syslinux should be installed to"""
641 logging.info("Would install syslinux as bootloader on %s", device)
644 # syslinux -d boot/isolinux /dev/sdb1
645 logging.info("Installing syslinux as bootloader")
646 logging.debug("syslinux -d boot/syslinux %s", device)
647 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
649 if proc.returncode != 0:
650 raise CriticalException("Error executing syslinux (either try --fat16 or use grub?)")
653 def install_bootloader(device):
654 """Install bootloader on specified device.
656 @device: partition where bootloader should be installed to"""
658 # by default we use grub, so install syslinux only on request
660 if not which("grub-install"):
661 logging.critical("Fatal: grub-install not available (please install the grub package or use the --syslinux option)")
667 except CriticalException, error:
668 logging.critical("Fatal: %s", error)
673 install_syslinux(device)
674 except CriticalException, error:
675 logging.critical("Fatal: %s", error)
680 def execute_lilo(lilo, device):
681 """execute lilo for activating the partitions in the MBR
683 @lilo: path of lilo executable
684 @device: device where lilo should be executed on"""
686 # to support -A for extended partitions:
687 logging.info("Activating partitions in MBR via lilo")
688 logging.debug("%s -S /dev/null -M %s ext", lilo, device)
689 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-M", device, "ext"])
691 if proc.returncode != 0:
692 raise Exception("error executing lilo")
694 # activate partition:
695 logging.debug("%s -S /dev/null -A %s 1", lilo, device)
696 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-A", device, "1"])
698 if proc.returncode != 0:
699 raise Exception("error executing lilo")
702 def install_syslinux_mbr(device):
703 """install syslinux's master boot record (MBR) on the specified device
705 @device: device where MBR of syslinux should be installed to"""
707 # make sure we have syslinux available
708 if not which("syslinux") and not options.copyonly:
709 raise Exception("syslinux not available (either install it or consider using the --grub option)")
711 # lilo's mbr is broken, use the one from syslinux instead:
712 if not os.path.isfile("/usr/lib/syslinux/mbr.bin"):
713 raise Exception("/usr/lib/syslinux/mbr.bin can not be read")
715 logging.info("Installing syslinux MBR")
716 logging.debug("cat /usr/lib/syslinux/mbr.bin > %s", device)
718 # TODO -> use Popen instead?
719 retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True)
721 logging.critical("Error copying MBR to device (%s)", retcode)
722 except OSError, error:
723 logging.critical("Execution failed: %s", error)
726 def install_mir_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
727 """install 'mbr' master boot record (MBR) on a device
729 Retrieve the partition table from "device", install an MBR from the
730 "mbrtemplate" file, set the "partition" (0..3) active, and install the
731 result back to "device".
733 @mbrtemplate: default MBR file
735 @device: name of a file assumed to be a hard disc (or USB stick) image, or
736 something like "/dev/sdb"
738 @partition: must be a number between 0 and 3, inclusive
740 @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
743 @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
744 specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
745 false then leave the mirbsdmbr specific flag set to FFh, set all
746 active flags to 0 and set the active flag of the partition to 80h. Note:
747 behaviour of mirbsdmbr: if flag = 0/1/2/3 then use it, otherwise search for
750 logging.info("Installing default MBR")
752 if not os.path.isfile(mbrtemplate):
753 logging.critical("Error: %s can not be read.", mbrtemplate)
754 raise CriticalException("Error installing MBR (either try --syslinux-mbr or install missing file?)")
756 if (partition < 0) or (partition > 3):
757 raise ValueError("partition must be between 0 and 3")
764 tmpf = tempfile.NamedTemporaryFile()
766 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1", device, tmpf.name)
767 proc = subprocess.Popen(["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"], stderr=file(os.devnull, "r+"))
769 if proc.returncode != 0:
770 raise Exception("error executing dd (first run)")
772 logging.debug("executing: dd if=%s of=%s bs=%s count=1 conv=notrunc", mbrtemplate,
773 tmpf.name, nmbrbytes)
774 proc = subprocess.Popen(["dd", "if=%s" % mbrtemplate, "of=%s" % tmpf.name, "bs=%s" % nmbrbytes,
775 "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+"))
777 if proc.returncode != 0:
778 raise Exception("error executing dd (second run)")
780 mbrcode = tmpf.file.read(512)
781 if len(mbrcode) < 512:
782 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
785 mbrcode = mbrcode[0:439] + chr(partition) + \
786 mbrcode[440:510] + "\x55\xAA"
788 actives = ["\x00", "\x00", "\x00", "\x00"]
789 actives[partition] = "\x80"
790 mbrcode = mbrcode[0:446] + actives[0] + \
791 mbrcode[447:462] + actives[1] + \
792 mbrcode[463:478] + actives[2] + \
793 mbrcode[479:494] + actives[3] + \
794 mbrcode[495:510] + "\x55\xAA"
798 tmpf.file.write(mbrcode)
801 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device)
802 proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1",
803 "conv=notrunc"], stderr=file(os.devnull, "r+"))
805 if proc.returncode != 0:
806 raise Exception("error executing dd (third run)")
810 def handle_syslinux_mbr(device):
811 """Install syslinux master boot record on given device
813 @device: device where MBR should be installed to"""
815 if not is_writeable(device):
816 raise IOError("device not writeable for user")
818 # try to use system's lilo
822 # otherwise fall back to our static version
823 from platform import architecture
824 if architecture()[0] == '64bit':
825 lilo = GRML2USB_BASE + '/lilo/lilo.static.amd64'
827 lilo = GRML2USB_BASE + '/lilo/lilo.static.i386'
828 # finally prefer a specified lilo executable
830 lilo = options.lilobin
833 raise Exception("lilo executable can not be execute")
836 logging.info("Would install MBR running lilo and using syslinux.")
839 execute_lilo(lilo, device)
840 install_syslinux_mbr(device)
843 def is_writeable(device):
844 """Check if the device is writeable for the current user
846 @device: partition where bootloader should be installed to"""
850 #raise Exception("no device for checking write permissions")
852 if not os.path.exists(device):
855 return os.access(device, os.W_OK) and os.access(device, os.R_OK)
858 def mount(source, target, mount_options):
859 """Mount specified source on given target
861 @source: name of device/ISO that should be mounted
862 @target: directory where the ISO should be mounted to
863 @options: mount specific options"""
865 # note: options.dryrun does not work here, as we have to
866 # locate files and identify the grml flavour
868 for x in file('/proc/mounts').readlines():
869 if x.startswith(source):
870 raise CriticalException("Error executing mount: %s already mounted - please unmount before invoking grml2usb" % source)
872 if os.path.isdir(source):
873 logging.debug("Source %s is not a device, therefore not mounting.", source)
876 logging.debug("mount %s %s %s", mount_options, source, target)
877 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
879 if proc.returncode != 0:
880 raise CriticalException("Error executing mount (no filesystem on the partition?)")
882 logging.debug("register_mountpoint(%s)", target)
883 register_mountpoint(target)
886 def unmount(target, unmount_options):
887 """Unmount specified target
889 @target: target where something is mounted on and which should be unmounted
890 @options: options for umount command"""
892 # make sure we unmount only already mounted targets
893 target_unmount = False
894 mounts = open('/proc/mounts').readlines()
895 mountstring = re.compile(".*%s.*" % re.escape(os.path.realpath(target)))
897 if re.match(mountstring, line):
898 target_unmount = True
900 if not target_unmount:
901 logging.debug("%s not mounted anymore", target)
903 logging.debug("umount %s %s", list(unmount_options), target)
904 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
906 if proc.returncode != 0:
907 raise Exception("Error executing umount")
909 logging.debug("unregister_mountpoint(%s)", target)
910 unregister_mountpoint(target)
913 def check_for_usbdevice(device):
914 """Check whether the specified device is a removable USB device
916 @device: device name, like /dev/sda1 or /dev/sda
919 usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
921 usbdev = os.path.realpath('/sys/class/block/' + usbdevice + '/removable')
922 if not os.path.isfile(usbdev):
923 # Ubuntu with kernel 2.6.24 for example:
924 usbdev = os.path.realpath('/sys/block/' + usbdevice + '/removable')
926 if os.path.isfile(usbdev):
927 is_usb = open(usbdev).readline()
934 def check_for_fat(partition):
935 """Check whether specified partition is a valid VFAT/FAT16 filesystem
937 @partition: device name of partition"""
940 udev_info = subprocess.Popen(["/sbin/blkid", "-s", "TYPE", "-o", "value", partition],
941 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
942 filesystem = udev_info.communicate()[0].rstrip()
944 if udev_info.returncode == 2:
945 raise CriticalException("Failed to read device %s"
946 " (wrong UID/permissions or device/directory not present?)" % partition)
948 if filesystem != "vfat":
949 raise CriticalException("Partition %s does not contain a FAT16 filesystem. (Use --fat16 or run mkfs.vfat %s)" % (partition, partition))
952 raise CriticalException("Sorry, /sbin/blkid not available (install e2fsprogs?)")
955 def mkdir(directory):
956 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
958 # just silently pass as it's just fine it the directory exists
959 if not os.path.isdir(directory):
961 os.makedirs(directory)
962 # pylint: disable-msg=W0704
967 def exec_rsync(source, target):
968 """Simple wrapper around rsync to install files
970 @source: source file/directory
971 @target: target file/directory"""
972 logging.debug("Source: %s / Target: %s", source, target)
973 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", source, target])
975 if proc.returncode == 12:
976 logging.critical("Fatal: No space left on device")
980 if proc.returncode != 0:
981 logging.critical("Fatal: could not install %s", source)
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 exec_rsync(squashfs, squashfs_target + grml_flavour + '.squashfs')
1002 filesystem_module = search_file('filesystem.module', iso_mount)
1003 if filesystem_module is None:
1004 logging.critical("Fatal: filesystem.module not found")
1005 raise CriticalException("error locating filesystem.module file")
1007 exec_rsync(filesystem_module, squashfs_target + 'filesystem.module')
1009 release_target = target + '/boot/release/' + grml_flavour.replace('-', '')
1010 execute(mkdir, release_target)
1012 kernel = search_file('linux26', iso_mount)
1014 logging.critical("Fatal kernel not found")
1015 raise CriticalException("error locating kernel file")
1017 exec_rsync(kernel, release_target + '/linux26')
1019 initrd = search_file('initrd.gz', iso_mount)
1021 logging.critical("Fatal: initrd not found")
1022 raise CriticalException("error locating initrd file")
1024 exec_rsync(initrd, release_target + '/initrd.gz')
1027 def copy_grml_files(iso_mount, target):
1028 """copy some minor grml files to a given target
1030 @iso_mount: path where a grml ISO is mounted on
1031 @target: path where grml's main files should be copied to"""
1033 grml_target = target + '/grml/'
1034 execute(mkdir, grml_target)
1036 for myfile in 'grml-cheatcodes.txt', 'grml-version', 'LICENSE.txt', 'md5sums', 'README.txt':
1037 grml_file = search_file(myfile, iso_mount)
1038 if grml_file is None:
1039 logging.warn("Warning: myfile %s could not be found - can not install it", myfile)
1041 exec_rsync(grml_file, grml_target + myfile)
1043 grml_web_target = grml_target + '/web/'
1044 execute(mkdir, grml_web_target)
1046 for myfile in 'index.html', 'style.css':
1047 grml_file = search_file(myfile, iso_mount)
1048 if grml_file is None:
1049 logging.warn("Warning: myfile %s could not be found - can not install it")
1051 exec_rsync(grml_file, grml_web_target + myfile)
1053 grml_webimg_target = grml_web_target + '/images/'
1054 execute(mkdir, grml_webimg_target)
1056 for myfile in 'button.png', 'favicon.png', 'linux.jpg', 'logo.png':
1057 grml_file = search_file(myfile, iso_mount)
1058 if grml_file is None:
1059 logging.warn("Warning: myfile %s could not be found - can not install it")
1061 exec_rsync(grml_file, grml_webimg_target + myfile)
1064 def copy_addons(iso_mount, target):
1065 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1067 @iso_mount: path where a grml ISO is mounted on
1068 @target: path where grml's main files should be copied to"""
1070 addons = target + '/boot/addons/'
1071 execute(mkdir, addons)
1073 # grub all-in-one image
1074 allinoneimg = search_file('allinone.img', iso_mount)
1075 if allinoneimg is None:
1076 logging.warn("Warning: allinone.img not found (that's fine if you don't need it)")
1078 exec_rsync(allinoneimg, addons + 'allinone.img')
1081 bsdimg = search_file('bsd4grml', iso_mount)
1083 logging.warn("Warning: bsd4grml not found (that's fine if you don't need it)")
1085 exec_rsync(bsdimg, addons + '/')
1088 balderimg = search_file('balder10.imz', iso_mount)
1089 if balderimg is None:
1090 logging.warn("Warning: balder10.imz not found (that's fine if you don't need it)")
1092 exec_rsync(balderimg, addons + 'balder10.imz')
1094 # install hdt and pci.ids only when using syslinux (grub doesn't support it)
1095 if options.syslinux:
1096 # hdt (hardware detection tool) image
1097 hdtimg = search_file('hdt.c32', iso_mount)
1099 exec_rsync(hdtimg, addons + '/hdt.c32')
1102 picids = search_file('pci.ids', iso_mount)
1104 exec_rsync(picids, addons + '/pci.ids')
1107 memdiskimg = search_file('memdisk', iso_mount)
1108 if memdiskimg is None:
1109 logging.warn("Warning: memdisk not found (that's fine if you don't need it)")
1111 exec_rsync(memdiskimg, addons + 'memdisk')
1114 memtestimg = search_file('memtest', iso_mount)
1115 if memtestimg is None:
1116 logging.warn("Warning: memtest not found (that's fine if you don't need it)")
1118 exec_rsync(memtestimg, addons + 'memtest')
1121 gpxeimg = search_file('gpxe.lkrn', iso_mount)
1123 logging.warn("Warning: gpxe.lkrn not found (that's fine if you don't need it)")
1125 exec_rsync(gpxeimg, addons + 'gpxe.lkrn')
1127 def copy_bootloader_files(iso_mount, target):
1128 """copy grml's bootloader files to a given target
1130 @iso_mount: path where a grml ISO is mounted on
1131 @target: path where grml's main files should be copied to"""
1133 syslinux_target = target + '/boot/syslinux/'
1134 execute(mkdir, syslinux_target)
1136 logo = search_file('logo.16', iso_mount)
1137 exec_rsync(logo, syslinux_target + 'logo.16')
1139 for ffile in ['f%d' % number for number in range(1,11) ]:
1140 bootsplash = search_file(ffile, iso_mount)
1143 exec_rsync(bootsplash, syslinux_target + ffile)
1145 # avoid the "file is read only, overwrite anyway (y/n) ?" question
1146 # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
1147 if os.path.isfile(syslinux_target + 'ldlinux.sys'):
1148 os.unlink(syslinux_target + 'ldlinux.sys')
1150 bootloader_dirs = ['/boot/isolinux/', '/boot/syslinux/']
1152 for dir in bootloader_dirs:
1153 if glob.glob(iso_mount + dir + '*default.cfg'):
1157 logging.critical("Fatal: file default.cfg could not be found.")
1158 logging.critical("Note: this grml2usb version requires an ISO generated by grml-live >=0.9.24 ...")
1159 logging.critical(" ... either use grml releases >=2009.10 or switch to an older grml2usb version.")
1160 logging.critical(" Please visit http://grml.org/grml2usb/#grml2usb-compat for further information.")
1163 for expr in '*default.cfg', 'distri.cfg', \
1164 '*grml.cfg', 'grml.png', 'hd.cfg', 'isolinux.cfg', 'isolinux.bin', \
1165 'isoprompt.cfg', 'options.cfg', \
1166 'prompt.cfg', 'vesamenu.c32', 'vesamenu.cfg', 'grml.png':
1167 files = glob.glob(iso_mount + source_dir + expr)
1169 filename = os.path.basename(path)
1170 exec_rsync(path, syslinux_target + filename)
1172 # copy the addons_*.cfg file to the new syslinux directory
1173 for filename in glob.glob(iso_mount + source_dir + 'addon*.cfg'):
1174 exec_rsync(filename, syslinux_target)
1176 path = search_file('hidden.cfg', iso_mount + source_dir)
1178 exec_rsync(path, syslinux_target + "new_" + 'hidden.cfg')
1181 grub_target = target + '/boot/grub/'
1182 execute(mkdir, grub_target)
1184 if not os.path.isfile(GRML2USB_BASE + "/grub/splash.xpm.gz"):
1185 logging.critical("Error: %s/grub/splash.xpm.gz can not be read.", GRML2USB_BASE)
1186 logging.critical("Please make sure you've installed the grml2usb (Debian) package!")
1189 exec_rsync(GRML2USB_BASE + '/grub/splash.xpm.gz', grub_target + 'splash.xpm.gz')
1191 # grml splash in grub
1192 if os.path.isfile(GRML2USB_BASE + "/grub/grml.png"):
1193 exec_rsync(GRML2USB_BASE + '/grub/grml.png', grub_target + 'grml.png')
1195 # font file for graphical bootsplash in grub
1196 if os.path.isfile("/usr/share/grub/ascii.pf2"):
1197 exec_rsync('/usr/share/grub/ascii.pf2', grub_target + 'ascii.pf2')
1199 # always copy grub content as it might be useful
1200 for file in glob.glob(iso_mount + '/boot/grub/*.mod'):
1201 exec_rsync(file, grub_target)
1203 for file in glob.glob(iso_mount + '/boot/grub/*.img'):
1204 exec_rsync(file, grub_target)
1206 for file in glob.glob(iso_mount + '/boot/grub/stage*'):
1207 exec_rsync(file, grub_target)
1209 def install_iso_files(grml_flavour, iso_mount, device, target):
1210 """Copy files from ISO to given target
1212 @grml_flavour: name of grml flavour the configuration should be generated for
1213 @iso_mount: path where a grml ISO is mounted on
1214 @device: device/partition where bootloader should be installed to
1215 @target: path where grml's main files should be copied to"""
1217 # TODO => several improvements:
1218 # * make sure grml_flavour, iso_mount, target are set when the function is called, otherwise raise exception
1219 # * provide alternative search_file() if file information is stored in a config.ini file?
1220 # * catch "install: .. No space left on device" & CO
1224 GRML_DEFAULT = grml_flavour
1226 elif not options.bootloaderonly:
1227 logging.info("Copying files. This might take a while....")
1229 copy_system_files(grml_flavour, iso_mount, target)
1230 copy_grml_files(iso_mount, target)
1231 except CriticalException, error:
1232 logging.critical("Execution failed: %s", error)
1235 if not options.skipaddons:
1236 if not search_file('addons', iso_mount):
1237 logging.info("Could not find addons, therefore not installing.")
1239 copy_addons(iso_mount, target)
1241 if not options.copyonly:
1242 copy_bootloader_files(iso_mount, target)
1244 if not options.dryrun:
1245 handle_bootloader_config(grml_flavour, device, target)
1247 # make sure we sync filesystems before returning
1248 proc = subprocess.Popen(["sync"])
1252 def uninstall_files(device):
1253 """Get rid of all grml files on specified device
1255 @device: partition where grml2usb files should be removed from"""
1257 # TODO - not implemented yet
1258 logging.critical("TODO: uninstalling files from %s not yet implement, sorry.", device)
1261 def identify_grml_flavour(mountpath):
1262 """Get name of grml flavour
1264 @mountpath: path where the grml ISO is mounted to
1265 @return: name of grml-flavour"""
1267 version_file = search_file('grml-version', mountpath)
1269 if version_file == "":
1270 logging.critical("Error: could not find grml-version file.")
1274 tmpfile = open(version_file, 'r')
1275 grml_info = tmpfile.readline()
1276 grml_flavour = re.match(r'[\w-]*', grml_info).group()
1279 except Exception, e:
1280 logging.critical("Unexpected error: %s", e)
1286 def modify_grub_config(filename):
1287 if options.removeoption:
1289 for regex in options.removeoption:
1290 regexe.append(re.compile(r'%s' % regex))
1292 option_re = re.compile(r'(.*/boot/release/.*linux26.*)')
1294 for line in fileinput.input(filename, inplace=1):
1295 if regexe and option_re.search(line):
1296 for regex in regexe:
1297 line = regex.sub(' ', line)
1299 sys.stdout.write(line)
1303 def handle_grub1_config(grml_flavour, install_partition, grub_target, bootopt):
1304 """Main handler for generating grub1 configuration
1306 @grml_flavour: name of grml flavour the configuration should be generated for
1307 @install_partition: partition number for use in (hd0,X)
1308 @grub_target: path of grub's configuration files
1309 @bootoptions: additional bootoptions that should be used by default"""
1312 grub1_cfg = grub_target + 'menu.lst'
1313 logging.debug("Creating grub1 configuration file (menu.lst)")
1315 # install main configuration only *once*, no matter how many ISOs we have:
1316 if os.path.isfile(grub1_cfg):
1317 string = open(grub1_cfg).readline()
1318 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1319 if not re.match(main_identifier, string):
1320 grub1_config_file = open(grub1_cfg, 'w')
1321 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1322 grub1_config_file.close()
1324 grub1_config_file = open(grub1_cfg, 'w')
1325 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1326 grub1_config_file.close()
1328 grub_flavour_config = True
1329 if os.path.isfile(grub1_cfg):
1330 string = open(grub1_cfg).readlines()
1331 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1333 if flavour.match(line):
1334 grub_flavour_config = False
1336 if grub_flavour_config:
1337 grub1_config_file = open(grub1_cfg, 'a')
1338 grub1_config_file.write(generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootopt))
1339 grub1_config_file.close()
1341 modify_grub_config(grub1_cfg)
1343 # make sure grub.conf isn't a symlink but a plain file instead,
1344 # otherwise it will break on FAT16 filesystems
1345 # this works around grub-install of (at least) Fedora 10
1346 if os.path.isfile(grub1_cfg):
1347 grubconf = grub_target + 'grub.conf'
1348 if not os.path.islink(grubconf):
1350 shutil.copyfile(grub1_cfg, grub_target + 'grub.conf')
1352 def handle_grub2_config(grml_flavour, grub_target, bootopt):
1353 """Main handler for generating grub2 configuration
1355 @grml_flavour: name of grml flavour the configuration should be generated for
1356 @grub_target: path of grub's configuration files
1357 @bootoptions: additional bootoptions that should be used by default"""
1360 grub2_cfg = grub_target + 'grub.cfg'
1361 logging.debug("Creating grub2 configuration file (grub.lst)")
1365 # install main configuration only *once*, no matter how many ISOs we have:
1366 grub_flavour_is_default = False
1367 if os.path.isfile(grub2_cfg):
1368 string = open(grub2_cfg).readline()
1369 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1370 if not re.match(main_identifier, string):
1371 grub2_config_file = open(grub2_cfg, 'w')
1372 GRML_DEFAULT = grml_flavour
1373 grub_flavour_is_default = True
1374 grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1375 grub2_config_file.close()
1377 grub2_config_file = open(grub2_cfg, 'w')
1378 GRML_DEFAULT = grml_flavour
1379 grub_flavour_is_default = True
1380 grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1381 grub2_config_file.close()
1383 # install flavour specific configuration only *once* as well
1384 grub_flavour_config = True
1385 if os.path.isfile(grub2_cfg):
1386 string = open(grub2_cfg).readlines()
1387 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1389 if flavour.match(line):
1390 grub_flavour_config = False
1392 if grub_flavour_config:
1393 grub2_config_file = open(grub2_cfg, 'a')
1394 # display only if the grml flavour isn't the default
1395 if not grub_flavour_is_default:
1396 GRML_FLAVOURS.add(grml_flavour)
1397 grub2_config_file.write(generate_flavour_specific_grub2_config(grml_flavour, bootopt))
1398 grub2_config_file.close()
1400 modify_grub_config(grub2_cfg)
1403 def handle_grub_config(grml_flavour, device, target):
1404 """Main handler for generating grub (v1 and v2) configuration
1406 @grml_flavour: name of grml flavour the configuration should be generated for
1407 @device: device/partition where grub should be installed to
1408 @target: path of grub's configuration files"""
1410 logging.debug("Generating grub configuration")
1412 grub_target = target + '/boot/grub/'
1413 execute(mkdir, grub_target)
1415 if os.path.isdir(device):
1416 install_grub1_partition = None
1418 if device[-1:].isdigit():
1419 install_grub1_partition = int(device[-1:]) - 1
1421 raise CriticalException("error validating partition schema (raw device?)")
1423 # do NOT write "None" in kernel cmdline
1424 if options.bootoptions is None:
1427 bootopt = options.bootoptions
1430 handle_grub1_config(grml_flavour, install_grub1_partition, grub_target, bootopt)
1432 handle_grub2_config(grml_flavour, grub_target, bootopt)
1435 def initial_syslinux_config(target):
1436 """Generates intial syslinux configuration
1438 @target path of syslinux's configuration files"""
1440 target = target + "/"
1441 filename = target + "grmlmain.cfg"
1442 if os.path.isfile(target + "grmlmain.cfg"):
1444 data = open(filename, "w")
1445 data.write(generate_main_syslinux_config())
1448 filename = target + "hiddens.cfg"
1449 data = open(filename, "w")
1450 data.write("include hidden.cfg\n")
1453 def add_entry_if_not_present(filename, entry):
1454 data = open(filename, "a+")
1465 def adjust_syslinux_bootoptions(src, flavour):
1466 append_re = re.compile("^(\s*append.*)$", re.I)
1467 boot_re = re.compile("/boot/([a-zA-Z0-9_]+/)+([a-zA-Z0-9._]+)")
1468 # flavour_re = re.compile("(label.*)(grml\w+)")
1469 default_re = re.compile("(default.cfg)")
1471 # do NOT write "None" in kernel cmdline
1472 if options.bootoptions is None:
1475 bootopt = options.bootoptions
1479 if options.removeoption:
1480 option_re = re.compile(r'/boot/release/.*/initrd.gz')
1482 for regex in options.removeoption:
1483 regexe.append(re.compile(r'%s' % regex))
1485 for line in fileinput.input(src, inplace=1):
1486 line = boot_re.sub(r'/boot/release/%s/\2 ' % flavour.replace('-', ''), line)
1487 # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
1488 line = default_re.sub(r'%s-\1' % flavour, line)
1489 line = append_re.sub(r'\1 live-media-path=/live/%s/ ' % flavour, line)
1490 line = append_re.sub(r'\1 boot=live %s ' % bootopt, line)
1491 if option_re and option_re.search(line):
1492 for regex in regexe:
1493 line = regex.sub(' ', line)
1494 sys.stdout.write(line)
1497 def adjust_labels(src, replacement):
1498 label_re = re.compile("^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
1499 for line in fileinput.input(src, inplace=1):
1500 line = label_re.sub(replacement, line)
1501 sys.stdout.write(line)
1505 def add_syslinux_entry(filename, grml_flavour):
1506 entry_filename = "option_%s.cfg" % grml_flavour
1507 entry = "include %s\n" % entry_filename
1509 add_entry_if_not_present(filename, entry)
1510 path = os.path.dirname(filename)
1512 data = open(path + "/" + entry_filename, "w")
1513 data.write(generate_flavour_specific_syslinux_config(grml_flavour))
1516 def modify_filenames(grml_flavour, target, filenames):
1517 grml_filename = grml_flavour.replace('-', '_')
1518 for filename in filenames:
1519 old_filename = "%s/%s" % (target, filename)
1520 new_filename = "%s/%s_%s" % (target, grml_filename, filename)
1521 os.rename(old_filename, new_filename)
1522 adjust_syslinux_bootoptions(new_filename, grml_flavour)
1525 def remove_default_entry(filename):
1526 default_re = re.compile("^(\s*menu\s*default\s*)$", re.I)
1527 for line in fileinput.input(filename, inplace=1):
1528 if default_re.match(line): continue
1529 sys.stdout.write(line)
1533 def handle_syslinux_config(grml_flavour, target):
1534 """Main handler for generating syslinux configuration
1536 @grml_flavour: name of grml flavour the configuration should be generated for
1537 @target: path of syslinux's configuration files"""
1539 # do NOT write "None" in kernel cmdline
1540 if options.bootoptions is None:
1543 bootopt = options.bootoptions
1545 logging.debug("Generating syslinux configuration")
1546 syslinux_target = target + '/boot/syslinux/'
1547 # should be present via copy_bootloader_files(), but make sure it exits:
1548 execute(mkdir, syslinux_target)
1549 syslinux_cfg = syslinux_target + 'syslinux.cfg'
1553 # install main configuration only *once*, no matter how many ISOs we have:
1554 syslinux_flavour_is_default = False
1555 syslinux_config_file = open(syslinux_cfg, 'w')
1556 syslinux_config_file.write("TIMEOUT 300\n")
1557 syslinux_config_file.write("include vesamenu.cfg\n")
1558 syslinux_config_file.close()
1560 prompt_name = open(syslinux_target + 'promptname.cfg', 'w')
1561 prompt_name.write('menu label S^yslinux prompt\n')
1564 initial_syslinux_config(syslinux_target)
1565 if search_file('default.cfg', syslinux_target):
1566 modify_filenames(grml_flavour, syslinux_target, ['grml.cfg', 'default.cfg'])
1568 filename = search_file("new_hidden.cfg", syslinux_target)
1571 flavour_filename = grml_flavour.replace('-', '_')
1572 # process hidden file
1573 if not search_file("hidden.cfg", syslinux_target):
1574 new_hidden = syslinux_target + "hidden.cfg"
1575 os.rename(filename, new_hidden)
1576 adjust_syslinux_bootoptions(new_hidden, grml_flavour)
1578 new_hidden = "%s_hidden.cfg" % (flavour_filename)
1579 new_hidden_file = "%s/%s" % (syslinux_target, new_hidden)
1580 os.rename(filename, new_hidden_file)
1581 adjust_labels(new_hidden_file, r'\1 %s-\2' % grml_flavour)
1582 adjust_syslinux_bootoptions(new_hidden_file, grml_flavour)
1583 entry = 'include %s\n' % new_hidden
1584 add_entry_if_not_present("%s/hiddens.cfg" % syslinux_target, entry)
1588 new_default = "%s_default.cfg" % (flavour_filename)
1589 entry = 'include %s\n' % new_default
1590 defaults_file = '%s/defaults.cfg' % syslinux_target
1592 if os.path.isfile(defaults_file):
1593 new_default_with_path = "%s/%s" % (syslinux_target, new_default)
1594 new_grml_cfg = "%s/%s_grml.cfg" % ( syslinux_target, flavour_filename)
1596 # remove default menu entry in menu
1597 remove_default_entry(new_default_with_path)
1599 # adjust all labels for additional isos
1600 adjust_labels(new_default_with_path, r'\1 %s' % grml_flavour)
1601 adjust_labels(new_grml_cfg, r'\1 %s-\2' % grml_flavour)
1604 add_entry_if_not_present("%s/defaults.cfg" % syslinux_target, entry)
1606 add_syslinux_entry("%s/additional.cfg" % syslinux_target, flavour_filename)
1609 def handle_bootloader_config(grml_flavour, device, target):
1610 """Main handler for generating bootloader's configuration
1612 @grml_flavour: name of grml flavour the configuration should be generated for
1613 @device: device/partition where bootloader should be installed to
1614 @target: path of bootloader's configuration files"""
1616 if options.skipsyslinuxconfig:
1617 logging.info("Skipping generation of syslinux configuration as requested.")
1620 handle_syslinux_config(grml_flavour, target)
1621 except CriticalException, error:
1622 logging.critical("Fatal: %s", error)
1625 if options.skipgrubconfig:
1626 logging.info("Skipping generation of grub configuration as requested.")
1629 handle_grub_config(grml_flavour, device, target)
1630 except CriticalException, error:
1631 logging.critical("Fatal: %s", error)
1635 def handle_dir(live_image, device):
1636 """Main logic for copying files of the currently running grml system.
1638 @live_image: directory where currently running live system resides (usually /live/image)
1639 @device: partition where the specified ISO should be installed to"""
1641 logging.info("Using %s as install base", live_image)
1643 if os.path.isdir(device):
1644 logging.info("Specified target is a directory, therefore not mounting.")
1645 device_mountpoint = device
1646 remove_device_mountpoint = False
1648 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1649 register_tmpfile(device_mountpoint)
1650 remove_device_mountpoint = True
1652 mount(device, device_mountpoint, "")
1653 except CriticalException, error:
1654 logging.critical("Fatal: %s", error)
1660 grml_flavour = identify_grml_flavour(live_image)
1661 logging.info("Identified grml flavour \"%s\".", grml_flavour)
1662 install_iso_files(grml_flavour, live_image, device, device_mountpoint)
1664 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1667 if remove_device_mountpoint:
1669 unmount(device_mountpoint, "")
1670 if os.path.isdir(device_mountpoint):
1671 os.rmdir(device_mountpoint)
1672 unregister_tmpfile(device_mountpoint)
1673 except CriticalException, error:
1674 logging.critical("Fatal: %s", error)
1678 def handle_iso(iso, device):
1679 """Main logic for mounting ISOs and copying files.
1681 @iso: full path to the ISO that should be installed to the specified device
1682 @device: partition where the specified ISO should be installed to"""
1684 logging.info("Using ISO %s", iso)
1686 iso_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1687 register_tmpfile(iso_mountpoint)
1688 remove_iso_mountpoint = True
1690 if not os.path.isfile(iso):
1691 logging.critical("Fatal: specified ISO %s could not be read", iso)
1696 mount(iso, iso_mountpoint, ["-o", "loop", "-t", "iso9660"])
1697 except CriticalException, error:
1698 logging.critical("Fatal: %s", error)
1701 if os.path.isdir(device):
1702 logging.info("Specified target is a directory, therefore not mounting.")
1703 device_mountpoint = device
1704 remove_device_mountpoint = False
1707 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1708 register_tmpfile(device_mountpoint)
1709 remove_device_mountpoint = True
1711 mount(device, device_mountpoint, "")
1712 except CriticalException, error:
1713 logging.critical("Fatal: %s", error)
1719 grml_flavour = identify_grml_flavour(iso_mountpoint)
1720 logging.info("Identified grml flavour \"%s\".", grml_flavour)
1721 install_iso_files(grml_flavour, iso_mountpoint, device, device_mountpoint)
1723 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1726 if os.path.isdir(iso_mountpoint) and remove_iso_mountpoint:
1727 unmount(iso_mountpoint, "")
1728 os.rmdir(iso_mountpoint)
1729 unregister_tmpfile(iso_mountpoint)
1730 if remove_device_mountpoint:
1732 unmount(device_mountpoint, "")
1733 if os.path.isdir(device_mountpoint):
1734 os.rmdir(device_mountpoint)
1735 unregister_tmpfile(device_mountpoint)
1736 except CriticalException, error:
1737 logging.critical("Fatal: %s", error)
1741 def handle_mbr(device):
1742 """Main handler for installing master boot record (MBR)
1744 @device: device where the MBR should be installed to"""
1747 logging.info("Would install MBR")
1750 if device[-1:].isdigit():
1751 mbr_device = re.match(r'(.*?)\d*$', device).group(1)
1752 partition_number = int(device[-1:]) - 1
1753 skip_install_mir_mbr = False
1755 # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1756 # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1757 if mbr_device == "/dev/loop":
1759 logging.info("Detected loop device - using %s as MBR device therefore", mbr_device)
1760 skip_install_mir_mbr = True
1763 if options.syslinuxmbr:
1764 handle_syslinux_mbr(mbr_device)
1765 elif not skip_install_mir_mbr:
1767 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrmgr', mbr_device, partition_number, True)
1769 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrldr', mbr_device, partition_number, False)
1770 except IOError, error:
1771 logging.critical("Execution failed: %s", error)
1773 except Exception, error:
1774 logging.critical("Execution failed: %s", error)
1778 def handle_vfat(device):
1779 """Check for FAT specific settings and options
1781 @device: device that should checked / formated"""
1783 # make sure we have mkfs.vfat available
1785 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1786 logging.critical('Sorry, mkfs.vfat not available. Exiting.')
1787 logging.critical('Please make sure to install dosfstools.')
1792 print "Forcing mkfs.fat16 on %s as requested via option --force." % device
1795 # make sure the user is aware of what he is doing
1796 f = raw_input("Are you sure you want to format the specified partition with fat16? y/N ")
1797 if f == "y" or f == "Y":
1798 logging.info("Note: you can skip this question using the option --force")
1804 except CriticalException, error:
1805 logging.critical("Execution failed: %s", error)
1810 # check for vfat filesystem
1811 if device is not None and not os.path.isdir(device):
1813 if options.syslinux: check_for_fat(device)
1814 except CriticalException, error:
1815 logging.critical("Execution failed: %s", error)
1818 if not os.path.isdir(device) and not check_for_usbdevice(device):
1819 print "Warning: the specified device %s does not look like a removable usb device." % device
1820 f = raw_input("Do you really want to continue? y/N ")
1821 if f == "y" or f == "Y":
1827 def handle_compat_warning(device):
1828 """Backwards compatible checks
1830 @device: device that should be checked"""
1832 # make sure we can replace old grml2usb script and warn user when using old way of life:
1833 if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
1834 print "Warning: the semantics of grml2usb has changed."
1835 print "Instead of using grml2usb /path/to/iso %s you might" % device
1836 print "want to use grml2usb /path/to/iso /dev/... instead."
1837 print "Please check out the grml2usb manpage for details."
1838 f = raw_input("Do you really want to continue? y/N ")
1839 if f == "y" or f == "Y":
1845 def handle_logging():
1846 """Log handling and configuration"""
1848 if options.verbose and options.quiet:
1849 parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
1852 FORMAT = "Debug: %(asctime)-15s %(message)s"
1853 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1855 FORMAT = "Critical: %(message)s"
1856 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1858 FORMAT = "%(message)s"
1859 logging.basicConfig(level=logging.INFO, format=FORMAT)
1862 def handle_bootloader(device):
1863 """wrapper for installing bootloader
1865 @device: device where bootloader should be installed to"""
1867 # Install bootloader only if not using the --copy-only option
1868 if options.copyonly:
1869 logging.info("Not installing bootloader and its files as requested via option copyonly.")
1870 elif os.path.isdir(device):
1871 logging.info("Not installing bootloader as %s is a directory.", device)
1873 install_bootloader(device)
1877 """Main function [make pylint happy :)]"""
1880 print os.path.basename(sys.argv[0]) + " " + PROG_VERSION
1884 parser.error("invalid usage")
1889 # make sure we have the appropriate permissions
1892 logging.info("Executing grml2usb version %s", PROG_VERSION)
1895 logging.info("Running in simulation mode as requested via option dry-run.")
1897 if options.grubmbr and not options.grub:
1898 logging.critical("Error: --grub-mbr requires --grub option.")
1901 # specified arguments
1902 device = args[len(args) - 1]
1903 isos = args[0:len(args) - 1]
1905 if not os.path.isdir(device):
1906 if device[-1:].isdigit():
1907 if int(device[-1:]) > 4 or device[-2:].isdigit():
1908 logging.critical("Fatal: installation on partition number >4 not supported. (BIOS won't support it.)")
1911 if os.path.exists(device):
1912 logging.critical("Fatal: installation on raw device not supported. (BIOS won't support it.)")
1915 if not which("rsync"):
1916 logging.critical("Fatal: rsync not available, can not continue - sorry.")
1919 # provide upgrade path
1920 handle_compat_warning(device)
1922 # check for vfat partition
1925 # main operation (like installing files)
1927 if os.path.isdir(iso):
1928 handle_dir(iso, device)
1930 handle_iso(iso, device)
1933 if not os.path.isdir(device):
1934 if not options.skipmbr:
1937 handle_bootloader(device)
1939 logging.info("Note: grml flavour %s was installed as the default booting system.", GRML_DEFAULT)
1941 for flavour in GRML_FLAVOURS:
1942 logging.info("Note: you can boot flavour %s using '%s' on the commandline.", flavour, flavour)
1944 # finally be politely :)
1945 logging.info("Finished execution of grml2usb (%s). Have fun with your grml system.", PROG_VERSION)
1948 if __name__ == "__main__":
1951 except KeyboardInterrupt:
1952 logging.info("Received KeyboardInterrupt")
1955 ## END OF FILE #################################################################
1956 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8