Merge pull request #24475 from yuwata/devpath

sd-device: introduce two helper functions for devnum
This commit is contained in:
Luca Boccassi
2022-08-28 21:09:10 +01:00
committed by GitHub
24 changed files with 312 additions and 280 deletions
+20 -17
View File
@@ -57,6 +57,24 @@ int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) {
return 0;
}
int device_path_make_inaccessible(mode_t mode, char **ret) {
char *s;
assert(ret);
if (S_ISCHR(mode))
s = strdup("/run/systemd/inaccessible/chr");
else if (S_ISBLK(mode))
s = strdup("/run/systemd/inaccessible/blk");
else
return -ENODEV;
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
_cleanup_free_ char *p = NULL;
int r;
@@ -65,25 +83,10 @@ int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
assert(ret);
if (major(devnum) == 0 && minor(devnum) == 0) {
char *s;
if (major(devnum) == 0 && minor(devnum) == 0)
/* A special hack to make sure our 'inaccessible' device nodes work. They won't have symlinks in
* /dev/block/ and /dev/char/, hence we handle them specially here. */
if (S_ISCHR(mode))
s = strdup("/run/systemd/inaccessible/chr");
else if (S_ISBLK(mode))
s = strdup("/run/systemd/inaccessible/blk");
else
return -ENODEV;
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
return device_path_make_inaccessible(mode, ret);
r = device_path_make_major_minor(mode, devnum, &p);
if (r < 0)
+1
View File
@@ -29,6 +29,7 @@ int parse_devnum(const char *s, dev_t *ret);
})
int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret);
int device_path_make_inaccessible(mode_t mode, char **ret);
int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret);
int device_path_parse_major_minor(const char *path, mode_t *ret_mode, dev_t *ret_devnum);
+3 -7
View File
@@ -295,9 +295,8 @@ static int swap_verify(Swap *s) {
}
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);
@@ -305,16 +304,13 @@ static int swap_load_devnode(Swap *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);
}
+7 -12
View File
@@ -14,6 +14,8 @@
#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"
@@ -859,11 +861,9 @@ static int action_copy(DissectedImage *m, LoopDevice *d) {
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);
@@ -882,15 +882,10 @@ static int action_umount(const char *path) {
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);
r = devpath_from_devnum(S_IFBLK, devno, &devname);
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);
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)
+3 -8
View File
@@ -19,6 +19,7 @@
#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"
@@ -3121,20 +3122,14 @@ int home_resize_luks(
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);
+2
View File
@@ -121,6 +121,8 @@ libsystemd_sources = files(
'sd-bus/sd-bus.c',
'sd-device/device-enumerator-private.h',
'sd-device/device-enumerator.c',
'sd-device/device-filter.c',
'sd-device/device-filter.h',
'sd-device/device-internal.h',
'sd-device/device-monitor-private.h',
'sd-device/device-monitor.c',
@@ -7,6 +7,7 @@
#include "alloc-util.h"
#include "device-enumerator-private.h"
#include "device-filter.h"
#include "device-util.h"
#include "dirent-util.h"
#include "fd-util.h"
+115
View File
@@ -0,0 +1,115 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fnmatch.h>
#include "device-filter.h"
#include "path-util.h"
int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null) {
char **strv;
int r;
assert(match_strv);
assert(key);
strv = hashmap_get(*match_strv, key);
if (strv) {
if (!value) {
char **v;
if (strv_isempty(strv) || !clear_on_null)
return 0;
/* Accept all value. Clear previous assignment. */
v = new0(char*, 1);
if (!v)
return -ENOMEM;
strv_free_and_replace(strv, v);
} else {
if (strv_contains(strv, value))
return 0;
r = strv_extend(&strv, value);
if (r < 0)
return r;
}
r = hashmap_update(*match_strv, key, strv);
if (r < 0)
return r;
} else {
_cleanup_strv_free_ char **strv_alloc = NULL;
_cleanup_free_ char *key_alloc = NULL;
key_alloc = strdup(key);
if (!key_alloc)
return -ENOMEM;
strv_alloc = strv_new(value);
if (!strv_alloc)
return -ENOMEM;
r = hashmap_ensure_put(match_strv, &string_hash_ops_free_strv_free, key_alloc, strv_alloc);
if (r < 0)
return r;
TAKE_PTR(key_alloc);
TAKE_PTR(strv_alloc);
}
return 1;
}
static bool device_match_sysattr_value(sd_device *device, const char *sysattr, char * const *patterns) {
const char *value;
assert(device);
assert(sysattr);
if (sd_device_get_sysattr_value(device, sysattr, &value) < 0)
return false;
return strv_fnmatch_or_empty(patterns, value, 0);
}
bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr) {
char * const *patterns;
const char *sysattr;
assert(device);
HASHMAP_FOREACH_KEY(patterns, sysattr, match_sysattr)
if (!device_match_sysattr_value(device, sysattr, patterns))
return false;
HASHMAP_FOREACH_KEY(patterns, sysattr, nomatch_sysattr)
if (device_match_sysattr_value(device, sysattr, patterns))
return false;
return true;
}
bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent) {
const char *syspath_parent, *syspath;
assert(device);
if (sd_device_get_syspath(device, &syspath) < 0)
return false;
SET_FOREACH(syspath_parent, nomatch_parent)
if (path_startswith(syspath, syspath_parent))
return false;
if (set_isempty(match_parent))
return true;
SET_FOREACH(syspath_parent, match_parent)
if (path_startswith(syspath, syspath_parent))
return true;
return false;
}
+13
View File
@@ -0,0 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdbool.h>
#include "sd-device.h"
#include "hashmap.h"
#include "set.h"
int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null);
bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr);
bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent);
@@ -12,6 +12,7 @@
#include "MurmurHash2.h"
#include "alloc-util.h"
#include "device-filter.h"
#include "device-monitor-private.h"
#include "device-private.h"
#include "device-util.h"
@@ -10,6 +10,7 @@
#include "macro.h"
int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum);
int device_new_from_nulstr(sd_device **ret, char *nulstr, size_t len);
int device_new_from_strv(sd_device **ret, char **strv);
int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd);
+44 -94
View File
@@ -1,115 +1,65 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fnmatch.h>
#include "device-private.h"
#include "device-util.h"
#include "path-util.h"
#include "devnum-util.h"
#include "fd-util.h"
#include "string-util.h"
int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null) {
char **strv;
int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *s = NULL;
const char *devname;
int r;
assert(match_strv);
assert(key);
assert(ret);
strv = hashmap_get(*match_strv, key);
if (strv) {
if (!value) {
char **v;
if (major(devnum) == 0 && minor(devnum) == 0)
return device_path_make_inaccessible(mode, ret);
if (strv_isempty(strv) || !clear_on_null)
return 0;
r = device_new_from_mode_and_devnum(&dev, mode, devnum);
if (r < 0)
return r;
/* Accept all value. Clear previous assignment. */
r = sd_device_get_devname(dev, &devname);
if (r < 0)
return r;
v = new0(char*, 1);
if (!v)
return -ENOMEM;
s = strdup(devname);
if (!s)
return -ENOMEM;
strv_free_and_replace(strv, v);
} else {
if (strv_contains(strv, value))
return 0;
*ret = TAKE_PTR(s);
return 0;
}
r = strv_extend(&strv, value);
if (r < 0)
return r;
}
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int fd = -1;
int r;
r = hashmap_update(*match_strv, key, strv);
r = device_new_from_mode_and_devnum(&dev, mode, devnum);
if (r < 0)
return r;
fd = sd_device_open(dev, flags);
if (fd < 0)
return fd;
if (ret) {
const char *devname;
char *s;
r = sd_device_get_devname(dev, &devname);
if (r < 0)
return r;
} else {
_cleanup_strv_free_ char **strv_alloc = NULL;
_cleanup_free_ char *key_alloc = NULL;
key_alloc = strdup(key);
if (!key_alloc)
s = strdup(devname);
if (!s)
return -ENOMEM;
strv_alloc = strv_new(value);
if (!strv_alloc)
return -ENOMEM;
r = hashmap_ensure_put(match_strv, &string_hash_ops_free_strv_free, key_alloc, strv_alloc);
if (r < 0)
return r;
TAKE_PTR(key_alloc);
TAKE_PTR(strv_alloc);
*ret = s;
}
return 1;
}
static bool device_match_sysattr_value(sd_device *device, const char *sysattr, char * const *patterns) {
const char *value;
assert(device);
assert(sysattr);
if (sd_device_get_sysattr_value(device, sysattr, &value) < 0)
return false;
return strv_fnmatch_or_empty(patterns, value, 0);
}
bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr) {
char * const *patterns;
const char *sysattr;
assert(device);
HASHMAP_FOREACH_KEY(patterns, sysattr, match_sysattr)
if (!device_match_sysattr_value(device, sysattr, patterns))
return false;
HASHMAP_FOREACH_KEY(patterns, sysattr, nomatch_sysattr)
if (device_match_sysattr_value(device, sysattr, patterns))
return false;
return true;
}
bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent) {
const char *syspath_parent, *syspath;
assert(device);
if (sd_device_get_syspath(device, &syspath) < 0)
return false;
SET_FOREACH(syspath_parent, nomatch_parent)
if (path_startswith(syspath, syspath_parent))
return false;
if (set_isempty(match_parent))
return true;
SET_FOREACH(syspath_parent, match_parent)
if (path_startswith(syspath, syspath_parent))
return true;
return false;
return TAKE_FD(fd);
}
+3 -5
View File
@@ -2,13 +2,12 @@
#pragma once
#include <stdbool.h>
#include <sys/types.h>
#include "sd-device.h"
#include "hashmap.h"
#include "log.h"
#include "macro.h"
#include "set.h"
#define device_unref_and_replace(a, b) \
unref_and_replace_full(a, b, sd_device_ref, sd_device_unref)
@@ -82,6 +81,5 @@
#define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__)
#define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__)
int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null);
bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr);
bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent);
int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret);
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret);
+12 -5
View File
@@ -279,7 +279,7 @@ _public_ int sd_device_new_from_syspath(sd_device **ret, const char *syspath) {
return device_new_from_syspath(ret, syspath, /* strict = */ true);
}
static int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) {
int device_new_from_mode_and_devnum(sd_device **ret, mode_t mode, dev_t devnum) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *syspath = NULL;
const char *t, *subsystem;
@@ -2422,13 +2422,20 @@ _public_ int sd_device_open(sd_device *device, int flags) {
if (FLAGS_SET(flags, O_PATH))
return TAKE_FD(fd);
r = device_get_property_bool(device, "ID_IGNORE_DISKSEQ");
if (r < 0 && r != -ENOENT)
/* If the device is not initialized, then we cannot determine if we should check diskseq through
* ID_IGNORE_DISKSEQ property. Let's skip to check diskseq in that case. */
r = sd_device_get_is_initialized(device);
if (r < 0)
return r;
if (r <= 0) {
r = sd_device_get_diskseq(device, &diskseq);
if (r > 0) {
r = device_get_property_bool(device, "ID_IGNORE_DISKSEQ");
if (r < 0 && r != -ENOENT)
return r;
if (r <= 0) {
r = sd_device_get_diskseq(device, &diskseq);
if (r < 0 && r != -ENOENT)
return r;
}
}
fd2 = open(FORMAT_PROC_FD_PATH(fd), flags);
+31
View File
@@ -14,6 +14,7 @@
#include "nulstr-util.h"
#include "path-util.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "string-util.h"
#include "tests.h"
#include "time-util.h"
@@ -499,4 +500,34 @@ TEST(sd_device_new_from_path) {
}
}
static void test_devpath_from_devnum_one(const char *path) {
_cleanup_free_ char *resolved = NULL;
struct stat st;
log_debug("> %s", path);
if (stat(path, &st) < 0) {
assert_se(errno == ENOENT);
log_notice("Path %s not found, skipping test", path);
return;
}
assert_se(devpath_from_devnum(st.st_mode, st.st_rdev, &resolved) >= 0);
assert_se(path_equal(path, resolved));
}
TEST(devpath_from_devnum) {
test_devpath_from_devnum_one("/dev/null");
test_devpath_from_devnum_one("/dev/zero");
test_devpath_from_devnum_one("/dev/full");
test_devpath_from_devnum_one("/dev/random");
test_devpath_from_devnum_one("/dev/urandom");
test_devpath_from_devnum_one("/dev/tty");
if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
test_devpath_from_devnum_one("/run/systemd/inaccessible/chr");
test_devpath_from_devnum_one("/run/systemd/inaccessible/blk");
}
}
DEFINE_TEST_MAIN(LOG_INFO);
+2 -1
View File
@@ -8,6 +8,7 @@
#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"
@@ -899,7 +900,7 @@ static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags
}
/* 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 {
+12 -46
View File
@@ -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_devpath(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_devpath(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_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)
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_devpath(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);
+16 -48
View File
@@ -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_devpath(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);
+1 -6
View File
@@ -221,7 +221,6 @@ int get_block_device_harder(const char *path, dev_t *ret) {
}
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;
@@ -232,14 +231,10 @@ int lock_whole_block_device(dev_t devt, int operation) {
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;
+16 -19
View File
@@ -45,9 +45,9 @@ static int verify_esp_blkid(
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);
@@ -151,21 +151,20 @@ static int verify_esp_udev(
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");
@@ -509,10 +508,10 @@ static int verify_xbootldr_blkid(
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);
@@ -583,19 +582,17 @@ static int verify_xbootldr_udev(
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);
if (r < 0)
return log_error_errno(r, "Failed to format block device path for %u:%u: %m",
major(devid), minor(devid));
r = sd_device_new_from_devnum(&d, 'b', devid);
if (r < 0)
return log_error_errno(r, "%s: Failed to get block device: %m", node);
return log_error_errno(r, "Failed to get block device for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid));
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_PART_ENTRY_SCHEME", &type);
if (r < 0)
+2 -6
View File
@@ -806,7 +806,7 @@ int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) {
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;
@@ -865,14 +865,10 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
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,
+2 -2
View File
@@ -5,7 +5,7 @@
#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) {
@@ -15,7 +15,7 @@ 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;
+2 -2
View File
@@ -7,7 +7,7 @@
#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"
@@ -608,7 +608,7 @@ int resource_resolve_path(
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;
+2 -2
View File
@@ -7,7 +7,7 @@
#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"
@@ -321,7 +321,7 @@ int lock_main(int argc, char *argv[], void *userdata) {
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");