#include "chase-symlinks.h"
#include "devnum-util.h"
-#include "fs-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "string-util.h"
return 0;
}
-int device_path_make_major_minor_sysfs(mode_t mode, dev_t devnum, char **ret) {
- _cleanup_free_ char *syspath = NULL, *link = NULL, *fname = NULL;
- _cleanup_free_ char *devpath = NULL;
- const char *t;
- int r;
-
- /* Generates the /dev/... path given a dev_t. What makes this different
- * from device_path_make_major_minor is that it works even when udev
- * hasn't yet run */
-
- if (S_ISCHR(mode))
- t = "char";
- else if (S_ISBLK(mode))
- t = "block";
- else
- return -ENODEV;
-
- if (asprintf(&syspath, "/sys/dev/%s/" DEVNUM_FORMAT_STR, t, DEVNUM_FORMAT_VAL(devnum)) < 0)
- return -ENOMEM;
-
- r = readlink_malloc(syspath, &link);
- if (r < 0)
- return r;
-
- r = path_extract_filename(link, &fname);
- if (r < 0)
- return r;
-
- devpath = path_join("/dev", fname);
- if (!devpath)
- return -ENOMEM;
-
- struct stat st;
- if (stat(devpath, &st) < 0)
- return -errno;
-
- if (((st.st_mode ^ mode) & S_IFMT) != 0)
- return S_ISBLK(mode) ? -ENOTBLK : -ENODEV;
-
- if (st.st_rdev != devnum)
- return -ENXIO;
-
- *ret = TAKE_PTR(devpath);
-
- return 0;
-}
-
int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
_cleanup_free_ char *p = NULL;
int r;
#include <sys/types.h>
#include <sys/vfs.h>
+#include "sd-device.h"
+
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "cryptsetup-util.h"
#include "device-nodes.h"
+#include "device-util.h"
#include "devnum-util.h"
#include "dissect-image.h"
#include "escape.h"
#if HAVE_LIBCRYPTSETUP
static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
- _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
+ _cleanup_(sd_device_unrefp) sd_device *main_dev = NULL, *dev = NULL;
+ const char *devpath, *main_devpath;
_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 = device_path_make_major_minor_sysfs(S_IFBLK, main_devno, &main_devpath);
+ r = sd_device_new_from_devnum(&main_dev, 'b', main_devno);
+ if (r < 0)
+ return log_error_errno(r, "Failed to create main sd-device for block device " DEVNUM_FORMAT_STR ": %m",
+ DEVNUM_FORMAT_VAL(main_devno));
+
+ r = sd_device_get_devpath(main_dev, &main_devpath);
if (r < 0)
- return log_error_errno(r, "Failed to format device major/minor path: %m");
+ return log_device_error_errno(main_dev, r, "Failed to get main devpath: %m");
- main_devfd = open(main_devpath, O_RDONLY|O_CLOEXEC);
+ main_devfd = sd_device_open(main_dev, O_RDONLY|O_CLOEXEC);
if (main_devfd < 0)
- return log_error_errno(errno, "Failed to open \"%s\": %m", main_devpath);
+ 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 = device_path_make_major_minor_sysfs(S_IFBLK, devno, &devpath);
+ 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_devpath(dev, &devpath);
if (r < 0)
- return log_error_errno(r, "Failed to format major/minor path: %m");
+ return log_device_error_errno(dev, r, "Failed to get devpath: %m");
r = sym_crypt_init(&cd, devpath);
if (r < 0)
const char *mountpath,
dev_t main_devno) {
- _cleanup_free_ char *fstype = NULL, *devpath = NULL;
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+ _cleanup_free_ char *fstype = NULL;
+ const char *devpath;
dev_t devno;
int r;
if (devno == 0)
return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" not backed by block device.", arg_target);
- log_debug("Underlying device %d:%d, main dev %d:%d, %s",
- major(devno), minor(devno),
- major(main_devno), minor(main_devno),
+ log_debug("Underlying device " DEVNUM_FORMAT_STR ", main dev " DEVNUM_FORMAT_STR ", %s",
+ DEVNUM_FORMAT_VAL(devno),
+ DEVNUM_FORMAT_VAL(main_devno),
devno == main_devno ? "same" : "different");
if (devno == main_devno)
return 0;
- r = device_path_make_major_minor_sysfs(S_IFBLK, devno, &devpath);
+ r = sd_device_new_from_devnum(&dev, 'b', devno);
if (r < 0)
- return log_error_errno(r, "Failed to format device major/minor path: %m");
+ 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_devpath(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)
}
static int run(int argc, char *argv[]) {
+ _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int mountfd = -1, devfd = -1;
- _cleanup_free_ char *devpath = NULL;
+ const char *devpath;
uint64_t size, newsize;
struct stat st;
dev_t devno;
if (r < 0)
log_warning_errno(r, "Unable to resize underlying device of \"%s\", proceeding anyway: %m", arg_target);
- r = device_path_make_major_minor_sysfs(S_IFBLK, devno, &devpath);
+ 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_devpath(dev, &devpath);
if (r < 0)
- return log_error_errno(r, "Failed to format device major/minor path: %m");
+ return log_device_error_errno(dev, r, "Failed to get devpath: %m");
- devfd = open(devpath, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ devfd = sd_device_open(dev, O_RDONLY|O_CLOEXEC);
if (devfd < 0)
- return log_error_errno(errno, "Failed to open \"%s\": %m", devpath);
+ 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);