diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-06 05:37:13 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-09-07 20:45:24 +0900 |
commit | 369de26fdf7152e7f2ff790b65533d101772e073 (patch) | |
tree | a97cd3b4f54e90c280002a4a9ff16705eea31cb4 | |
parent | e77cab824829f33e7870eb188ccd014acac49075 (diff) | |
download | systemd-369de26fdf7152e7f2ff790b65533d101772e073.tar.gz |
dissect-image: use backing_file stored in LoopDevice object to generate image name
Follow-up for e374439f4b8def786031ddbbd7dfdae3a335d4d2 (#24322).
This also simplify the logic of generating image name from image path.
-rw-r--r-- | src/core/namespace.c | 1 | ||||
-rw-r--r-- | src/portable/portable.c | 1 | ||||
-rw-r--r-- | src/shared/discover-image.c | 1 | ||||
-rw-r--r-- | src/shared/dissect-image.c | 66 | ||||
-rw-r--r-- | src/shared/dissect-image.h | 15 | ||||
-rw-r--r-- | src/test/test-loop-block.c | 12 |
6 files changed, 44 insertions, 52 deletions
diff --git a/src/core/namespace.c b/src/core/namespace.c index 3fbf094851..468b27905e 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -2065,7 +2065,6 @@ int setup_namespace( r = dissect_loop_device( loop_device, - root_image, &verity, root_image_options, dissect_image_flags, diff --git a/src/portable/portable.c b/src/portable/portable.c index 9ac34ced6d..5d0a965db5 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -367,7 +367,6 @@ static int portable_extract_by_path( r = dissect_loop_device( d, - path, NULL, NULL, DISSECT_IMAGE_READ_ONLY | DISSECT_IMAGE_GENERIC_ROOT | diff --git a/src/shared/discover-image.c b/src/shared/discover-image.c index 6ddb279f0d..b3b59fc0bb 100644 --- a/src/shared/discover-image.c +++ b/src/shared/discover-image.c @@ -1198,7 +1198,6 @@ int image_read_metadata(Image *i) { r = dissect_loop_device( d, - i->path, NULL, NULL, DISSECT_IMAGE_GENERIC_ROOT | DISSECT_IMAGE_REQUIRE_ROOT | diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index dc7cf817f3..fce05bcfbd 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -193,7 +193,7 @@ static int make_partition_devname( int dissect_image( int fd, - const char *original_path, + const char *image_path, const VeritySettings *verity, const MountOptions *mount_options, uint64_t diskseq, @@ -213,7 +213,7 @@ int dissect_image( _cleanup_(blkid_free_probep) blkid_probe b = NULL; _cleanup_free_ char *generic_node = NULL; sd_id128_t generic_uuid = SD_ID128_NULL; - const char *pttype = NULL, *sysname = NULL, *devname = NULL; + const char *pttype = NULL, *devname = NULL; blkid_partlist pl; int r, generic_nr = -1, n_partitions; struct stat st; @@ -305,51 +305,45 @@ int dissect_image( .has_init_system = -1, }; - r = sd_device_get_sysname(d, &sysname); - if (r < 0) - return log_debug_errno(r, "Failed to get device sysname: %m"); - if (original_path) { - _cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL; - r = path_extract_filename(original_path, &extracted_filename); - if (r < 0) - return r; - r = raw_strip_suffixes(extracted_filename, &name_stripped); - if (r < 0) - return r; + if (!image_path) { + const char *sysname; - free_and_replace(m->image_name, name_stripped); - } else if (startswith(sysname, "loop")) { - _cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL; - const char *full_path; + /* If image_path is not provided and the device is a loopback block device, then use the path + * to the backing file. Note that the backing_file reference resolves symlinks, while for + * sysext images we want the original path. */ - /* Note that the backing_file reference resolves symlinks, while for sysext images we want the original path */ - r = sd_device_get_sysattr_value(d, "loop/backing_file", &full_path); + r = sd_device_get_sysname(d, &sysname); if (r < 0) - log_debug_errno(r, "Failed to lookup image name via loop device backing file sysattr, ignoring: %m"); - else { - r = path_extract_filename(full_path, &extracted_filename); - if (r < 0) - return r; - r = raw_strip_suffixes(extracted_filename, &name_stripped); + return log_debug_errno(r, "Failed to get device sysname: %m"); + + if (startswith(sysname, "loop")) { + r = sd_device_get_sysattr_value(d, "loop/backing_file", &image_path); if (r < 0) - return r; + log_debug_errno(r, "Failed to lookup image name via loop device backing file sysattr, ignoring: %m"); } + } - free_and_replace(m->image_name, name_stripped); - } else { - r = free_and_strdup(&m->image_name, sysname); + if (image_path) { + _cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL; + + r = path_extract_filename(image_path, &extracted_filename); + if (r < 0) + return r; + + r = raw_strip_suffixes(extracted_filename, &name_stripped); if (r < 0) return r; + + if (!image_name_is_valid(name_stripped)) + log_debug("Image name %s is not valid, ignoring.", strna(name_stripped)); + else + m->image_name = TAKE_PTR(name_stripped); } + r = sd_device_get_devname(d, &devname); if (r < 0) return log_debug_errno(r, "Failed to get device devname: %m"); - if (!image_name_is_valid(m->image_name)) { - log_debug("Image name %s is not valid, ignoring", strempty(m->image_name)); - m->image_name = mfree(m->image_name); - } - if ((!(flags & DISSECT_IMAGE_GPT_ONLY) && (flags & DISSECT_IMAGE_GENERIC_ROOT)) || (flags & DISSECT_IMAGE_NO_PARTITION_TABLE)) { @@ -2827,7 +2821,7 @@ int dissect_loop_device_and_warn( if (!name) name = ASSERT_PTR(loop->node); - r = dissect_loop_device(loop, name, verity, mount_options, flags, ret); + r = dissect_loop_device(loop, verity, mount_options, flags, ret); switch (r) { case -EOPNOTSUPP: @@ -3093,7 +3087,6 @@ int verity_dissect_and_mount( r = dissect_loop_device( loop_device, - src, &verity, options, dissect_image_flags, @@ -3102,7 +3095,6 @@ int verity_dissect_and_mount( if (!verity.data_path && r == -ENOPKG) r = dissect_loop_device( loop_device, - src, &verity, options, dissect_image_flags | DISSECT_IMAGE_NO_PARTITION_TABLE, diff --git a/src/shared/dissect-image.h b/src/shared/dissect-image.h index cc0cf66180..c8d2e6de8f 100644 --- a/src/shared/dissect-image.h +++ b/src/shared/dissect-image.h @@ -253,10 +253,19 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all); const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator); int probe_filesystem(const char *node, char **ret_fstype); -int dissect_image(int fd, const char *original_path, const VeritySettings *verity, const MountOptions *mount_options, uint64_t diskseq, uint64_t uevent_seqnum_not_before, usec_t timestamp_not_before, DissectImageFlags flags, DissectedImage **ret); -static inline int dissect_loop_device(const LoopDevice *loop, const char *original_path, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret) { +int dissect_image( + int fd, + const char *image_path, + const VeritySettings *verity, + const MountOptions *mount_options, + uint64_t diskseq, + uint64_t uevent_seqnum_not_before, + usec_t timestamp_not_before, + DissectImageFlags flags, + DissectedImage **ret); +static inline int dissect_loop_device(const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret) { assert(loop); - return dissect_image(loop->fd, original_path, verity, mount_options, loop->diskseq, loop->uevent_seqnum_not_before, loop->timestamp_not_before, flags, ret); + return dissect_image(loop->fd, loop->backing_file, verity, mount_options, loop->diskseq, loop->uevent_seqnum_not_before, loop->timestamp_not_before, flags, ret); } int dissect_loop_device_and_warn(const char *name, const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret); diff --git a/src/test/test-loop-block.c b/src/test/test-loop-block.c index 70d4aedb42..ee28ad4235 100644 --- a/src/test/test-loop-block.c +++ b/src/test/test-loop-block.c @@ -39,7 +39,6 @@ static void* thread_func(void *ptr) { _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL; _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL; _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL; - _cleanup_free_ char *path = NULL; if (now(CLOCK_MONOTONIC) >= end) { log_notice("Time's up, exiting thread's loop"); @@ -59,12 +58,7 @@ static void* thread_func(void *ptr) { log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted); - r = fd_get_path(fd, &path); - if (r < 0) - log_error_errno(r, "Failed to get path from fd: %m"); - assert_se(r >= 0); - - r = dissect_loop_device(loop, path, NULL, NULL, DISSECT_IMAGE_READ_ONLY, &dissected); + r = dissect_loop_device(loop, NULL, NULL, DISSECT_IMAGE_READ_ONLY, &dissected); if (r < 0) log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node); assert_se(r >= 0); @@ -228,7 +222,7 @@ static int run(int argc, char *argv[]) { pthread_t threads[arg_n_threads]; sd_id128_t id; - assert_se(dissect_loop_device(loop, p, NULL, NULL, 0, &dissected) >= 0); + assert_se(dissect_loop_device(loop, NULL, NULL, 0, &dissected) >= 0); assert_se(dissected->partitions[PARTITION_ESP].found); assert_se(dissected->partitions[PARTITION_ESP].node); @@ -252,7 +246,7 @@ static int run(int argc, char *argv[]) { assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", id, true) >= 0); dissected = dissected_image_unref(dissected); - assert_se(dissect_loop_device(loop, p, NULL, NULL, 0, &dissected) >= 0); + assert_se(dissect_loop_device(loop, NULL, NULL, 0, &dissected) >= 0); assert_se(mkdtemp_malloc(NULL, &mounted) >= 0); |