From: Nick Labich Date: Tue, 22 Apr 2025 00:03:46 +0000 (-0400) Subject: repart: Allow devices as sources for --copy-from X-Git-Tag: v258-rc1~749 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d49aec4c160a189b9eda54941698f697ce927c9;p=thirdparty%2Fsystemd.git repart: Allow devices as sources for --copy-from Implements #37208 --- diff --git a/man/systemd-repart.xml b/man/systemd-repart.xml index 40376bc7738..0c41ef38ab0 100644 --- a/man/systemd-repart.xml +++ b/man/systemd-repart.xml @@ -551,16 +551,16 @@ - + Instructs systemd-repart to synthesize partition definitions from - the partition table in the given image. This option can be specified multiple times to synthesize - definitions from each of the given images. The generated definitions will copy the partitions into - the destination partition table. The copied partitions will have the same size, metadata and contents - but might have a different partition number and might be located at a different offset in the - destination partition table. These definitions can be combined with partition definitions read from - regular partition definition files. The synthesized definitions take precedence over the definitions - read from partition definition files. + the partition table in the given image or device. This option can be specified multiple times to + synthesize definitions from each of the given images or devices. The generated definitions will copy + the partitions into the destination partition table. The copied partitions will have the same size, + metadata and contents but might have a different partition number and might be located at a different + offset in the destination partition table. These definitions can be combined with partition + definitions read from regular partition definition files. The synthesized definitions take precedence + over the definitions read from partition definition files. diff --git a/src/repart/repart.c b/src/repart/repart.c index c0fd1cc7bcf..80fbafdb4b3 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -2676,6 +2676,26 @@ static int determine_current_padding( return 0; } +static int verify_regular_or_block(int fd) { + struct stat st; + + assert(fd >= 0); + + if (fstat(fd, &st) < 0) + return -errno; + + if (S_ISDIR(st.st_mode)) + return -EISDIR; + + if (S_ISLNK(st.st_mode)) + return -ELOOP; + + if (!S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode)) + return -EBADFD; + + return 0; +} + static int context_copy_from_one(Context *context, const char *src) { _cleanup_close_ int fd = -EBADF; _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; @@ -2691,9 +2711,9 @@ static int context_copy_from_one(Context *context, const char *src) { if (r < 0) return r; - r = fd_verify_regular(fd); + r = verify_regular_or_block(fd); if (r < 0) - return log_error_errno(r, "%s is not a file: %m", src); + return log_error_errno(r, "%s is not a file nor a block device: %m", src); r = fdisk_new_context_at(fd, /* path = */ NULL, /* read_only = */ true, /* sector_size = */ UINT32_MAX, &c); if (r < 0)