]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add superficial test for partscan test 33057/head
authorLennart Poettering <lennart@poettering.net>
Tue, 28 May 2024 08:01:24 +0000 (10:01 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 28 May 2024 09:08:06 +0000 (11:08 +0200)
src/test/test-blockdev-util.c

index 134386c3f68f8062eda9a086f67bcfb5b2ae999c..19626e0b771d01df8f71af0c994df6b7b61b4556 100644 (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);