From: Lennart Poettering Date: Fri, 19 Mar 2021 21:19:08 +0000 (+0100) Subject: dissect: split read-only flag into two X-Git-Tag: v249-rc1~388^2~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ef9c184d3d2971f41ffc4bc9b8b4df17a704a29e;p=thirdparty%2Fsystemd.git dissect: split read-only flag into two Let's have one flag to request that when dissecting an image the loopback device is made read-only, and another one to request that when it is mounted to make it read-only. Previously both concepts were always done read-only together. (Of course, making the loopback device read-only but mounting it read-write doesn't make too much sense, but the kernel should catch that for us, no need to make restrictions from our side there) Use-case for this: in systemd-repart we'd like to operate on images for adding partitions. Thus we'd like to have the loopback device writable, but if we read repart.d/ snippets from it, we want to do that read-only. --- diff --git a/src/core/namespace.c b/src/core/namespace.c index 7eb42ee405e..9f9d47d34a2 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -1853,7 +1853,7 @@ int setup_namespace( r = loop_device_make_by_path( root_image, - FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : -1 /* < 0 means writable if possible, read-only as fallback */, + FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : -1 /* < 0 means writable if possible, read-only as fallback */, FLAGS_SET(dissect_image_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, &loop_device); if (r < 0) diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 65ddb1d1493..c21d3e47e40 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -770,7 +770,7 @@ static int run(int argc, char *argv[]) { r = loop_device_make_by_path( arg_image, - FLAGS_SET(arg_flags, DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, + FLAGS_SET(arg_flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : O_RDWR, FLAGS_SET(arg_flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, &d); if (r < 0) diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index ce8a683bd68..1624e9daa82 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -1408,7 +1408,7 @@ static int mount_partition( if (streq(fstype, "crypto_LUKS")) return -EUNATCH; - rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY); + rw = m->rw && !(flags & DISSECT_IMAGE_MOUNT_READ_ONLY); if (FLAGS_SET(flags, DISSECT_IMAGE_FSCK) && rw) { r = run_fsck(node, fstype); @@ -1756,7 +1756,7 @@ static int decrypt_partition( return log_debug_errno(r, "Failed to load LUKS metadata: %m"); r = sym_crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), - ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | + ((flags & DISSECT_IMAGE_DEVICE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) | ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0)); if (r < 0) { log_debug_errno(r, "Failed to activate LUKS device: %m"); @@ -2674,7 +2674,7 @@ int mount_image_privately_interactively( r = loop_device_make_by_path( image, - FLAGS_SET(flags, DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, + FLAGS_SET(flags, DISSECT_IMAGE_DEVICE_READ_ONLY) ? O_RDONLY : O_RDWR, FLAGS_SET(flags, DISSECT_IMAGE_NO_PARTITION_TABLE) ? 0 : LO_FLAGS_PARTSCAN, &d); if (r < 0) diff --git a/src/shared/dissect-image.h b/src/shared/dissect-image.h index f07955230ba..d51049e78a6 100644 --- a/src/shared/dissect-image.h +++ b/src/shared/dissect-image.h @@ -87,13 +87,13 @@ static inline PartitionDesignator PARTITION_VERITY_OF(PartitionDesignator p) { } typedef enum DissectImageFlags { - DISSECT_IMAGE_READ_ONLY = 1 << 0, + DISSECT_IMAGE_DEVICE_READ_ONLY = 1 << 0, /* Make device read-only */ DISSECT_IMAGE_DISCARD_ON_LOOP = 1 << 1, /* Turn on "discard" if on a loop device and file system supports it */ DISSECT_IMAGE_DISCARD = 1 << 2, /* Turn on "discard" if file system supports it, on all block devices */ DISSECT_IMAGE_DISCARD_ON_CRYPTO = 1 << 3, /* Turn on "discard" also on crypto devices */ - DISSECT_IMAGE_DISCARD_ANY = DISSECT_IMAGE_DISCARD_ON_LOOP | - DISSECT_IMAGE_DISCARD | - DISSECT_IMAGE_DISCARD_ON_CRYPTO, + DISSECT_IMAGE_DISCARD_ANY = DISSECT_IMAGE_DISCARD_ON_LOOP | + DISSECT_IMAGE_DISCARD | + DISSECT_IMAGE_DISCARD_ON_CRYPTO, DISSECT_IMAGE_GPT_ONLY = 1 << 4, /* Only recognize images with GPT partition tables */ DISSECT_IMAGE_GENERIC_ROOT = 1 << 5, /* If no partition table or only single generic partition, assume it's the root fs */ DISSECT_IMAGE_MOUNT_ROOT_ONLY = 1 << 6, /* Mount only the root and /usr partitions */ @@ -107,6 +107,9 @@ typedef enum DissectImageFlags { DISSECT_IMAGE_MKDIR = 1 << 14, /* Make top-level directory to mount right before mounting, if missing */ DISSECT_IMAGE_USR_NO_ROOT = 1 << 15, /* If no root fs is in the image, but /usr is, then allow this (so that we can mount the rootfs as tmpfs or so */ DISSECT_IMAGE_REQUIRE_ROOT = 1 << 16, /* Don't accept disks without root partition (or at least /usr partition if DISSECT_IMAGE_USR_NO_ROOT is set) */ + DISSECT_IMAGE_MOUNT_READ_ONLY = 1 << 17, /* Make mounts read-only */ + DISSECT_IMAGE_READ_ONLY = DISSECT_IMAGE_DEVICE_READ_ONLY | + DISSECT_IMAGE_MOUNT_READ_ONLY, } DissectImageFlags; struct DissectedImage {