[LEDE-DEV] fstools: add middle layer (original root overlay) to overlayfs when pivot the /overlay to the USB disk.

Message ID ac39635e-ce8f-43a4-ba25-d3b897bfb1a0@akosowski.local
State New
Headers show
Series
  • [LEDE-DEV] fstools: add middle layer (original root overlay) to overlayfs when pivot the /overlay to the USB disk.
Related show

Commit Message

Andrzej Lossowsk May 15, 2018, 9:02 a.m.
Currently after pivot to the USB disk (extroot),
configuration and data from original root overlay are gone.
Still accessible, but all previous configuration steps must be repeated
(or data from original root overlay must be copied to new USB disk overlay).

The idea is to "merge" original root overlay with overlay from the USB disk.
Using overlayfs multiple lower layers it can be done.
All data from original root overlay will be accessible (as readonly layer) after pivot to the USB disk.

Original root overlay (for exmaple /dev/mtdblock3) will be mounted to /overlay/internal (as readonly)
Upper directory from original root overlay (/dev/mtdblock3/upper) will be the top (readonly layer),
then "/" will be the bottom layer (readonly layer), an extroot still will be upper (writable layer).

Signed-off-by: Andrzej Lossowski <alossek@wp.pl>
---
 libfstools/extroot.c    | 73 ++++++++++++++++++++++++++++++++++++++++++++++++-
 libfstools/libfstools.h |  3 +-
 libfstools/mount.c      | 21 +++++++++-----
 libfstools/overlay.c    |  6 ++--
 libfstools/snapshot.c   |  2 +-
 5 files changed, 92 insertions(+), 13 deletions(-)

Patch

diff --git a/libfstools/extroot.c b/libfstools/extroot.c
index 418df94..d7590eb 100644
--- a/libfstools/extroot.c
+++ b/libfstools/extroot.c
@@ -22,6 +22,7 @@ 
 #include <libgen.h>
 
 #include "libfstools.h"
+#include "volume.h"
 
 char const *extroot_prefix = NULL;
 
@@ -104,7 +105,7 @@  int mount_extroot(void)
 				if (mount_move("/tmp/extroot", "", "/overlay")) {
 					ULOG_ERR("moving extroot failed - continue normal boot\n");
 					umount("/tmp/extroot/overlay");
-				} else if (fopivot("/overlay", "/rom")) {
+				} else if (fopivot_multi("/overlay", "/rom")) {
 					ULOG_ERR("switching to extroot failed - continue normal boot\n");
 					umount("/overlay");
 				} else {
@@ -119,3 +120,73 @@  int mount_extroot(void)
 	}
 	return -1;
 }
+
+static int mount_internal_overlay()
+{
+	struct volume *v = volume_find("rootfs_data");
+	char *mp;
+	char *fstype;
+
+	if (!v)
+		return -1;
+
+	volume_init(v);
+	mp = find_mount_point(v->blk, 0);
+	if (mp) {
+		ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
+		return -1;
+	}
+
+	switch (volume_identify(v)) {
+		case FS_EXT4:
+			fstype = "ext4";
+			break;
+		case FS_F2FS:
+			fstype = "f2fs";
+			break;
+		case FS_UBIFS:
+			fstype = "ubifs";
+			break;
+		case FS_JFFS2:
+		default:
+			fstype = "jffs2";
+			break;
+	}
+
+	if (mkdir("/tmp/overlay", 0755)) {
+		ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
+		return -1;
+	}
+
+	if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME | MS_RDONLY, NULL)) {
+		ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n", fstype, v->blk);
+		return -1;
+	}
+
+	return 0;
+}
+
+int fopivot_multi(char *rw_root, char *ro_root)
+{
+	if (mount_internal_overlay()) {
+		ULOG_ERR("mounting /tmp/overlay failed\n");
+		return -1;
+	}
+
+	if (mkdir("/overlay/internal", 0755)) {
+		ULOG_ERR("failed to mkdir /overlay/internal: %m\n");
+		return -1;
+	} 
+	
+	if (mount_move("/tmp/overlay", "/overlay/internal", "")) {
+		umount("/tmp/overlay");
+		return -1;
+	}
+	
+	if (fopivot(rw_root, ro_root, "/overlay/internal/upper")) {
+		umount("/overlay/internal");
+		return -1;
+	}
+	
+	return 0;
+}
\ No newline at end of file
diff --git a/libfstools/libfstools.h b/libfstools/libfstools.h
index f27307a..8554f4c 100644
--- a/libfstools/libfstools.h
+++ b/libfstools/libfstools.h
@@ -45,7 +45,8 @@  extern int mount_overlay(struct volume *v);
 
 extern int mount_move(const char *oldroot, const char *newroot, const char *dir);
 extern int pivot(char *new, char *old);
