Drop oudated tarball-online make target and support prepare-release instead
[grml2usb.git] / grml2usb
index 7d9f92e..751d8cc 100755 (executable)
--- a/grml2usb
+++ b/grml2usb
@@ -376,7 +376,7 @@ menu begin grml %(grml_flavour)s
     include %(grml_flavour)s_grml.cfg
     menu hide
 menu end
-""" % {'grml_flavour': grml_flavour, 'display_name' : grml_flavour.replace('_', '-') } )
+""" % {'grml_flavour': grml_flavour, 'display_name' : get_flavour_filename(grml_flavour) } )
 
 
 def install_grub(device):
@@ -546,7 +546,7 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
                     mbrcode[463:478] + actives[2] + \
                     mbrcode[479:494] + actives[3] + \
                     mbrcode[495:510] + "\x55\xAA"
-    
+
     tmpf.file.seek(0)
     tmpf.file.truncate()
     tmpf.file.write(mbrcode)
@@ -740,6 +740,11 @@ def get_uuid(target):
         execute(mkdir, conf_target)
         return write_uuid(uuid_file_name)
 
+def get_shortname(grml_flavour):
+    """Get shortname based from grml_flavour name. The rules applied are the same as in grml-live
+    @grml_flavour: flavour name which shold be translated to shortname"""
+
+    return re.sub(r'[,._-]', '', grml_flavour)
 
 def copy_system_files(grml_flavour, iso_mount, target):
     """copy grml's main files (like squashfs, kernel and initrd) to a given target
@@ -768,10 +773,10 @@ def copy_system_files(grml_flavour, iso_mount, target):
     else:
         exec_rsync(filesystem_module, squashfs_target + 'filesystem.module')
 
-    cleaned_flavour = re.sub(r'[,._-]', '', grml_flavour)
 
-    if os.path.isdir(iso_mount + '/boot/' + cleaned_flavour):
-        exec_rsync(iso_mount + '/boot/' + cleaned_flavour, target + '/boot')
+    shortname = get_shortname(grml_flavour)
+    if os.path.isdir(iso_mount + '/boot/' + shortname):
+        exec_rsync(iso_mount + '/boot/' + shortname, target + '/boot')
     else:
         kernel = search_file('vmlinuz', iso_mount)
         if kernel is None:
@@ -909,6 +914,36 @@ def copy_addons(iso_mount, target):
     # ipxe.lkrn
     handle_addon_copy('ipxe.lkrn', addons, iso_mount)
 
+
+def build_loopbackcfg(target):
+    """Generate GRUB's loopback.cfg based on existing config files.
+
+    @target: target directory
+    """
+
+    grub_dir='/boot/grub/'
+    mkdir(os.path.join(target, grub_dir))
+
+    f = open(target + grub_dir + 'loopback.cfg','w')
+
+    f.write("# grml2usb generated grub2 configuration file\n")
+    f.write("source /boot/grub/header.cfg\n")
+
+    for defaults in glob.glob(target + os.path.sep + grub_dir + os.path.sep + "*_default.cfg"):
+        sourcefile = defaults.split(target + os.path.sep)[1]
+        logging.debug("Found source file" + sourcefile)
+        os.path.isfile(defaults) and f.write("source " + sourcefile + "\n")
+
+    for ops in glob.glob(target + os.path.sep + grub_dir + os.path.sep + "*_options.cfg"):
+        sourcefile = ops.split(target + os.path.sep)[1]
+        logging.debug("Found source file" + sourcefile)
+        os.path.isfile(ops) and f.write("source " + sourcefile + "\n")
+
+    f.write("source /boot/grub/adddons.cfg\n")
+    f.write("source /boot/grub/footer.cfg\n")
+    f.close()
+
+
 def glob_and_copy(filepattern, dst):
     """Glob on specified filepattern and copy the result to dst
 
@@ -967,13 +1002,6 @@ def copy_bootloader_files(iso_mount, target, grml_flavour):
     for ffile in ['f%d' % number for number in range(1, 11) ]:
         search_and_copy(ffile, iso_mount, syslinux_target + ffile)
 
-    loopback_cfg = search_file("loopback.cfg", iso_mount)
-    if loopback_cfg:
-        directory = os.path.dirname(loopback_cfg)
-        directory = directory.replace(iso_mount, "")
-        mkdir(os.path.join(target, directory))
-        exec_rsync(loopback_cfg, target + os.path.sep + directory)
-
     # avoid the "file is read only, overwrite anyway (y/n) ?" question
     # of mtools by syslinux ("mmove -D o -D O s:/ldlinux.sys $target_file")
     if os.path.isfile(syslinux_target + 'ldlinux.sys'):
@@ -1009,6 +1037,10 @@ def copy_bootloader_files(iso_mount, target, grml_flavour):
     # copy all grub files from ISO
     glob_and_copy(iso_mount + '/boot/grub/*', grub_target)
 
+    # finally (after all GRUB files have been been installed) build static loopback.cfg
+    build_loopbackcfg(target)
+
+
 def install_iso_files(grml_flavour, iso_mount, device, target):
     """Copy files from ISO to given target
 
@@ -1121,13 +1153,16 @@ def handle_grub_config(grml_flavour, device, target):
         for regex in options.removeoption:
             remove_regexes.append(re.compile(regex))
 
+    shortname = get_shortname(grml_flavour)
     for filename in glob.glob(grub_target + '*.cfg'):
         for line in fileinput.input(filename, inplace=1):
             line = line.rstrip("\r\n")
             if option_re.search(line):
                 line = bootid_re.sub('', line)
-                line = live_media_path_re.sub('', line)
-                line = line.rstrip() + r' live-media-path=/live/%s/ bootid=%s %s ' % (grml_flavour, UUID, bootopt)
+                if shortname in filename:
+                    line = live_media_path_re.sub('', line)
+                    line = line.rstrip() + ' live-media-path=/live/%s/ ' % (grml_flavour)
+                line = line.rstrip() + r' bootid=%s %s ' % (UUID, bootopt)
                 for regex in remove_regexes:
                     line = regex.sub(' ', line)
             print line
@@ -1247,7 +1282,7 @@ def modify_filenames(grml_flavour, target, filenames):
     @target: directory where the files are located
     @filenames: list of filenames to alter
     """
-    grml_filename = grml_flavour.replace('-', '_')
+    grml_filename = get_flavour_filename(grml_flavour)
     for filename in filenames:
         old_filename = "%s/%s" % (target, filename)
         new_filename = "%s/%s_%s" % (target, grml_filename, filename)
@@ -1291,7 +1326,7 @@ def handle_syslinux_config(grml_flavour, target):
     prompt_name.close()
 
     initial_syslinux_config(syslinux_target)
-    flavour_filename = grml_flavour.replace('-', '_')
+    flavour_filename = get_flavour_filename(grml_flavour)
 
     if search_file('default.cfg', syslinux_target):
         modify_filenames(grml_flavour, syslinux_target, ['grml.cfg', 'default.cfg'])