开发者

get device flags by device name

开发者 https://www.devze.com 2023-04-10 05:52 出处:网络
hope you can help me: I\'m trying to determine whether the device is removable or not, all i have is device name (/dev/sdc). Actually, I need to determine when the file on removable media or on local

hope you can help me: I'm trying to determine whether the device is removable or not, all i have is device name (/dev/sdc). Actually, I need to determine when the file on removable media or on local disk by full path of this file.

I've tryed to search in the current->fs->pwd and all I could find is a set of flags here: *current->fs->pwd.mnt->mnt_sb->s_bdev->bd_disk->flags* where GENHD_FL_RE开发者_运维知识库MOVABLE set for removable devices

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

So now i get the device name (/dev/sdc) that contains my file by parsing mtab, but still can't find out, removable it or not.

Is there possible way to get block_device structure by device name? (for example, "file" structure may be obtained by calling fd = open("name") fl = fged(fd) where fl points to "file" structure)


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

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消