-extern int fopivot(char *rw_root, char *ro_root);
+extern int fopivot(char *rw_root, char *ro_lowerlevel0, char *ro_lowerlevel1);
+extern int fopivot_multi(char *rw_root, char *ro_root);
 extern int ramoverlay(void);
 
 extern int find_overlay_mount(char *overlay);
diff --git a/libfstools/mount.c b/libfstools/mount.c
index c7f2789..619ac20 100644
--- a/libfstools/mount.c
+++ b/libfstools/mount.c
@@ -89,12 +89,13 @@  pivot(char *new, char *old)
  * fopivot - switch to overlay using passed dir as upper one
  *
  * @rw_root: writable directory that will be used as upper dir
- * @ro_root: directory where old root will be put
+ * @ro_root: directory where old root will be put 
+ * @ro_lowerlevel1: directory where internal overlay will be put
  */
 int
-fopivot(char *rw_root, char *ro_root)
+fopivot(char *rw_root, char *ro_root, char *ro_lowerlevel1)
 {
-	char overlay[64], mount_options[64];
+	char overlay[64], mount_options[128];
 
 	if (find_filesystem("overlay")) {
 		ULOG_ERR("BUG: no suitable fs found\n");
@@ -110,15 +111,21 @@  fopivot(char *rw_root, char *ro_root)
 	 */
 	snprintf(mount_options, sizeof(mount_options), "lowerdir=/,upperdir=%s", rw_root);
 	if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, mount_options)) {
-		char upperdir[64], workdir[64], upgrade[64], upgrade_dest[64];
+		char upperdir[64], workdir[64], upgrade[64], upgrade_dest[64], lowerdir[64];
 		struct stat st;
 
 		snprintf(upperdir, sizeof(upperdir), "%s/upper", rw_root);
 		snprintf(workdir, sizeof(workdir), "%s/work", rw_root);
 		snprintf(upgrade, sizeof(upgrade), "%s/sysupgrade.tgz", rw_root);
 		snprintf(upgrade_dest, sizeof(upgrade_dest), "%s/sysupgrade.tgz", upperdir);
-		snprintf(mount_options, sizeof(mount_options), "lowerdir=/,upperdir=%s,workdir=%s",
-			 upperdir, workdir);
+
+		if (ro_lowerlevel1)
+			snprintf(lowerdir, sizeof(lowerdir), "%s:/", ro_lowerlevel1);
+		else
+			snprintf(lowerdir, sizeof(lowerdir), "/");
+
+		snprintf(mount_options, sizeof(mount_options), "lowerdir=%s,upperdir=%s,workdir=%s",
+			lowerdir, upperdir, workdir);
 
 		/*
 		 * Overlay FS v23 and later requires both a upper and
@@ -154,5 +161,5 @@  ramoverlay(void)
 	mkdir("/tmp/root", 0755);
 	mount("tmpfs", "/tmp/root", "tmpfs", MS_NOATIME, "mode=0755");
 
-	return fopivot("/tmp/root", "/rom");
+	return fopivot("/tmp/root", "/rom", NULL);
 }
diff --git a/libfstools/overlay.c b/libfstools/overlay.c
index ebc43f7..10444e1 100644
--- a/libfstools/overlay.c
+++ b/libfstools/overlay.c
@@ -205,7 +205,7 @@  switch2jffs(struct volume *v)
 		return -1;
 	}
 
-	ret = fopivot("/overlay", "/rom");
+	ret = fopivot("/overlay", "/rom", NULL);
 
 	/*
 	 * Besides copying overlay data from "tmpfs" to "jffs2" we should also
@@ -320,7 +320,7 @@  jffs2_switch(struct volume *v)
 	case FS_UBIFS:
 		if (overlay_mount(v, fs_name))
 			return -1;
-		if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
+		if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom", NULL)) {
 			ULOG_ERR("switching to %s failed\n", fs_name);
 			return -1;
 		}
@@ -428,7 +428,7 @@  int mount_overlay(struct volume *v)
 
 	fs_name = overlay_fs_name(volume_identify(v));
 	ULOG_INFO("switching to %s overlay\n", fs_name);
-	if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
+	if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom", NULL)) {
 		ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name);
 		return ramoverlay();
 	}
diff --git a/libfstools/snapshot.c b/libfstools/snapshot.c
index 4870cf7..c22cdc0 100644
--- a/libfstools/snapshot.c
+++ b/libfstools/snapshot.c
@@ -320,7 +320,7 @@  static int
 _ramoverlay(char *rom, char *overlay)
 {
 	mount("tmpfs", overlay, "tmpfs", MS_NOATIME, "mode=0755");
-	return fopivot(overlay, rom);
+	return fopivot(overlay, rom, NULL);
 }
 
 int