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.

  • 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, 2011 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.
    – phemmer
    Jul 5, 2012 at 23:17

2 Answers 2


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
$ cat /sys/block/loop1/ro

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" ]

  • This works only for whole block devices like sda, e.g. not for single partitions. But there is /sys/class/block/sda1/ro to retrieve the ro state of such block devices like the sda partition. Jan 11, 2019 at 8:28

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .