[OpenWrt-Devel] [PATCH] fstools: add a hook before mounting the overlay

Alin Nastac alin.nastac at gmail.com
Wed Oct 9 08:41:37 EDT 2019


Scripts located in the directory /lib/mount_root will be executed
before mounting the overlay.

Signed-off-by: Alin Nastac <alin.nastac at gmail.com>
---
 libfstools/hook.h    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libfstools/overlay.c |  3 ++-
 2 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 libfstools/hook.h

diff --git a/libfstools/hook.h b/libfstools/hook.h
new file mode 100644
index 0000000..76ee9d0
--- /dev/null
+++ b/libfstools/hook.h
@@ -0,0 +1,51 @@
+#ifndef _HOOK_H
+#define _HOOK_H
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+static inline int hook_execute(const char *path)
+{
+	DIR *dir;
+	struct dirent *dent;
+	char script[256];
+	pid_t pid;
+
+	ULOG_INFO("executing script in %s\n", path);
+
+	if ((dir = opendir(path)) == NULL) {
+		ULOG_INFO("cannot open %s (%s)\n", path, strerror(errno));
+		return 0;
+	}
+
+	while ((dent = readdir(dir)) != NULL) {
+		struct stat st;
+		int wstatus;
+
+		snprintf(script, sizeof(script), "%s/%s", path, dent->d_name);
+		if (stat(script, &st))
+			continue;
+		if (!S_ISREG(st.st_mode))
+			continue;
+		ULOG_INFO("%s\n", script);
+		pid = fork();
+		if (!pid) {
+			char *cmd[] = {script, NULL};
+
+			execvp(cmd[0], cmd);
+			ULOG_ERR("Failed to execute %s\n", script);
+			exit(-1);
+		}
+		if (pid <= 0) {
+			ULOG_INFO("Failed to fork() for %s\n", script);
+			continue;
+		}
+		waitpid(pid, &wstatus, 0);
+	}
+
+	closedir(dir);
+
+	return 0;
+}
+
+#endif
diff --git a/libfstools/overlay.c b/libfstools/overlay.c
index 14214a3..10a16b5 100644
--- a/libfstools/overlay.c
+++ b/libfstools/overlay.c
@@ -29,6 +29,7 @@
 
 #include "libfstools.h"
 #include "volume.h"
+#include "hook.h"
 
 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
 
@@ -439,7 +440,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") || hook_execute("/lib/mount_root") || fopivot("/overlay", "/rom")) {
 		ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name);
 		return ramoverlay();
 	}
-- 
2.7.4


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list