Re: "detect" ro devices automatically

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Till Maas wrote:

> currently cryptsetup tries to detect whether or not a device is readonly
> with the BLKROGET ioctl call. This does not work for cdroms under linux. I
> do not know why and whether or not this is intended. The attached patch
> changes this. It makes cryptsetup try to open the devide Read-Write. When
> this fails and the error is EROFS, then it is assumed that the device is
> readonly and cryptsetup tries to open it read-only and proceeds as normal.

I modified the patch to also try BLKROGET, because it seems that read-only
loop devices (created with losetup -r) may still be opened. With this patch
cryptsetup first tries the method described above and in case this does not
return, that the device is read-only, it also tries BLKROGET.

Regards,
Till
--- 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


---------------------------------------------------------------------
dm-crypt mailing list - http://www.saout.de/misc/dm-crypt/
To unsubscribe, e-mail: dm-crypt-unsubscribe@xxxxxxxx
For additional commands, e-mail: dm-crypt-help@xxxxxxxx

[Index of Archives]     [Device Mapper Devel]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Packaging]     [Fedora SELinux]     [Yosemite News]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux