Support installation of the currently running grml live system
[grml2usb.git] / grml2usb
index 9709ad5..f8a53c4 100755 (executable)
--- a/grml2usb
+++ b/grml2usb
@@ -18,7 +18,7 @@ from inspect import isroutine, isclass
 import datetime, logging, os, re, subprocess, sys, tempfile, time
 
 # global variables
-PROG_VERSION = "0.9.5"
+PROG_VERSION = "0.9.6"
 MOUNTED = set()  # register mountpoints
 TMPFILES = set() # register tmpfiles
 DATESTAMP = time.mktime(datetime.datetime.now().timetuple()) # unique identifier for syslinux.cfg
@@ -612,7 +612,7 @@ def install_grub(device):
             proc.wait()
             if proc.returncode != 0:
                 # raise Exception("error executing grub-install")
-                logging.critical("Fatal: error executing grub-install (please check FAQ)" % error)
+                logging.critical("Fatal: error executing grub-install (please check the grml2usb FAQ)" % error)
                 cleanup()
                 sys.exit(1)
         except CriticalException, error:
@@ -959,6 +959,7 @@ def copy_system_files(grml_flavour, iso_mount, target):
     squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
     if squashfs is None:
         logging.critical("Fatal: squashfs file not found")
+        raise CriticalException("error locating squashfs file")
     else:
         squashfs_target = target + '/live/' + grml_flavour + '/'
         execute(mkdir, squashfs_target)
@@ -1173,8 +1174,12 @@ def install_iso_files(grml_flavour, iso_mount, device, target):
         return 0
     elif not options.bootloaderonly:
         logging.info("Copying files. This might take a while....")
-        copy_system_files(grml_flavour, iso_mount, target)
-        copy_grml_files(iso_mount, target)
+        try:
+            copy_system_files(grml_flavour, iso_mount, target)
+            copy_grml_files(iso_mount, target)
+        except CriticalException, error:
+            logging.critical("Execution failed: %s", error)
+            sys.exit(1)
 
     if not options.skipaddons:
         if grml_flavour.endswith('-small'):
@@ -1420,6 +1425,47 @@ def handle_bootloader_config(grml_flavour, device, target):
             logging.critical("Fatal: %s" % error)
             sys.exit(1)
 
+def handle_dir(live_image, device):
+    """Main logic for copying files of the currently running grml system.
+
+    @live_image: directory where currently running live system resides (usually /live/image)
+    @device: partition where the specified ISO should be installed to"""
+
+    logging.info("Using %s as install base" % live_image)
+
+    if os.path.isdir(device):
+        logging.info("Specified target is a directory, not mounting therefor.")
+        device_mountpoint = device
+        remove_device_mountpoint = False
+    else:
+        device_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
+        register_tmpfile(device_mountpoint)
+        remove_device_mountpoint = True
+        try:
+            mount(device, device_mountpoint, "")
+        except CriticalException, error:
+            logging.critical("Fatal: %s" % error)
+            cleanup()
+            sys.exit(1)
+
+    try:
+        grml_flavour = identify_grml_flavour(live_image)
+        logging.info("Identified grml flavour \"%s\"." % grml_flavour)
+        install_iso_files(grml_flavour, live_image, device, device_mountpoint)
+    except TypeError:
+        logging.critical("Fatal: a critical error happend during execution (not a grml ISO?), giving up")
+        sys.exit(1)
+    finally:
+        if remove_device_mountpoint:
+            try:
+                unmount(device_mountpoint, "")
+                if os.path.isdir(device_mountpoint):
+                    os.rmdir(device_mountpoint)
+                    unregister_tmpfile(device_mountpoint)
+            except CriticalException, error:
+                logging.critical("Fatal: %s" % error)
+                cleanup()
+
 
 def handle_iso(iso, device):
     """Main logic for mounting ISOs and copying files.
@@ -1429,10 +1475,6 @@ def handle_iso(iso, device):
 
     logging.info("Using ISO %s" % iso)
 
-    if os.path.isdir(iso):
-        logging.critical("TODO: /live/image handling not yet implemented - sorry")
-        sys.exit(1)
-
     iso_mountpoint = tempfile.mkdtemp(prefix="grml2usb")
     register_tmpfile(iso_mountpoint)
     remove_iso_mountpoint = True
@@ -1657,7 +1699,10 @@ def main():
 
     # main operation (like installing files)
     for iso in isos:
-        handle_iso(iso, device)
+        if os.path.isdir(iso):
+            handle_dir(iso, device)
+        else:
+            handle_iso(iso, device)
 
     # install mbr
     if not os.path.isdir(device):