[PATCH] discover: Mount journaled filesystems with norecovery

Samuel Mendoza-Jonas sam.mj at au1.ibm.com
Mon Mar 23 16:59:33 AEDT 2015


Journaled filesytems may still write to their disk even if the disk is
mounted read only. Petitboot should avoid modifying any disks
automatically, and in mixed-endian systems this can also cause journal
operations to fail. Use the 'norecovery' option on filesystems that
support it to skip the journal replay.

Signed-off-by: Samuel Mendoza-Jonas <sam.mj at au1.ibm.com>
---
 discover/device-handler.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/discover/device-handler.c b/discover/device-handler.c
index 5d9f988..8820178 100644
--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -1082,6 +1082,20 @@ static void device_handler_reinit_sources(struct device_handler *handler)
 			handler->dry_run);
 }
 
+static char *fs_parameters(unsigned int rw_flags, const char *fstype)
+{
+	if (rw_flags != MS_RDONLY)
+		return "";
+
+	/* Avoid writing back to the disk on journaled filesystems */
+	if (!strncmp(fstype, "ext4", strlen("ext4")))
+		return "norecovery";
+	if (!strncmp(fstype, "xfs", strlen("xfs")))
+		return "norecovery";
+
+	return "";
+}
+
 static bool check_existing_mount(struct discover_device *dev)
 {
 	struct stat devstat, mntstat;
@@ -1155,6 +1169,13 @@ static int mount_device(struct discover_device *dev)
 	if (!fstype)
 		return 0;
 
+	/* ext3 treats the norecovery option as an error, so mount the device
+	 * as an ext4 filesystem instead */
+	if (!strncmp(fstype, "ext3", strlen("ext3"))) {
+		pb_debug("Mounting ext3 filesystem as ext4\n");
+		fstype = talloc_asprintf(dev, "ext4");
+	}
+
 	dev->mount_path = join_paths(dev, mount_base(),
 					dev->device_path);
 
@@ -1167,7 +1188,8 @@ static int mount_device(struct discover_device *dev)
 	pb_log("mounting device %s read-only\n", dev->device_path);
 	errno = 0;
 	rc = mount(dev->device_path, dev->mount_path, fstype,
-			MS_RDONLY | MS_SILENT, "");
+			MS_RDONLY | MS_SILENT,
+			fs_parameters(MS_RDONLY, fstype));
 	if (!rc) {
 		dev->mounted = true;
 		dev->mounted_rw = false;
@@ -1221,7 +1243,8 @@ int device_request_write(struct discover_device *dev, bool *release)
 
 	pb_log("remounting device %s read-write\n", dev->device_path);
 	rc = mount(dev->device_path, dev->mount_path, "",
-			MS_REMOUNT | MS_SILENT, "");
+			MS_REMOUNT | MS_SILENT,
+			fs_parameters(MS_RDONLY, ""));
 	if (rc)
 		return -1;
 
@@ -1237,7 +1260,8 @@ void device_release_write(struct discover_device *dev, bool release)
 
 	pb_log("remounting device %s read-only\n", dev->device_path);
 	mount(dev->device_path, dev->mount_path, "",
-			MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
+			MS_REMOUNT | MS_RDONLY | MS_SILENT,
+			fs_parameters(MS_RDONLY, ""));
 	dev->mounted_rw = false;
 }
 
-- 
2.1.0



More information about the Petitboot mailing list