Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On a CentOS6 system I have /dev/block/sr0 (read only) and /dev/block/sda (read/write). How can I find out programmatically which devices are read-only? There is a ro file in there, but it's '0' for both devices.

There are some hacks I could use (compare names ^sr vs ^sd, look for cdrom ownership of device node), but I'd like to have a generic solution.

share|improve this question
2  
I don't really think there is a way without attempting to write. The way I understand how this works is when it attempts to write the device itself is what rejects the command, not the kernel. So I think you'd have to test a write before you could be sure. There are some other cases where the device might be locked that you could probably test for with lsof but not the writeability. –  polynomial Oct 6 '11 at 3:10
    
@polynomial is correct. There are some cases where the kernel is aware a device is read-only, but the only 100% guaranteed way is to try and write. –  Patrick Jul 5 '12 at 23:17

2 Answers 2

How about look in /proc/mounts (or output of mount command).

my /dev/sr0 entry says ro,....
my /dev/sda1 entry says rw,....

Often your root partition will be mounted using the UUID, or, like in my case, the volume label. Have to be smart about that part, but the cdrom and additional drives are listed as /dev/...

Need to remember, you're looking for RO/RW status on the FILESYSTEM, not the DEVICE.

share|improve this answer

If /sys/block/xxx/ro is 1, then the kernel guarantees it's read-only

$ sudo losetup /dev/loop0 a
$ sudo losetup -r /dev/loop1 b
$ cat /sys/block/loop0/ro
0
$ cat /sys/block/loop1/ro
1

If it's not, then the kernel may let you try to write on it, but the writing may be blocked at a lower level.

For CDs/DVDs, you may find out the type of media (read-only, writable, rewritable, open session...), with cdrdao disk-info.

Now the device may be writable, but you may not have write permission to the device file, but that's another story and can be easily checked with [ -w "$dev" ]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.