WIP unmount partitions
authorŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 2 Oct 2023 19:48:00 +0000 (21:48 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 2 Oct 2023 19:58:11 +0000 (21:58 +0200)
Change-Id: I80270de45ef871f0be3cd6cef27ef61cedc66111
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
scripts/tizen/sd_fusing.py

index 64e8bfa..ce1ab8e 100755 (executable)
@@ -381,18 +381,38 @@ def mkpart(args, target):
         logging.error('sfdisk too old')
         sys.exit(1)
 
-    #TODO: unmount target devices
+    with open('/proc/self/mounts') as mounts:
+        device_kname = '/dev/' + get_device_kname(Device)
+        device_re = re.compile(device_kname + '[^ ]*')
+        logging.debug("Checking for mounted partitions on {device_kname}")
+        for m in mounts:
+            match = device_re.match(m)
+            if match:
+                logging.warning('Found mounted device: ' + match[0])
+                argv = ['umount', match[0]]
+                logging.debug(" ".join(argv))
+                proc = subprocess.run(argv)
+                if proc.returncode != 0:
+                    logging.error(f"Failed to unmount {match[0]}")
+                    sys.exit(1)
+
     if support_delete:
         logging.info("Removing old partitions")
         argv = ['sfdisk', '--delete', Device]
         logging.debug(" ".join(argv))
-        subprocess.run(argv)
+        proc = subprocess.run(argv)
+        if proc.returncode != 0:
+            logging.error(f"Failed to remove the old partitions from {Device}")
+            sys.exit(1)
     else:
         logging.info("Removing old partition table")
         argv = ['dd', 'if=/dev/zero', 'of=' + Device,
                 'bs=512', 'count=32', 'conv=notrunc']
         logging.debug(" ".join(argv))
-        subprocess.run(argv)
+        proc = subprocess.run(argv)
+        if proc.returncode != 0:
+            logging.error(f"Failed to clear the old partition table on {Device}")
+            sys.exit(1)
 
     logging.debug("New partition table:\n" + str(target.label))
     argv = ['sfdisk', Device]
@@ -529,6 +549,17 @@ def get_partition_device(device, idx):
             return match[1]
     return None
 
+def get_device_kname(device):
+    argv = ['lsblk', device, '-o', 'TYPE,KNAME']
+    logging.debug(" ".join(argv))
+    proc = subprocess.run(argv,
+                          stdout=subprocess.PIPE)
+    for l in proc.stdout.decode('utf-8').splitlines():
+        match = re.search(f"^(disk|loop)\s+(.*)", l)
+        if match:
+            return match[2]
+    return None
+
 def do_fuse_file(f, name, target):
     idx = target.get_partition_index(name)
     if idx is None: