Fixes #24465.
}
static int swap_load_devnode(Swap *s) {
- _cleanup_(sd_device_unrefp) sd_device *d = NULL;
+ _cleanup_free_ char *p = NULL;
struct stat st;
- const char *p;
int r;
assert(s);
if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
return 0;
- r = sd_device_new_from_stat_rdev(&d, &st);
+ r = devpath_from_devnum(S_IFBLK, st.st_rdev, &p);
if (r < 0) {
log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
- "Failed to allocate device for swap %s: %m", s->what);
+ "Failed to get device node for swap %s: %m", s->what);
return 0;
}
- if (sd_device_get_devname(d, &p) < 0)
- return 0;
-
return swap_set_devnode(s, p);
}
#include "blockdev-util.h"
#include "chase-symlinks.h"
#include "copy.h"
+#include "device-util.h"
+#include "devnum-util.h"
#include "dissect-image.h"
#include "env-util.h"
#include "fd-util.h"
static int action_umount(const char *path) {
_cleanup_close_ int fd = -1;
- _cleanup_free_ char *canonical = NULL;
- dev_t devno;
- const char *devname;
+ _cleanup_free_ char *canonical = NULL, *devname = NULL;
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
- _cleanup_(sd_device_unrefp) sd_device *device = NULL;
+ dev_t devno;
int r, k;
fd = chase_symlinks_and_open(path, NULL, 0, O_DIRECTORY, &canonical);
if (r < 0)
return log_error_errno(r, "Failed to find backing block device for '%s': %m", canonical);
- r = sd_device_new_from_devnum(&device, 'b', devno);
- if (r < 0)
- return log_error_errno(r, "Failed to create sd-device object for block device %u:%u: %m",
- major(devno), minor(devno));
-
- r = sd_device_get_devname(device, &devname);
+ r = devpath_from_devnum(S_IFBLK, devno, &devname);
if (r < 0)
- return log_error_errno(r, "Failed to get devname of block device %u:%u: %m",
- major(devno), minor(devno));
+ return log_error_errno(r, "Failed to get devname of block device " DEVNUM_FORMAT_STR ": %m",
+ DEVNUM_FORMAT_VAL(devno));
r = loop_device_open(devname, 0, &d);
if (r < 0)
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "chattr-util.h"
+#include "device-util.h"
#include "devnum-util.h"
#include "dm-util.h"
#include "env-util.h"
log_info("Operating on partition device %s, using parent device.", ip);
- r = device_path_make_major_minor(st.st_mode, parent, &whole_disk);
+ opened_image_fd = r = device_open_from_devnum(st.st_mode, parent, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK, &whole_disk);
if (r < 0)
- return log_error_errno(r, "Failed to derive whole disk path for %s: %m", ip);
-
- opened_image_fd = open(whole_disk, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
- if (opened_image_fd < 0)
- return log_error_errno(errno, "Failed to open whole block device %s: %m", whole_disk);
+ return log_error_errno(r, "Failed to open whole block device for %s: %m", ip);
image_fd = opened_image_fd;
if (fstat(image_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat whole block device %s: %m", whole_disk);
- if (!S_ISBLK(st.st_mode))
- return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Whole block device %s is not actually a block device, refusing.", whole_disk);
} else
log_info("Operating on whole block device %s.", ip);
#include "bus-util.h"
#include "cap-list.h"
#include "cpu-set-util.h"
+#include "device-util.h"
#include "devnum-util.h"
#include "env-util.h"
#include "format-util.h"
}
/* Suppress a couple of implicit device nodes */
- r = device_path_make_canonical(node->mode, makedev(node->major, node->minor), &path);
+ r = devpath_from_devnum(node->mode, makedev(node->major, node->minor), &path);
if (r < 0)
json_log(e, flags|JSON_DEBUG, 0, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor);
else {
#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;
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)
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;
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);
}
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;
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);
#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"
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;
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)
"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);
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);
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);
}
int lock_whole_block_device(dev_t devt, int operation) {
- _cleanup_free_ char *whole_node = NULL;
_cleanup_close_ int lock_fd = -1;
dev_t whole_devt;
int r;
if (r < 0)
return r;
- r = device_path_make_major_minor(S_IFBLK, whole_devt, &whole_node);
+ lock_fd = r = device_open_from_devnum(S_IFBLK, whole_devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, NULL);
if (r < 0)
return r;
- lock_fd = open(whole_node, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
- if (lock_fd < 0)
- return -errno;
-
if (flock(lock_fd, operation) < 0)
return -errno;
const char *v;
int r;
- r = device_path_make_major_minor(S_IFBLK, devid, &node);
+ r = devpath_from_devnum(S_IFBLK, devid, &node);
if (r < 0)
- return log_error_errno(r, "Failed to format major/minor device path: %m");
+ return log_error_errno(r, "Failed to get device path for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid));
errno = 0;
b = blkid_new_probe_from_filename(node);
sd_id128_t *ret_uuid) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
- _cleanup_free_ char *node = NULL;
sd_id128_t uuid = SD_ID128_NULL;
uint64_t pstart = 0, psize = 0;
uint32_t part = 0;
- const char *v;
+ const char *node, *v;
int r;
- r = device_path_make_major_minor(S_IFBLK, devid, &node);
- if (r < 0)
- return log_error_errno(r, "Failed to format major/minor device path: %m");
-
r = sd_device_new_from_devnum(&d, 'b', devid);
if (r < 0)
return log_error_errno(r, "Failed to get device from device number: %m");
+ r = sd_device_get_devname(d, &node);
+ if (r < 0)
+ return log_error_errno(r, "Failed to get device node: %m");
+
r = sd_device_get_property_value(d, "ID_FS_TYPE", &v);
if (r < 0)
return log_error_errno(r, "Failed to get device property: %m");
const char *type, *v;
int r;
- r = device_path_make_major_minor(S_IFBLK, devid, &node);
+ r = devpath_from_devnum(S_IFBLK, devid, &node);
if (r < 0)
- return log_error_errno(r, "Failed to format block device path for %u:%u: %m",
- major(devid), minor(devid));
+ return log_error_errno(r, "Failed to get block device path for " DEVNUM_FORMAT_STR ": %m",
+ DEVNUM_FORMAT_VAL(devid));
errno = 0;
b = blkid_new_probe_from_filename(node);
sd_id128_t *ret_uuid) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
- _cleanup_free_ char *node = NULL;
sd_id128_t uuid = SD_ID128_NULL;
- const char *type, *v;
+ const char *node, *type, *v;
int r;
- r = device_path_make_major_minor(S_IFBLK, devid, &node);
+ r = sd_device_new_from_devnum(&d, 'b', devid);
if (r < 0)
- return log_error_errno(r, "Failed to format block device path for %u:%u: %m",
- major(devid), minor(devid));
+ return log_error_errno(r, "Failed to get block device for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid));
- r = sd_device_new_from_devnum(&d, 'b', devid);
+ r = sd_device_get_devname(d, &node);
if (r < 0)
- return log_error_errno(r, "%s: Failed to get block device: %m", node);
+ return log_error_errno(r, "Failed to get device node: %m");
r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &type);
if (r < 0)
static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
char sysfs[STRLEN("/sys/dev/block/:/partition") + 2*DECIMAL_STR_MAX(dev_t) + 1];
- _cleanup_free_ char *whole = NULL, *buffer = NULL;
+ _cleanup_free_ char *buffer = NULL;
uint64_t current_offset, current_size, partno;
_cleanup_close_ int whole_fd = -1;
struct stat st;
if (r < 0)
return r;
- r = device_path_make_major_minor(S_IFBLK, devno, &whole);
+ whole_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL);
if (r < 0)
return r;
- whole_fd = open(whole, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
- if (whole_fd < 0)
- return -errno;
-
struct blkpg_partition bp = {
.pno = partno,
.start = offset == UINT64_MAX ? current_offset : offset,
#include "alloc-util.h"
#include "blockdev-util.h"
-#include "devnum-util.h"
+#include "device-util.h"
#include "quota-util.h"
int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr) {
/* Like quotactl() but takes a dev_t instead of a path to a device node, and fixes caddr_t → void*,
* like we should, today */
- r = device_path_make_major_minor(S_IFBLK, devnum, &devnode);
+ r = devpath_from_devnum(S_IFBLK, devnum, &devnode);
if (r < 0)
return r;
#include "alloc-util.h"
#include "blockdev-util.h"
#include "chase-symlinks.h"
-#include "devnum-util.h"
+#include "device-util.h"
#include "dirent-util.h"
#include "env-util.h"
#include "fd-util.h"
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"File system is not placed on a partition block device, cannot determine whole block device backing root file system.");
- r = device_path_make_canonical(S_IFBLK, d, &p);
+ r = devpath_from_devnum(S_IFBLK, d, &p);
if (r < 0)
return r;
#include "blockdev-util.h"
#include "btrfs-util.h"
-#include "devnum-util.h"
+#include "device-util.h"
#include "fd-util.h"
#include "fdset.h"
#include "main-func.h"
for (size_t i = 0; i < n_devnos; i++) {
_cleanup_free_ char *node = NULL;
- r = device_path_make_canonical(S_IFBLK, devnos[i], &node);
+ r = devpath_from_devnum(S_IFBLK, devnos[i], &node);
if (r < 0)
return log_error_errno(r, "Failed to format block device path: %m");