jffs2reset: use jffs2_mark if rootfs_data isn't mounted
[project/fstools.git] / libfstools / extroot.c
index 2ed9b37..418df94 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <libgen.h>
 
-#include "../fs-state.h"
+#include "libfstools.h"
 
 char const *extroot_prefix = NULL;
 
-static int mount_extroot(void)
+/*
+ * This will execute "block extroot" and make use of mounted extroot or return
+ * an error.
+ */
+int mount_extroot(void)
 {
+       char ldlib_path[32];
        char block_path[32];
        char kmod_loader[64];
        struct stat s;
@@ -34,13 +40,33 @@ static int mount_extroot(void)
        if (!extroot_prefix)
                return -1;
 
-       sprintf(block_path, "%s/sbin/block", extroot_prefix);
+       /* try finding the library directory */
+       snprintf(ldlib_path, sizeof(ldlib_path), "%s/upper/lib", extroot_prefix);
+
+       if (stat(ldlib_path, &s) || !S_ISDIR(s.st_mode))
+               snprintf(ldlib_path, sizeof(ldlib_path), "%s/lib", extroot_prefix);
+
+       /* try finding the block executable */
+       snprintf(block_path, sizeof(block_path), "%s/upper/sbin/block", extroot_prefix);
 
-       if (stat(block_path, &s))
+       if (stat(block_path, &s) || !S_ISREG(s.st_mode))
+               snprintf(block_path, sizeof(block_path), "%s/sbin/block", extroot_prefix);
+
+       if (stat(block_path, &s) || !S_ISREG(s.st_mode))
+               snprintf(block_path, sizeof(block_path), "/sbin/block");
+
+       if (stat(block_path, &s) || !S_ISREG(s.st_mode))
                return -1;
 
-       sprintf(kmod_loader, "/sbin/kmodloader %s/etc/modules-boot.d/ %s", extroot_prefix, extroot_prefix);
-       system(kmod_loader);
+       /* set LD_LIBRARY_PATH env var and load kmods from overlay if we found a lib directory there */
+       if (!stat(ldlib_path, &s) && S_ISDIR(s.st_mode)) {
+               ULOG_INFO("loading kmods from internal overlay\n");
+               setenv("LD_LIBRARY_PATH", ldlib_path, 1);
+               snprintf(kmod_loader, sizeof(kmod_loader),
+                        "/sbin/kmodloader %s/etc/modules-boot.d/", dirname(ldlib_path));
+               if (system(kmod_loader))
+                       ULOG_ERR("failed to launch kmodloader from internal overlay\n");
+       }
 
        pid = fork();
        if (!pid) {
@@ -62,10 +88,10 @@ static int mount_extroot(void)
                                mkdir("/tmp/extroot/mnt/rom", 0755);
 
                                if (mount_move("/tmp/extroot", "", "/mnt")) {
-                                       fprintf(stderr, "moving pivotroot failed - continue normal boot\n");
+                                       ULOG_ERR("moving pivotroot failed - continue normal boot\n");
                                        umount("/tmp/extroot/mnt");
                                } else if (pivot("/mnt", "/rom")) {
-                                       fprintf(stderr, "switching to pivotroot failed - continue normal boot\n");
+                                       ULOG_ERR("switching to pivotroot failed - continue normal boot\n");
                                        umount("/mnt");
                                } else {
                                        umount("/tmp/overlay");
@@ -76,10 +102,10 @@ static int mount_extroot(void)
                                }
                        } else if (find_mount("/tmp/extroot/overlay")) {
                                if (mount_move("/tmp/extroot", "", "/overlay")) {
-                                       fprintf(stderr, "moving extroot failed - continue normal boot\n");
+                                       ULOG_ERR("moving extroot failed - continue normal boot\n");
                                        umount("/tmp/extroot/overlay");
                                } else if (fopivot("/overlay", "/rom")) {
-                                       fprintf(stderr, "switching to extroot failed - continue normal boot\n");
+                                       ULOG_ERR("switching to extroot failed - continue normal boot\n");
                                        umount("/overlay");
                                } else {
                                        umount("/tmp/overlay");
@@ -93,9 +119,3 @@ static int mount_extroot(void)
        }
        return -1;
 }
-
-static struct backend extroot_backend = {
-       .name = "extroot",
-       .mount = mount_extroot,
-};
-BACKEND(extroot_backend);