codecheck: fix flake8 issues with versions >=3.8.3
[grml2usb.git] / grml2usb
index 9948451..223dae2 100755 (executable)
--- a/grml2usb
+++ b/grml2usb
@@ -184,7 +184,7 @@ parser.add_option(
     "--skip-bootflag",
     dest="skipbootflag",
     action="store_true",
-    help="do not try to check whether the destination has the bootflag set",
+    help="do not try to check whether the destination has the boot flag set",
 )
 parser.add_option(
     "--skip-grub-config",
@@ -503,8 +503,15 @@ def get_partition_for_path(path):
 
 
 def check_boot_flag(device):
+    if os.path.isdir(device):
+        logging.debug(
+            "Device %s is a directory, skipping check for boot flag." % device
+        )
+        return
+
     boot_dev, x = get_device_from_partition(device)
 
+    logging.info("Checking for boot flag")
     try:
         import parted
 
@@ -512,28 +519,19 @@ def check_boot_flag(device):
         if part is None:
             raise HodorException("parted could not find partition")
         if part.getFlag(parted.PARTITION_BOOT):
-            logging.debug("bootflag is enabled on %s" % device)
+            logging.debug("boot flag is enabled on %s" % device)
             return
-    except HodorException as e:
-        logging.info("%s, falling back to old bootflag detection", e)
-    except ImportError:
-        logging.debug("could not import parted, falling back to old bootflag detection")
-
-    with open(boot_dev, "rb") as image:
-        data = image.read(520)
-        bootcode = data[440:]
-        gpt_data = bootcode[70:80]
-
-        if gpt_data == GPT_HEADER:
-            logging.info("GPT detected, skipping bootflag check")
-        elif bootcode[6] == b"\x80":
-            logging.debug("bootflag is enabled")
         else:
-            logging.debug("bootflag is NOT enabled")
+            logging.debug("boot flag is NOT enabled on %s" % device)
             raise VerifyException(
-                "Device %s does not have the bootflag set. "
+                "Device %s does not have the boot flag set. "
                 "Please enable it to be able to boot." % device
             )
+    except ImportError:
+        raise VerifyException(
+            "Could not import parted to verify boot flag on %s, please make sure python3-parted is installed."
+            % device
+        )
 
 
 def mkfs_fat16(device):
@@ -756,16 +754,14 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
     "mbrtemplate" file, set the "partition" (0..3) active, and install the
     result back to "device".
 
-    @mbrtemplate: default MBR file
+    @mbrtemplate: default MBR file (must be a valid MBR file of at least 440
+    (or 439 if ismirbsdmbr) bytes)
 
     @device: name of a file assumed to be a hard disc (or USB stick) image, or
     something like "/dev/sdb"
 
     @partition: must be a number between 0 and 3, inclusive
 
-    @mbrtemplate: must be a valid MBR file of at least 440 (or 439 if
-    ismirbsdmbr) bytes.
-
     @ismirbsdmbr: if true then ignore the active flag, set the mirbsdmbr
     specific flag to 0/1/2/3 and set the MBR's default value accordingly. If
     false then leave the mirbsdmbr specific flag set to FFh, set all
@@ -879,9 +875,18 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
     del tmpf
 
     # make sure we sync filesystems before returning
+    logging.debug("executing: sync")
     proc = subprocess.Popen(["sync"])
     proc.wait()
 
+    logging.debug("Probing device via 'blockdev --rereadpt %s'", device)
+    proc = subprocess.Popen(["blockdev", "--rereadpt", device])
+    proc.wait()
+    if proc.returncode != 0:
+        raise Exception(
+            "Couldn't execute blockdev on '%s' (install util-linux?)", device
+        )
+
     set_rw(device)
 
 
@@ -913,7 +918,7 @@ def mount(source, target, mount_options):
         if x.startswith(source):
             raise CriticalException(
                 (
-                    "Error executing mount: %s already mounted - "
+                    "Error executing mount: {0} already mounted - "
                     "please unmount before invoking grml2usb"
                 ).format(source)
             )
@@ -1403,6 +1408,7 @@ def install_iso_files(grml_flavour, iso_mount, device, target):
             handle_bootloader_config(grml_flavour, device, target)
 
     # make sure we sync filesystems before returning
+    logging.info("Synching data (this might take a while)")
     proc = subprocess.Popen(["sync"])
     proc.wait()
 
@@ -1881,10 +1887,6 @@ def install_grml(mountpoint, device):
         register_tmpfile(device_mountpoint)
         remove_device_mountpoint = True
         try:
-            check_for_fat(device)
-            if not options.skipbootflag:
-                check_boot_flag(device)
-
             set_rw(device)
             mount(device, device_mountpoint, ["-o", "utf8,iocharset=iso8859-1"])
         except CriticalException:
@@ -1951,7 +1953,7 @@ def handle_mbr(device):
                 break
 
         if not mbrcode:
-            str_locations = " or ".join(['"%s"' % l for l in mbr_locations])
+            str_locations = " or ".join(['"%s"' % x for x in mbr_locations])
             logging.error("Cannot find syslinux MBR, install it at %s)", str_locations)
             raise CriticalException(
                 "syslinux MBR  can not be found at %s." % str_locations
@@ -2169,6 +2171,9 @@ def main():
         # provide upgrade path
         handle_compat_warning(device)
 
+        if not options.skipbootflag:
+            check_boot_flag(device)
+
         # check for vfat partition
         handle_vfat(device)