From fd9fe57a266942c96e136d23dc032e3d20fc5019 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 14 Aug 2023 10:27:57 +0200 Subject: [PATCH 1/2] fdisk-util: Make fdisk_new_context_fd() more generic Let's make this an openat() style function so we can also pass a device path. --- src/home/homework-luks.c | 6 +++--- src/partition/repart.c | 4 ++-- src/shared/fdisk-util.c | 20 ++++++++++++++------ src/shared/fdisk-util.h | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 7f23d1e1586..5920fbb732c 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -1870,7 +1870,7 @@ static int make_partition_table( if (r < 0) return log_error_errno(r, "Failed to initialize partition type: %m"); - r = fdisk_new_context_fd(fd, /* read_only= */ false, sector_size, &c); + r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ false, sector_size, &c); if (r < 0) return log_error_errno(r, "Failed to open device: %m"); @@ -2698,7 +2698,7 @@ static int prepare_resize_partition( return 0; } - r = fdisk_new_context_fd(fd, /* read_only= */ false, UINT32_MAX, &c); + r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ false, UINT32_MAX, &c); if (r < 0) return log_error_errno(r, "Failed to open device: %m"); @@ -2855,7 +2855,7 @@ static int apply_resize_partition( if ((size_t) n != ssz * 2) return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while wiping partition table."); - r = fdisk_new_context_fd(fd, /* read_only= */ false, ssz, &c); + r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ false, ssz, &c); if (r < 0) return log_error_errno(r, "Failed to open device: %m"); diff --git a/src/partition/repart.c b/src/partition/repart.c index 88669db5865..52b03ec7f0a 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -1924,7 +1924,7 @@ static int context_copy_from_one(Context *context, const char *src) { if (r < 0) return log_error_errno(r, "%s is not a file: %m", src); - r = fdisk_new_context_fd(fd, /* read_only = */ true, /* sector_size = */ UINT32_MAX, &c); + r = fdisk_new_context_at(fd, /* path = */ NULL, /* read_only = */ true, /* sector_size = */ UINT32_MAX, &c); if (r < 0) return log_error_errno(r, "Failed to create fdisk context: %m"); @@ -6931,7 +6931,7 @@ static int resize_pt(int fd, uint64_t sector_size) { * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and * immediately write it again, with no changes. */ - r = fdisk_new_context_fd(fd, /* read_only= */ false, sector_size, &c); + r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ false, sector_size, &c); if (r < 0) return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd)); diff --git a/src/shared/fdisk-util.c b/src/shared/fdisk-util.c index 9a301f38ac3..20f32d19aff 100644 --- a/src/shared/fdisk-util.c +++ b/src/shared/fdisk-util.c @@ -8,26 +8,34 @@ #if HAVE_LIBFDISK -int fdisk_new_context_fd( - int fd, +int fdisk_new_context_at( + int dir_fd, + const char *path, bool read_only, uint32_t sector_size, struct fdisk_context **ret) { _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; + _cleanup_close_ int fd = -EBADF; int r; + assert(dir_fd >= 0 || dir_fd == AT_FDCWD); assert(ret); - if (fd < 0) - return -EBADF; + if (!isempty(path)) { + fd = openat(dir_fd, path, (read_only ? O_RDONLY : O_RDWR)|O_CLOEXEC); + if (fd < 0) + return -errno; + + dir_fd = fd; + } c = fdisk_new_context(); if (!c) return -ENOMEM; if (sector_size == UINT32_MAX) { - r = probe_sector_size_prefer_ioctl(fd, §or_size); + r = probe_sector_size_prefer_ioctl(dir_fd, §or_size); if (r < 0) return r; } @@ -38,7 +46,7 @@ int fdisk_new_context_fd( return r; } - r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), read_only); + r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(dir_fd), read_only); if (r < 0) return r; diff --git a/src/shared/fdisk-util.h b/src/shared/fdisk-util.h index b82ff705d77..a72a596f472 100644 --- a/src/shared/fdisk-util.h +++ b/src/shared/fdisk-util.h @@ -14,7 +14,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_partition*, fdisk_unref_partition, DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_parttype*, fdisk_unref_parttype, NULL); DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table*, fdisk_unref_table, NULL); -int fdisk_new_context_fd(int fd, bool read_only, uint32_t sector_size, struct fdisk_context **ret); +int fdisk_new_context_at(int dir_fd, const char *path, bool read_only, uint32_t sector_size, struct fdisk_context **ret); int fdisk_partition_get_uuid_as_id128(struct fdisk_partition *p, sd_id128_t *ret); int fdisk_partition_get_type_as_id128(struct fdisk_partition *p, sd_id128_t *ret); From 4492eb118678de1783a38e132f6a44f68d39d1d0 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 14 Aug 2023 10:42:43 +0200 Subject: [PATCH 2/2] tree-wide: Use fdisk_new_context_at() more --- src/home/homework-luks.c | 8 ++------ src/sysupdate/sysupdate-partition.c | 16 ++++------------ src/sysupdate/sysupdate-resource.c | 8 ++------ 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 5920fbb732c..e41920ce9c6 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -2764,13 +2764,9 @@ static int get_maximum_partition_size( assert(p); assert(ret_maximum_partition_size); - c = fdisk_new_context(); - if (!c) - return log_oom(); - - r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), 0); + r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ true, /* sector_size= */ UINT32_MAX, &c); if (r < 0) - return log_error_errno(r, "Failed to open device: %m"); + return log_error_errno(r, "Failed to create fdisk context: %m"); start_lba = fdisk_partition_get_start(p); assert(start_lba <= UINT64_MAX/512); diff --git a/src/sysupdate/sysupdate-partition.c b/src/sysupdate/sysupdate-partition.c index 587265482bd..fa4453d6651 100644 --- a/src/sysupdate/sysupdate-partition.c +++ b/src/sysupdate/sysupdate-partition.c @@ -119,13 +119,9 @@ int find_suitable_partition( assert(device); assert(ret); - c = fdisk_new_context(); - if (!c) - return log_oom(); - - r = fdisk_assign_device(c, device, /* readonly= */ true); + r = fdisk_new_context_at(AT_FDCWD, device, /* read_only= */ true, /* sector_size= */ UINT32_MAX, &c); if (r < 0) - return log_error_errno(r, "Failed to open device '%s': %m", device); + return log_error_errno(r, "Failed to create fdisk context from '%s': %m", device); if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", device); @@ -188,13 +184,9 @@ int patch_partition( if (change == 0) /* Nothing to do */ return 0; - c = fdisk_new_context(); - if (!c) - return log_oom(); - - r = fdisk_assign_device(c, device, /* readonly= */ false); + r = fdisk_new_context_at(AT_FDCWD, device, /* read_only= */ false, /* sector_size= */ UINT32_MAX, &c); if (r < 0) - return log_error_errno(r, "Failed to open device '%s': %m", device); + return log_error_errno(r, "Failed to create fdisk context from '%s': %m", device); assert_se((fd = fdisk_get_devfd(c)) >= 0); diff --git a/src/sysupdate/sysupdate-resource.c b/src/sysupdate/sysupdate-resource.c index 30973a360df..a6efa289615 100644 --- a/src/sysupdate/sysupdate-resource.c +++ b/src/sysupdate/sysupdate-resource.c @@ -168,13 +168,9 @@ static int resource_load_from_blockdev(Resource *rr) { assert(rr); - c = fdisk_new_context(); - if (!c) - return log_oom(); - - r = fdisk_assign_device(c, rr->path, /* readonly= */ true); + r = fdisk_new_context_at(AT_FDCWD, rr->path, /* read_only= */ true, /* sector_size= */ UINT32_MAX, &c); if (r < 0) - return log_error_errno(r, "Failed to open device '%s': %m", rr->path); + return log_error_errno(r, "Failed to create fdisk context from '%s': %m", rr->path); if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT)) return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", rr->path);