From d25697f578d1a6c717620d9122a8acb4b64219bd Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 28 Jul 2022 00:54:16 +0200 Subject: [PATCH] blockdev-util: Introduce block_device_add/remove_partition() Extracted from dissect-image.c. --- src/shared/blockdev-util.c | 51 ++++++++++++++++++++++++++++++ src/shared/blockdev-util.h | 3 ++ src/shared/dissect-image.c | 63 +++----------------------------------- 3 files changed, 58 insertions(+), 59 deletions(-) diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index de72f843826..b0ab5b440e3 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include +#include #include #include "alloc-util.h" @@ -8,6 +10,7 @@ #include "btrfs-util.h" #include "devnum-util.h" #include "dirent-util.h" +#include "errno-util.h" #include "fd-util.h" #include "fileio.h" #include "missing_magic.h" @@ -425,3 +428,51 @@ int path_get_whole_disk(const char *path, bool backing, dev_t *ret) { return fd_get_whole_disk(fd, backing, ret); } + +int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size) { + assert(fd >= 0); + assert(name); + assert(nr > 0); + + struct blkpg_partition bp = { + .pno = nr, + .start = start, + .length = size, + }; + + struct blkpg_ioctl_arg ba = { + .op = BLKPG_ADD_PARTITION, + .data = &bp, + .datalen = sizeof(bp), + }; + + if (strlen(name) >= sizeof(bp.devname)) + return -EINVAL; + + strcpy(bp.devname, name); + + return RET_NERRNO(ioctl(fd, BLKPG, &ba)); +} + +int block_device_remove_partition(int fd, const char *name, int nr) { + assert(fd >= 0); + assert(name); + assert(nr > 0); + + struct blkpg_partition bp = { + .pno = nr, + }; + + struct blkpg_ioctl_arg ba = { + .op = BLKPG_DEL_PARTITION, + .data = &bp, + .datalen = sizeof(bp), + }; + + if (strlen(name) >= sizeof(bp.devname)) + return -EINVAL; + + strcpy(bp.devname, name); + + return RET_NERRNO(ioctl(fd, BLKPG, &ba)); +} diff --git a/src/shared/blockdev-util.h b/src/shared/blockdev-util.h index e30692b509a..f7055625a36 100644 --- a/src/shared/blockdev-util.h +++ b/src/shared/blockdev-util.h @@ -30,3 +30,6 @@ int path_is_encrypted(const char *path); int fd_get_whole_disk(int fd, bool backing, dev_t *ret); int path_get_whole_disk(const char *path, bool backing, dev_t *ret); + +int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size); +int block_device_remove_partition(int fd, const char *name, int nr); diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index c7a336d4326..87712abfb3b 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -4,7 +4,6 @@ #include #endif -#include #include #include #include @@ -149,29 +148,6 @@ static void check_partition_flags( log_debug("Unexpected partition flag %llu set on %s!", bit, node); } } - -static int ioctl_partition_remove(int fd, const char *name, int nr) { - assert(fd >= 0); - assert(name); - assert(nr > 0); - - struct blkpg_partition bp = { - .pno = nr, - }; - - struct blkpg_ioctl_arg ba = { - .op = BLKPG_DEL_PARTITION, - .data = &bp, - .datalen = sizeof(bp), - }; - - if (strlen(name) >= sizeof(bp.devname)) - return -EINVAL; - - strcpy(bp.devname, name); - - return RET_NERRNO(ioctl(fd, BLKPG, &ba)); -} #endif static void dissected_partition_done(int fd, DissectedPartition *p) { @@ -182,7 +158,7 @@ static void dissected_partition_done(int fd, DissectedPartition *p) { if (p->node && p->partno > 0 && !p->relinquished) { int r; - r = ioctl_partition_remove(fd, p->node, p->partno); + r = block_device_remove_partition(fd, p->node, p->partno); if (r < 0) log_debug_errno(r, "BLKPG_DEL_PARTITION failed, ignoring: %m"); } @@ -202,37 +178,6 @@ static void dissected_partition_done(int fd, DissectedPartition *p) { } #if HAVE_BLKID -static int ioctl_partition_add( - int fd, - const char *name, - int nr, - uint64_t start, - uint64_t size) { - - assert(fd >= 0); - assert(name); - assert(nr > 0); - - struct blkpg_partition bp = { - .pno = nr, - .start = start, - .length = size, - }; - - struct blkpg_ioctl_arg ba = { - .op = BLKPG_ADD_PARTITION, - .data = &bp, - .datalen = sizeof(bp), - }; - - if (strlen(name) >= sizeof(bp.devname)) - return -EINVAL; - - strcpy(bp.devname, name); - - return RET_NERRNO(ioctl(fd, BLKPG, &ba)); -} - static int make_partition_devname( const char *whole_devname, int nr, @@ -548,7 +493,7 @@ int dissect_image( * Kernel returns EBUSY if there's already a partition by that number or an overlapping * partition already existent. */ - r = ioctl_partition_add(fd, node, nr, (uint64_t) start * 512, (uint64_t) size * 512); + r = block_device_add_partition(fd, node, nr, (uint64_t) start * 512, (uint64_t) size * 512); if (r < 0) { if (r != -EBUSY) return log_debug_errno(r, "BLKPG_ADD_PARTITION failed: %m"); @@ -831,7 +776,7 @@ int dissect_image( if (!PARTITION_DESIGNATOR_VERSIONED(designator) || strverscmp_improved(m->partitions[designator].label, label) >= 0) { - r = ioctl_partition_remove(fd, node, nr); + r = block_device_remove_partition(fd, node, nr); if (r < 0) log_debug_errno(r, "BLKPG_DEL_PARTITION failed, ignoring: %m"); continue; @@ -908,7 +853,7 @@ int dissect_image( /* First one wins */ if (m->partitions[PARTITION_XBOOTLDR].found) { - r = ioctl_partition_remove(fd, node, nr); + r = block_device_remove_partition(fd, node, nr); if (r < 0) log_debug_errno(r, "BLKPG_DEL_PARTITION failed, ignoring: %m"); continue; -- 2.47.3