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]
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: