]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/sysfs: add function blkdev_is_removable
authorThomas Weißschuh <thomas@t-8ch.de>
Wed, 11 Jan 2023 18:13:50 +0000 (18:13 +0000)
committerKarel Zak <kzak@redhat.com>
Tue, 24 Jan 2023 16:26:15 +0000 (17:26 +0100)
This exactly represents the kernels notion of a "removable" blockdevice.
That is, the device itself is persistent but can contain some sort of
medium that can be changed at runtime.
Specifically it is *not* hotpluggable.

include/sysfs.h
lib/sysfs.c

index ad6b609f2a4c57192dcceb058a94c64c3a4cea74..8d77f757983df1619e45ab17c70bb9490c93f0d1 100644 (file)
@@ -87,6 +87,7 @@ char *sysfs_blkdev_get_devchain(struct path_cxt *pc, char *buf, size_t bufsz);
 int sysfs_blkdev_next_subsystem(struct path_cxt *pc __attribute__((unused)), char *devchain, char **subsys);
 
 int sysfs_blkdev_is_hotpluggable(struct path_cxt *pc);
+int sysfs_blkdev_is_removable(struct path_cxt *pc);
 int sysfs_blkdev_get_wholedisk( struct path_cxt *pc,
                                 char *diskname,
                                 size_t len,
index 92a7e290b082e88a26596c4eb5fd05cff645e6b4..f3253595607305ffde16abc6c1c7c606ae224296 100644 (file)
@@ -465,7 +465,7 @@ int sysfs_blkdev_is_hotpluggable(struct path_cxt *pc)
        int rc = 0;
 
 
-       /* check /sys/dev/block/<maj>:<min>/removable attribute */
+       /* check /sys/devices/.../removable attribute */
        if (ul_path_read_s32(pc, &rc, "removable") == 0 && rc == 1)
                return 1;
 
@@ -483,6 +483,17 @@ int sysfs_blkdev_is_hotpluggable(struct path_cxt *pc)
        return rc;
 }
 
+int sysfs_blkdev_is_removable(struct path_cxt *pc)
+{
+       int rc = 0;
+
+       /* check /sys/dev/block/<maj>:<min>/removable attribute */
+       if (ul_path_read_s32(pc, &rc, "removable") == 0)
+               return rc;
+
+       return 0;
+}
+
 static int get_dm_wholedisk(struct path_cxt *pc, char *diskname,
                 size_t len, dev_t *diskdevno)
 {
@@ -1182,6 +1193,7 @@ int main(int argc, char *argv[])
        }
 
        printf(" HOTPLUG: %s\n", sysfs_blkdev_is_hotpluggable(pc) ? "yes" : "no");
+       printf(" REMOVABLE: %s\n", sysfs_blkdev_is_removable(pc) ? "yes" : "no");
        printf(" SLAVES: %d\n", ul_path_count_dirents(pc, "slaves"));
 
        if (!is_part) {