70231c01af39bd9188aad50e11f95760de2b1d3c
[grml2usb.git] / grml2usb
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 grml2usb
5 ~~~~~~~~
6
7 This script installs a grml system (either a running system or ISO[s]) to a USB device
8
9 :copyright: (c) 2009 by Michael Prokop <mika@grml.org>
10 :license: GPL v2 or any later version
11 :bugreports: http://grml.org/bugs/
12
13 """
14
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
19 import fileinput
20 import glob
21
22 # global variables
23 PROG_VERSION = "0.9.15"
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?
28 global GRML_DEFAULT
29
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)
34
35 # cmdline parsing
36 USAGE = "Usage: %prog [options] <[ISO[s] | /live/image]> </dev/sdX#>\n\
37 \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\
41 \n\
42 Run %prog --help for usage hints, further information via: man grml2usb"
43
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()
89
90
91 GRML2USB_BASE = '/usr/share/grml2usb'
92 if not os.path.isdir(GRML2USB_BASE):
93     GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
94
95
96 class CriticalException(Exception):
97     """Throw critical exception if the exact error is not known but fatal."
98
99     @Exception: message"""
100     pass
101
102
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.
108
109
110 def array2string(a):
111     """Convert a list of integers [0;255] to a string."""
112     return struct.pack("%sB" % len(a), *a)
113
114
115 def string2array(s):
116     """Convert a (bytes) string into a list of integers."""
117     return struct.unpack("%sB" % len(s), s)
118
119
120 def cleanup():
121     """Cleanup function to make sure there aren't any mounted devices left behind.
122     """
123
124     logging.info("Cleaning up before exiting...")
125     proc = subprocess.Popen(["sync"])
126     proc.wait()
127
128     try:
129         for device in MOUNTED:
130             unmount(device, "")
131     # ignore: RuntimeError: Set changed size during iteration
132     except RuntimeError:
133         logging.debug('caught expection RuntimeError, ignoring')
134
135
136 def register_tmpfile(path):
137     """
138     TODO - not implemented yet
139     """
140
141     TMPFILES.add(path)
142
143
144 def unregister_tmpfile(path):
145     """
146     TODO - not implemented yet
147     """
148
149     if path in TMPFILES:
150         TMPFILES.remove(path)
151
152
153 def register_mountpoint(target):
154     """register specified target in a set() for handling clean exiting
155
156     @target: destination target of mountpoint
157     """
158
159     MOUNTED.add(target)
160
161
162 def unregister_mountpoint(target):
163     """unregister specified target in a set() for handling clean exiting
164
165     @target: destination target of mountpoint
166     """
167
168     if target in MOUNTED:
169         MOUNTED.remove(target)
170
171
172 def get_function_name(obj):
173     """Helper function for use in execute() to retrive name of a function
174
175     @obj: the function object
176     """
177     if not (isroutine(obj) or isclass(obj)):
178         obj = type(obj)
179     return obj.__module__ + '.' + obj.__name__
180
181
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?
188     if options.dryrun:
189         # pylint: disable-msg=W0141
190         logging.debug('dry-run only: %s(%s)', get_function_name(f), ', '.join(map(repr, exec_arguments)))
191     else:
192         # pylint: disable-msg=W0142
193         return f(*exec_arguments)
194
195
196 def is_exe(fpath):
197     """Check whether a given file can be executed
198
199     @fpath: full path to file
200     @return:"""
201     return os.path.exists(fpath) and os.access(fpath, os.X_OK)
202
203
204 def which(program):
205     """Check whether a given program is available in PATH
206
207     @program: name of executable"""
208     fpath = os.path.split(program)[0]
209     if fpath:
210         if is_exe(program):
211             return program
212     else:
213         for path in os.environ["PATH"].split(os.pathsep):
214             exe_file = os.path.join(path, program)
215             if is_exe(exe_file):
216                 return exe_file
217
218     return None
219
220
221 def search_file(filename, search_path='/bin' + os.pathsep + '/usr/bin'):
222     """Given a search path, find file
223
224     @filename: name of file to search for
225     @search_path: path where searching for the specified filename"""
226     file_found = 0
227     paths = search_path.split(os.pathsep)
228     current_dir = '' # make pylint happy :)
229     for path in paths:
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)):
233                 file_found = 1
234                 break
235     if file_found:
236         return os.path.abspath(os.path.join(current_dir, filename))
237     else:
238         return None
239
240
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).")
245
246
247 def mkfs_fat16(device):
248     """Format specified device with VFAT/FAT16 filesystem.
249
250     @device: partition that should be formated"""
251
252     if options.dryrun:
253         logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
254         return 0
255
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])
259     proc.wait()
260     if proc.returncode != 0:
261         raise CriticalException("error executing mkfs.vfat")
262
263
264 def generate_main_grub2_config(grml_flavour, bootoptions):
265     """Generate grub2 configuration for use via grub.cfg
266
267     @grml_flavour: name of grml flavour the configuration should be generated for
268     @bootoptions: additional bootoptions that should be used by default"""
269
270     local_datestamp = DATESTAMP
271
272     return("""\
273 ## main grub2 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
274 set default=0
275 set timeout=10
276
277 insmod fat
278
279 if loadfont /boot/grub/ascii.pf2 ; then
280    insmod png
281    set gfxmode=640x480
282    insmod gfxterm
283    insmod vbe
284    if terminal_output.gfxterm ; then true ; else
285     # For backward compatibility with versions of terminal.mod that don't
286     # understand terminal_output
287     terminal gfxterm
288    fi
289 fi
290
291 if background_image /boot/grub/grml.png ; then
292   set color_normal=black/black
293   set color_highlight=red/black
294 else
295   set menu_color_normal=white/black
296   set menu_color_highlight=black/yellow
297 fi
298
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
303 }
304
305 menuentry "Memory test (memtest86+)" {
306     linux16   /boot/addons/memtest
307 }
308
309 menuentry "Boot Grub (all in one image)" {
310     linux   /boot/addons/memdisk
311     initrd  /boot/addons/allinone.img
312 }
313
314 menuentry "Boot FreeDOS" {
315     linux   /boot/addons/memdisk
316     initrd  /boot/addons/balder10.imz
317 }
318
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
329 }
330
331 menuentry "Boot OS of first partition on first disk" {
332     set root=(hd0,1)
333     chainloader +1
334 }
335
336 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
337         'flavour_filename': grml_flavour.replace('-', ''),
338         'bootoptions': bootoptions } )
339
340
341 def generate_flavour_specific_grub2_config(grml_flavour, bootoptions):
342     """Generate grub2 configuration for use via grub.cfg
343
344     @grml_flavour: name of grml flavour the configuration should be generated for
345     @bootoptions: additional bootoptions that should be used by default"""
346
347     local_datestamp = DATESTAMP
348
349     return("""\
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
355 }
356
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
362 }
363
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
369 }
370
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
376 }
377
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
383 }
384
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
389 }
390
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
395 }
396
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
402 }
403
404 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
405        'flavour_filename': grml_flavour.replace('-', ''),
406        'bootoptions': bootoptions } )
407
408
409 def generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootoptions):
410     """Generate grub1 configuration for use via menu.lst
411
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"""
415
416     local_datestamp = DATESTAMP
417
418     return("""\
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
423
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
428
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
433
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
438
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
443
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
448
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
453
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
458
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
463
464 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
465        'flavour_filename': grml_flavour.replace('-', ''),
466        'bootoptions': bootoptions, 'install_partition': install_partition } )
467
468
469 def generate_main_grub1_config(grml_flavour, install_partition, bootoptions):
470     """Generate grub1 configuration for use via menu.lst
471
472     @grml_flavour: name of grml flavour the configuration should be generated for"""
473
474     local_datestamp = DATESTAMP
475
476     return("""\
477 ## main grub1 configuration - generated by grml2usb [main config generated at: %(local_datestamp)s]
478 # misc options:
479 timeout 30
480 # color red/blue green/black
481 splashimage=(hd0,%(install_partition)s)/boot/grub/splash.xpm.gz
482 foreground  = 000000
483 background  = FFCC33
484
485 # define entries:
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
489
490 title Memory test (memtest86+)
491 kernel (hd0,%(install_partition)s)/boot/addons/memtest
492
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
496
497 title FreeDOS
498 kernel (hd0,%(install_partition)s)/boot/addons/memdisk
499 initrd (hd0,%(install_partition)s)/boot/addons/balder10.imz
500
501 title MirOS BSD
502 kernel (hd0,%(install_partition)s)/boot/addons/bsd4grml/ldbsd.com
503
504 """ % {'grml_flavour': grml_flavour, 'local_datestamp': local_datestamp,
505        'flavour_filename': grml_flavour.replace('-', ''),
506        'bootoptions': bootoptions, 'install_partition': install_partition } )
507
508
509 def generate_isolinux_splash(grml_flavour):
510     """Generate bootsplash for isolinux/syslinux
511
512     @grml_flavour: name of grml flavour the configuration should be generated for"""
513
514     # TODO: adjust last bootsplash line (the one following the "Some information and boot ...")
515
516     grml_name = grml_flavour
517
518     return("""\
519 \ f17\f\18/boot/syslinux/logo.16
520
521 Some information and boot options available via keys F2 - F10. http://grml.org/
522 %(grml_name)s
523 """ % {'grml_name': grml_name} )
524
525
526 def generate_main_syslinux_config(*args):
527     """Generate main configuration for use in syslinux.cfg
528
529     @*args: just for backward compatibility"""
530
531     local_datestamp = DATESTAMP
532
533     return("""\
534 label -
535 menu label Default boot modes:
536 menu disable
537 include defaults.cfg
538
539 menu end
540 menu separator
541
542 # flavours:
543 label -
544 menu label Additional boot entries for:
545 menu disable
546 include additional.cfg
547
548 menu separator
549 include options.cfg
550 include addons.cfg
551
552 label help
553   include promptname.cfg
554   config prompt.cfg
555   text help
556                                         Jump to old style isolinux prompt
557                                         featuring further information
558                                         regarding available boot options.
559   endtext
560
561
562 include hiddens.cfg
563 """)
564
565
566 def generate_flavour_specific_syslinux_config(grml_flavour):
567     """Generate flavour specific configuration for use in syslinux.cfg
568
569     @grml_flavour: name of grml flavour the configuration should be generated for"""
570
571
572     return("""\
573 menu begin grml %(grml_flavour)s
574     menu title %(display_name)s
575     label mainmenu
576     menu label ^Back to main menu...
577     menu exit
578     menu separator
579     # include config for boot parameters from disk
580     include %(grml_flavour)s_grml.cfg
581     menu hide
582 menu end
583 """ % {'grml_flavour': grml_flavour, 'display_name' : grml_flavour.replace('_', '-') } )
584
585
586 def install_grub(device):
587     """Install grub on specified device.
588
589     @mntpoint: mountpoint of device where grub should install its files to
590     @device: partition where grub should be installed to"""
591
592     if options.dryrun:
593         logging.info("Would execute grub-install [--root-directory=mount_point] %s now.", device)
594     else:
595         device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
596         register_tmpfile(device_mountpoint)
597         try:
598             try:
599                 mount(device, device_mountpoint, "")
600
601                 # If using --grub-mbr then make sure we install grub in MBR instead of PBR
602                 # Thanks to grub2. NOT.
603                 if options.grubmbr:
604                     logging.debug("Using option --grub-mbr ...")
605                     if device[-1:].isdigit():
606                         grub_device = re.match(r'(.*?)\d*$', device).group(1)
607                     else:
608                         grub_device = device
609                 else:
610                     grub_device = device
611
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+"))
617                 proc.wait()
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.")
622                     cleanup()
623                     sys.exit(1)
624             except CriticalException, error:
625                 logging.critical("Fatal: %s", error)
626                 cleanup()
627                 sys.exit(1)
628
629         finally:
630             unmount(device_mountpoint, "")
631             os.rmdir(device_mountpoint)
632             unregister_tmpfile(device_mountpoint)
633
634
635 def install_syslinux(device):
636     """Install syslinux on specified device.
637
638     @device: partition where syslinux should be installed to"""
639
640     if options.dryrun:
641         logging.info("Would install syslinux as bootloader on %s", device)
642         return 0
643
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])
648     proc.wait()
649     if proc.returncode != 0:
650         raise CriticalException("Error executing syslinux (either try --fat16 or use grub?)")
651
652
653 def install_bootloader(device):
654     """Install bootloader on specified device.
655
656     @device: partition where bootloader should be installed to"""
657
658     # by default we use grub, so install syslinux only on request
659     if options.grub:
660         if not which("grub-install"):
661             logging.critical("Fatal: grub-install not available (please install the grub package or use the --syslinux option)")
662             cleanup()
663             sys.exit(1)
664         else:
665             try:
666                 install_grub(device)
667             except CriticalException, error:
668                 logging.critical("Fatal: %s", error)
669                 cleanup()
670                 sys.exit(1)
671     else:
672         try:
673             install_syslinux(device)
674         except CriticalException, error:
675             logging.critical("Fatal: %s", error)
676             cleanup()
677             sys.exit(1)
678
679
680 def execute_lilo(lilo, device):
681     """execute lilo for activating the partitions in the MBR
682
683     @lilo: path of lilo executable
684     @device: device where lilo should be executed on"""
685
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"])
690     proc.wait()
691     if proc.returncode != 0:
692         raise Exception("error executing lilo")
693
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"])
697     proc.wait()
698     if proc.returncode != 0:
699         raise Exception("error executing lilo")
700
701
702 def install_syslinux_mbr(device):
703     """install syslinux's master boot record (MBR) on the specified device
704
705     @device: device where MBR of syslinux should be installed to"""
706
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)")
710
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")
714
715     logging.info("Installing syslinux MBR")
716     logging.debug("cat /usr/lib/syslinux/mbr.bin > %s", device)
717     try:
718         # TODO -> use Popen instead?
719         retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True)
720         if retcode < 0:
721             logging.critical("Error copying MBR to device (%s)", retcode)
722     except OSError, error:
723         logging.critical("Execution failed: %s", error)
724
725
726 def install_mir_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
727     """install 'mbr' master boot record (MBR) on a device
728
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".
732
733     @mbrtemplate: default MBR file
734
735     @device: name of a file assumed to be a hard disc (or USB stick) image, or
736     something like "/dev/sdb"
737
738     @partition: must be a number between 0 and 3, inclusive
739
740     @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
741     ismirbsdmbr) bytes.
742
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
748     the active flag."""
749
750     logging.info("Installing default MBR")
751
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?)")
755
756     if (partition < 0) or (partition > 3):
757         raise ValueError("partition must be between 0 and 3")
758
759     if ismirbsdmbr:
760         nmbrbytes = 439
761     else:
762         nmbrbytes = 440
763
764     tmpf = tempfile.NamedTemporaryFile()
765
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+"))
768     proc.wait()
769     if proc.returncode != 0:
770         raise Exception("error executing dd (first run)")
771
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+"))
776     proc.wait()
777     if proc.returncode != 0:
778         raise Exception("error executing dd (second run)")
779
780     mbrcode = tmpf.file.read(512)
781     if len(mbrcode) < 512:
782         raise EOFError("MBR size (%d) < 512" % len(mbrcode))
783
784     if ismirbsdmbr:
785         mbrcode = mbrcode[0:439] + chr(partition) + \
786           mbrcode[440:510] + "\x55\xAA"
787     else:
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"
795
796     tmpf.file.seek(0)
797     tmpf.file.truncate()
798     tmpf.file.write(mbrcode)
799     tmpf.file.close()
800
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+"))
804     proc.wait()
805     if proc.returncode != 0:
806         raise Exception("error executing dd (third run)")
807     del tmpf
808
809
810 def handle_syslinux_mbr(device):
811     """Install syslinux master boot record on given device
812
813     @device: device where MBR should be installed to"""
814
815     if not is_writeable(device):
816         raise IOError("device not writeable for user")
817
818     # try to use system's lilo
819     if which("lilo"):
820         lilo = which("lilo")
821     else:
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'
826         else:
827             lilo = GRML2USB_BASE + '/lilo/lilo.static.i386'
828     # finally prefer a specified lilo executable
829     if options.lilobin:
830         lilo = options.lilobin
831
832     if not is_exe(lilo):
833         raise Exception("lilo executable can not be execute")
834
835     if options.dryrun:
836         logging.info("Would install MBR running lilo and using syslinux.")
837         return 0
838
839     execute_lilo(lilo, device)
840     install_syslinux_mbr(device)
841
842
843 def is_writeable(device):
844     """Check if the device is writeable for the current user
845
846     @device: partition where bootloader should be installed to"""
847
848     if not device:
849         return False
850         #raise Exception("no device for checking write permissions")
851
852     if not os.path.exists(device):
853         return False
854
855     return os.access(device, os.W_OK) and os.access(device, os.R_OK)
856
857
858 def mount(source, target, mount_options):
859     """Mount specified source on given target
860
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"""
864
865     # note: options.dryrun does not work here, as we have to
866     # locate files and identify the grml flavour
867
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)
871
872     if os.path.isdir(source):
873         logging.debug("Source %s is not a device, therefore not mounting.", source)
874         return 0
875
876     logging.debug("mount %s %s %s", mount_options, source, target)
877     proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
878     proc.wait()
879     if proc.returncode != 0:
880         raise CriticalException("Error executing mount (no filesystem on the partition?)")
881     else:
882         logging.debug("register_mountpoint(%s)", target)
883         register_mountpoint(target)
884
885
886 def unmount(target, unmount_options):
887     """Unmount specified target
888
889     @target: target where something is mounted on and which should be unmounted
890     @options: options for umount command"""
891
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)))
896     for line in mounts:
897         if re.match(mountstring, line):
898             target_unmount = True
899
900     if not target_unmount:
901         logging.debug("%s not mounted anymore", target)
902     else:
903         logging.debug("umount %s %s", list(unmount_options), target)
904         proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
905         proc.wait()
906         if proc.returncode != 0:
907             raise Exception("Error executing umount")
908         else:
909             logging.debug("unregister_mountpoint(%s)", target)
910             unregister_mountpoint(target)
911
912
913 def check_for_usbdevice(device):
914     """Check whether the specified device is a removable USB device
915
916     @device: device name, like /dev/sda1 or /dev/sda
917     """
918
919     usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
920     # newer systems:
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')
925
926     if os.path.isfile(usbdev):
927         is_usb = open(usbdev).readline()
928         if is_usb.find("1"):
929             return 0
930
931     return 1
932
933
934 def check_for_fat(partition):
935     """Check whether specified partition is a valid VFAT/FAT16 filesystem
936
937     @partition: device name of partition"""
938
939     try:
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()
943
944         if udev_info.returncode == 2:
945             raise CriticalException("Failed to read device %s"
946                                     " (wrong UID/permissions or device/directory not present?)" % partition)
947
948         if options.syslinux and filesystem != "vfat":
949             raise CriticalException("Partition %s does not contain a FAT16 filesystem. (Use --fat16 or run mkfs.vfat %s)" % (partition, partition))
950
951     except OSError:
952         raise CriticalException("Sorry, /sbin/blkid not available (install e2fsprogs?)")
953
954
955 def mkdir(directory):
956     """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
957
958     # just silently pass as it's just fine it the directory exists
959     if not os.path.isdir(directory):
960         try:
961             os.makedirs(directory)
962         # pylint: disable-msg=W0704
963         except OSError:
964             pass
965
966
967 def exec_rsync(source, target):
968     """Simple wrapper around rsync to install files
969
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])
974     proc.wait()
975     if proc.returncode == 12:
976         logging.critical("Fatal: No space left on device")
977         cleanup()
978         sys.exit(1)
979
980     if proc.returncode != 0:
981         logging.critical("Fatal: could not install %s", source)
982         cleanup()
983         sys.exit(1)
984
985
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
988
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"""
992
993     squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
994     if squashfs is None:
995         logging.critical("Fatal: squashfs file not found")
996         raise CriticalException("error locating squashfs file")
997     else:
998         squashfs_target = target + '/live/' + grml_flavour + '/'
999         execute(mkdir, squashfs_target)
1000     exec_rsync(squashfs, squashfs_target + grml_flavour + '.squashfs')
1001
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")
1006     else:
1007         exec_rsync(filesystem_module, squashfs_target + 'filesystem.module')
1008
1009     release_target = target + '/boot/release/' + grml_flavour.replace('-', '')
1010     execute(mkdir, release_target)
1011
1012     kernel = search_file('linux26', iso_mount)
1013     if kernel is None:
1014         logging.critical("Fatal kernel not found")
1015         raise CriticalException("error locating kernel file")
1016     else:
1017         exec_rsync(kernel, release_target + '/linux26')
1018
1019     initrd = search_file('initrd.gz', iso_mount)
1020     if initrd is None:
1021         logging.critical("Fatal: initrd not found")
1022         raise CriticalException("error locating initrd file")
1023     else:
1024         exec_rsync(initrd, release_target + '/initrd.gz')
1025
1026
1027 def copy_grml_files(iso_mount, target):
1028     """copy some minor grml files to a given target
1029
1030     @iso_mount: path where a grml ISO is mounted on
1031     @target: path where grml's main files should be copied to"""
1032
1033     grml_target = target + '/grml/'
1034     execute(mkdir, grml_target)
1035
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)
1040         else:
1041             exec_rsync(grml_file, grml_target + myfile)
1042
1043     grml_web_target = grml_target + '/web/'
1044     execute(mkdir, grml_web_target)
1045
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")
1050         else:
1051             exec_rsync(grml_file, grml_web_target + myfile)
1052
1053     grml_webimg_target = grml_web_target + '/images/'
1054     execute(mkdir, grml_webimg_target)
1055
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")
1060         else:
1061             exec_rsync(grml_file, grml_webimg_target + myfile)
1062
1063
1064 def copy_addons(iso_mount, target):
1065     """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
1066
1067     @iso_mount: path where a grml ISO is mounted on
1068     @target: path where grml's main files should be copied to"""
1069
1070     addons = target + '/boot/addons/'
1071     execute(mkdir, addons)
1072
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)")
1077     else:
1078         exec_rsync(allinoneimg, addons + 'allinone.img')
1079
1080     # bsd imag
1081     bsdimg = search_file('bsd4grml', iso_mount)
1082     if bsdimg is None:
1083         logging.warn("Warning: bsd4grml not found (that's fine if you don't need it)")
1084     else:
1085         exec_rsync(bsdimg, addons + '/')
1086
1087     # freedos image
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)")
1091     else:
1092         exec_rsync(balderimg, addons + 'balder10.imz')
1093
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)
1098         if hdtimg:
1099             exec_rsync(hdtimg, addons + '/hdt.c32')
1100
1101         # pci.ids file
1102         picids = search_file('pci.ids', iso_mount)
1103         if picids:
1104             exec_rsync(picids, addons + '/pci.ids')
1105
1106     # memdisk image
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)")
1110     else:
1111         exec_rsync(memdiskimg, addons + 'memdisk')
1112
1113     # memtest86+ image
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)")
1117     else:
1118         exec_rsync(memtestimg, addons + 'memtest')
1119
1120
1121 def copy_bootloader_files(iso_mount, target):
1122     """copy grml's bootloader files to a given target
1123
1124     @iso_mount: path where a grml ISO is mounted on
1125     @target: path where grml's main files should be copied to"""
1126
1127     syslinux_target = target + '/boot/syslinux/'
1128     execute(mkdir, syslinux_target)
1129
1130     logo = search_file('logo.16', iso_mount)
1131     exec_rsync(logo, syslinux_target + 'logo.16')
1132
1133     for ffile in ['f%d' % number for number in range(1,11) ]:
1134         bootsplash = search_file(ffile, iso_mount)
1135         if not bootsplash:
1136             continue
1137         exec_rsync(bootsplash, syslinux_target + ffile)
1138
1139     # avoid the "file is read only, overwrite anyway (y/n) ?" question
1140     # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
1141     if os.path.isfile(syslinux_target + 'ldlinux.sys'):
1142         os.unlink(syslinux_target + 'ldlinux.sys')
1143
1144     bootloader_dirs = ['/boot/isolinux/', '/boot/syslinux/']
1145     source_dir = None
1146     for dir in bootloader_dirs:
1147         if glob.glob(iso_mount + dir + '*default.cfg'):
1148             source_dir = dir
1149             break
1150     else:
1151         logging.critical("Fatal: file default.cfg could not be found.")
1152         logging.critical("Note:  this grml2usb version requires an ISO generated by grml-live >=0.9.24 ...")
1153         logging.critical("       ... either use grml releases >=2009.10 or switch to an older grml2usb version.")
1154         logging.critical("       Please visit http://grml.org/grml2usb/#grml2usb-compat for further information.")
1155         raise
1156
1157     for expr in '*default.cfg', 'distri.cfg', \
1158                     '*grml.cfg', 'grml.png', 'hd.cfg', 'isolinux.cfg', 'isolinux.bin', \
1159                     'isoprompt.cfg', 'options.cfg', \
1160                     'prompt.cfg', 'vesamenu.c32', 'vesamenu.cfg', 'grml.png':
1161         files = glob.glob(iso_mount + source_dir + expr)
1162         for path in files:
1163             filename = os.path.basename(path)
1164             exec_rsync(path, syslinux_target + filename)
1165
1166     # copy the addons_*.cfg file to the new syslinux directory
1167     for filename in glob.glob(iso_mount + source_dir + 'addon*.cfg'):
1168         exec_rsync(filename, syslinux_target)
1169
1170     path = search_file('hidden.cfg', iso_mount + source_dir)
1171     if path:
1172         exec_rsync(path, syslinux_target + "new_" + 'hidden.cfg')
1173
1174
1175     grub_target = target + '/boot/grub/'
1176     execute(mkdir, grub_target)
1177
1178     if not os.path.isfile(GRML2USB_BASE + "/grub/splash.xpm.gz"):
1179         logging.critical("Error: %s/grub/splash.xpm.gz can not be read.", GRML2USB_BASE)
1180         logging.critical("Please make sure you've installed the grml2usb (Debian) package!")
1181         raise
1182     else:
1183         exec_rsync(GRML2USB_BASE + '/grub/splash.xpm.gz', grub_target + 'splash.xpm.gz')
1184
1185     # grml splash in grub
1186     if os.path.isfile(GRML2USB_BASE + "/grub/grml.png"):
1187         exec_rsync(GRML2USB_BASE + '/grub/grml.png', grub_target + 'grml.png')
1188
1189     # font file for graphical bootsplash in grub
1190     if os.path.isfile("/usr/share/grub/ascii.pf2"):
1191         exec_rsync('/usr/share/grub/ascii.pf2', grub_target + 'ascii.pf2')
1192
1193
1194 def install_iso_files(grml_flavour, iso_mount, device, target):
1195     """Copy files from ISO to given target
1196
1197     @grml_flavour: name of grml flavour the configuration should be generated for
1198     @iso_mount: path where a grml ISO is mounted on
1199     @device: device/partition where bootloader should be installed to
1200     @target: path where grml's main files should be copied to"""
1201
1202     # TODO => several improvements:
1203     # * make sure grml_flavour, iso_mount, target are set when the function is called, otherwise raise exception
1204     # * provide alternative search_file() if file information is stored in a config.ini file?
1205     # * catch "install: .. No space left on device" & CO
1206
1207     if options.dryrun:
1208         return 0
1209     elif not options.bootloaderonly:
1210         logging.info("Copying files. This might take a while....")
1211         try:
1212             copy_system_files(grml_flavour, iso_mount, target)
1213             copy_grml_files(iso_mount, target)
1214         except CriticalException, error:
1215             logging.critical("Execution failed: %s", error)
1216             sys.exit(1)
1217
1218     if not options.skipaddons:
1219         if grml_flavour.endswith('-small'):
1220             logging.info("Note: grml-small doesn't provide any addons, not installing them therefore.")
1221         else:
1222             copy_addons(iso_mount, target)
1223
1224     if not options.copyonly:
1225         copy_bootloader_files(iso_mount, target)
1226
1227         if not options.dryrun:
1228             handle_bootloader_config(grml_flavour, device, target)
1229
1230     # make sure we sync filesystems before returning
1231     proc = subprocess.Popen(["sync"])
1232     proc.wait()
1233
1234
1235 def uninstall_files(device):
1236     """Get rid of all grml files on specified device
1237
1238     @device: partition where grml2usb files should be removed from"""
1239
1240     # TODO - not implemented yet
1241     logging.critical("TODO: uninstalling files from %s not yet implement, sorry.", device)
1242
1243
1244 def identify_grml_flavour(mountpath):
1245     """Get name of grml flavour
1246
1247     @mountpath: path where the grml ISO is mounted to
1248     @return: name of grml-flavour"""
1249
1250     version_file = search_file('grml-version', mountpath)
1251
1252     if version_file == "":
1253         logging.critical("Error: could not find grml-version file.")
1254         raise
1255
1256     try:
1257         tmpfile = open(version_file, 'r')
1258         grml_info = tmpfile.readline()
1259         grml_flavour = re.match(r'[\w-]*', grml_info).group()
1260     except TypeError:
1261         raise
1262     except Exception, e:
1263         logging.critical("Unexpected error: %s", e)
1264         raise
1265
1266     return grml_flavour
1267
1268
1269 def modify_grub_config(filename):
1270     if options.removeoption:
1271         regexe = []
1272         for regex in options.removeoption:
1273             regexe.append(re.compile(r'%s' % regex))
1274
1275         option_re = re.compile(r'(.*/boot/release/.*linux26.*)')
1276
1277         for line in fileinput.input(filename, inplace=1):
1278             if regexe and option_re.search(line):
1279                 for regex in regexe:
1280                     line = regex.sub(' ', line)
1281
1282             sys.stdout.write(line)
1283
1284         fileinput.close()
1285
1286 def handle_grub1_config(grml_flavour, install_partition, grub_target, bootopt):
1287     """Main handler for generating grub1 configuration
1288
1289     @grml_flavour: name of grml flavour the configuration should be generated for
1290     @install_partition: partition number for use in (hd0,X)
1291     @grub_target: path of grub's configuration files
1292     @bootoptions: additional bootoptions that should be used by default"""
1293
1294     # grub1 config
1295     grub1_cfg = grub_target + 'menu.lst'
1296     logging.debug("Creating grub1 configuration file (menu.lst)")
1297
1298     # install main configuration only *once*, no matter how many ISOs we have:
1299     if os.path.isfile(grub1_cfg):
1300         string = open(grub1_cfg).readline()
1301         main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1302         if not re.match(main_identifier, string):
1303             grub1_config_file = open(grub1_cfg, 'w')
1304             grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1305             grub1_config_file.close()
1306     else:
1307         grub1_config_file = open(grub1_cfg, 'w')
1308         grub1_config_file.write(generate_main_grub1_config(grml_flavour, install_partition, bootopt))
1309         grub1_config_file.close()
1310
1311     grub_flavour_config = True
1312     if os.path.isfile(grub1_cfg):
1313         string = open(grub1_cfg).readlines()
1314         flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1315         for line in string:
1316             if flavour.match(line):
1317                 grub_flavour_config = False
1318
1319     if grub_flavour_config:
1320         grub1_config_file = open(grub1_cfg, 'a')
1321         grub1_config_file.write(generate_flavour_specific_grub1_config(grml_flavour, install_partition, bootopt))
1322         grub1_config_file.close()
1323
1324     modify_grub_config(grub1_cfg)
1325
1326     # make sure grub.conf isn't a symlink but a plain file instead,
1327     # otherwise it will break on FAT16 filesystems
1328     # this works around grub-install of (at least) Fedora 10
1329     if os.path.isfile(grub1_cfg):
1330         grubconf = grub_target + 'grub.conf'
1331         if not os.path.islink(grubconf):
1332             import shutil
1333             shutil.copyfile(grub1_cfg, grub_target + 'grub.conf')
1334
1335 def handle_grub2_config(grml_flavour, grub_target, bootopt):
1336     """Main handler for generating grub2 configuration
1337
1338     @grml_flavour: name of grml flavour the configuration should be generated for
1339     @grub_target: path of grub's configuration files
1340     @bootoptions: additional bootoptions that should be used by default"""
1341
1342     # grub2 config
1343     grub2_cfg = grub_target + 'grub.cfg'
1344     logging.debug("Creating grub2 configuration file (grub.lst)")
1345
1346     global GRML_DEFAULT
1347
1348     # install main configuration only *once*, no matter how many ISOs we have:
1349     grub_flavour_is_default = False
1350     if os.path.isfile(grub2_cfg):
1351         string = open(grub2_cfg).readline()
1352         main_identifier = re.compile(".*main config generated at: %s.*" % re.escape(str(DATESTAMP)))
1353         if not re.match(main_identifier, string):
1354             grub2_config_file = open(grub2_cfg, 'w')
1355             GRML_DEFAULT = grml_flavour
1356             grub_flavour_is_default = True
1357             grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1358             grub2_config_file.close()
1359     else:
1360         grub2_config_file = open(grub2_cfg, 'w')
1361         GRML_DEFAULT = grml_flavour
1362         grub_flavour_is_default = True
1363         grub2_config_file.write(generate_main_grub2_config(grml_flavour, bootopt))
1364         grub2_config_file.close()
1365
1366     # install flavour specific configuration only *once* as well
1367     grub_flavour_config = True
1368     if os.path.isfile(grub2_cfg):
1369         string = open(grub2_cfg).readlines()
1370         flavour = re.compile("grml2usb for %s: %s" % (re.escape(grml_flavour), re.escape(str(DATESTAMP))))
1371         for line in string:
1372             if flavour.match(line):
1373                 grub_flavour_config = False
1374
1375     if grub_flavour_config:
1376         grub2_config_file = open(grub2_cfg, 'a')
1377         # display only if the grml flavour isn't the default
1378         if not grub_flavour_is_default:
1379             GRML_FLAVOURS.add(grml_flavour)
1380         grub2_config_file.write(generate_flavour_specific_grub2_config(grml_flavour, bootopt))
1381         grub2_config_file.close()
1382
1383     modify_grub_config(grub2_cfg)
1384
1385
1386 def handle_grub_config(grml_flavour, device, target):
1387     """Main handler for generating grub (v1 and v2) configuration
1388
1389     @grml_flavour: name of grml flavour the configuration should be generated for
1390     @device: device/partition where grub should be installed to
1391     @target: path of grub's configuration files"""
1392
1393     logging.debug("Generating grub configuration")
1394
1395     grub_target = target + '/boot/grub/'
1396     execute(mkdir, grub_target)
1397
1398     if os.path.isdir(device):
1399         install_grub1_partition = None
1400     else:
1401         if device[-1:].isdigit():
1402             install_grub1_partition = int(device[-1:]) - 1
1403         else:
1404             raise CriticalException("error validating partition schema (raw device?)")
1405
1406     # do NOT write "None" in kernel cmdline
1407     if options.bootoptions is None:
1408         bootopt = ""
1409     else:
1410         bootopt = options.bootoptions
1411
1412     # write menu.lst
1413     handle_grub1_config(grml_flavour, install_grub1_partition, grub_target, bootopt)
1414     # write grub.cfg
1415     handle_grub2_config(grml_flavour, grub_target, bootopt)
1416
1417
1418 def initial_syslinux_config(target):
1419     """Generates intial syslinux configuration
1420
1421     @target path of syslinux's configuration files"""
1422
1423     target = target + "/"
1424     filename = target + "grmlmain.cfg"
1425     if os.path.isfile(target + "grmlmain.cfg"):
1426         return
1427     data = open(filename, "w")
1428     data.write(generate_main_syslinux_config())
1429     data.close
1430
1431     filename = target + "hiddens.cfg"
1432     data = open(filename, "w")
1433     data.write("include hidden.cfg\n")
1434     data.close()
1435
1436 def add_entry_if_not_present(filename, entry):
1437     data = open(filename, "a+")
1438     for line in data:
1439         if line == entry:
1440             break
1441     else:
1442         data.write(entry)
1443
1444     data.close()
1445
1446
1447
1448 def adjust_syslinux_bootoptions(src, flavour):
1449     append_re = re.compile("^(\s*append.*)$", re.I)
1450     boot_re = re.compile("/boot/([a-zA-Z0-9_]+/)+([a-zA-Z0-9._]+)")
1451     # flavour_re = re.compile("(label.*)(grml\w+)")
1452     default_re = re.compile("(default.cfg)")
1453
1454     # do NOT write "None" in kernel cmdline
1455     if options.bootoptions is None:
1456         bootopt = ""
1457     else:
1458         bootopt = options.bootoptions
1459
1460     regexe = []
1461     option_re = None
1462     if options.removeoption:
1463         option_re = re.compile(r'/boot/release/.*/initrd.gz')
1464
1465         for regex in options.removeoption:
1466             regexe.append(re.compile(r'%s' % regex))
1467
1468     for line in fileinput.input(src, inplace=1):
1469         line = boot_re.sub(r'/boot/release/%s/\2 ' % flavour.replace('-', ''), line)
1470         # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
1471         line = default_re.sub(r'%s-\1' % flavour, line)
1472         line = append_re.sub(r'\1 live-media-path=/live/%s/ ' % flavour, line)
1473         line = append_re.sub(r'\1 boot=live %s ' % bootopt, line)
1474         if option_re and option_re.search(line):
1475             for regex in regexe:
1476                 line = regex.sub(' ', line)
1477         sys.stdout.write(line)
1478     fileinput.close()
1479
1480 def adjust_labels(src, flavour):
1481     label_re = re.compile("^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
1482     for line in fileinput.input(src, inplace=1):
1483         line = label_re.sub(r'\1 %s-\2' % flavour, line)
1484         sys.stdout.write(line)
1485     fileinput.close()
1486
1487
1488 def add_syslinux_entry(filename, grml_flavour):
1489     entry_filename = "option_%s.cfg" % grml_flavour
1490     entry = "include %s\n" % entry_filename
1491
1492     add_entry_if_not_present(filename, entry)
1493     path = os.path.dirname(filename)
1494
1495     data = open(path + "/" + entry_filename, "w")
1496     data.write(generate_flavour_specific_syslinux_config(grml_flavour))
1497     data.close()
1498
1499 def modify_filenames(grml_flavour, target, filenames):
1500     grml_filename = grml_flavour.replace('-', '_')
1501     for filename in filenames:
1502         old_filename = "%s/%s" % (target, filename)
1503         new_filename = "%s/%s_%s" % (target, grml_filename, filename)
1504         os.rename(old_filename, new_filename)
1505         adjust_syslinux_bootoptions(new_filename, grml_flavour)
1506
1507
1508 def remove_default_entry(filename):
1509     default_re = re.compile("^(\s*menu\s*default\s*)$", re.I)
1510     for line in fileinput.input(filename, inplace=1):
1511         if default_re.match(line): continue
1512         sys.stdout.write(line)
1513     fileinput.close()
1514
1515
1516 def handle_syslinux_config(grml_flavour, target):
1517     """Main handler for generating syslinux configuration
1518
1519     @grml_flavour: name of grml flavour the configuration should be generated for
1520     @target: path of syslinux's configuration files"""
1521
1522     # do NOT write "None" in kernel cmdline
1523     if options.bootoptions is None:
1524         bootopt = ""
1525     else:
1526         bootopt = options.bootoptions
1527
1528     logging.debug("Generating syslinux configuration")
1529     syslinux_target = target + '/boot/syslinux/'
1530     # should be present via  copy_bootloader_files(), but make sure it exits:
1531     execute(mkdir, syslinux_target)
1532     syslinux_cfg = syslinux_target + 'syslinux.cfg'
1533
1534     global GRML_DEFAULT
1535
1536     # install main configuration only *once*, no matter how many ISOs we have:
1537     syslinux_flavour_is_default = False
1538     syslinux_config_file = open(syslinux_cfg, 'w')
1539     syslinux_config_file.write("TIMEOUT 300\n")
1540     syslinux_config_file.write("include vesamenu.cfg\n")
1541     syslinux_config_file.close()
1542
1543     prompt_name = open(syslinux_target + 'promptname.cfg', 'w')
1544     prompt_name.write('menu label S^yslinux prompt\n')
1545     prompt_name.close()
1546
1547     initial_syslinux_config(syslinux_target)
1548     if search_file('default.cfg', syslinux_target):
1549         modify_filenames(grml_flavour, syslinux_target, ['grml.cfg', 'default.cfg'])
1550
1551     filename = search_file("new_hidden.cfg", syslinux_target)
1552
1553
1554     flavour_filename = grml_flavour.replace('-', '_')
1555     # process hidden file
1556     if not search_file("hidden.cfg", syslinux_target):
1557         new_hidden = syslinux_target + "hidden.cfg"
1558         os.rename(filename, new_hidden)
1559         adjust_syslinux_bootoptions(new_hidden, grml_flavour)
1560     else:
1561         new_hidden =  "%s_hidden.cfg" % (flavour_filename)
1562         new_hidden_file =  "%s/%s" % (syslinux_target, new_hidden)
1563         os.rename(filename, new_hidden_file)
1564         adjust_labels(new_hidden_file, flavour_filename)
1565         adjust_syslinux_bootoptions(new_hidden_file, flavour_filename)
1566         entry = 'include %s\n' % new_hidden
1567         add_entry_if_not_present("%s/hiddens.cfg" % syslinux_target, entry)
1568
1569
1570
1571     new_default = "%s_default.cfg" % (flavour_filename)
1572     entry = 'include %s\n' % new_default
1573     defaults_file = '%s/defaults.cfg' % syslinux_target
1574
1575     if os.path.isfile(defaults_file):
1576         remove_default_entry('%s/%s_default.cfg' % (syslinux_target, flavour_filename))
1577
1578     add_entry_if_not_present("%s/defaults.cfg" % syslinux_target, entry)
1579
1580     add_syslinux_entry("%s/additional.cfg" % syslinux_target, flavour_filename)
1581
1582
1583 def handle_bootloader_config(grml_flavour, device, target):
1584     """Main handler for generating bootloader's configuration
1585
1586     @grml_flavour: name of grml flavour the configuration should be generated for
1587     @device: device/partition where bootloader should be installed to
1588     @target: path of bootloader's configuration files"""
1589
1590     if options.skipsyslinuxconfig:
1591         logging.info("Skipping generation of syslinux configuration as requested.")
1592     else:
1593         try:
1594             handle_syslinux_config(grml_flavour, target)
1595         except CriticalException, error:
1596             logging.critical("Fatal: %s", error)
1597             sys.exit(1)
1598
1599     if options.skipgrubconfig:
1600         logging.info("Skipping generation of grub configuration as requested.")
1601     else:
1602         try:
1603             handle_grub_config(grml_flavour, device, target)
1604         except CriticalException, error:
1605             logging.critical("Fatal: %s", error)
1606             sys.exit(1)
1607
1608
1609 def handle_dir(live_image, device):
1610     """Main logic for copying files of the currently running grml system.
1611
1612     @live_image: directory where currently running live system resides (usually /live/image)
1613     @device: partition where the specified ISO should be installed to"""
1614
1615     logging.info("Using %s as install base", live_image)
1616
1617     if os.path.isdir(device):
1618         logging.info("Specified target is a directory, therefore not mounting.")
1619         device_mountpoint = device
1620         remove_device_mountpoint = False
1621     else:
1622         device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1623         register_tmpfile(device_mountpoint)
1624         remove_device_mountpoint = True
1625         try:
1626             mount(device, device_mountpoint, "")
1627         except CriticalException, error:
1628             logging.critical("Fatal: %s", error)
1629             cleanup()
1630             sys.exit(1)
1631
1632     try:
1633         try:
1634             grml_flavour = identify_grml_flavour(live_image)
1635             logging.info("Identified grml flavour \"%s\".", grml_flavour)
1636             install_iso_files(grml_flavour, live_image, device, device_mountpoint)
1637         except TypeError:
1638             logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1639             sys.exit(1)
1640     finally:
1641         if remove_device_mountpoint:
1642             try:
1643                 unmount(device_mountpoint, "")
1644                 if os.path.isdir(device_mountpoint):
1645                     os.rmdir(device_mountpoint)
1646                     unregister_tmpfile(device_mountpoint)
1647             except CriticalException, error:
1648                 logging.critical("Fatal: %s", error)
1649                 cleanup()
1650
1651
1652 def handle_iso(iso, device):
1653     """Main logic for mounting ISOs and copying files.
1654
1655     @iso: full path to the ISO that should be installed to the specified device
1656     @device: partition where the specified ISO should be installed to"""
1657
1658     logging.info("Using ISO %s", iso)
1659
1660     iso_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1661     register_tmpfile(iso_mountpoint)
1662     remove_iso_mountpoint = True
1663
1664     if not os.path.isfile(iso):
1665         logging.critical("Fatal: specified ISO %s could not be read", iso)
1666         cleanup()
1667         sys.exit(1)
1668
1669     try:
1670         mount(iso, iso_mountpoint, ["-o", "loop", "-t", "iso9660"])
1671     except CriticalException, error:
1672         logging.critical("Fatal: %s", error)
1673         sys.exit(1)
1674
1675     if os.path.isdir(device):
1676         logging.info("Specified target is a directory, therefore not mounting.")
1677         device_mountpoint = device
1678         remove_device_mountpoint = False
1679         # skip_mbr = True
1680     else:
1681         device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1682         register_tmpfile(device_mountpoint)
1683         remove_device_mountpoint = True
1684         try:
1685             mount(device, device_mountpoint, "")
1686         except CriticalException, error:
1687             logging.critical("Fatal: %s", error)
1688             cleanup()
1689             sys.exit(1)
1690
1691     try:
1692         try:
1693             grml_flavour = identify_grml_flavour(iso_mountpoint)
1694             logging.info("Identified grml flavour \"%s\".", grml_flavour)
1695             install_iso_files(grml_flavour, iso_mountpoint, device, device_mountpoint)
1696         except TypeError:
1697             logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
1698             sys.exit(1)
1699     finally:
1700         if os.path.isdir(iso_mountpoint) and remove_iso_mountpoint:
1701             unmount(iso_mountpoint, "")
1702             os.rmdir(iso_mountpoint)
1703             unregister_tmpfile(iso_mountpoint)
1704         if remove_device_mountpoint:
1705             try:
1706                 unmount(device_mountpoint, "")
1707                 if os.path.isdir(device_mountpoint):
1708                     os.rmdir(device_mountpoint)
1709                     unregister_tmpfile(device_mountpoint)
1710             except CriticalException, error:
1711                 logging.critical("Fatal: %s", error)
1712                 cleanup()
1713
1714
1715 def handle_mbr(device):
1716     """Main handler for installing master boot record (MBR)
1717
1718     @device: device where the MBR should be installed to"""
1719
1720     if options.dryrun:
1721         logging.info("Would install MBR")
1722         return 0
1723
1724     if device[-1:].isdigit():
1725         mbr_device = re.match(r'(.*?)\d*$', device).group(1)
1726         partition_number = int(device[-1:]) - 1
1727         skip_install_mir_mbr = False
1728
1729     # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1730     # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1731     if mbr_device == "/dev/loop":
1732         mbr_device = device
1733         logging.info("Detected loop device - using %s as MBR device therefore", mbr_device)
1734         skip_install_mir_mbr = True
1735
1736     try:
1737         if options.syslinuxmbr:
1738             handle_syslinux_mbr(mbr_device)
1739         elif not skip_install_mir_mbr:
1740             if options.mbrmenu:
1741                 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrmgr', mbr_device, partition_number, True)
1742             else:
1743                 install_mir_mbr(GRML2USB_BASE + '/mbr/mbrldr', mbr_device, partition_number, False)
1744     except IOError, error:
1745         logging.critical("Execution failed: %s", error)
1746         sys.exit(1)
1747     except Exception, error:
1748         logging.critical("Execution failed: %s", error)
1749         sys.exit(1)
1750
1751
1752 def handle_vfat(device):
1753     """Check for FAT specific settings and options
1754
1755     @device: device that should checked / formated"""
1756
1757     # make sure we have mkfs.vfat available
1758     if options.fat16:
1759         if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1760             logging.critical('Sorry, mkfs.vfat not available. Exiting.')
1761             logging.critical('Please make sure to install dosfstools.')
1762             sys.exit(1)
1763
1764         exec_mkfs = False
1765         if options.force:
1766             print "Forcing mkfs.fat16 on %s as requested via option --force." % device
1767             exec_mkfs = True
1768         else:
1769             # make sure the user is aware of what he is doing
1770             f = raw_input("Are you sure you want to format the specified partition with fat16? y/N ")
1771             if f == "y" or f == "Y":
1772                 logging.info("Note: you can skip this question using the option --force")
1773                 exec_mkfs = True
1774
1775         if exec_mkfs:
1776             try:
1777                 mkfs_fat16(device)
1778             except CriticalException, error:
1779                 logging.critical("Execution failed: %s", error)
1780                 sys.exit(1)
1781         else:
1782             sys.exit(1)
1783
1784     # check for vfat filesystem
1785     if device is not None and not os.path.isdir(device):
1786         try:
1787             check_for_fat(device)
1788         except CriticalException, error:
1789             logging.critical("Execution failed: %s", error)
1790             sys.exit(1)
1791
1792     if not os.path.isdir(device) and not check_for_usbdevice(device):
1793         print "Warning: the specified device %s does not look like a removable usb device." % device
1794         f = raw_input("Do you really want to continue? y/N ")
1795         if f == "y" or f == "Y":
1796             pass
1797         else:
1798             sys.exit(1)
1799
1800
1801 def handle_compat_warning(device):
1802     """Backwards compatible checks
1803
1804     @device: device that should be checked"""
1805
1806     # make sure we can replace old grml2usb script and warn user when using old way of life:
1807     if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
1808         print "Warning: the semantics of grml2usb has changed."
1809         print "Instead of using grml2usb /path/to/iso %s you might" % device
1810         print "want to use grml2usb /path/to/iso /dev/... instead."
1811         print "Please check out the grml2usb manpage for details."
1812         f = raw_input("Do you really want to continue? y/N ")
1813         if f == "y" or f == "Y":
1814             pass
1815         else:
1816             sys.exit(1)
1817
1818
1819 def handle_logging():
1820     """Log handling and configuration"""
1821
1822     if options.verbose and options.quiet:
1823         parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
1824
1825     if options.verbose:
1826         FORMAT = "Debug: %(asctime)-15s %(message)s"
1827         logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1828     elif options.quiet:
1829         FORMAT = "Critical: %(message)s"
1830         logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1831     else:
1832         FORMAT = "%(message)s"
1833         logging.basicConfig(level=logging.INFO, format=FORMAT)
1834
1835
1836 def handle_bootloader(device):
1837     """wrapper for installing bootloader
1838
1839     @device: device where bootloader should be installed to"""
1840
1841     # Install bootloader only if not using the --copy-only option
1842     if options.copyonly:
1843         logging.info("Not installing bootloader and its files as requested via option copyonly.")
1844     elif os.path.isdir(device):
1845         logging.info("Not installing bootloader as %s is a directory.", device)
1846     else:
1847         install_bootloader(device)
1848
1849
1850 def main():
1851     """Main function [make pylint happy :)]"""
1852
1853     if options.version:
1854         print os.path.basename(sys.argv[0]) + " " + PROG_VERSION
1855         sys.exit(0)
1856
1857     if len(args) < 2:
1858         parser.error("invalid usage")
1859
1860     # log handling
1861     handle_logging()
1862
1863     # make sure we have the appropriate permissions
1864     check_uid_root()
1865
1866     logging.info("Executing grml2usb version %s", PROG_VERSION)
1867
1868     if options.dryrun:
1869         logging.info("Running in simulation mode as requested via option dry-run.")
1870
1871     # specified arguments
1872     device = args[len(args) - 1]
1873     isos = args[0:len(args) - 1]
1874
1875     if not os.path.isdir(device):
1876         if device[-1:].isdigit():
1877             if int(device[-1:]) > 4 or device[-2:].isdigit():
1878                 logging.critical("Fatal: installation on partition number >4 not supported. (BIOS won't support it.)")
1879                 sys.exit(1)
1880         else:
1881             if os.path.exists(device):
1882                 logging.critical("Fatal: installation on raw device not supported. (BIOS won't support it.)")
1883                 sys.exit(1)
1884
1885     if not which("rsync"):
1886         logging.critical("Fatal: rsync not available, can not continue - sorry.")
1887         sys.exit(1)
1888
1889     # provide upgrade path
1890     handle_compat_warning(device)
1891
1892     # check for vfat partition
1893     handle_vfat(device)
1894
1895     # main operation (like installing files)
1896     for iso in isos:
1897         if os.path.isdir(iso):
1898             handle_dir(iso, device)
1899         else:
1900             handle_iso(iso, device)
1901
1902     # install mbr
1903     if not os.path.isdir(device):
1904         if not options.skipmbr:
1905             handle_mbr(device)
1906
1907     handle_bootloader(device)
1908
1909     logging.info("Note: grml flavour %s was installed as the default booting system.", GRML_DEFAULT)
1910
1911     for flavour in GRML_FLAVOURS:
1912         logging.info("Note: you can boot flavour %s using '%s' on the commandline.", flavour, flavour)
1913
1914     # finally be politely :)
1915     logging.info("Finished execution of grml2usb (%s). Have fun with your grml system.", PROG_VERSION)
1916
1917
1918 if __name__ == "__main__":
1919     try:
1920         main()
1921     except KeyboardInterrupt:
1922         logging.info("Received KeyboardInterrupt")
1923         cleanup()
1924
1925 ## END OF FILE #################################################################
1926 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8