2 # -*- coding: utf-8 -*-
3 # pylint: disable-msg=C0302
8 This script installs a Grml system (either a running system or ISO[s]) to a USB device
10 :copyright: (c) 2009, 2010, 2011 by Michael Prokop <mika@grml.org>
11 :license: GPL v2 or any later version
12 :bugreports: http://grml.org/bugs/
16 from __future__ import print_function
17 from optparse import OptionParser
18 from inspect import isroutine, isclass
34 # The line following this line is patched by debian/rules and tarball.sh.
35 PROG_VERSION = '***UNRELEASED***'
38 MOUNTED = set() # register mountpoints
39 TMPFILES = set() # register tmpfiles
40 DATESTAMP = time.mktime(datetime.datetime.now().timetuple()) # unique identifier for syslinux.cfg
41 GRML_FLAVOURS = set() # which flavours are being installed?
44 SYSLINUX_LIBS = "/usr/lib/syslinux/"
45 GPT_HEADER = "\x55\xaa\x45\x46\x49\x20\x50\x41\x52\x54" # original GPT header
48 RE_PARTITION = re.compile(r'([a-z/]*?)(\d+)$')
49 RE_P_PARTITION = re.compile(r'(.*?\d+)p(\d+)$')
50 RE_LOOP_DEVICE = re.compile(r'/dev/loop\d+$')
53 def syslinux_warning(option, opt, value, opt_parser):
54 """A helper function for printing a warning about deprecated option
56 # pylint: disable-msg=W0613
57 sys.stderr.write("Note: the --syslinux option is deprecated as syslinux "
58 "is grml2usb's default. Continuing anyway.\n")
59 setattr(opt_parser.values, option.dest, True)
62 # if grub option is set, unset syslinux option
63 def grub_option(option, opt, value, opt_parser):
64 """A helper function adjusting other option values
66 # pylint: disable-msg=W0613
67 setattr(opt_parser.values, option.dest, True)
68 setattr(opt_parser.values, 'syslinux', False)
71 USAGE = "Usage: %prog [options] <[ISO[s] | /lib/live/mount/medium]> </dev/sdX#>\n\
73 %prog installs Grml ISO[s] to an USB device to be able to boot from it.\n\
74 Make sure you have at least one Grml ISO or a running Grml system (/lib/live/mount/medium),\n\
75 grub or syslinux and root access.\n\
77 Run %prog --help for usage hints, further information via: man grml2usb"
79 # pylint: disable-msg=C0103
80 # pylint: disable-msg=W0603
81 parser = OptionParser(usage=USAGE)
82 parser.add_option("--bootoptions", dest="bootoptions",
83 action="append", type="string",
84 help="use specified bootoptions as default")
85 parser.add_option("--bootloader-only", dest="bootloaderonly", action="store_true",
86 help="do not copy files but just install a bootloader")
87 parser.add_option("--copy-only", dest="copyonly", action="store_true",
88 help="copy files only but do not install bootloader")
89 parser.add_option("--dry-run", dest="dryrun", action="store_true",
90 help="avoid executing commands")
91 parser.add_option("--fat16", dest="fat16", action="store_true",
92 help="format specified partition with FAT16")
93 parser.add_option("--force", dest="force", action="store_true",
94 help="force any actions requiring manual interaction")
95 parser.add_option("--grub", dest="grub", action="callback",
97 help="install grub bootloader instead of (default) syslinux")
98 parser.add_option("--grub-mbr", dest="grubmbr", action="store_true",
99 help="install grub into MBR instead of (default) PBR")
100 parser.add_option("--mbr-menu", dest="mbrmenu", action="store_true",
101 help="enable interactive boot menu in MBR")
102 parser.add_option("--quiet", dest="quiet", action="store_true",
103 help="do not output anything but just errors on console")
104 parser.add_option("--remove-bootoption", dest="removeoption", action="append",
105 help="regex for removing existing bootoptions")
106 parser.add_option("--skip-addons", dest="skipaddons", action="store_true",
107 help="do not install /boot/addons/ files")
108 parser.add_option("--skip-bootflag", dest="skipbootflag", action="store_true",
109 help="do not try to check whether the destination has the bootflag set")
110 parser.add_option("--skip-grub-config", dest="skipgrubconfig", action="store_true",
111 help="skip generation of grub configuration files")
112 parser.add_option("--skip-mbr", dest="skipmbr", action="store_true",
113 help="do not install a master boot record (MBR) on the device")
114 parser.add_option("--skip-syslinux-config", dest="skipsyslinuxconfig", action="store_true",
115 help="skip generation of syslinux configuration files")
116 parser.add_option("--skip-usb-check", dest="skipusbcheck", action="store_true",
117 help="skip check to verify whether given device is removable")
118 parser.add_option("--syslinux", dest="syslinux", action="callback", default=True,
119 callback=syslinux_warning,
120 help="install syslinux bootloader (deprecated as it's the default)")
121 parser.add_option("--syslinux-mbr", dest="syslinuxmbr", action="store_true",
122 help="install syslinux master boot record (MBR) instead of default")
123 parser.add_option("--tmpdir", dest="tmpdir", default="/tmp",
124 help="directory to be used for temporary files")
125 parser.add_option("--verbose", dest="verbose", action="store_true",
126 help="enable verbose mode")
127 parser.add_option("-v", "--version", dest="version", action="store_true",
128 help="display version and exit")
129 (options, args) = parser.parse_args()
132 GRML2USB_BASE = '/usr/share/grml2usb'
133 if not os.path.isdir(GRML2USB_BASE):
134 GRML2USB_BASE = os.path.dirname(os.path.realpath(__file__))
137 class HodorException(Exception):
138 """Throw exception if the exact error is not known and not fatal.
140 @Exception: message"""
143 class CriticalException(Exception):
144 """Throw critical exception if the exact error is not known but fatal.
146 @Exception: message"""
149 class VerifyException(Exception):
150 """Throw critical exception if there is an fatal error when verifying something.
152 @Exception: message"""
155 # The following two functions help to operate on strings as
156 # array (list) of bytes (octets). In Python 3000, the bytes
157 # datatype will need to be used. This is intended for using
158 # with manipulation of files on the octet level, like shell
159 # arrays, e.g. in MBR creation.
162 def array2string(*a):
163 """Convert a list of integers [0;255] to a string."""
164 return struct.pack("%sB" % len(a), *a)
168 """Convert a (bytes) string into a list of integers."""
169 return struct.unpack("%sB" % len(s), s)
173 """Cleanup function to make sure there aren't any mounted devices left behind.
175 def del_failed(fn, filepath, exc):
176 msg = "Deletion of %s failed in temporary folder %s"
177 logging.warn(msg % (filepath, path))
179 logging.info("Cleaning up before exiting...")
180 proc = subprocess.Popen(["sync"])
183 for device in MOUNTED.copy():
186 logging.debug('Unmounted %s' % device)
187 except StandardError:
188 logging.debug('RuntimeError while umount %s, ignoring' % device)
190 for tmppath in TMPFILES.copy():
192 if os.path.isdir(tmppath) and not os.path.islink(tmppath):
193 # symbolic links to directories are ignored
194 # without the check it will throw an OSError
195 shutil.rmtree(tmppath, onerror=del_failed)
196 logging.debug('temporary directory %s deleted' % tmppath)
197 unregister_tmpfile(tmppath)
200 logging.debug('temporary file %s deleted' % tmppath)
201 unregister_tmpfile(tmppath)
202 except StandardError:
203 msg = 'RuntimeError while removing temporary %s, ignoring'
204 logging.debug(msg % tmppath)
207 def register_tmpfile(path):
215 def unregister_tmpfile(path):
217 remove registered tmpfile
221 TMPFILES.remove(path)
226 def register_mountpoint(target):
227 """register specified target in a set() for handling clean exiting
229 @target: destination target of mountpoint
235 def unregister_mountpoint(target):
236 """unregister specified target in a set() for handling clean exiting
238 @target: destination target of mountpoint
241 if target in MOUNTED:
242 MOUNTED.remove(target)
245 def get_function_name(obj):
246 """Helper function for use in execute() to retrive name of a function
248 @obj: the function object
250 if not (isroutine(obj) or isclass(obj)):
252 return obj.__module__ + '.' + obj.__name__
255 def execute(f, *exec_arguments):
256 """Wrapper for executing a command. Either really executes
257 the command (default) or when using --dry-run commandline option
258 just displays what would be executed."""
259 # usage: execute(subprocess.Popen, (["ls", "-la"]))
261 # pylint: disable-msg=W0141
262 logging.debug('dry-run only: %s(%s)', get_function_name(f), ', '.join(map(repr, exec_arguments)))
264 # pylint: disable-msg=W0142
265 return f(*exec_arguments)
269 """Check whether a given file can be executed
271 @fpath: full path to file
273 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
277 """Check whether a given program is available in PATH
279 @program: name of executable"""
280 fpath = os.path.split(program)[0]
285 for path in os.environ["PATH"].split(os.pathsep):
286 exe_file = os.path.join(path, program)
293 def get_defaults_file(iso_mount, flavour, name):
294 """get the default file for syslinux
296 bootloader_dirs = ['/boot/isolinux/', '/boot/syslinux/']
297 for directory in bootloader_dirs:
299 "%s_%s" % (get_flavour_filename(flavour), name):
300 if os.path.isfile(iso_mount + directory + name):
301 return (directory, name)
305 def search_file(filename, search_path='/bin' + os.pathsep + '/usr/bin', lst_return=False):
306 """Given a search path, find file
308 @filename: name of file to search for
309 @search_path: path where searching for the specified filename
310 @lst_return: return list of matching files instead one file"""
311 paths = search_path.split(os.pathsep)
312 current_dir = '' # make pylint happy :)
316 """Helper function ffor testing if specified file exists in cwd
318 @cwd: current working directory
320 return os.path.exists(os.path.join(cwd, filename))
324 if match_file(current_dir):
325 retval.append(os.path.abspath(os.path.join(current_dir, filename)))
328 # pylint: disable-msg=W0612
329 for current_dir, directories, files in os.walk(path):
330 if match_file(current_dir):
331 retval.append(os.path.abspath(os.path.join(current_dir, filename)))
342 def check_uid_root():
343 """Check for root permissions"""
344 if not os.geteuid() == 0:
345 raise CriticalException("please run this script with uid 0 (root).")
348 # for usage inside check_boot_flag
349 def get_partition_for_path(path):
352 boot_dev, x = get_device_from_partition(path)
354 d = parted.getDevice(boot_dev)
355 disk = parted.Disk(d)
356 return disk.getPartitionByPath(path)
359 def check_boot_flag(device):
360 boot_dev, x = get_device_from_partition(device)
364 part = get_partition_for_path(device)
366 raise HodorException("parted could not find partition")
367 if part.getFlag(parted.PARTITION_BOOT):
368 logging.debug("bootflag is enabled on %s" % device)
370 except HodorException, e:
371 logging.info("%s, falling back to old bootflag detection", e)
372 except ImportError, e:
373 logging.debug("could not import parted, falling back to old bootflag detection")
375 with open(boot_dev, 'r') as image:
376 data = image.read(520)
377 bootcode = data[440:]
378 gpt_data = bootcode[70:80]
380 if gpt_data == GPT_HEADER:
381 logging.info("GPT detected, skipping bootflag check")
382 elif bootcode[6] == '\x80':
383 logging.debug("bootflag is enabled")
385 logging.debug("bootflag is NOT enabled")
386 raise VerifyException("Device %s does not have the bootflag set. "
387 "Please enable it to be able to boot." % device)
390 def mkfs_fat16(device):
391 """Format specified device with VFAT/FAT16 filesystem.
393 @device: partition that should be formated"""
396 logging.info("Would execute mkfs.vfat -F 16 %s now.", device)
399 logging.info("Formating partition with fat16 filesystem")
400 logging.debug("mkfs.vfat -F 16 %s", device)
401 proc = subprocess.Popen(["mkfs.vfat", "-F", "16", device])
403 if proc.returncode != 0:
404 raise CriticalException("error executing mkfs.vfat")
407 def generate_isolinux_splash(grml_flavour):
408 """Generate bootsplash for isolinux/syslinux
410 @grml_flavour: name of grml flavour the configuration should be generated for"""
412 grml_name = grml_flavour
415 \ f17
\f\18/boot/syslinux/logo.16
417 Some information and boot options available via keys F2 - F10. http://grml.org/
419 """ % {'grml_name': grml_name})
422 def generate_main_syslinux_config(*arg):
423 """Generate main configuration for use in syslinux.cfg
425 @*arg: just for backward compatibility"""
426 # pylint: disable-msg=W0613
427 # remove warning about unused arg
431 menu label Default boot modes:
440 menu label Additional boot entries for:
442 include additional.cfg
449 include promptname.cfg
452 Jump to old style isolinux prompt
453 featuring further information
454 regarding available boot options.
462 def generate_flavour_specific_syslinux_config(grml_flavour):
463 """Generate flavour specific configuration for use in syslinux.cfg
465 @grml_flavour: name of grml flavour the configuration should be generated for"""
468 menu begin grml %(grml_flavour)s
469 menu title %(display_name)s
471 menu label ^Back to main menu...
474 # include config for boot parameters from disk
475 include %(grml_flavour)s_grml.cfg
478 """ % {'grml_flavour': grml_flavour, 'display_name': get_flavour_filename(grml_flavour)})
481 def install_grub(device):
482 """Install grub on specified device.
484 @mntpoint: mountpoint of device where grub should install its files to
485 @device: partition where grub should be installed to"""
488 logging.info("Would execute grub-install [--root-directory=mount_point] %s now.", device)
490 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
491 register_tmpfile(device_mountpoint)
493 mount(device, device_mountpoint, "")
495 # If using --grub-mbr then make sure we install grub in MBR instead of PBR
497 logging.debug("Using option --grub-mbr ...")
498 grub_device, x = get_device_from_partition(device)
502 logging.info("Installing grub as bootloader")
503 for opt in ["", "--force"]:
504 logging.debug("grub-install --recheck %s --no-floppy --root-directory=%s %s",
505 opt, device_mountpoint, grub_device)
506 proc = subprocess.Popen([GRUB_INSTALL, "--recheck", opt,
507 "--no-floppy", "--target=i386-pc",
508 "--root-directory=%s" % device_mountpoint, grub_device],
509 stdout=file(os.devnull, "r+"))
511 if proc.returncode == 0:
514 if proc.returncode != 0:
515 # raise Exception("error executing grub-install")
516 logging.critical("Fatal: error executing grub-install "
517 + "(please check the grml2usb FAQ or drop the --grub option)")
518 logging.critical("Note: if using grub2 consider using "
519 + "the --grub-mbr option as grub considers PBR problematic.")
522 except CriticalException as error:
523 logging.critical("Fatal: %s", error)
527 unmount(device_mountpoint, "")
528 os.rmdir(device_mountpoint)
529 unregister_tmpfile(device_mountpoint)
532 def install_syslinux(device):
533 """Install syslinux on specified device.
535 @device: partition where syslinux should be installed to"""
538 logging.info("Would install syslinux as bootloader on %s", device)
541 # syslinux -d boot/isolinux /dev/sdb1
542 logging.info("Installing syslinux as bootloader")
543 logging.debug("syslinux -d boot/syslinux %s", device)
544 proc = subprocess.Popen(["syslinux", "-d", "boot/syslinux", device])
546 if proc.returncode != 0:
547 raise CriticalException("Error executing syslinux (either try --fat16 or use grub?)")
550 def install_bootloader(device):
551 """Install bootloader on specified device.
553 @device: partition where bootloader should be installed to"""
555 # by default we use grub, so install syslinux only on request
559 except CriticalException as error:
560 logging.critical("Fatal: %s", error)
565 install_syslinux(device)
566 except CriticalException as error:
567 logging.critical("Fatal: %s", error)
572 def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
573 """install 'mbr' master boot record (MBR) on a device
575 Retrieve the partition table from "device", install an MBR from the
576 "mbrtemplate" file, set the "partition" (0..3) active, and install the
577 result back to "device".
579 @mbrtemplate: default MBR file
581 @device: name of a file assumed to be a hard disc (or USB stick) image, or
582 something like "/dev/sdb"
584 @partition: must be a number between 0 and 3, inclusive
586 @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
589 @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
590 specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
591 false then leave the mirbsdmbr specific flag set to FFh, set all
592 active flags to 0 and set the active flag of the partition to 80h. Note:
593 behaviour of mirbsdmbr: if flag = 0/1/2/3 then use it, otherwise search for
596 logging.info("Installing default MBR")
598 if not os.path.isfile(mbrtemplate):
599 logging.error('Error installing MBR (either try --syslinux-mbr or '
600 'install missing file "%s"?)', mbrtemplate)
601 raise CriticalException("%s can not be read." % mbrtemplate)
603 if partition is not None and ((partition < 0) or (partition > 3)):
604 logging.warn("Cannot activate partition %d", partition)
612 tmpf = tempfile.NamedTemporaryFile()
614 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1", device, tmpf.name)
615 proc = subprocess.Popen(["dd", "if=%s" % device, "of=%s" % tmpf.name, "bs=512", "count=1"],
616 stderr=file(os.devnull, "r+"))
618 if proc.returncode != 0:
619 raise Exception("error executing dd (first run)")
621 logging.debug("executing: dd if=%s of=%s bs=%s count=1 conv=notrunc", mbrtemplate,
622 tmpf.name, nmbrbytes)
623 proc = subprocess.Popen(["dd", "if=%s" % mbrtemplate, "of=%s" % tmpf.name, "bs=%s" % nmbrbytes,
624 "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+"))
626 if proc.returncode != 0:
627 raise Exception("error executing dd (second run)")
629 mbrcode = tmpf.file.read(512)
630 if len(mbrcode) < 512:
631 raise EOFError("MBR size (%d) < 512" % len(mbrcode))
633 if partition is not None:
635 mbrcode = mbrcode[0:439] + chr(partition) + \
636 mbrcode[440:510] + "\x55\xAA"
638 actives = ["\x00", "\x00", "\x00", "\x00"]
639 actives[partition] = "\x80"
640 mbrcode = mbrcode[0:446] + actives[0] + \
641 mbrcode[447:462] + actives[1] + \
642 mbrcode[463:478] + actives[2] + \
643 mbrcode[479:494] + actives[3] + \
644 mbrcode[495:510] + "\x55\xAA"
648 tmpf.file.write(mbrcode)
651 logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device)
652 proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1",
653 "conv=notrunc"], stderr=file(os.devnull, "r+"))
655 if proc.returncode != 0:
656 raise Exception("error executing dd (third run)")
659 # make sure we sync filesystems before returning
660 proc = subprocess.Popen(["sync"])
664 def is_writeable(device):
665 """Check if the device is writeable for the current user
667 @device: partition where bootloader should be installed to"""
671 #raise Exception("no device for checking write permissions")
673 if not os.path.exists(device):
676 return os.access(device, os.W_OK) and os.access(device, os.R_OK)
679 def mount(source, target, mount_options):
680 """Mount specified source on given target
682 @source: name of device/ISO that should be mounted
683 @target: directory where the ISO should be mounted to
684 @options: mount specific options"""
686 # note: options.dryrun does not work here, as we have to
687 # locate files and identify the grml flavour
689 for x in file('/proc/mounts').readlines():
690 if x.startswith(source):
691 raise CriticalException("Error executing mount: %s already mounted - " % source
692 + "please unmount before invoking grml2usb")
694 if os.path.isdir(source):
695 logging.debug("Source %s is not a device, therefore not mounting.", source)
698 logging.debug("mount %s %s %s", mount_options, source, target)
699 proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target])
701 if proc.returncode != 0:
702 raise CriticalException("Error executing mount (no filesystem on the partition?)")
704 logging.debug("register_mountpoint(%s)", target)
705 register_mountpoint(target)
708 def unmount(target, unmount_options):
709 """Unmount specified target
711 @target: target where something is mounted on and which should be unmounted
712 @options: options for umount command"""
714 # make sure we unmount only already mounted targets
715 target_unmount = False
716 mounts = open('/proc/mounts').readlines()
717 mountstring = re.compile(".*%s.*" % re.escape(os.path.realpath(target)))
719 if re.match(mountstring, line):
720 target_unmount = True
722 if not target_unmount:
723 logging.debug("%s not mounted anymore", target)
725 logging.debug("umount %s %s", list(unmount_options), target)
726 proc = subprocess.Popen(["umount"] + list(unmount_options) + [target])
728 if proc.returncode != 0:
729 raise Exception("Error executing umount")
731 logging.debug("unregister_mountpoint(%s)", target)
732 unregister_mountpoint(target)
735 def check_for_usbdevice(device):
736 """Check whether the specified device is a removable USB device
738 @device: device name, like /dev/sda1 or /dev/sda
741 usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
743 usbdev = os.path.realpath('/sys/class/block/' + usbdevice + '/removable')
744 if not os.path.isfile(usbdev):
745 # Ubuntu with kernel 2.6.24 for example:
746 usbdev = os.path.realpath('/sys/block/' + usbdevice + '/removable')
748 if os.path.isfile(usbdev):
749 is_usb = open(usbdev).readline()
756 def check_for_fat(partition):
757 """Check whether specified partition is a valid VFAT/FAT16 filesystem
759 @partition: device name of partition"""
761 if not os.access(partition, os.R_OK):
762 raise CriticalException("Failed to read device %s"
763 " (wrong UID/permissions or device/directory not present?)" % partition)
766 udev_info = subprocess.Popen(["/sbin/blkid", "-s", "TYPE", "-o", "value", partition],
767 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
768 filesystem = udev_info.communicate()[0].rstrip()
770 if filesystem != "vfat":
771 raise CriticalException(
772 "Partition %s does not contain a FAT16 filesystem. "
773 "(Use --fat16 or run mkfs.vfat %s)" % (partition, partition))
776 raise CriticalException("Sorry, /sbin/blkid not available (install e2fsprogs?)")
779 def mkdir(directory):
780 """Simple wrapper around os.makedirs to get shell mkdir -p behaviour"""
782 # just silently pass as it's just fine it the directory exists
783 if not os.path.isdir(directory):
785 os.makedirs(directory)
786 # pylint: disable-msg=W0704
791 def exec_rsync(source, target):
792 """Simple wrapper around rsync to install files
794 @source: source file/directory
795 @target: target file/directory"""
796 logging.debug("Source: %s / Target: %s", source, target)
797 proc = subprocess.Popen(["rsync", "-rlptDH", "--inplace", source, target])
799 if proc.returncode == 12:
800 logging.critical("Fatal: No space left on device")
804 if proc.returncode != 0:
805 logging.critical("Fatal: could not install %s", source)
810 def write_uuid(target_file):
811 """Generates an returns uuid and write it to the specified file
813 @target_file: filename to write the uuid to
816 fileh = open(target_file, 'w')
817 uid = str(uuid.uuid4())
823 def get_uuid(target):
824 """Get the uuid of the specified target. Will generate an uuid if none exist.
826 @target: directory/mountpoint containing the grml layout
829 conf_target = target + "/conf/"
830 uuid_file_name = conf_target + "/bootid.txt"
831 if os.path.isdir(conf_target):
832 if os.path.isfile(uuid_file_name):
833 uuid_file = open(uuid_file_name, 'r')
834 uid = uuid_file.readline().strip()
838 return write_uuid(uuid_file_name)
840 execute(mkdir, conf_target)
841 return write_uuid(uuid_file_name)
844 def get_shortname(grml_flavour):
845 """Get shortname based from grml_flavour name. The rules applied are the same as in grml-live
846 @grml_flavour: flavour name which shold be translated to shortname"""
848 return re.sub(r'[,._-]', '', grml_flavour)
851 def copy_system_files(grml_flavour, iso_mount, target):
852 """copy grml's main files (like squashfs, kernel and initrd) to a given target
854 @grml_flavour: name of grml flavour the configuration should be generated for
855 @iso_mount: path where a grml ISO is mounted on
856 @target: path where grml's main files should be copied to"""
858 squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
860 logging.error("error locating squashfs file")
861 raise CriticalException("squashfs file not found, please check that your iso is not corrupt")
863 squashfs_target = target + '/live/' + grml_flavour + '/'
864 execute(mkdir, squashfs_target)
865 exec_rsync(squashfs, squashfs_target + grml_flavour + '.squashfs')
867 for prefix in grml_flavour + "/", "":
868 filesystem_module = search_file(prefix + 'filesystem.module', iso_mount)
869 if filesystem_module:
871 if filesystem_module is None:
872 logging.error("error locating filesystem.module file")
873 raise CriticalException("filesystem.module not found")
875 exec_rsync(filesystem_module, squashfs_target + 'filesystem.module')
877 shortname = get_shortname(grml_flavour)
878 if os.path.isdir(iso_mount + '/boot/' + shortname):
879 exec_rsync(iso_mount + '/boot/' + shortname, target + '/boot')
881 kernel = search_file('vmlinuz', iso_mount)
883 # compat for releases < 2011.12
884 kernel = search_file('linux26', iso_mount)
887 logging.error("error locating kernel file")
888 raise CriticalException("Kernel not found")
890 source = os.path.dirname(kernel) + '/'
891 dest = target + '/' + os.path.dirname(kernel).replace(iso_mount, '') + '/'
893 exec_rsync(source, dest)
896 def update_grml_versions(iso_mount, target):
897 """Update the grml version file on a cd
898 Returns true if version was updated successfully,
899 False if grml-version does not exist yet on the mountpoint
901 @iso_mount: string of the iso mount point
902 @target: path of the target mount point
904 grml_target = target + '/grml/'
905 target_grml_version_file = search_file('grml-version', grml_target)
906 if target_grml_version_file:
907 iso_grml_version_file = search_file('grml-version', iso_mount)
908 if not iso_grml_version_file:
909 logging.warn("Warning: %s could not be found - can not install it", iso_grml_version_file)
912 # read the flavours from the iso image
914 iso_file = open(iso_grml_version_file, 'r')
915 for line in iso_file:
916 iso_versions[get_flavour(line)] = line.strip()
918 # update the existing flavours on the target
919 for line in fileinput.input([target_grml_version_file], inplace=1):
920 flavour = get_flavour(line)
921 if flavour in iso_versions.keys():
922 print(iso_versions.pop(flavour))
927 target_file = open(target_grml_version_file, 'a')
928 # add the new flavours from the current iso
929 for flavour in iso_versions:
930 target_file.write("%s\n" % iso_versions[flavour])
932 logging.warn("Warning: Could not write file")
941 def copy_grml_files(grml_flavour, iso_mount, target):
942 """copy some minor grml files to a given target
944 @grml_flavour: the current grml_flavour
945 @iso_mount: path where a grml ISO is mounted on
946 @target: path where grml's main files should be copied to"""
948 grml_target = target + '/grml/'
949 execute(mkdir, grml_target)
951 grml_prefixe = ["GRML", "grml"]
952 for prefix in grml_prefixe:
953 filename = "{0}/{1}/{2}".format(iso_mount, prefix, grml_flavour)
954 if os.path.exists(filename):
955 exec_rsync(filename, grml_target)
958 logging.warn("Warning: could not find flavour directory for %s ", grml_flavour)
961 def handle_addon_copy(filename, dst, iso_mount, ignore_errors=False):
962 """handle copy of optional addons
964 @filename: filename of the addon
965 @dst: destination directory
966 @iso_mount: location of the iso mount
967 @ignore_errors: don't report missing files
969 file_location = search_file(filename, iso_mount)
970 if file_location is None:
971 if not ignore_errors:
972 logging.warn("Warning: %s not found (that's fine if you don't need it)", filename)
974 exec_rsync(file_location, dst)
977 def copy_addons(iso_mount, target):
978 """copy grml's addons files (like allinoneimg, bsd4grml,..) to a given target
980 @iso_mount: path where a grml ISO is mounted on
981 @target: path where grml's main files should be copied to"""
983 addons = target + '/boot/addons/'
984 execute(mkdir, addons)
986 # grub all-in-one image
987 handle_addon_copy('allinone.img', addons, iso_mount)
990 handle_addon_copy('bsd4grml', addons, iso_mount)
993 handle_addon_copy('balder10.imz', addons, iso_mount)
995 # syslinux + pci.ids for hdt
996 for expr in '*.c32', 'pci.ids':
997 glob_and_copy(iso_mount + '/boot/addons/' + expr, addons)
1000 handle_addon_copy('memdisk', addons, iso_mount)
1003 handle_addon_copy('memtest', addons, iso_mount)
1005 # gpxe.lkrn: got replaced by ipxe
1006 handle_addon_copy('gpxe.lkrn', addons, iso_mount, ignore_errors=True)
1009 handle_addon_copy('ipxe.lkrn', addons, iso_mount)
1012 def build_loopbackcfg(target):
1013 """Generate GRUB's loopback.cfg based on existing config files.
1015 @target: target directory
1018 grub_dir = '/boot/grub/'
1019 mkdir(os.path.join(target, grub_dir))
1021 f = open(target + grub_dir + 'loopback.cfg', 'w')
1023 f.write("# grml2usb generated grub2 configuration file\n")
1024 f.write("source /boot/grub/header.cfg\n")
1026 for defaults in glob.glob(target + os.path.sep + grub_dir + os.path.sep + "*_default.cfg"):
1027 sourcefile = defaults.split(target + os.path.sep)[1]
1028 logging.debug("Found source file" + sourcefile)
1029 os.path.isfile(defaults) and f.write("source " + sourcefile + "\n")
1031 for ops in glob.glob(target + os.path.sep + grub_dir + os.path.sep + "*_options.cfg"):
1032 sourcefile = ops.split(target + os.path.sep)[1]
1033 logging.debug("Found source file" + sourcefile)
1034 os.path.isfile(ops) and f.write("source " + sourcefile + "\n")
1036 f.write("source /boot/grub/addons.cfg\n")
1037 f.write("source /boot/grub/footer.cfg\n")
1041 def glob_and_copy(filepattern, dst):
1042 """Glob on specified filepattern and copy the result to dst
1044 @filepattern: globbing pattern
1045 @dst: target directory
1047 for name in glob.glob(filepattern):
1048 copy_if_exist(name, dst)
1051 def search_and_copy(filename, search_path, dst):
1052 """Search for the specified filename at searchpath and copy it to dst
1054 @filename: filename to look for
1055 @search_path: base search file
1056 @dst: destionation to copy the file to
1058 file_location = search_file(filename, search_path)
1059 copy_if_exist(file_location, dst)
1062 def copy_if_exist(filename, dst):
1063 """Copy filename to dst if filename is set.
1065 @filename: a filename
1068 if filename and (os.path.isfile(filename) or os.path.isdir(filename)):
1069 exec_rsync(filename, dst)
1072 def copy_bootloader_files(iso_mount, target, grml_flavour):
1073 """Copy grml's bootloader files to a given target
1075 @iso_mount: path where a grml ISO is mounted on
1076 @target: path where grml's main files should be copied to
1077 @grml_flavour: name of the current processed grml_flavour
1080 syslinux_target = target + '/boot/syslinux/'
1081 execute(mkdir, syslinux_target)
1083 grub_target = target + '/boot/grub/'
1084 execute(mkdir, grub_target)
1086 logo = search_file('logo.16', iso_mount)
1087 exec_rsync(logo, syslinux_target + 'logo.16')
1089 bootx64_efi = search_file('bootx64.efi', iso_mount)
1091 mkdir(target + '/efi/boot/')
1092 exec_rsync(bootx64_efi, target + '/efi/boot/bootx64.efi')
1094 efi_img = search_file('efi.img', iso_mount)
1096 mkdir(target + '/boot/')
1097 exec_rsync(efi_img, target + '/boot/efi.img')
1099 for ffile in ['f%d' % number for number in range(1, 11)]:
1100 search_and_copy(ffile, iso_mount, syslinux_target + ffile)
1102 # avoid the "file is read only, overwrite anyway (y/n) ?" question
1103 # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
1104 if os.path.isfile(syslinux_target + 'ldlinux.sys'):
1105 os.unlink(syslinux_target + 'ldlinux.sys')
1107 (source_dir, name) = get_defaults_file(iso_mount, grml_flavour, "default.cfg")
1108 (source_dir, defaults_file) = get_defaults_file(iso_mount, grml_flavour, "grml.cfg")
1111 raise CriticalException(
1112 "file default.cfg could not be found.\n"
1113 "Note: this grml2usb version requires an ISO generated by grml-live >=0.9.24 ...\n"
1114 " ... either use grml releases >=2009.10 or switch to an older grml2usb version.")
1116 if not os.path.exists(iso_mount + '/boot/grub/footer.cfg'):
1117 logging.warning("Warning: Grml releases older than 2011.12 support only one flavour in grub.")
1119 for expr in name, 'distri.cfg', \
1120 defaults_file, 'grml.png', 'hd.cfg', 'isolinux.cfg', 'isolinux.bin', \
1121 'isoprompt.cfg', 'options.cfg', \
1122 'prompt.cfg', 'vesamenu.cfg', 'grml.png', '*.c32':
1123 glob_and_copy(iso_mount + source_dir + expr, syslinux_target)
1125 for filename in glob.glob1(syslinux_target, "*.c32"):
1126 copy_if_exist(os.path.join(SYSLINUX_LIBS, filename), syslinux_target)
1128 # copy the addons_*.cfg file to the new syslinux directory
1129 glob_and_copy(iso_mount + source_dir + 'addon*.cfg', syslinux_target)
1131 search_and_copy('hidden.cfg', iso_mount + source_dir, syslinux_target + "new_" + 'hidden.cfg')
1133 # copy all grub files from ISO
1134 glob_and_copy(iso_mount + '/boot/grub/*', grub_target)
1136 # finally (after all GRUB files have been been installed) build static loopback.cfg
1137 build_loopbackcfg(target)
1140 def install_iso_files(grml_flavour, iso_mount, device, target):
1141 """Copy files from ISO to given target
1143 @grml_flavour: name of grml flavour the configuration should be generated for
1144 @iso_mount: path where a grml ISO is mounted on
1145 @device: device/partition where bootloader should be installed to
1146 @target: path where grml's main files should be copied to"""
1149 GRML_DEFAULT = GRML_DEFAULT or grml_flavour
1152 elif not options.bootloaderonly:
1153 logging.info("Copying files. This might take a while....")
1155 copy_system_files(grml_flavour, iso_mount, target)
1156 copy_grml_files(grml_flavour, iso_mount, target)
1157 except CriticalException as error:
1158 logging.critical("Execution failed: %s", error)
1161 if not options.skipaddons:
1162 if not search_file('addons', iso_mount):
1163 logging.info("Could not find addons, therefore not installing.")
1165 copy_addons(iso_mount, target)
1167 if not options.copyonly:
1168 copy_bootloader_files(iso_mount, target, grml_flavour)
1170 if not options.dryrun:
1171 handle_bootloader_config(grml_flavour, device, target)
1173 # make sure we sync filesystems before returning
1174 proc = subprocess.Popen(["sync"])
1178 def get_device_from_partition(partition):
1180 partition_number = None
1181 if partition[-1].isdigit() and not RE_LOOP_DEVICE.match(partition):
1182 m = RE_P_PARTITION.match(partition) or RE_PARTITION.match(partition)
1185 partition_number = int(m.group(2)) - 1
1186 return (device, partition_number)
1189 def get_flavour(flavour_str):
1190 """Returns the flavour of a grml version string
1192 return re.match(r'[\w-]*', flavour_str).group()
1195 def identify_grml_flavour(mountpath):
1196 """Get name of grml flavour
1198 @mountpath: path where the grml ISO is mounted to
1199 @return: name of grml-flavour"""
1201 version_files = search_file('grml-version', mountpath, lst_return=True)
1203 if not version_files:
1204 if mountpath.startswith("/lib/live/mount/medium"):
1205 logging.critical("Error: could not find grml-version file.")
1206 logging.critical("Looks like your system is running from RAM but required files are not available.")
1207 logging.critical("Please either boot without toram=... or use boot option toram instead of toram=...")
1209 logging.critical("Error: could not find grml-version file.")
1214 logging.debug("version_files = %s", version_files)
1215 for version_file in version_files:
1218 tmpfile = open(version_file, 'r')
1219 for line in tmpfile.readlines():
1220 flavours.append(get_flavour(line))
1228 def get_bootoptions(grml_flavour):
1229 """Returns bootoptions for specific flavour
1231 @grml_flavour: name of the grml_flavour
1233 # do NOT write "None" in kernel cmdline
1234 if not options.bootoptions:
1237 bootopt = " ".join(options.bootoptions)
1238 bootopt = bootopt.replace("%flavour", grml_flavour)
1242 def handle_grub_config(grml_flavour, device, target):
1243 """Main handler for generating grub (v1 and v2) configuration
1245 @grml_flavour: name of grml flavour the configuration should be generated for
1246 @device: device/partition where grub should be installed to
1247 @target: path of grub's configuration files"""
1251 logging.debug("Updating grub configuration")
1253 grub_target = target + '/boot/grub/'
1255 bootid_re = re.compile("bootid=[\w_-]+")
1256 live_media_path_re = re.compile("live-media-path=[\w_/-]+")
1258 bootopt = get_bootoptions(grml_flavour)
1261 option_re = re.compile(r'(.*/boot/.*(linux26|vmlinuz).*)')
1263 if options.removeoption:
1264 for regex in options.removeoption:
1265 remove_regexes.append(re.compile(regex))
1267 shortname = get_shortname(grml_flavour)
1268 for filename in glob.glob(grub_target + '*.cfg'):
1269 for line in fileinput.input(filename, inplace=1):
1270 line = line.rstrip("\r\n")
1271 if option_re.search(line):
1272 line = bootid_re.sub('', line)
1273 if shortname in filename:
1274 line = live_media_path_re.sub('', line)
1275 line = line.rstrip() + ' live-media-path=/live/%s/ ' % (grml_flavour)
1276 line = line.rstrip() + r' bootid=%s %s ' % (UUID, bootopt)
1277 for regex in remove_regexes:
1278 line = regex.sub(' ', line)
1283 def initial_syslinux_config(target):
1284 """Generates intial syslinux configuration
1286 @target path of syslinux's configuration files"""
1288 target = target + "/"
1289 filename = target + "grmlmain.cfg"
1290 if os.path.isfile(target + "grmlmain.cfg"):
1292 data = open(filename, "w")
1293 data.write(generate_main_syslinux_config())
1296 filename = target + "hiddens.cfg"
1297 data = open(filename, "w")
1298 data.write("include hidden.cfg\n")
1302 def add_entry_if_not_present(filename, entry):
1303 """Write entry into filename if entry is not already in the file
1305 @filename: name of the file
1306 @entry: data to write to the file
1308 data = open(filename, "a+")
1318 def get_flavour_filename(flavour):
1319 """Generate a iso9960 save filename out of the specified flavour
1321 @flavour: grml flavour
1323 return flavour.replace('-', '_')
1326 def adjust_syslinux_bootoptions(src, flavour):
1327 """Adjust existing bootoptions of specified syslinux config to
1328 grml2usb specific ones, e.g. change the location of the kernel...
1330 @src: config file to alter
1331 @flavour: grml flavour
1334 append_re = re.compile("^(\s*append.*/boot/.*)$", re.I)
1335 # flavour_re = re.compile("(label.*)(grml\w+)")
1336 default_re = re.compile("(default.cfg)")
1337 bootid_re = re.compile("bootid=[\w_-]+")
1338 live_media_path_re = re.compile("live-media-path=[\w_/-]+")
1340 bootopt = get_bootoptions(flavour)
1344 if options.removeoption:
1345 option_re = re.compile(r'/boot/.*/(initrd.gz|initrd.img)')
1347 for regex in options.removeoption:
1348 regexe.append(re.compile(r'%s' % regex))
1350 for line in fileinput.input(src, inplace=1):
1351 # line = flavour_re.sub(r'\1 %s-\2' % flavour, line)
1352 line = default_re.sub(r'%s-\1' % flavour, line)
1353 line = bootid_re.sub('', line)
1354 line = live_media_path_re.sub('', line)
1355 line = append_re.sub(r'\1 live-media-path=/live/%s/ ' % flavour, line)
1356 line = append_re.sub(r'\1 boot=live %s ' % bootopt, line)
1357 line = append_re.sub(r'\1 %s=%s ' % ("bootid", UUID), line)
1358 if option_re and option_re.search(line):
1359 for regex in regexe:
1360 line = regex.sub(' ', line)
1361 sys.stdout.write(line)
1365 def adjust_labels(src, replacement):
1366 """Adjust the specified labels in the syslinux config file src with
1367 specified replacement
1369 label_re = re.compile("^(\s*label\s*) ([a-zA-Z0-9_-]+)", re.I)
1370 for line in fileinput.input(src, inplace=1):
1371 line = label_re.sub(replacement, line)
1372 sys.stdout.write(line)
1376 def add_syslinux_entry(filename, grml_flavour):
1377 """Add includes for a specific grml_flavour to the specified filename
1379 @filename: syslinux config file
1380 @grml_flavour: grml flavour to add
1383 entry_filename = "option_%s.cfg" % grml_flavour
1384 entry = "include %s\n" % entry_filename
1386 add_entry_if_not_present(filename, entry)
1387 path = os.path.dirname(filename)
1389 data = open(path + "/" + entry_filename, "w")
1390 data.write(generate_flavour_specific_syslinux_config(grml_flavour))
1394 def modify_filenames(grml_flavour, target, filenames):
1395 """Replace the standard filenames with the new ones
1397 @grml_flavour: grml-flavour strin
1398 @target: directory where the files are located
1399 @filenames: list of filenames to alter
1401 grml_filename = get_flavour_filename(grml_flavour)
1402 for filename in filenames:
1403 old_filename = "%s/%s" % (target, filename)
1404 new_filename = "%s/%s_%s" % (target, grml_filename, filename)
1405 os.rename(old_filename, new_filename)
1408 def remove_default_entry(filename):
1409 """Remove the default entry from specified syslinux file
1411 @filename: syslinux config file
1413 default_re = re.compile("^(\s*menu\s*default\s*)$", re.I)
1414 for line in fileinput.input(filename, inplace=1):
1415 if default_re.match(line):
1417 sys.stdout.write(line)
1421 def handle_syslinux_config(grml_flavour, target):
1422 """Main handler for generating syslinux configuration
1424 @grml_flavour: name of grml flavour the configuration should be generated for
1425 @target: path of syslinux's configuration files"""
1427 logging.debug("Generating syslinux configuration")
1428 syslinux_target = target + '/boot/syslinux/'
1429 # should be present via copy_bootloader_files(), but make sure it exits:
1430 execute(mkdir, syslinux_target)
1431 syslinux_cfg = syslinux_target + 'syslinux.cfg'
1433 # install main configuration only *once*, no matter how many ISOs we have:
1434 syslinux_config_file = open(syslinux_cfg, 'w')
1435 syslinux_config_file.write("timeout 300\n")
1436 syslinux_config_file.write("include vesamenu.cfg\n")
1437 syslinux_config_file.close()
1439 prompt_name = open(syslinux_target + 'promptname.cfg', 'w')
1440 prompt_name.write('menu label S^yslinux prompt\n')
1443 initial_syslinux_config(syslinux_target)
1444 flavour_filename = get_flavour_filename(grml_flavour)
1446 if search_file('default.cfg', syslinux_target):
1447 modify_filenames(grml_flavour, syslinux_target, ['grml.cfg', 'default.cfg'])
1449 filename = search_file("new_hidden.cfg", syslinux_target)
1451 # process hidden file
1452 if not search_file("hidden.cfg", syslinux_target):
1453 new_hidden = syslinux_target + "hidden.cfg"
1454 os.rename(filename, new_hidden)
1455 adjust_syslinux_bootoptions(new_hidden, grml_flavour)
1457 new_hidden_file = "%s/%s_hidden.cfg" % (syslinux_target, flavour_filename)
1458 os.rename(filename, new_hidden_file)
1459 adjust_labels(new_hidden_file, r'\1 %s-\2' % grml_flavour)
1460 adjust_syslinux_bootoptions(new_hidden_file, grml_flavour)
1461 entry = 'include %s_hidden.cfg\n' % flavour_filename
1462 add_entry_if_not_present("%s/hiddens.cfg" % syslinux_target, entry)
1464 new_default = "%s_default.cfg" % (flavour_filename)
1465 entry = 'include %s\n' % new_default
1466 defaults_file = '%s/defaults.cfg' % syslinux_target
1467 new_default_with_path = "%s/%s" % (syslinux_target, new_default)
1468 new_grml_cfg = "%s/%s_grml.cfg" % (syslinux_target, flavour_filename)
1470 if os.path.isfile(defaults_file):
1472 # remove default menu entry in menu
1473 remove_default_entry(new_default_with_path)
1475 # adjust all labels for additional isos
1476 adjust_labels(new_default_with_path, r'\1 %s' % grml_flavour)
1477 adjust_labels(new_grml_cfg, r'\1 %s-\2' % grml_flavour)
1479 # always adjust bootoptions
1480 adjust_syslinux_bootoptions(new_default_with_path, grml_flavour)
1481 adjust_syslinux_bootoptions(new_grml_cfg, grml_flavour)
1483 add_entry_if_not_present("%s/defaults.cfg" % syslinux_target, entry)
1485 add_syslinux_entry("%s/additional.cfg" % syslinux_target, flavour_filename)
1488 def handle_bootloader_config(grml_flavour, device, target):
1489 """Main handler for generating bootloader's configuration
1491 @grml_flavour: name of grml flavour the configuration should be generated for
1492 @device: device/partition where bootloader should be installed to
1493 @target: path of bootloader's configuration files"""
1496 UUID = get_uuid(target)
1497 if options.skipsyslinuxconfig:
1498 logging.info("Skipping generation of syslinux configuration as requested.")
1501 handle_syslinux_config(grml_flavour, target)
1502 except CriticalException as error:
1503 logging.critical("Fatal: %s", error)
1506 if options.skipgrubconfig:
1507 logging.info("Skipping generation of grub configuration as requested.")
1510 handle_grub_config(grml_flavour, device, target)
1511 except CriticalException as error:
1512 logging.critical("Fatal: %s", error)
1516 def install(image, device):
1517 """Install a grml image to the specified device
1519 @image: directory or is file
1520 @device: partition or directory to install the device
1522 iso_mountpoint = image
1523 remove_image_mountpoint = False
1524 if os.path.isdir(image):
1525 if options.force or os.path.exists(os.path.join(image, 'live')):
1526 logging.info("Using %s as install base", image)
1528 q = raw_input("%s does not look like a Grml system. "
1529 "Do you really want to use this image? y/N " % image)
1530 if q.lower() == 'y':
1531 logging.info("Using %s as install base", image)
1533 logging.info("Skipping install base %s", image)
1535 logging.info("Using ISO %s", image)
1536 iso_mountpoint = tempfile.mkdtemp(prefix="grml2usb", dir=os.path.abspath(options.tmpdir))
1537 register_tmpfile(iso_mountpoint)
1538 remove_image_mountpoint = True
1540 mount(image, iso_mountpoint, ["-o", "loop,ro", "-t", "iso9660"])
1541 except CriticalException as error:
1542 logging.critical("Fatal: %s", error)
1546 install_grml(iso_mountpoint, device)
1548 if remove_image_mountpoint:
1550 remove_mountpoint(iso_mountpoint)
1551 except CriticalException as error:
1556 def install_grml(mountpoint, device):
1557 """Main logic for copying files of the currently running Grml system.
1559 @mountpoint: directory where currently running live system resides (usually /lib/live/mount/medium)
1560 @device: partition where the specified ISO should be installed to"""
1562 device_mountpoint = device
1563 if os.path.isdir(device):
1564 logging.info("Specified device is a directory, therefore not mounting.")
1565 remove_device_mountpoint = False
1567 device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
1568 register_tmpfile(device_mountpoint)
1569 remove_device_mountpoint = True
1571 check_for_fat(device)
1572 if not options.skipbootflag:
1573 check_boot_flag(device)
1574 mount(device, device_mountpoint, ['-o', 'utf8,iocharset=iso8859-1'])
1575 except CriticalException as error:
1576 mount(device, device_mountpoint, "")
1578 grml_flavours = identify_grml_flavour(mountpoint)
1579 for flavour in set(grml_flavours):
1581 logging.warning("No valid flavour found, please check your iso")
1582 logging.info("Identified grml flavour \"%s\".", flavour)
1583 install_iso_files(flavour, mountpoint, device, device_mountpoint)
1584 GRML_FLAVOURS.add(flavour)
1586 if remove_device_mountpoint:
1587 remove_mountpoint(device_mountpoint)
1590 def remove_mountpoint(mountpoint):
1591 """remove a registered mountpoint
1595 unmount(mountpoint, "")
1596 if os.path.isdir(mountpoint):
1597 os.rmdir(mountpoint)
1598 unregister_tmpfile(mountpoint)
1599 except CriticalException as error:
1604 def handle_mbr(device):
1605 """Main handler for installing master boot record (MBR)
1607 @device: device where the MBR should be installed to"""
1610 logging.info("Would install MBR")
1613 mbr_device, partition_number = get_device_from_partition(device)
1614 if partition_number is None:
1615 logging.warn("Could not detect partition number, not activating partition")
1617 # if we get e.g. /dev/loop1 as device we don't want to put the MBR
1618 # into /dev/loop of course, therefore use /dev/loop1 as mbr_device
1619 if mbr_device == "/dev/loop":
1621 logging.info("Detected loop device - using %s as MBR device therefore", mbr_device)
1623 mbrcode = GRML2USB_BASE + '/mbr/mbrldr'
1624 if options.syslinuxmbr:
1626 mbr_locations = ('/usr/lib/syslinux/mbr.bin',
1627 '/usr/lib/syslinux/bios/mbr.bin',
1628 '/usr/share/syslinux/mbr.bin')
1629 for mbrpath in mbr_locations:
1630 if os.path.isfile(mbrpath):
1635 str_locations = " or ".join(['"%s"' % l for l in mbr_locations])
1636 logging.error('Cannot find syslinux MBR, install it at %s)',
1638 raise CriticalException("syslinux MBR can not be found at %s."
1640 elif options.mbrmenu:
1641 mbrcode = GRML2USB_BASE + '/mbr/mbrldr'
1644 install_mbr(mbrcode, mbr_device, partition_number, True)
1645 except (IOError, Exception) as error:
1646 logging.critical("Execution failed: %s", error)
1650 def handle_vfat(device):
1651 """Check for FAT specific settings and options
1653 @device: device that should checked / formated"""
1655 # make sure we have mkfs.vfat available
1657 if not which("mkfs.vfat") and not options.copyonly and not options.dryrun:
1658 logging.critical('Sorry, mkfs.vfat not available. Exiting.')
1659 logging.critical('Please make sure to install dosfstools.')
1663 print("Forcing mkfs.fat16 on %s as requested via option --force." % device)
1665 # make sure the user is aware of what he is doing
1666 f = raw_input("Are you sure you want to format the specified partition with fat16? y/N ")
1667 if f == "y" or f == "Y":
1668 logging.info("Note: you can skip this question using the option --force")
1673 except CriticalException as error:
1674 logging.critical("Execution failed: %s", error)
1677 # check for vfat filesystem
1678 if device is not None and not os.path.isdir(device) and options.syslinux:
1680 check_for_fat(device)
1681 except CriticalException as error:
1682 logging.critical("Execution failed: %s", error)
1685 if options.skipusbcheck:
1686 logging.info("Not checking for removable USB device as requested via option --skip-usb-check.")
1689 if not os.path.isdir(device) and not check_for_usbdevice(device) and not options.force:
1690 print("Warning: the specified device %s does not look like a removable usb device." % device)
1691 f = raw_input("Do you really want to continue? y/N ")
1692 if f.lower() != "y":
1696 def handle_compat_warning(device):
1697 """Backwards compatible checks
1699 @device: device that should be checked"""
1701 # make sure we can replace old grml2usb script and warn user when using old way of life:
1702 if device.startswith("/mnt/external") or device.startswith("/mnt/usb") and not options.force:
1703 print("Warning: the semantics of grml2usb has changed.")
1704 print("Instead of using grml2usb /path/to/iso %s you might" % device)
1705 print("want to use grml2usb /path/to/iso /dev/... instead.")
1706 print("Please check out the grml2usb manpage for details.")
1707 f = raw_input("Do you really want to continue? y/N ")
1708 if f.lower() != "y":
1712 def handle_logging():
1713 """Log handling and configuration"""
1715 if options.verbose and options.quiet:
1716 parser.error("please use either verbose (--verbose) or quiet (--quiet) option")
1718 FORMAT = "%(message)s"
1720 FORMAT = "%(asctime)-15s %(message)s"
1721 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
1723 logging.basicConfig(level=logging.CRITICAL, format=FORMAT)
1725 logging.basicConfig(level=logging.INFO, format=FORMAT)
1728 def handle_bootloader(device):
1729 """wrapper for installing bootloader
1731 @device: device where bootloader should be installed to"""
1733 # Install bootloader only if not using the --copy-only option
1734 if options.copyonly:
1735 logging.info("Not installing bootloader and its files as requested via option copyonly.")
1736 elif os.path.isdir(device):
1737 logging.info("Not installing bootloader as %s is a directory.", device)
1739 install_bootloader(device)
1742 def check_options(opts):
1743 """Check compability of provided user opts
1745 @opts option dict from OptionParser
1747 if opts.grubmbr and not opts.grub:
1748 raise CriticalException("--grub-mbr requires --grub option.")
1750 if opts.copyonly and opts.grub:
1751 raise CriticalException("Cannot use --copy-only and --grub at the same time.")
1754 def check_programs():
1755 """check if all needed programs are installed"""
1758 GRUB_INSTALL = which("grub-install") or which("grub2-install")
1759 if not GRUB_INSTALL:
1760 logging.critical("Fatal: grub-install not available (please install the "
1761 + "grub package or drop the --grub option)")
1764 if options.syslinux:
1765 if not which("syslinux"):
1766 logging.critical("Fatal: syslinux not available (please install the "
1767 + "syslinux package or use the --grub option)")
1770 if not which("rsync"):
1771 logging.critical("Fatal: rsync not available, can not continue - sorry.")
1776 """Runs modprobe loop and throws away it's output"""
1777 if not which("modprobe"):
1778 logging.critical("Fatal: modprobe not available, can not continue - sorry.")
1779 logging.critical("Hint: is /sbin missing in PATH?")
1782 proc = subprocess.Popen(["modprobe", "loop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1787 """Main function [make pylint happy :)]"""
1791 print(os.path.basename(sys.argv[0]) + " " + PROG_VERSION)
1795 parser.error("invalid usage")
1800 # make sure we have the appropriate permissions
1803 check_options(options)
1807 logging.info("Executing grml2usb version %s", PROG_VERSION)
1810 logging.info("Running in simulation mode as requested via option dry-run.")
1814 # specified arguments
1815 device = os.path.realpath(args[len(args) - 1])
1816 isos = args[0:len(args) - 1]
1818 if not os.path.isdir(device):
1819 if device[-1:].isdigit():
1820 if int(device[-1:]) > 4 or device[-2:].isdigit():
1821 logging.critical("Fatal: installation on partition number >4 not supported. (BIOS won't support it.)")
1824 # provide upgrade path
1825 handle_compat_warning(device)
1827 # check for vfat partition
1830 # main operation (like installing files)
1832 install(iso, device)
1835 is_superfloppy = not device[-1:].isdigit()
1837 logging.info("Detected superfloppy format - not installing MBR")
1839 if not options.skipmbr and not os.path.isdir(device) and not is_superfloppy:
1842 handle_bootloader(device)
1844 logging.info("Note: grml flavour %s was installed as the default booting system.", GRML_DEFAULT)
1846 for flavour in GRML_FLAVOURS:
1847 logging.info("Note: you can boot flavour %s using '%s' on the commandline.", flavour, flavour)
1849 # finally be polite :)
1850 logging.info("Finished execution of grml2usb (%s). Have fun with your Grml system.", PROG_VERSION)
1852 except Exception as error:
1853 logging.critical("Fatal: %s", str(error))
1857 if __name__ == "__main__":
1860 except KeyboardInterrupt:
1861 logging.info("Received KeyboardInterrupt")
1864 ## END OF FILE #################################################################
1865 # vim:foldmethod=indent expandtab ai ft=python tw=120 fileencoding=utf-8