Merge pull request #33057 from poettering/partscan-no-part

blockdev-util: for partition block devices partition scanning is always off
This commit is contained in:
Luca Boccassi
2024-05-28 16:39:15 +02:00
committed by GitHub
2 changed files with 42 additions and 0 deletions

View File

@@ -418,6 +418,11 @@ int blockdev_partscan_enabled(int fd) {
if (r != -ENOENT)
return r;
/* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
* partitions. */
if (device_is_devtype(dev, "partition"))
return false;
/* For loopback block device, especially for v5.19 or newer. Even if this is enabled, we also need to
* check GENHD_FL_NO_PART flag through 'ext_range' and 'capability' sysfs attributes below. */
if (device_get_sysattr_bool(dev, "loop/partscan") == 0)

View File

@@ -1,7 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "blockdev-util.h"
#include "device-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "tests.h"
static void test_path_is_encrypted_one(const char *p, int expect) {
@@ -38,4 +40,39 @@ TEST(path_is_encrypted) {
test_path_is_encrypted_one("/dev", booted > 0 ? false : -1);
}
TEST(partscan_enabled) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
int r;
assert_se(sd_device_enumerator_new(&e) >= 0);
assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
assert_se(sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true) >= 0);
FOREACH_DEVICE(e, dev) {
_cleanup_close_ int fd = -EBADF;
const char *name;
r = sd_device_get_devname(dev, &name);
if (r < 0) {
log_warning_errno(r, "Found block device without a name, skipping.");
continue;
}
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0) {
log_warning_errno(fd, "Found block device '%s' which we cannot open, skipping: %m", name);
continue;
}
r = blockdev_partscan_enabled(fd);
if (r < 0) {
log_warning_errno(r, "Failed to determine if block device '%s' has partition scanning enabled, skipping: %m", name);
continue;
}
log_info("%s has partition scanning enabled: %s", name, yes_no(r));
}
}
DEFINE_TEST_MAIN(LOG_INFO);