--- cryptsetup-1.0.5.orig/lib/setup.c 2007-05-02 16:44:06.000000000 +0200 +++ cryptsetup-1.0.5/lib/setup.c 2007-08-17 19:18:31.000000000 +0200 @@ -318,11 +318,21 @@ char buf[128]; uint64_t size; unsigned long size_small; - int readonly; + int readonly = 0; int ret = -1; int fd; - fd = open(device, O_RDONLY); + /* Try to open read-write to check whether it is a read-only device */ + fd = open(device, O_RDWR); + if (fd < 0) { + if (errno == EROFS) { + readonly = 1; + fd = open(device, O_RDONLY); + } + } else { + close(fd); + fd = open(device, O_RDONLY); + } if (fd < 0) { set_error("Error opening device: %s", strerror_r(errno, buf, 128)); @@ -330,13 +340,19 @@ } #ifdef BLKROGET - if (ioctl(fd, BLKROGET, &readonly) < 0) { - set_error("BLKROGET failed on device: %s", - strerror_r(errno, buf, 128)); - return -1; + /* If the device can be opened read-write, i.e. readonly is still 0, then + * check whether BKROGET says that it is read-only. E.g. read-only loop + * devices may be openend read-write but are read-only according to BLKROGET + */ + if (readonly == 0) { + if (ioctl(fd, BLKROGET, &readonly) < 0) { + set_error("BLKROGET failed on device: %s", + strerror_r(errno, buf, 128)); + return -1; + } } #else -# error BLKROGET not available +#error BLKROGET not available #endif #ifdef BLKGETSIZE64