/* 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"
#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"
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));
+}
#include <valgrind/memcheck.h>
#endif
-#include <linux/blkpg.h>
#include <linux/dm-ioctl.h>
#include <linux/loop.h>
#include <sys/file.h>
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) {
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");
}
}
#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,
* 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");
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;
/* 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;