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.
Additionally, mounting an XFS filesystem as read-write in such a case
will cause the call to mount to hang indefinitely. Avoid this generally
by explicitly unmounting and (re)mounting when mounting read-write.
Signed-off-by: Samuel Mendoza-Jonas <sam.mj@au1.ibm.com>
+static const char *fs_parameters(unsigned int rw_flags, const char *fstype)
+{
+ if ((rw_flags | MS_RDONLY) != 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;
static bool check_existing_mount(struct discover_device *dev)
{
struct stat devstat, mntstat;
+ /* 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);
dev->mount_path = join_paths(dev, mount_base(),
dev->device_path);
pb_log("mounting device %s read-only\n", dev->device_path);
errno = 0;
rc = mount(dev->device_path, dev->mount_path, fstype,
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;
if (!rc) {
dev->mounted = true;
dev->mounted_rw = false;
int device_request_write(struct discover_device *dev, bool *release)
{
int device_request_write(struct discover_device *dev, bool *release)
{
int rc;
*release = false;
int rc;
*release = false;
if (dev->mounted_rw)
return 0;
if (dev->mounted_rw)
return 0;
+ fstype = discover_device_get_param(dev, "ID_FS_TYPE");
+
pb_log("remounting device %s read-write\n", dev->device_path);
pb_log("remounting device %s read-write\n", dev->device_path);
- rc = mount(dev->device_path, dev->mount_path, "",
- MS_REMOUNT | MS_SILENT, "");
- if (rc)
+
+ rc = umount(dev->mount_path);
+ if (rc) {
+ pb_log("Failed to unmount %s\n", dev->mount_path);
+ }
+ rc = mount(dev->device_path, dev->mount_path, fstype,
+ MS_SILENT,
+ fs_parameters(MS_REMOUNT, fstype));
+ if (rc)
+ goto mount_ro;
dev->mounted_rw = true;
*release = true;
return 0;
dev->mounted_rw = true;
*release = true;
return 0;
+
+mount_ro:
+ pb_log("Unable to remount device %s read-write\n", dev->device_path);
+ rc = mount(dev->device_path, dev->mount_path, fstype,
+ MS_RDONLY | MS_SILENT,
+ fs_parameters(MS_RDONLY, fstype));
+ if (rc)
+ pb_log("Unable to recover mount for %s\n", dev->device_path);
+ return -1;
}
void device_release_write(struct discover_device *dev, bool release)
{
}
void device_release_write(struct discover_device *dev, bool release)
{
+ fstype = discover_device_get_param(dev, "ID_FS_TYPE");
+
pb_log("remounting device %s read-only\n", dev->device_path);
mount(dev->device_path, dev->mount_path, "",
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, fstype));
dev->mounted_rw = false;
}
dev->mounted_rw = false;
}