udev: new "image_dissect" builtin that can be used to generate symlinks based on partition designators (#36631)

This replaces #36209 and implements this via a new builtin, rather than
by shelling out to systemd-dissect.

Replaces: #36209 
Replaces: #28894
Fixes: #34319 
Fixes: #33453 
Fixes: #27897
Fixes: #18035
This commit is contained in:
Lennart Poettering
2025-04-03 13:32:11 +02:00
committed by GitHub
36 changed files with 1601 additions and 261 deletions
+1
View File
@@ -106,6 +106,7 @@ simple_tests += files(
'test-hostname-setup.c',
'test-hostname-util.c',
'test-id128.c',
'test-image-filter.c',
'test-image-policy.c',
'test-import-util.c',
'test-in-addr-prefix-util.c',
+39
View File
@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "dissect-image.h"
#include "tests.h"
TEST(image_filter) {
_cleanup_(image_filter_freep) ImageFilter *f = NULL;
ASSERT_OK(image_filter_parse(NULL, &f));
ASSERT_NULL(f);
ASSERT_OK(image_filter_parse("", &f));
ASSERT_NULL(f);
ASSERT_OK(image_filter_parse("root=*", &f));
ASSERT_NOT_NULL(f);
ASSERT_STREQ(f->pattern[PARTITION_ROOT], "*");
f = image_filter_free(f);
ASSERT_OK(image_filter_parse("usr=foox?:root=kn*arz", &f));
ASSERT_NOT_NULL(f);
ASSERT_STREQ(f->pattern[PARTITION_ROOT], "kn*arz");
ASSERT_STREQ(f->pattern[PARTITION_USR], "foox?");
f = image_filter_free(f);
ASSERT_OK(image_filter_parse("usr=foox?:root=kn*arz:home=wumpi", &f));
ASSERT_NOT_NULL(f);
ASSERT_STREQ(f->pattern[PARTITION_ROOT], "kn*arz");
ASSERT_STREQ(f->pattern[PARTITION_USR], "foox?");
ASSERT_STREQ(f->pattern[PARTITION_HOME], "wumpi");
f = image_filter_free(f);
ASSERT_ERROR(image_filter_parse("usr=foox?:root=kn*arz:home=wumpi:schlumpf=smurf", &f), EINVAL);
ASSERT_ERROR(image_filter_parse(":", &f), EINVAL);
ASSERT_ERROR(image_filter_parse("::", &f), EINVAL);
ASSERT_ERROR(image_filter_parse("-", &f), EINVAL);
ASSERT_ERROR(image_filter_parse("root=knuff:root=knuff", &f), EINVAL);
}
DEFINE_TEST_MAIN(LOG_INFO);
+40 -5
View File
@@ -81,7 +81,14 @@ static void* thread_func(void *ptr) {
log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted);
r = dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected);
r = dissect_loop_device(
loop,
/* verity= */ NULL,
/* mount_options= */ NULL,
/* image_policy= */ NULL,
/* image_filter= */ NULL,
DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES,
&dissected);
if (r < 0)
log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node);
assert_se(r >= 0);
@@ -218,7 +225,14 @@ static int run(int argc, char *argv[]) {
sfdisk = NULL;
#if HAVE_BLKID
assert_se(dissect_image_file(p, NULL, NULL, NULL, 0, &dissected) >= 0);
assert_se(dissect_image_file(
p,
/* verity= */ NULL,
/* mount_options= */ NULL,
/* image_policy= */ NULL,
/* image_filter= */ NULL,
/* flags= */ 0,
&dissected) >= 0);
verify_dissected_image(dissected);
dissected = dissected_image_unref(dissected);
#endif
@@ -232,7 +246,14 @@ static int run(int argc, char *argv[]) {
assert_se(loop_device_make(fd, O_RDWR, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_EX, &loop) >= 0);
#if HAVE_BLKID
assert_se(dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
assert_se(dissect_loop_device(
loop,
/* verity= */ NULL,
/* mount_options= */ NULL,
/* image_policy= */ NULL,
/* image_filter= */ NULL,
DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES,
&dissected) >= 0);
verify_dissected_image(dissected);
FOREACH_STRING(fs, "vfat", "ext4") {
@@ -268,12 +289,26 @@ static int run(int argc, char *argv[]) {
/* Try to read once, without pinning or adding partitions, i.e. by only accessing the whole block
* device. */
assert_se(dissect_loop_device(loop, NULL, NULL, NULL, 0, &dissected) >= 0);
assert_se(dissect_loop_device(
loop,
/* verity= */ NULL,
/* mount_options= */ NULL,
/* image_policy= */ NULL,
/* image_filter= */ NULL,
/* flags= */ 0,
&dissected) >= 0);
verify_dissected_image_harder(dissected);
dissected = dissected_image_unref(dissected);
/* Now go via the loopback device after all, but this time add/pin, because now we want to mount it. */
assert_se(dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
assert_se(dissect_loop_device(
loop,
/* verity= */ NULL,
/* mount_options= */ NULL,
/* image_policy= */ NULL,
/* image_filter= */ NULL,
DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES,
&dissected) >= 0);
verify_dissected_image_harder(dissected);
assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);