summaryrefslogtreecommitdiff
path: root/src/partition
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-08-28 05:56:25 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2022-08-28 10:10:50 +0900
commitca8228295ec20e561119729a2cf117e4531d7f56 (patch)
treed31f8189834a0f126b68dcdb976b66bd4ffdbd23 /src/partition
parent42899bcee5a7ed08640e0f958fd9b48e5b21eb3d (diff)
downloadsystemd-ca8228295ec20e561119729a2cf117e4531d7f56.tar.gz
tree-wide: use devpath_from_devnum() and device_open_from_devnum()
Fixes #24465.
Diffstat (limited to 'src/partition')
-rw-r--r--src/partition/growfs.c58
-rw-r--r--src/partition/repart.c64
2 files changed, 28 insertions, 94 deletions
diff --git a/src/partition/growfs.c b/src/partition/growfs.c
index e5f570f0d3..a2c95453d4 100644
--- a/src/partition/growfs.c
+++ b/src/partition/growfs.c
@@ -34,8 +34,7 @@ static bool arg_dry_run = false;
#if HAVE_LIBCRYPTSETUP
static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
- _cleanup_(sd_device_unrefp) sd_device *main_dev = NULL, *dev = NULL;
- const char *devpath, *main_devpath;
+ _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
_cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
_cleanup_close_ int main_devfd = -1;
uint64_t size;
@@ -45,33 +44,21 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_
if (r < 0)
return log_error_errno(r, "Cannot resize LUKS device: %m");
- r = sd_device_new_from_devnum(&main_dev, 'b', main_devno);
+ main_devfd = r = device_open_from_devnum(S_IFBLK, main_devno, O_RDONLY|O_CLOEXEC, &main_devpath);
if (r < 0)
- return log_error_errno(r, "Failed to create main sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+ return log_error_errno(r, "Failed to open main block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(main_devno));
- r = sd_device_get_devname(main_dev, &main_devpath);
- if (r < 0)
- return log_device_error_errno(main_dev, r, "Failed to get main devpath: %m");
-
- main_devfd = sd_device_open(main_dev, O_RDONLY|O_CLOEXEC);
- if (main_devfd < 0)
- return log_device_error_errno(main_dev, main_devfd, "Failed to open block device \"%s\": %m",
- main_devpath);
-
if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m",
main_devpath);
log_debug("%s is %"PRIu64" bytes", main_devpath, size);
- r = sd_device_new_from_devnum(&dev, 'b', devno);
- if (r < 0)
- return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
- DEVNUM_FORMAT_VAL(devno));
- r = sd_device_get_devname(dev, &devpath);
+ r = devpath_from_devnum(S_IFBLK, devno, &devpath);
if (r < 0)
- return log_device_error_errno(dev, r, "Failed to get devpath: %m");
+ return log_error_errno(r, "Failed to get devpath of " DEVNUM_FORMAT_STR ": %m",
+ DEVNUM_FORMAT_VAL(devno));
r = sym_crypt_init(&cd, devpath);
if (r < 0)
@@ -105,9 +92,7 @@ static int maybe_resize_underlying_device(
const char *mountpath,
dev_t main_devno) {
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
- _cleanup_free_ char *fstype = NULL;
- const char *devpath;
+ _cleanup_free_ char *devpath = NULL, *fstype = NULL;
dev_t devno;
int r;
@@ -132,15 +117,11 @@ static int maybe_resize_underlying_device(
if (devno == main_devno)
return 0;
- r = sd_device_new_from_devnum(&dev, 'b', devno);
+ r = devpath_from_devnum(S_IFBLK, devno, &devpath);
if (r < 0)
- return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+ return log_error_errno(r, "Failed to get devpath for block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
- r = sd_device_get_devname(dev, &devpath);
- if (r < 0)
- return log_device_error_errno(dev, r, "Failed to get devpath: %m");
-
r = probe_filesystem(devpath, &fstype);
if (r == -EUCLEAN)
return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath);
@@ -224,11 +205,9 @@ static int parse_argv(int argc, char *argv[]) {
}
static int run(int argc, char *argv[]) {
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int mountfd = -1, devfd = -1;
- const char *devpath;
+ _cleanup_free_ char *devpath = NULL;
uint64_t size, newsize;
- struct stat st;
dev_t devno;
int r;
@@ -260,24 +239,11 @@ static int run(int argc, char *argv[]) {
if (r < 0)
log_warning_errno(r, "Unable to resize underlying device of \"%s\", proceeding anyway: %m", arg_target);
- r = sd_device_new_from_devnum(&dev, 'b', devno);
+ devfd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC, &devpath);
if (r < 0)
- return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+ return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
- r = sd_device_get_devname(dev, &devpath);
- if (r < 0)
- return log_device_error_errno(dev, r, "Failed to get devpath: %m");
-
- devfd = sd_device_open(dev, O_RDONLY|O_CLOEXEC);
- if (devfd < 0)
- return log_device_error_errno(dev, devfd, "Failed to open block device \"%s\": %m", devpath);
-
- if (fstat(devfd, &st) < 0)
- return log_error_errno(r, "Failed to stat() device %s: %m", devpath);
- if (!S_ISBLK(st.st_mode))
- return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Backing device of file system is not a block device, refusing.");
-
if (ioctl(devfd, BLKGETSIZE64, &size) != 0)
return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath);
diff --git a/src/partition/repart.c b/src/partition/repart.c
index 588aaa6132..815e81052b 100644
--- a/src/partition/repart.c
+++ b/src/partition/repart.c
@@ -24,6 +24,7 @@
#include "conf-parser.h"
#include "cryptsetup-util.h"
#include "def.h"
+#include "device-util.h"
#include "devnum-util.h"
#include "dirent-util.h"
#include "efivars.h"
@@ -3673,9 +3674,9 @@ static int resolve_copy_blocks_auto_candidate(
sd_id128_t *ret_uuid) {
_cleanup_(blkid_free_probep) blkid_probe b = NULL;
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int fd = -1;
- const char *pttype, *t, *p;
+ _cleanup_free_ char *p = NULL;
+ const char *pttype, *t;
sd_id128_t pt_parsed, u;
blkid_partition pp;
dev_t whole_devno;
@@ -3702,19 +3703,10 @@ static int resolve_copy_blocks_auto_candidate(
major(partition_devno), minor(partition_devno),
major(restrict_devno), minor(restrict_devno));
- r = sd_device_new_from_devnum(&dev, 'b', whole_devno);
+ fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
if (r < 0)
- return log_error_errno(r, "Failed to create sd-device for block device %u:%u: %m",
- major(whole_devno), minor(whole_devno));
-
- r = sd_device_get_devname(dev, &p);
- if (r < 0)
- return log_error_errno(r, "Failed to get name of block device %u:%u: %m",
- major(whole_devno), minor(whole_devno));
-
- fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
- if (fd < 0)
- return log_error_errno(fd, "Failed to open block device %s: %m", p);
+ return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
+ DEVNUM_FORMAT_VAL(whole_devno));
b = blkid_new_probe();
if (!b)
@@ -3996,29 +3988,16 @@ static int context_open_copy_block_paths(
"Copying from block device node is not permitted in --image=/--root= mode, refusing.");
} else if (p->copy_blocks_auto) {
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
- const char *devname;
dev_t devno;
r = resolve_copy_blocks_auto(p->type_uuid, root, restrict_devno, &devno, &uuid);
if (r < 0)
return r;
- r = sd_device_new_from_devnum(&dev, 'b', devno);
+ source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
if (r < 0)
- return log_error_errno(r, "Failed to create sd-device object for device %u:%u: %m", major(devno), minor(devno));
-
- r = sd_device_get_devname(dev, &devname);
- if (r < 0)
- return log_error_errno(r, "Failed to get device name of %u:%u: %m", major(devno), minor(devno));
-
- opened = strdup(devname);
- if (!opened)
- return log_oom();
-
- source_fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
- if (source_fd < 0)
- return log_error_errno(source_fd, "Failed to open automatically determined source block copy device '%s': %m", opened);
+ return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
+ DEVNUM_FORMAT_VAL(devno));
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
@@ -4026,38 +4005,27 @@ static int context_open_copy_block_paths(
continue;
if (S_ISDIR(st.st_mode)) {
- _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
- const char *bdev;
+ _cleanup_free_ char *bdev = NULL;
+ dev_t devt;
/* If the file is a directory, automatically find the backing block device */
if (major(st.st_dev) != 0)
- r = sd_device_new_from_devnum(&dev, 'b', st.st_dev);
+ devt = st.st_dev;
else {
- dev_t devt;
-
/* Special support for btrfs */
-
r = btrfs_get_block_device_fd(source_fd, &devt);
if (r == -EUCLEAN)
return btrfs_log_dev_root(LOG_ERR, r, opened);
if (r < 0)
return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
-
- r = sd_device_new_from_devnum(&dev, 'b', devt);
}
- if (r < 0)
- return log_error_errno(r, "Failed to create sd-device object for block device backing '%s': %m", opened);
-
- r = sd_device_get_devname(dev, &bdev);
- if (r < 0)
- return log_error_errno(r, "Failed to get device name for block device backing '%s': %m", opened);
safe_close(source_fd);
- source_fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
- if (source_fd < 0)
- return log_error_errno(source_fd, "Failed to open block device '%s': %m", bdev);
+ source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
+ if (r < 0)
+ return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
@@ -4548,7 +4516,7 @@ static int acquire_root_devno(
if (r < 0)
log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
- r = device_path_make_canonical(S_IFBLK, devno, ret);
+ r = devpath_from_devnum(S_IFBLK, devno, ret);
if (r < 0)
return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);