return RET_NERRNO(ioctl(fd, BLKPG, &ba));
}
-int block_device_remove_all_partitions(int fd) {
- struct stat stat;
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+int block_device_remove_all_partitions(sd_device *dev, int fd) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+ _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
+ _cleanup_close_ int fd_close = -1;
sd_device *part;
int r, k = 0;
- if (fstat(fd, &stat) < 0)
- return -errno;
+ assert(dev || fd >= 0);
- r = sd_device_new_from_devnum(&dev, 'b', stat.st_rdev);
- if (r < 0)
- return r;
+ if (!dev) {
+ struct stat st;
+
+ if (fstat(fd, &st) < 0)
+ return -errno;
+
+ r = sd_device_new_from_stat_rdev(&dev_unref, &st);
+ if (r < 0)
+ return r;
+
+ dev = dev_unref;
+ }
r = sd_device_enumerator_new(&e);
if (r < 0)
if (r < 0)
return r;
+ if (fd < 0) {
+ fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
+ if (fd_close < 0)
+ return fd_close;
+
+ fd = fd_close;
+ }
+
FOREACH_DEVICE(e, part) {
const char *v, *devname;
int nr;
#include <sys/types.h>
+#include "sd-device.h"
+
#include "macro.h"
#include "stdio-util.h"
#include "string-util.h"
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);
int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size);
-int block_device_remove_all_partitions(int fd);
+int block_device_remove_all_partitions(sd_device *dev, int fd);
/* Unbound but has children? Remove all partitions, and report this to the caller, to try
* again, and count this as an attempt. */
- r = block_device_remove_all_partitions(fd);
+ r = block_device_remove_all_partitions(dev, fd);
if (r < 0)
return r;
if (flock(d->fd, LOCK_EX) < 0)
log_debug_errno(errno, "Failed to lock loop block device, ignoring: %m");
- r = block_device_remove_all_partitions(d->fd);
+ r = block_device_remove_all_partitions(d->dev, d->fd);
if (r < 0)
log_debug_errno(r, "Failed to remove partitions of loopback block device, ignoring: %m");