]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: Introduce block_device_add/remove_partition()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 27 Jul 2022 22:54:16 +0000 (00:54 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 3 Aug 2022 18:55:29 +0000 (20:55 +0200)
Extracted from dissect-image.c.

src/shared/blockdev-util.c
src/shared/blockdev-util.h
src/shared/dissect-image.c

index de72f8438265e426bcdb1f40ef8d7cdfb676fa82..b0ab5b440e37de25efee57e596c39aeabf8f5abe 100644 (file)
@@ -1,6 +1,8 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <linux/blkpg.h>
 #include <sys/file.h>
+#include <sys/ioctl.h>
 #include <unistd.h>
 
 #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));
+}
index e30692b509ad6ad71020ab5a549cb5951f0c6230..f7055625a36c57ed182050249bbf2ca3d437c934 100644 (file)
@@ -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);
index c7a336d4326e3ab518ec7355266d5e4b85350ee5..87712abfb3bcb005ee74972be2f4c12244b847b9 100644 (file)
@@ -4,7 +4,6 @@
 #include <valgrind/memcheck.h>
 #endif
 
-#include <linux/blkpg.h>
 #include <linux/dm-ioctl.h>
 #include <linux/loop.h>
 #include <sys/file.h>
@@ -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;