Get device flags by device name

hope you can help me: I am trying to determine if the device is removable or not, all I have is the name of the device (/ dev / sdc). In fact, I need to determine when a file is on removable media or on a local drive along the full path to this file.

I tried searching in current-> FS-> PWD and all I could find was a set of flags: * current-> FS-> pwd.mnt-> mnt_sb-> s_bdev-> bd_disk-> flags * where GENHD_FL_REMOVABLE is set for removable devices

But I always get the same flags (as I understand it, s_bdev always points to the same device (/ dev / sda)).

So now I get the name of the device (/ dev / sdc) that contains my file by analyzing mtab, but still I can not find whether to delete it or not.

Is there a way to get the block_device structure by device name? (for example, the structure of the β€œfile” can be obtained by calling fd = open ("name") fl = fged (fd) where fl indicates the structure of the "file")

+6
source share
2 answers

You can iterate over block devices with class_dev_iter_init and class_dev_iter_next . See the code in block/genhd.c blk_lookup_devt for use.

Once you have the device, you can use dev_to_disk to get a struct gendisk * in which you can check the switch flag.

+1
source

Read / sys / block / dev-name / removeable as it should contain 1 if the device is removable or 0 if not. (dev-name = device name: sda, hda, fd0, ...)

+1
source

Source: https://habr.com/ru/post/898478/


All Articles