2 # -*- coding: utf-8 -*-
7 This script installs a grml system (either a running system or ISO[s]) to a USB device
9 :copyright: (c) 2009 by Michael Prokop <mika@grml.org>
10 :license: GPL v2 or any later version
11 :bugreports: http://grml.org/bugs/
15 # from __future__ import with_statement
16 from optparse import OptionParser
17 from inspect import isroutine, isclass
18 import datetime, logging, os, re, subprocess, sys, tempfile, time, os.path
21 PROG_VERSION = "0.9.13-pre1"
22 MOUNTED = set() # register mountpoints
23 TMPFILES = set() # register tmpfiles
24 DATESTAMP = time.mktime(datetime.datetime.now().timetuple()) # unique identifier for syslinux.cfg
25 GRML_FLAVOURS = set() # which flavours are being installed?
29 USAGE = "Usage: %prog [options] <[ISO[s] | /live/image]> </dev/sdX#>\n\
31 %prog installs grml ISO[s] to an USB device to be able to boot from it.\n\
32 Make sure you have at least one grml ISO or a running grml system (/live/image),\n\
33 grub or syslinux and root access.\n\
35 Run %prog --help for usage hints, further information via: man grml2usb"
37 # pylint: disable-msg=C0103
38 parser = OptionParser(usage=USAGE)
39 parser.add_option("--bootoptions", dest="bootoptions",
40 action="store", type="string",
41 help="use specified bootoptions as default")
42 parser.add_option("--bootloader-only", dest="bootloaderonly", action="store_true",
43 help="do not copy files but just install a bootloader")
44 parser.add_option("--copy-only", dest="copyonly", action="store_true",
45 help="copy files only but do not install bootloader")
46 parser.add_option("--dry-run", dest="dryrun", action="store_true",
47 help="avoid executing commands")
48 parser.add_option("--fat16", dest="fat16", action="store_true",
49 help="format specified partition with FAT16")
50 parser.add_option("--force", dest="force", action="store_true",
51 help="force any actions requiring manual interaction")
52 parser.add_option("--grub", dest="grub", action="store_true",
53 help="install grub bootloader instead of (default) syslinux")
54 parser.add_option("--grub-mbr", dest="grubmbr", action="store_true",
55 help="install grub into MBR instead of (default) PBR")
56 parser.add_option("--lilo-binary", dest="lilobin", action="store", type="string",
57 help="lilo executable to be used for installing MBR")
58 parser.add_option("--mbr-menu", dest="mbrmenu", action="store_true",
59 help="enable interactive boot menu in MBR")
60 parser.add_option("--quiet", dest="quiet", action="store_true",
61 help="do not output anything but just errors on console")
62 parser.add_option("--skip-addons", dest="skipaddons", action="store_true",
63 help="do not install /boot/addons/ files")
64 parser.add_option("--skip-grub-config", dest="skipgrubconfig", action="store_true",
65 help="skip generation of grub configuration files")
66 parser.add_option("--skip-mbr", dest="skipmbr", action="store_true",
67 help="do not install a master boot record (MBR) on the device")
68 parser.add_option("--skip-syslinux-config", dest="skipsyslinuxconfig", action="store_true",
69 help="skip generation of syslinux configuration files")
70 parser.add_option("--syslinux", dest="syslinux", action="store_true",
71 help="install syslinux bootloader (deprecated as it's the default)")
72 parser.add_option("--syslinux-mbr", dest="syslinuxmbr", action="store_true",
73 help="install syslinux master boot record (MBR) instead of default")
74 parser.add_option("--verbose", dest="verbose", action="store_true",
75 help="enable verbose mode")
76 parser.add_option("-v", "--version", dest="version", action="store_true",
77 help="display version and exit")
78 (options, args) = parser.parse_args()
81 GRML2USB_BASE = '/usr/share/grml2usb'
82 if not os.path.isdir(GRML2USB_BASE):
83 GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
86 class CriticalException(Exception):
87 """Throw critical exception if the exact error is not known but fatal."
89 @Exception: message"""
93 # The following two functions help to operate on strings as
94 # array (list) of bytes (octets). In Python 3000, the bytes
95 # datatype will need to be used. This is intended for using
96 # with manipulation of files on the octet level, like shell
97 # arrays, e.g. in MBR creation.
101 """Convert a list of integers [0;255] to a string."""
102 return struct.pack("%sB" % len(a), *a)
106 """Convert a (bytes) string into a list of integers."""
107 return struct.unpack("%sB" % len(s), s)
111 """Cleanup function to make sure there aren't any mounted devices left behind.
114 logging.info("Cleaning up before exiting...")
115 proc = subprocess.Popen(["sync"])
119 for device in MOUNTED:
121 # ignore: RuntimeError: Set changed size during iteration
123 logging.debug('caught expection RuntimeError, ignoring')
126 def register_tmpfile(path):
128 TODO - not implemented yet
134 def unregister_tmpfile(path):
136 TODO - not implemented yet
140 TMPFILES.remove(path)
143 def register_mountpoint(target):
144 """register specified target in a set() for handling clean exiting
146 @target: destination target of mountpoint
152 def unregister_mountpoint(target):
153 """unregister specified target in a set() for handling clean exiting
155 @target: destination target of mountpoint
158 if target in MOUNTED:
159 MOUNTED.remove(target)
162 def get_function_name(obj):
163 """Helper function for use in execute() to retrive name of a function
165 @obj: the function object
167 if not (isroutine(obj) or isclass(obj)):
169 return obj.__module__ + '.' + obj.__name__
172 def execute(f, *exec_arguments):
173 """Wrapper for executing a command. Either really executes
174 the command (default) or when using --dry-run commandline option
175 just displays what would be executed."""
176 # usage: execute(subprocess.Popen, (["ls", "-la"]))
177 # TODO: doesn't work for proc = execute(subprocess.Popen...() -> any ideas?
179 # pylint: disable-msg=W0141
180 logging.debug('dry-run only: %s(%s)', get_function_name(f), ', '.join(map(repr, exec_arguments)))
182 # pylint: disable-msg=W0142
183 return f(*exec_arguments)
187 """Check whether a given file can be executed
189 @fpath: full path to file
191 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
195 """Check whether a given program is available in PATH
197 @program: name of executable"""
198 fpath = os.path.split(program)[0]
203 for path in os.environ["PATH"].split(os.pathsep):
204 exe_file = os.path.join(path, program)
211 def search_file(filename, search_path='/bin' + os.pathsep + '/usr/bin'):
212 """Given a search path, find file
214 @filename: name of file to search for
215 @search_path: path where searching for the specified filename"""
217 paths = search_path.split(os.pathsep)
218 current_dir = '' # make pylint happy :)
220 # pylint: disable-msg=W0612
221 for current_dir, directories, files in os.walk(path):
222 if os.path.exists(os.path.join(current_dir, filename)):
226 return os.path.abspath(os.path.join(current_dir, filename))
231 def check_uid_root():
232 """Check for root permissions"""
233 if not os.geteuid()==0:
234 sys.exit("Error: please run this script with uid 0 (root).")
237 def mkfs_fat16(device):
238 """Format specified device with VFAT/FAT16 filesystem.
240 @device: partition that should be formated"""
243 logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
246 logging.info("Formating partition with fat16 filesystem")
247 logging.debug("mkfs.vfat -F 16 %s", device)
248 proc = subprocess.Popen(["mkfs.vfat", "-F", "16", device])
250 if proc.returncode != 0:
251 raise CriticalException("error executing mkfs.vfat")
254 def generate_main_grub2_config(grml_flavour, bootoptions):
255 """Generate grub2 configuration for use via grub.cfg
257 @grml_flavour: name of grml flavour the configuration should be generated for
258 @bootoptions: additional bootoptions that should be used by default"""
260 local_datestamp = DATESTAMP
263 ## main grub2 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
269 if loadfont /boot/grub/ascii.pf2 ; then
274 if terminal_output.gfxterm ; then true ; else
275 # For backward compatibility with versions of terminal.mod that don't
276 # understand terminal_output
281 if background_image /boot/grub/grml.png ; then
282 set color_normal=black/black
283 set color_highlight=red/black
285 set menu_color_normal=white/black
286 set menu_color_highlight=black/yellow
289 menuentry "%(grml_flavour)s (default)" {
290 set gfxpayload=1024x768x16,1024x768
291 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
292 initrd /boot/release/%(grml_flavour)s/initrd.gz
295 menuentry "Memory test (memtest86+)" {
296 linux16 /boot/addons/memtest
299 menuentry "Boot Grub (all in one image)" {
300 linux /boot/addons/memdisk
301 initrd /boot/addons/allinone.img
304 menuentry "Boot FreeDOS" {
305 linux /boot/addons/memdisk
306 initrd /boot/addons/balder10.imz
309 menuentry "Boot MirOS bsd4grml" {
310 multiboot /boot/addons/bsd4grml/ldbsd.com
311 module /boot/addons/bsd4grml/bsd.rd
312 module /boot/addons/bsd4grml/boot.1
313 module /boot/addons/bsd4grml/boot.2
314 module /boot/addons/bsd4grml/boot.3
315 module /boot/addons/bsd4grml/boot.4
316 module /boot/addons/bsd4grml/boot.5
317 module /boot/addons/bsd4grml/boot.6
318 module /boot/addons/bsd4grml/boot.cfg
321 menuentry "Boot OS of first partition on first disk" {
326 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
327 'bootoptions': bootoptions } )
330 def generate_flavour_specific_grub2_config(grml_flavour, bootoptions):
331 """Generate grub2 configuration for use via grub.cfg
333 @grml_flavour: name of grml flavour the configuration should be generated for
334 @bootoptions: additional bootoptions that should be used by default"""
336 local_datestamp = DATESTAMP
339 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
340 menuentry "%(grml_flavour)s - boot in default mode" {
341 set gfxpayload=1024x768x16,1024x768
342 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
343 initrd /boot/release/%(grml_flavour)s/initrd.gz
346 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
347 menuentry "%(grml_flavour)s-persistent - enable persistency feature" {
348 set gfxpayload=1024x768x16,1024x768
349 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
350 initrd /boot/release/%(grml_flavour)s/initrd.gz
353 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
354 menuentry "%(grml_flavour)s2ram - copy compressed grml file to RAM" {
355 set gfxpayload=1024x768x16,1024x768
356 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ toram=%(grml_flavour)s.squashfs %(bootoptions)s
357 initrd /boot/release/%(grml_flavour)s/initrd.gz
360 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
361 menuentry "%(grml_flavour)s-debug - enable debugging options" {
362 set gfxpayload=1024x768x16,1024x768
363 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ debug boot=live initcall_debug%(bootoptions)s
364 initrd /boot/release/%(grml_flavour)s/initrd.gz
367 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
368 menuentry "%(grml_flavour)s-x - start X Window System" {
369 set gfxpayload=1024x768x16,1024x768
370 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ startx=wm-ng %(bootoptions)s
371 initrd /boot/release/%(grml_flavour)s/initrd.gz
374 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
375 menuentry "%(grml_flavour)s-nofb - disable framebuffer" {
376 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal video=ofonly %(bootoptions)s
377 initrd /boot/release/%(grml_flavour)s/initrd.gz
380 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
381 menuentry "%(grml_flavour)s-failsafe - disable hardware detection" {
382 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal boot=live noautoconfig atapicd noapic noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia nosmp maxcpus=0 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm noresume selinux=0 edd=off pci=nomsi %(bootoptions)s
383 initrd /boot/release/%(grml_flavour)s/initrd.gz
386 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
387 menuentry "%(grml_flavour)s-forensic - do not touch harddisks during hw recognition" {
388 set gfxpayload=1024x768x16,1024x768
389 linux /boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ nofstab noraid nolvm noautoconfig noswap raid=noautodetect forensic readonly %(bootoptions)s
390 initrd /boot/release/%(grml_flavour)s/initrd.gz
393 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
394 'bootoptions': bootoptions } )
397 def generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootoptions):
398 """Generate grub1 configuration for use via menu.lst
400 @grml_flavour: name of grml flavour the configuration should be generated for
401 @install_partition: partition number for use in (hd0,X)
402 @bootoptions: additional bootoptions that should be used by default"""
404 local_datestamp = DATESTAMP
407 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
408 title %(grml_flavour)s
409 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
410 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
412 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
413 title %(grml_flavour)s-persistent
414 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet persistent live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
415 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
417 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
418 title %(grml_flavour)s2ram
419 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ toram=%(grml_flavour)s.squashfs %(bootoptions)s
420 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
422 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
423 title %(grml_flavour)s-debug
424 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
425 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
427 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
428 title %(grml_flavour)s-x
429 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
430 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
432 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
433 title %(grml_flavour)s-nofb
434 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
435 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
437 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
438 title %(grml_flavour)s-failsafe
439 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce quiet live-media-path=/live/%(grml_flavour)s/ vga=normal boot=live noautoconfig atapicd noapic noacpi acpi=off nomodules nofirewire noudev nousb nohotplug noapm nopcmcia nosmp maxcpus=0 noscsi noagp nodma ide=nodma noswap nofstab nosound nogpm nosyslog nodhcp nocpu nodisc nomodem xmodule=vesa noraid nolvm noresume selinux=0 edd=off pci=nomsi %(bootoptions)s
440 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
442 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
443 title %(grml_flavour)s-forensic
444 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off boot=live nomce vga=791 quiet live-media-path=/live/%(grml_flavour)s/ nofstab noraid nolvm noautoconfig noswap raid=noautodetect forensic readonly %(bootoptions)s
445 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
447 ## flavour specific configuration for %(grml_flavour)s [grml2usb for %(grml_flavour)s: %(local_datestamp)s]
448 title %(grml_flavour)s-serial
449 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
450 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
452 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
453 'bootoptions': bootoptions, 'install_partition': install_partition } )
456 def generate_main_grub1_config(grml_flavour, install_partition, bootoptions):
457 """Generate grub1 configuration for use via menu.lst
459 @grml_flavour: name of grml flavour the configuration should be generated for"""
461 local_datestamp = DATESTAMP
464 ## main grub1 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
467 # color red/blue green/black
468 splashimage=(hd0,%(install_partition)s)/boot/grub/splash.xpm.gz
473 title %(grml_flavour)s - Default boot (using 1024x768 framebuffer)
474 kernel (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/linux26 apm=power-off vga=791 quiet boot=live nomce live-media-path=/live/%(grml_flavour)s/ %(bootoptions)s
475 initrd (hd0,%(install_partition)s)/boot/release/%(grml_flavour)s/initrd.gz
477 title Memory test (memtest86+)
478 kernel (hd0,%(install_partition)s)/boot/addons/memtest
480 title Grub - all in one image
481 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
482 initrd (hd0,%(install_partition)s)/boot/addons/allinone.img
485 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
486 initrd (hd0,%(install_partition)s)/boot/addons/balder10.imz
489 kernel (hd0,%(install_partition)s)/boot/addons/bsd4grml/ldbsd.com
491 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
492 'bootoptions': bootoptions, 'install_partition': install_partition } )
495 def generate_isolinux_splash(grml_flavour):
496 """Generate bootsplash for isolinux/syslinux
498 @grml_flavour: name of grml flavour the configuration should be generated for"""
500 # TODO: adjust last bootsplash line (the one following the "Some information and boot ...")
502 grml_name = grml_flavour
505 \ f17
\f\18/boot/syslinux/logo.16
507 Some information and boot options available via keys F2 - F10. http://grml.org/
509 """ % {'grml_name': grml_name} )
512 def generate_main_syslinux_config(*args):
513 """Generate main configuration for use in syslinux.cfg
515 @*args: just for backward compatibility"""
517 local_datestamp = DATESTAMP
521 menu label Default boot modes:
530 menu label Additional boot entries for:
532 include additional.cfg
539 def generate_flavour_specific_syslinux_config(grml_flavour):
540 """Generate flavour specific configuration for use in syslinux.cfg
542 @grml_flavour: name of grml flavour the configuration should be generated for"""
546 menu begin grml %(grml_flavour)s
547 menu title %(grml_flavour)s
549 menu label ^Back to main menu...
552 # include config for boot parameters from disk
553 include %(grml_flavour)s-grml.cfg
556 """ % {'grml_flavour': grml_flavour } )
559 def install_grub(device):
560 """Install grub on specified device.
562 @mntpoint: mountpoint of device where grub should install its files to
563 @device: partition where grub should be installed to"""
566 logging.info("Would execute grub-install [--root-directory=mount_point] %s now.", device)
568 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
569 register_tmpfile(device_mountpoint)
572 mount(device, device_mountpoint, "")
574 # If using --grub-mbr then make sure we install grub in MBR instead of PBR
575 # Thanks to grub2. NOT.
577 logging.debug("Using option --grub-mbr ...")
578 if device[-1:].isdigit():
579 grub_device = re.match(r'(.*?)\d*$', device).group(1)
585 logging.info("Installing grub as bootloader")
586 logging.debug("grub-install --recheck --no-floppy --root-directory=%s %s",
587 device_mountpoint, grub_device)
588 proc = subprocess.Popen(["grub-install", "--recheck", "--no-floppy",
589 "--root-directory=%s" % device_mountpoint, grub_device], stdout=file(os.devnull, "r+"))
591 if proc.returncode != 0:
592 # raise Exception("error executing grub-install")
593 logging.critical("Fatal: error executing grub-install (please check the grml2usb FAQ or drop the --grub option)")
594 logging.critical("Note: if using grub2 consider using the --grub-mbr option because grub2's PBR feature is broken.")
597 except CriticalException, error:
598 logging.critical("Fatal: %s", error)
603 unmount(device_mountpoint, "")
604 os.rmdir(device_mountpoint)
605 unregister_tmpfile(device_mountpoint)
608 def install_syslinux(device):
609 """Install syslinux on specified device.
611 @device: partition where syslinux should be installed to"""
614 logging.info("Would install syslinux as bootloader on %s", device)
617 # syslinux -d boot/isolinux /dev/sdb1
618 logging.info("Installing syslinux as bootloader")
619 logging.debug("syslinux -d boot/syslinux %s", device)
620 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
622 if proc.returncode != 0:
623 raise CriticalException("Error executing syslinux (either try --fat16 or use grub?)")
626 def install_bootloader(device):
627 """Install bootloader on specified device.
629 @device: partition where bootloader should be installed to"""
631 # by default we use grub, so install syslinux only on request
633 logging.info("Note: the --syslinux option is deprecated as syslinux is grml2usb's default. Continuing anyway.")
636 if not which("grub-install"):
637 logging.critical("Fatal: grub-install not available (please install the grub package or use the --syslinux option)")
643 except CriticalException, error:
644 logging.critical("Fatal: %s", error)
649 install_syslinux(device)
650 except CriticalException, error:
651 logging.critical("Fatal: %s", error)
656 def execute_lilo(lilo, device):
657 """execute lilo for activating the partitions in the MBR
659 @lilo: path of lilo executable
660 @device: device where lilo should be executed on"""
662 # to support -A for extended partitions:
663 logging.info("Activating partitions in MBR via lilo")
664 logging.debug("%s -S /dev/null -M %s ext", lilo, device)
665 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-M", device, "ext"])
667 if proc.returncode != 0:
668 raise Exception("error executing lilo")
670 # activate partition:
671 logging.debug("%s -S /dev/null -A %s 1", lilo, device)
672 proc = subprocess.Popen([lilo, "-S", "/dev/null", "-A", device, "1"])
674 if proc.returncode != 0:
675 raise Exception("error executing lilo")
678 def install_syslinux_mbr(device):
679 """install syslinux's master boot record (MBR) on the specified device
681 @device: device where MBR of syslinux should be installed to"""
683 # make sure we have syslinux available
684 if not which("syslinux") and not options.copyonly:
685 raise Exception("syslinux not available (either install it or consider using the --grub option)")
687 # lilo's mbr is broken, use the one from syslinux instead:
688 if not os.path.isfile("/usr/lib/syslinux/mbr.bin"):
689 raise Exception("/usr/lib/syslinux/mbr.bin can not be read")
691 logging.info("Installing syslinux MBR")
692 logging.debug("cat /usr/lib/syslinux/mbr.bin > %s", device)
694 # TODO -> use Popen instead?
695 retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True)
697 logging.critical("Error copying MBR to device (%s)", retcode)
698 except OSError, error:
699 logging.critical("Execution failed: %s", error)
702 def install_mir_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
703 """install 'mbr' master boot record (MBR) on a device
705 Retrieve the partition table from "device", install an MBR from the
706 "mbrtemplate" file, set the "partition" (0..3) active, and install the
707 result back to "device".
709 @mbrtemplate: default MBR file
711 @device: name of a file assumed to be a hard disc (or USB stick) image, or
712 something like "/dev/sdb"
714 @partition: must be a number between 0 and 3, inclusive
716 @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
719 @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
720 specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
721 false then leave the mirbsdmbr specific flag set to FFh, set all
722 active flags to 0 and set the active flag of the partition to 80h. Note:
723 behaviour of mirbsdmbr: if flag = 0/1/2/3 then use it, otherwise search for
726 logging.info("Installing default MBR")
728 if not os.path.isfile(mbrtemplate):
729 logging.critical("Error: %s can not be read.", mbrtemplate)
730 raise CriticalException("Error installing MBR (either try --syslinux-mbr or install missing file?)")
732 if (partition < 0) or (partition > 3):
733 raise ValueError("partition must be between 0 and 3")
740 tmpf = tempfile.NamedTemporaryFile()
742 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1", device, tmpf.name)
743 proc = subprocess.Popen(["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"], stderr=file(os.devnull, "r+"))
745 if proc.returncode != 0:
746 raise Exception("error executing dd (first run)")
748 logging.debug("executing: dd if=%s of=%s bs=%s count=1 conv=notrunc", mbrtemplate,
749 tmpf.name, nmbrbytes)
750 proc = subprocess.Popen(["dd", "if=%s" % mbrtemplate, "of=%s" % tmpf.name, "bs=%s" % nmbrbytes,
751 "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+"))
753 if proc.returncode != 0:
754 raise Exception("error executing dd (second run)")
756 mbrcode = tmpf.file.read(512)
757 if len(mbrcode) < 512:
758 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
761 mbrcode = mbrcode[0:439] + chr(partition) + \
762 mbrcode[440:510] + "\x55\xAA"
764 actives = ["\x00", "\x00", "\x00", "\x00"]
765 actives[partition] = "\x80"
766 mbrcode = mbrcode[0:446] + actives[0] + \
767 mbrcode[447:462] + actives[1] + \
768 mbrcode[463:478] + actives[2] + \
769 mbrcode[479:494] + actives[3] + \
770 mbrcode[495:510] + "\x55\xAA"
774 tmpf.file.write(mbrcode)
777 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device)
778 proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1",
779 "conv=notrunc"], stderr=file(os.devnull, "r+"))
781 if proc.returncode != 0:
782 raise Exception("error executing dd (third run)")
786 def handle_syslinux_mbr(device):
787 """Install syslinux master boot record on given device
789 @device: device where MBR should be installed to"""
791 if not is_writeable(device):
792 raise IOError("device not writeable for user")
794 # try to use system's lilo
798 # otherwise fall back to our static version
799 from platform import architecture
800 if architecture()[0] == '64bit':
801 lilo = GRML2USB_BASE + '/lilo/lilo.static.amd64'
803 lilo = GRML2USB_BASE + '/lilo/lilo.static.i386'
804 # finally prefer a specified lilo executable
806 lilo = options.lilobin
809 raise Exception("lilo executable can not be execute")
812 logging.info("Would install MBR running lilo and using syslinux.")
815 execute_lilo(lilo, device)
816 install_syslinux_mbr(device)
819 def is_writeable(device):
820 """Check if the device is writeable for the current user
822 @device: partition where bootloader should be installed to"""
826 #raise Exception("no device for checking write permissions")
828 if not os.path.exists(device):
831 return os.access(device, os.W_OK) and os.access(device, os.R_OK)
834 def mount(source, target, mount_options):
835 """Mount specified source on given target
837 @source: name of device/ISO that should be mounted
838 @target: directory where the ISO should be mounted to
839 @options: mount specific options"""
841 # note: options.dryrun does not work here, as we have to
842 # locate files and identify the grml flavour
844 for x in file('/proc/mounts').readlines():
845 if x.startswith(source):
846 raise CriticalException("Error executing mount: %s already mounted - please unmount before invoking grml2usb" % source)
848 if os.path.isdir(source):
849 logging.debug("Source %s is not a device, therefore not mounting.", source)
852 logging.debug("mount %s %s %s", mount_options, source, target)
853 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
855 if proc.returncode != 0:
856 raise CriticalException("Error executing mount (no filesystem on the partition?)")
858 logging.debug("register_mountpoint(%s)", target)
859 register_mountpoint(target)
862 def unmount(target, unmount_options):
863 """Unmount specified target
865 @target: target where something is mounted on and which should be unmounted
866 @options: options for umount command"""
868 # make sure we unmount only already mounted targets
869 target_unmount = False
870 mounts = open('/proc/mounts').readlines()
871 mountstring = re.compile(".*%s.*" % re.escape(os.path.realpath(target)))
873 if re.match(mountstring, line):
874 target_unmount = True
876 if not target_unmount:
877 logging.debug("%s not mounted anymore", target)
879 logging.debug("umount %s %s", list(unmount_options), target)
880 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
882 if proc.returncode != 0:
883 raise Exception("Error executing umount")
885 logging.debug("unregister_mountpoint(%s)", target)
886 unregister_mountpoint(target)
889 def check_for_usbdevice(device):
890 """Check whether the specified device is a removable USB device
892 @device: device name, like /dev/sda1 or /dev/sda
895 usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
897 usbdev = os.path.realpath('/sys/class/block/' + usbdevice + '/removable')
898 if not os.path.isfile(usbdev):
899 # Ubuntu with kernel 2.6.24 for example:
900 usbdev = os.path.realpath('/sys/block/' + usbdevice + '/removable')
902 if os.path.isfile(usbdev):
903 is_usb = open(usbdev).readline()
910 def check_for_fat(partition):
911 """Check whether specified partition is a valid VFAT/FAT16 filesystem
913 @partition: device name of partition"""
916 udev_info = subprocess.Popen(["/sbin/blkid", "-s", "TYPE", "-o", "value", partition],
917 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
918 filesystem = udev_info.communicate()[0].rstrip()
920 if udev_info.returncode == 2:
921 raise CriticalException("Failed to read device %s"
922 " (wrong UID/permissions or device/directory not present?)" % partition)
924 if options.syslinux and filesystem != "vfat":
925 raise CriticalException("Partition %s does not contain a FAT16 filesystem. (Use --fat16 or run mkfs.vfat %s)" % (partition, partition))
928 raise CriticalException("Sorry, /sbin/blkid not available (install e2fsprogs?)")
931 def mkdir(directory):
932 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
934 # just silently pass as it's just fine it the directory exists
935 if not os.path.isdir(directory):
937 os.makedirs(directory)
938 # pylint: disable-msg=W0704
943 def exec_rsync(source, target):
944 """Simple wrapper around rsync to install files
946 @source: source file/directory
947 @target: target file/directory"""
948 logging.debug("Source: %s / Target: %s", source, target)
949 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", source, target])
951 if proc.returncode == 12:
952 logging.critical("Fatal: No space left on device")
956 if proc.returncode != 0:
957 logging.critical("Fatal: could not install %s", source)
962 def copy_system_files(grml_flavour, iso_mount, target):
963 """copy grml's main files (like squashfs, kernel and initrd) to a given target
965 @grml_flavour: name of grml flavour the configuration should be generated for
966 @iso_mount: path where a grml ISO is mounted on
967 @target: path where grml's main files should be copied to"""
969 squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
971 logging.critical("Fatal: squashfs file not found")
972 raise CriticalException("error locating squashfs file")
974 squashfs_target = target + '/live/' + grml_flavour + '/'
975 execute(mkdir, squashfs_target)
976 exec_rsync(squashfs, squashfs_target + grml_flavour + '.squashfs')
978 filesystem_module = search_file('filesystem.module', iso_mount)
979 if filesystem_module is None:
980 logging.critical("Fatal: filesystem.module not found")
981 raise CriticalException("error locating filesystem.module file")
983 exec_rsync(filesystem_module, squashfs_target + 'filesystem.module')
985 release_target = target + '/boot/release/' + grml_flavour
986 execute(mkdir, release_target)
988 kernel = search_file('linux26', iso_mount)
990 logging.critical("Fatal kernel not found")
991 raise CriticalException("error locating kernel file")
993 exec_rsync(kernel, release_target + '/linux26')
995 initrd = search_file('initrd.gz', iso_mount)
997 logging.critical("Fatal: initrd not found")
998 raise CriticalException("error locating initrd file")
1000 exec_rsync(initrd, release_target + '/initrd.gz')
1003 def copy_grml_files(iso_mount, target):
1004 """copy some minor grml files to a given target
1006 @iso_mount: path where a grml ISO is mounted on
1007 @target: path where grml's main files should be copied to"""
1009 grml_target = target + '/grml/'
1010 execute(mkdir, grml_target)
1012 for myfile in 'grml-cheatcodes.txt', 'grml-version', 'LICENSE.txt', 'md5sums', 'README.txt':
1013 grml_file = search_file(myfile, iso_mount)
1014 if grml_file is None:
1015 logging.warn("Warning: myfile %s could not be found - can not install it", myfile)
1017 exec_rsync(grml_file, grml_target + myfile)
1019 grml_web_target = grml_target + '/web/'
1020 execute(mkdir, grml_web_target)
1022 for myfile in 'index.html', 'style.css':
1023 grml_file = search_file(myfile, iso_mount)
1024 if grml_file is None:
1025 logging.warn("Warning: myfile %s could not be found - can not install it")
1027 exec_rsync(grml_file, grml_web_target + myfile)
1029 grml_webimg_target = grml_web_target + '/images/'
1030 execute(mkdir, grml_webimg_target)
1032 for myfile in 'button.png', 'favicon.png', 'linux.jpg', 'logo.png':
1033 grml_file = search_file(myfile, iso_mount)
1034 if grml_file is None:
1035 logging.warn("Warning: myfile %s could not be found - can not install it")
1037 exec_rsync(grml_file, grml_webimg_target + myfile)
1040 def copy_addons(iso_mount, target):
1041 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1043 @iso_mount: path where a grml ISO is mounted on
1044 @target: path where grml's main files should be copied to"""
1046 addons = target + '/boot/addons/'
1047 execute(mkdir, addons)
1049 # grub all-in-one image
1050 allinoneimg = search_file('allinone.img', iso_mount)
1051 if allinoneimg is None:
1052 logging.warn("Warning: allinone.img not found (that's fine if you don't need it)")
1054 exec_rsync(allinoneimg, addons + 'allinone.img')
1057 bsdimg = search_file('bsd4grml', iso_mount)
1059 logging.warn("Warning: bsd4grml not found (that's fine if you don't need it)")
1061 exec_rsync(bsdimg, addons + '/')
1064 balderimg = search_file('balder10.imz', iso_mount)
1065 if balderimg is None:
1066 logging.warn("Warning: balder10.imz not found (that's fine if you don't need it)")
1068 exec_rsync(balderimg, addons + 'balder10.imz')
1070 # install hdt and pci.ids only when using syslinux (grub doesn't support it)
1071 if options.syslinux:
1072 # hdt (hardware detection tool) image
1073 hdtimg = search_file('hdt.c32', iso_mount)
1075 exec_rsync(hdtimg, addons + '/hdt.c32')
1078 picids = search_file('pci.ids', iso_mount)
1080 exec_rsync(picids, addons + '/pci.ids')
1083 memdiskimg = search_file('memdisk', iso_mount)
1084 if memdiskimg is None:
1085 logging.warn("Warning: memdisk not found (that's fine if you don't need it)")
1087 exec_rsync(memdiskimg, addons + 'memdisk')
1090 memtestimg = search_file('memtest', iso_mount)
1091 if memtestimg is None:
1092 logging.warn("Warning: memtest not found (that's fine if you don't need it)")
1094 exec_rsync(memtestimg, addons + 'memtest')
1097 def copy_bootloader_files(iso_mount, target):
1098 """copy grml's bootloader files to a given target
1100 @iso_mount: path where a grml ISO is mounted on
1101 @target: path where grml's main files should be copied to"""
1103 syslinux_target = target + '/boot/syslinux/'
1104 execute(mkdir, syslinux_target)
1106 logo = search_file('logo.16', iso_mount)
1107 exec_rsync(logo, syslinux_target + 'logo.16')
1109 for ffile in 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10':
1110 bootsplash = search_file(ffile, iso_mount)
1111 exec_rsync(bootsplash, syslinux_target + ffile)
1113 # avoid the "file is read only, overwrite anyway (y/n) ?" question
1114 # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
1115 if os.path.isfile(syslinux_target + 'ldlinux.sys'):
1116 os.unlink(syslinux_target + 'ldlinux.sys')
1118 if not search_file('default.cfg', iso_mount + '/boot/isolinux/'):
1119 logging.critical("Fatal: file default.cfg could not be found.")
1120 logging.critical("Note: this grml2usb version requires an ISO generated by grml-live >=0.9.24 ...")
1121 logging.critical(" ... either use grml releases >=2009.10 or switch to an older grml2usb version.")
1124 for filename in 'addons.cfg', 'default.cfg', 'distri.cfg', 'hidden.cfg', \
1125 'grml.cfg', 'grml.png', 'hd.cfg', 'isoprompt.cfg', 'options.cfg', \
1126 'vesamenu.c32', 'vesamenu.cfg', 'grml.png':
1127 path = search_file(filename, iso_mount + '/boot/isolinux/')
1128 exec_rsync(path, syslinux_target + filename)
1130 grub_target = target + '/boot/grub/'
1131 execute(mkdir, grub_target)
1133 if not os.path.isfile(GRML2USB_BASE + "/grub/splash.xpm.gz"):
1134 logging.critical("Error: %s/grub/splash.xpm.gz can not be read.", GRML2USB_BASE)
1135 logging.critical("Please make sure you've installed the grml2usb (Debian) package!")
1138 exec_rsync(GRML2USB_BASE + '/grub/splash.xpm.gz', grub_target + 'splash.xpm.gz')
1140 # grml splash in grub
1141 if os.path.isfile(GRML2USB_BASE + "/grub/grml.png"):
1142 exec_rsync(GRML2USB_BASE + '/grub/grml.png', grub_target + 'grml.png')
1144 # font file for graphical bootsplash in grub
1145 if os.path.isfile("/usr/share/grub/ascii.pf2"):
1146 exec_rsync('/usr/share/grub/ascii.pf2', grub_target + 'ascii.pf2')
1149 def install_iso_files(grml_flavour, iso_mount, device, target):
1150 """Copy files from ISO to given target
1152 @grml_flavour: name of grml flavour the configuration should be generated for
1153 @iso_mount: path where a grml ISO is mounted on
1154 @device: device/partition where bootloader should be installed to
1155 @target: path where grml's main files should be copied to"""
1157 # TODO => several improvements:
1158 # * make sure grml_flavour, iso_mount, target are set when the function is called, otherwise raise exception
1159 # * provide alternative search_file() if file information is stored in a config.ini file?
1160 # * catch "install: .. No space left on device" & CO
1164 elif not options.bootloaderonly:
1165 logging.info("Copying files. This might take a while....")
1167 copy_system_files(grml_flavour, iso_mount, target)
1168 copy_grml_files(iso_mount, target)
1169 except CriticalException, error:
1170 logging.critical("Execution failed: %s", error)
1173 if not options.skipaddons:
1174 if grml_flavour.endswith('-small'):
1175 logging.info("Note: grml-small doesn't provide any addons, not installing them therefore.")
1177 copy_addons(iso_mount, target)
1179 if not options.copyonly:
1180 copy_bootloader_files(iso_mount, target)
1182 if not options.dryrun:
1183 handle_bootloader_config(grml_flavour, device, target)
1185 # make sure we sync filesystems before returning
1186 proc = subprocess.Popen(["sync"])
1190 def uninstall_files(device):
1191 """Get rid of all grml files on specified device
1193 @device: partition where grml2usb files should be removed from"""
1195 # TODO - not implemented yet
1196 logging.critical("TODO: uninstalling files from %s not yet implement, sorry.", device)
1199 def identify_grml_flavour(mountpath):
1200 """Get name of grml flavour
1202 @mountpath: path where the grml ISO is mounted to
1203 @return: name of grml-flavour"""
1205 version_file = search_file('grml-version', mountpath)
1207 if version_file == "":
1208 logging.critical("Error: could not find grml-version file.")
1212 tmpfile = open(version_file, 'r')
1213 grml_info = tmpfile.readline()
1214 grml_flavour = re.match(r'[\w-]*', grml_info).group()
1217 except Exception, e:
1218 logging.critical("Unexpected error: %s", e)
1224 def handle_grub1_config(grml_flavour, install_partition, grub_target, bootopt):
1225 """Main handler for generating grub1 configuration
1227 @grml_flavour: name of grml flavour the configuration should be generated for
1228 @install_partition: partition number for use in (hd0,X)
1229 @grub_target: path of grub's configuration files
1230 @bootoptions: additional bootoptions that should be used by default"""
1233 grub1_cfg = grub_target + 'menu.lst'
1234 logging.debug("Creating grub1 configuration file (menu.lst)")
1236 # install main configuration only *once*, no matter how many ISOs we have:
1237 if os.path.isfile(grub1_cfg):
1238 string = open(grub1_cfg).readline()
1239 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1240 if not re.match(main_identifier, string):
1241 grub1_config_file = open(grub1_cfg, 'w')
1242 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1243 grub1_config_file.close()
1245 grub1_config_file = open(grub1_cfg, 'w')
1246 grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1247 grub1_config_file.close()
1249 grub_flavour_config = True
1250 if os.path.isfile(grub1_cfg):
1251 string = open(grub1_cfg).readlines()
1252 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1254 if flavour.match(line):
1255 grub_flavour_config = False
1257 if grub_flavour_config:
1258 grub1_config_file = open(grub1_cfg, 'a')
1259 grub1_config_file.write(generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootopt))
1260 grub1_config_file.close()
1262 # make sure grub.conf isn't a symlink but a plain file instead,
1263 # otherwise it will break on FAT16 filesystems
1264 # this works around grub-install of (at least) Fedora 10
1265 if os.path.isfile(grub1_cfg):
1266 grubconf = grub_target + 'grub.conf'
1267 if not os.path.islink(grubconf):
1269 shutil.copyfile(grub1_cfg, grub_target + 'grub.conf')
1271 def handle_grub2_config(grml_flavour, grub_target, bootopt):
1272 """Main handler for generating grub2 configuration
1274 @grml_flavour: name of grml flavour the configuration should be generated for
1275 @grub_target: path of grub's configuration files
1276 @bootoptions: additional bootoptions that should be used by default"""
1279 grub2_cfg = grub_target + 'grub.cfg'
1280 logging.debug("Creating grub2 configuration file (grub.lst)")
1284 # install main configuration only *once*, no matter how many ISOs we have:
1285 grub_flavour_is_default = False
1286 if os.path.isfile(grub2_cfg):
1287 string = open(grub2_cfg).readline()
1288 main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1289 if not re.match(main_identifier, string):
1290 grub2_config_file = open(grub2_cfg, 'w')
1291 GRML_DEFAULT = grml_flavour
1292 grub_flavour_is_default = True
1293 grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1294 grub2_config_file.close()
1296 grub2_config_file = open(grub2_cfg, 'w')
1297 GRML_DEFAULT = grml_flavour
1298 grub_flavour_is_default = True
1299 grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1300 grub2_config_file.close()
1302 # install flavour specific configuration only *once* as well
1303 grub_flavour_config = True
1304 if os.path.isfile(grub2_cfg):
1305 string = open(grub2_cfg).readlines()
1306 flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1308 if flavour.match(line):
1309 grub_flavour_config = False
1311 if grub_flavour_config:
1312 grub2_config_file = open(grub2_cfg, 'a')
1313 # display only if the grml flavour isn't the default
1314 if not grub_flavour_is_default:
1315 GRML_FLAVOURS.add(grml_flavour)
1316 grub2_config_file.write(generate_flavour_specific_grub2_config(grml_flavour, bootopt))
1317 grub2_config_file.close()
1320 def handle_grub_config(grml_flavour, device, target):
1321 """Main handler for generating grub (v1 and v2) configuration
1323 @grml_flavour: name of grml flavour the configuration should be generated for
1324 @device: device/partition where grub should be installed to
1325 @target: path of grub's configuration files"""
1327 logging.debug("Generating grub configuration")
1329 grub_target = target + '/boot/grub/'
1330 execute(mkdir, grub_target)
1332 if os.path.isdir(device):
1333 install_grub1_partition = None
1335 if device[-1:].isdigit():
1336 install_grub1_partition = int(device[-1:]) - 1
1338 raise CriticalException("error validating partition schema (raw device?)")
1340 # do NOT write "None" in kernel cmdline
1341 if options.bootoptions is None:
1344 bootopt = options.bootoptions
1347 handle_grub1_config(grml_flavour, install_grub1_partition, grub_target, bootopt)
1349 handle_grub2_config(grml_flavour, grub_target, bootopt)
1352 def initial_syslinux_config(target):
1353 """Generates intial syslinux configuration
1355 @target path of syslinux's configuration files"""
1357 target = target + "/"
1358 filename = target + "grmlmain.cfg"
1359 if os.path.isfile(target + "grmlmain.cfg"):
1361 data = open(filename, "w")
1362 data.write(generate_main_syslinux_config())
1366 def adjust_syslinux_bootoptions(src_name, dst_name, flavour):
1367 append_re = re.compile("^(\s*append.*)$", re.I)
1368 boot_re = re.compile("/boot/([a-zA-Z0-9_]+/)+([a-zA-Z0-9._]+)")
1369 flavour_re = re.compile("(label.*)(grml\w+)")
1370 default_re = re.compile("(default.cfg)")
1371 src = open(src_name, "r")
1372 dst = open(dst_name, "w")
1375 line = boot_re.sub(r'/boot/release/%s/\2 ' % flavour, line)
1376 line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
1377 line = default_re.sub(r'%s-\1' % flavour, line)
1378 line = append_re.sub(r'\1 live-media-path=/live/%s/ ' % flavour, line)
1384 def add_syslinux_entry(filename, grml_flavour):
1385 data = open(filename, "a+")
1386 entry_filename = "option-%s.cfg" % grml_flavour
1387 entry = "include %s\n" % entry_filename
1388 path = os.path.dirname(filename)
1396 data = open(path + "/" + entry_filename, "w")
1397 data.write(generate_flavour_specific_syslinux_config(grml_flavour))
1401 def handle_syslinux_config(grml_flavour, target):
1402 """Main handler for generating syslinux configuration
1404 @grml_flavour: name of grml flavour the configuration should be generated for
1405 @target: path of syslinux's configuration files"""
1407 # do NOT write "None" in kernel cmdline
1408 if options.bootoptions is None:
1411 bootopt = options.bootoptions
1413 logging.debug("Generating syslinux configuration")
1414 syslinux_target = target + '/boot/syslinux/'
1415 # should be present via copy_bootloader_files(), but make sure it exits:
1416 execute(mkdir, syslinux_target)
1417 syslinux_cfg = syslinux_target + 'syslinux.cfg'
1421 # install main configuration only *once*, no matter how many ISOs we have:
1422 syslinux_flavour_is_default = False
1423 syslinux_config_file = open(syslinux_cfg, 'w')
1424 syslinux_config_file.write("include vesamenu.cfg")
1425 syslinux_config_file.close()
1427 initial_syslinux_config(syslinux_target)
1428 for filename in 'grml.cfg', 'default.cfg', 'hidden.cfg':
1429 old_filename = "%s/%s" % (syslinux_target, filename)
1430 new_filename = "%s/%s-%s" % (syslinux_target, grml_flavour, filename)
1431 adjust_syslinux_bootoptions(old_filename, new_filename, grml_flavour)
1432 os.unlink(old_filename)
1434 new_hidden = "%s-hidden.cfg" % (grml_flavour)
1435 new_default = "%s-default.cfg" % (grml_flavour)
1436 default_file = open("%s/defaults.cfg" % syslinux_target, "a+")
1437 entry = "include %s\n" % new_default
1438 for line in default_file:
1442 default_file.write("include %s\n" % new_default)
1444 default_file.close()
1445 add_syslinux_entry("%s/additional.cfg" % syslinux_target, grml_flavour)
1448 def handle_bootloader_config(grml_flavour, device, target):
1449 """Main handler for generating bootloader's configuration
1451 @grml_flavour: name of grml flavour the configuration should be generated for
1452 @device: device/partition where bootloader should be installed to
1453 @target: path of bootloader's configuration files"""
1455 if options.skipsyslinuxconfig:
1456 logging.info("Skipping generation of syslinux configuration as requested.")
1459 handle_syslinux_config(grml_flavour, target)
1460 except CriticalException, error:
1461 logging.critical("Fatal: %s", error)
1464 if options.skipgrubconfig:
1465 logging.info("Skipping generation of grub configuration as requested.")
1468 handle_grub_config(grml_flavour, device, target)
1469 except CriticalException, error:
1470 logging.critical("Fatal: %s", error)
1474 def handle_dir(live_image, device):
1475 """Main logic for copying files of the currently running grml system.
1477 @live_image: directory where currently running live system resides (usually /live/image)
1478 @device: partition where the specified ISO should be installed to"""
1480 logging.info("Using %s as install base", live_image)
1482 if os.path.isdir(device):
1483 logging.info("Specified target is a directory, therefore not mounting.")
1484 device_mountpoint = device
1485 remove_device_mountpoint = False
1487 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1488 register_tmpfile(device_mountpoint)
1489 remove_device_mountpoint = True
1491 mount(device, device_mountpoint, "")
1492 except CriticalException, error:
1493 logging.critical("Fatal: %s", error)
1499 grml_flavour = identify_grml_flavour(live_image)
1500 logging.info("Identified grml flavour \"%s\".", grml_flavour)
1501 install_iso_files(grml_flavour, live_image, device, device_mountpoint)
1503 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1506 if remove_device_mountpoint:
1508 unmount(device_mountpoint, "")
1509 if os.path.isdir(device_mountpoint):
1510 os.rmdir(device_mountpoint)
1511 unregister_tmpfile(device_mountpoint)
1512 except CriticalException, error:
1513 logging.critical("Fatal: %s", error)
1517 def handle_iso(iso, device):
1518 """Main logic for mounting ISOs and copying files.
1520 @iso: full path to the ISO that should be installed to the specified device
1521 @device: partition where the specified ISO should be installed to"""
1523 logging.info("Using ISO %s", iso)
1525 iso_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1526 register_tmpfile(iso_mountpoint)
1527 remove_iso_mountpoint = True
1529 if not os.path.isfile(iso):
1530 logging.critical("Fatal: specified ISO %s could not be read", iso)
1535 mount(iso, iso_mountpoint, ["-o", "loop", "-t", "iso9660"])
1536 except CriticalException, error:
1537 logging.critical("Fatal: %s", error)
1540 if os.path.isdir(device):
1541 logging.info("Specified target is a directory, therefore not mounting.")
1542 device_mountpoint = device
1543 remove_device_mountpoint = False
1546 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1547 register_tmpfile(device_mountpoint)
1548 remove_device_mountpoint = True
1550 mount(device, device_mountpoint, "")
1551 except CriticalException, error:
1552 logging.critical("Fatal: %s", error)
1558 grml_flavour = identify_grml_flavour(iso_mountpoint)
1559 logging.info("Identified grml flavour \"%s\".", grml_flavour)
1560 install_iso_files(grml_flavour, iso_mountpoint, device, device_mountpoint)
1562 logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1565 if os.path.isdir(iso_mountpoint) and remove_iso_mountpoint:
1566 unmount(iso_mountpoint, "")
1567 os.rmdir(iso_mountpoint)
1568 unregister_tmpfile(iso_mountpoint)
1569 if remove_device_mountpoint:
1571 unmount(device_mountpoint, "")
1572 if os.path.isdir(device_mountpoint):
1573 os.rmdir(device_mountpoint)
1574 unregister_tmpfile(device_mountpoint)
1575 except CriticalException, error:
1576 logging.critical("Fatal: %s", error)
1580 def handle_mbr(device):
1581 """Main handler for installing master boot record (MBR)
1583 @device: device where the MBR should be installed to"""
1586 logging.info("Would install MBR")
1589 if device[-1:].isdigit():
1590 mbr_device = re.match(r'(.*?)\d*$', device).group(1)
1591 partition_number = int(device[-1:]) - 1
1592 skip_install_mir_mbr = False
1594 # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1595 # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1596 if mbr_device == "/dev/loop":
1598 logging.info("Detected loop device - using %s as MBR device therefore", mbr_device)
1599 skip_install_mir_mbr = True
1602 if options.syslinuxmbr:
1603 handle_syslinux_mbr(mbr_device)
1604 elif not skip_install_mir_mbr:
1606 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrmgr', mbr_device, partition_number, True)
1608 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrldr', mbr_device, partition_number, False)
1609 except IOError, error:
1610 logging.critical("Execution failed: %s", error)
1612 except Exception, error:
1613 logging.critical("Execution failed: %s", error)
1617 def handle_vfat(device):
1618 """Check for FAT specific settings and options
1620 @device: device that should checked / formated"""
1622 # make sure we have mkfs.vfat available
1624 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1625 logging.critical('Sorry, mkfs.vfat not available. Exiting.')
1626 logging.critical('Please make sure to install dosfstools.')
1631 print "Forcing mkfs.fat16 on %s as requested via option --force." % device
1634 # make sure the user is aware of what he is doing
1635 f = raw_input("Are you sure you want to format the specified partition with fat16? y/N ")
1636 if f == "y" or f == "Y":
1637 logging.info("Note: you can skip this question using the option --force")
1643 except CriticalException, error:
1644 logging.critical("Execution failed: %s", error)
1649 # check for vfat filesystem
1650 if device is not None and not os.path.isdir(device):
1652 check_for_fat(device)
1653 except CriticalException, error:
1654 logging.critical("Execution failed: %s", error)
1657 if not os.path.isdir(device) and not check_for_usbdevice(device):
1658 print "Warning: the specified device %s does not look like a removable usb device." % device
1659 f = raw_input("Do you really want to continue? y/N ")
1660 if f == "y" or f == "Y":
1666 def handle_compat_warning(device):
1667 """Backwards compatible checks
1669 @device: device that should be checked"""
1671 # make sure we can replace old grml2usb script and warn user when using old way of life:
1672 if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
1673 print "Warning: the semantics of grml2usb has changed."
1674 print "Instead of using grml2usb /path/to/iso %s you might" % device
1675 print "want to use grml2usb /path/to/iso /dev/... instead."
1676 print "Please check out the grml2usb manpage for details."
1677 f = raw_input("Do you really want to continue? y/N ")
1678 if f == "y" or f == "Y":
1684 def handle_logging():
1685 """Log handling and configuration"""
1687 if options.verbose and options.quiet:
1688 parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
1691 FORMAT = "Debug: %(asctime)-15s %(message)s"
1692 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1694 FORMAT = "Critical: %(message)s"
1695 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1697 FORMAT = "%(message)s"
1698 logging.basicConfig(level=logging.INFO, format=FORMAT)
1701 def handle_bootloader(device):
1702 """wrapper for installing bootloader
1704 @device: device where bootloader should be installed to"""
1706 # Install bootloader only if not using the --copy-only option
1707 if options.copyonly:
1708 logging.info("Not installing bootloader and its files as requested via option copyonly.")
1709 elif os.path.isdir(device):
1710 logging.info("Not installing bootloader as %s is a directory.", device)
1712 install_bootloader(device)
1716 """Main function [make pylint happy :)]"""
1719 print os.path.basename(sys.argv[0]) + " " + PROG_VERSION
1723 parser.error("invalid usage")
1728 # make sure we have the appropriate permissions
1731 logging.info("Executing grml2usb version %s", PROG_VERSION)
1734 logging.info("Running in simulation mode as requested via option dry-run.")
1736 # specified arguments
1737 device = args[len(args) - 1]
1738 isos = args[0:len(args) - 1]
1740 if not os.path.isdir(device):
1741 if device[-1:].isdigit():
1742 if int(device[-1:]) > 4 or device[-2:].isdigit():
1743 logging.critical("Fatal: installation on partition number >4 not supported. (BIOS won't support it.)")
1746 if os.path.exists(device):
1747 logging.critical("Fatal: installation on raw device not supported. (BIOS won't support it.)")
1750 if not which("rsync"):
1751 logging.critical("Fatal: rsync not available, can not continue - sorry.")
1754 # provide upgrade path
1755 handle_compat_warning(device)
1757 # check for vfat partition
1760 # main operation (like installing files)
1762 if os.path.isdir(iso):
1763 handle_dir(iso, device)
1765 handle_iso(iso, device)
1768 if not os.path.isdir(device):
1769 if not options.skipmbr:
1772 handle_bootloader(device)
1774 logging.info("Note: grml flavour %s was installed as the default booting system.", GRML_DEFAULT)
1776 for flavour in GRML_FLAVOURS:
1777 logging.info("Note: you can boot flavour %s using '%s' on the commandline.", flavour, flavour)
1779 # finally be politely :)
1780 logging.info("Finished execution of grml2usb (%s). Have fun with your grml system.", PROG_VERSION)
1782 if __name__ == "__main__":
1785 except KeyboardInterrupt:
1786 logging.info("Received KeyboardInterrupt")
1789 ## END OF FILE #################################################################
1790 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8