From 5033f52497a444831696f178ba6ee77722a4533d Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Thu, 13 Apr 2017 23:47:47 +0200 Subject: [PATCH 1/1] Support --rw-blockdev option for usage with read-only/forensic like devices If the system is running in a read-only mode for devices and setting devices to read-only when they appear (e.g. partition table scans) then we need to explictely set read-write mode on the involved devices (e.g. /dev/sdx + /dev/sdx2 if /dev/sd2 is the target partition). Be aware that you might have to use the --skip-bootflag option as well then, because this option triggers parted code which is behaving bad with read-only devices. --- grml2usb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/grml2usb b/grml2usb index b188539..05067e4 100755 --- a/grml2usb +++ b/grml2usb @@ -103,6 +103,8 @@ parser.add_option("--quiet", dest="quiet", action="store_true", help="do not output anything but just errors on console") parser.add_option("--remove-bootoption", dest="removeoption", action="append", help="regex for removing existing bootoptions") +parser.add_option("--rw-blockdev", dest="rwblockdev", action="store_true", + help="enforce read-write mode on involved block devices") parser.add_option("--skip-addons", dest="skipaddons", action="store_true", help="do not install /boot/addons/ files") parser.add_option("--skip-bootflag", dest="skipbootflag", action="store_true", @@ -252,6 +254,17 @@ def get_function_name(obj): return obj.__module__ + '.' + obj.__name__ +def set_rw(device): + if not options.rwblockdev: + return + + logging.debug("executing: blockdev --setrw %s", device) + proc = subprocess.Popen(["blockdev", "--setrw", device]) + proc.wait() + if proc.returncode != 0: + raise Exception("error executing blockdev on %s" % device) + + def execute(f, *exec_arguments): """Wrapper for executing a command. Either really executes the command (default) or when using --dry-run commandline option @@ -498,6 +511,9 @@ def install_grub(device): else: grub_device = device + set_rw(device) + set_rw(grub_device) + logging.info("Installing grub as bootloader") for opt in ["", "--force"]: logging.debug("grub-install --recheck %s --no-floppy --root-directory=%s %s", @@ -537,6 +553,8 @@ def install_syslinux(device): logging.info("Would install syslinux as bootloader on %s", device) return 0 + set_rw(device) + # syslinux -d boot/isolinux /dev/sdb1 logging.info("Installing syslinux as bootloader") logging.debug("syslinux -d boot/syslinux %s", device) @@ -647,6 +665,8 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True): tmpf.file.write(mbrcode) tmpf.file.close() + set_rw(device) + logging.debug("executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device) proc = subprocess.Popen(["dd", "if=%s" % tmpf.name, "of=%s" % device, "bs=512", "count=1", "conv=notrunc"], stderr=file(os.devnull, "r+")) @@ -659,6 +679,8 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True): proc = subprocess.Popen(["sync"]) proc.wait() + set_rw(device) + def is_writeable(device): """Check if the device is writeable for the current user @@ -693,6 +715,8 @@ def mount(source, target, mount_options): logging.debug("Source %s is not a device, therefore not mounting.", source) return 0 + set_rw(source) + logging.debug("mount %s %s %s", mount_options, source, target) proc = subprocess.Popen(["mount"] + list(mount_options) + [source, target]) proc.wait() -- 2.1.4