From: Michael Prokop Date: Tue, 20 Jan 2009 14:43:06 +0000 (+0100) Subject: Implement install_mbr(device); add lilo stuff X-Git-Tag: v0.9.2~56 X-Git-Url: https://git.grml.org/?p=grml2usb.git;a=commitdiff_plain;h=81a509982fe400a4550b87efcc5312723a8ffc89 Implement install_mbr(device); add lilo stuff --- diff --git a/grml2usb.py b/grml2usb.py index 3ef8e42..c212e91 100755 --- a/grml2usb.py +++ b/grml2usb.py @@ -6,7 +6,7 @@ # License: This file is licensed under the GPL v2 or any later version. ################################################################################ -# TODO: +# TODO # * strongly improve error handling :) # * implement mount handling # * write error messages to stderr @@ -35,6 +35,9 @@ syslinux (just run 'aptitude install syslinux' on Debian-based systems)\n\ and root access." parser = OptionParser(usage=usage) +# TODO +# * --copy-only? +# * --bootloader-only? parser.add_option("--bootoptions", dest="bootoptions", action="store", type="string", help="use specified bootoptions as defaut") parser.add_option("--dry-run", dest="dryrun", action="store_true", @@ -59,10 +62,13 @@ parser.add_option("--verbose", dest="verbose", action="store_true", help="enable verbose mode") parser.add_option("-v", "--version", dest="version", action="store_true", help="display version and exit") + +(options, args) = parser.parse_args() # }}} # wrapper functions {{{ -# TODO: implement argument handling +# TODO +# * implement argument handling def execute(command): """Wrapper for executing a command. Either really executes the command (default) or when using --dry-run commandline option @@ -70,13 +76,16 @@ def execute(command): if options.dryrun: print "would execute %s now" % command else: - # TODO: actual execution ;) + # TODO + # * actual execution ;) + # * wrap subprocess.Popen()? print "executing %s" % command -def which(program): - def is_exe(fpath): - return os.path.exists(fpath) and os.access(fpath, os.X_OK) +def is_exe(fpath): + return os.path.exists(fpath) and os.access(fpath, os.X_OK) + +def which(program): fpath, fname = os.path.split(program) if fpath: if is_exe(program): @@ -89,6 +98,7 @@ def which(program): return None + def search_file(filename, search_path='/bin' + pathsep + '/usr/bin'): """Given a search path, find file""" file_found = 0 @@ -112,14 +122,17 @@ def check_uid_root(): def install_syslinux(device): + # TODO """Install syslinux on specified device.""" print("debug: syslinux %s [TODO]") % device + # syslinux -d boot/isolinux /dev/usb-sdb1 + def generate_grub_config(grml_flavour): """Generate grub configuration for use via menu,lst""" - # TODO: + # TODO # * what about system using grub2 without having grub available? # * support grub2 @@ -145,7 +158,7 @@ initrd /boot/release/%(grml_name)s/initrd.gz def generate_isolinux_splash(grml_flavour): """Generate bootsplash for isolinux/syslinux""" - # TODO: + # TODO # * adjust last bootsplash line grml_name = grml_flavour @@ -160,7 +173,7 @@ Some information and boot options available via keys F2 - F10. http://grml.org/ def generate_syslinux_config(grml_flavour): """Generate configuration for use in syslinux.cfg""" - # TODO: + # TODO # * unify isolinux and syslinux setup ("INCLUDE /boot/...") grml_name = grml_flavour @@ -210,15 +223,48 @@ def install_bootloader(partition): install_syslinux(device) -def install_mbr(target): - """Install a default master boot record on given target""" - print("TODO") +def isWriteable(device): + """Check if the device is writeable for the current user""" + + if not device: + raise "WrongArguments", "no device for checking write permissions" + + if not os.path.exists(device): + return False + + return os.access(device, os.W_OK) and os.access(device, os.R_OK) + +def install_mbr(device): + """Install a default master boot record on given device + + @device: device where MBR should be installed to""" + + if not isWriteable(device): + raise IOError, "device not writeable for user" + + lilo = './lilo/lilo.static' # FIXME + + if not is_exe(lilo): + raise Exception, "lilo executable not available." + + # to support -A for extended partitions: + print("debug: ./lilo/lilo.static -S /dev/null -M %s ext") % device + subprocess.Popen(["./lilo/lilo.static", "-S", "/dev/null", "-M", device, "ext"]) + + # activate partition: + print("debug: ./lilo/lilo.static -S /dev/null -A %s 1") % device + subprocess.Popen(["./lilo/lilo.static", "-S", "/dev/null", "-A", device, "1"]) + + # lilo's mbr is broken, use the one from syslinux instead: + print("debug: cat /usr/lib/syslinux/mbr.bin > %s") % device + try: + # FIXME: use Popen instead? + retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True) + if retcode < 0: + print >>sys.stderr, "Error copying MBR to device", -retcode + except OSError, e: + print >>sys.stderr, "Execution failed:", e - # Command logic (all executed *without* mounted device): - # lilo -S /dev/null -M /dev/usb-sdb ext - # lilo -S /dev/null -A /dev/usb-sdb 1 - # cat /usr/lib/syslinux/mbr.bin > /dev/usb-sdb - # syslinux -d boot/isolinux /dev/usb-sdb1 def loopback_mount(iso, target): """Loopback mount specified ISO on given target""" @@ -241,7 +287,8 @@ def check_for_vat(partition): if filesystem != "vfat": return(1) - # TODO: check for ID_FS_VERSION=FAT16 as well? + # TODO + # * check for ID_FS_VERSION=FAT16 as well? except OSError: print("Sorry, /lib/udev/vol_id not available.") @@ -259,7 +306,7 @@ def mkdir(directory): def copy_grml_files(grml_flavour, iso_mount, target): """Copy files from ISO on given target""" - # TODO: + # TODO # * provide alternative search_file() if file information is stored in a config.ini file? # * catch "install: .. No space left on device" & CO # * abstract copy logic to make the code shorter? @@ -330,6 +377,7 @@ def copy_grml_files(grml_flavour, iso_mount, target): def uninstall_files(device): """Get rid of all grml files on specified device""" + # TODO print("TODO") @@ -345,7 +393,6 @@ def identify_grml_flavour(mountpath): def main(): - (options, args) = parser.parse_args() if options.version: print("%s %s")% (os.path.basename(sys.argv[0]), prog_version) @@ -360,11 +407,12 @@ def main(): isos = args[0:len(args) - 1] if not which("syslinux"): - print("Sorry, syslinux not available. Exiting.") - print("Please install syslinux or consider using the --grub option.") + print >> sys.stderr, 'Sorry, syslinux not available. Exiting.' + print >> sys.stderr, 'Please install syslinux or consider using the --grub option.' sys.exit(1) - # TODO check for valid blockdevice, vfat and mount functions + # TODO + # * check for valid blockdevice, vfat and mount functions # if device is not None: # check_for_vat(device) # mount_target(partition) @@ -372,7 +420,8 @@ def main(): # FIXME target = '/mnt/usb-sdb1' - # TODO it doesn't need to be a ISO, could be /live/image as well + # TODO + # * it doesn't need to be a ISO, could be /live/image as well for iso in isos: print("debug: iso = %s") % iso # loopback_mount(iso) @@ -386,11 +435,21 @@ def main(): grml_flavour_short = grml_flavour.replace('-','') print("debug: grml_flavour_short = %s") % grml_flavour_short - copy_grml_files(grml_flavour, iso_mount, target) - + # copy_grml_files(grml_flavour, iso_mount, target) if options.mbr: - print("debug: would install MBR now") + # make sure we install MBR on /dev/sdX and not /dev/sdX# + if device[-1:].isdigit(): + device = re.match(r'(.*?)\d*$', device).group(1) + + try: + install_mbr(device) + except IOError, e: + print >>sys.stderr, "Execution failed:", e + sys.exit(1) + except Exception, e: + print >>sys.stderr, "Execution failed:", e + sys.exit(1) install_bootloader(device) diff --git a/lilo/COPYING b/lilo/COPYING new file mode 100644 index 0000000..fae625a --- /dev/null +++ b/lilo/COPYING @@ -0,0 +1,52 @@ +LInux LOader (LILO) program code, documentation, and auxiliary programs are +Copyright 1992-1998 Werner Almesberger. +Copyright 1999-2005 John Coffman. +All rights reserved. + + + +License +======= + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +* Neither the names of the author(s) nor the names of other contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + + + +Disclaimer +========== + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + + +(Note: The above license is copied from the BSD license at: + http://www.opensource.org/licenses/bsd-license.html, +substituting the appropriate references in the template.) + +(end) + + diff --git a/lilo/build_info.txt b/lilo/build_info.txt new file mode 100644 index 0000000..7b545d4 --- /dev/null +++ b/lilo/build_info.txt @@ -0,0 +1,3 @@ +apt-get source lilo # version 22.8-7 +cd lilo-22.8 +make alles diff --git a/lilo/lilo.static b/lilo/lilo.static new file mode 100755 index 0000000..56c6e80 Binary files /dev/null and b/lilo/lilo.static differ