1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-04 21:47:31 +03:00

blockdev-util: make block_device_remove_all_partitions() take sd_device object

Then, it is not necessary to recreate sd_device object when we already
have.
This commit is contained in:
Yu Watanabe 2022-09-06 04:46:04 +09:00
parent cc5bae6cc2
commit 46c3a28845
3 changed files with 29 additions and 11 deletions

View File

@ -509,19 +509,27 @@ int block_device_resize_partition(
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)
@ -539,6 +547,14 @@ int block_device_remove_all_partitions(int fd) {
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;

View File

@ -3,6 +3,8 @@
#include <sys/types.h>
#include "sd-device.h"
#include "macro.h"
#include "stdio-util.h"
#include "string-util.h"
@ -34,4 +36,4 @@ 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);
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);

View File

@ -181,7 +181,7 @@ static int loop_configure(
/* 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;
@ -695,7 +695,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
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");