block: support builtin fstab config
[project/fstools.git] / libfstools / overlay.c
index 85237e4..9e830a1 100644 (file)
@@ -27,7 +27,7 @@
 #include <dirent.h>
 #include <fcntl.h>
 
-#include "../fs-state.h"
+#include "libfstools.h"
 #include "volume.h"
 
 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
@@ -142,22 +142,9 @@ handle_whiteout(const char *dir)
        return 0;
 }
 
-static int
-ask_user(int argc, char **argv)
-{
-       if ((argc < 2) || strcmp(argv[1], "-y")) {
-               fprintf(stderr, "This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
-               if (getchar() != 'y')
-                       return -1;
-       }
-       return 0;
-
-}
-
 int
-jffs2_switch(int argc, char **argv)
+jffs2_switch(struct volume *v)
 {
-       struct volume *v;
        char *mp;
        int ret = -1;
 
@@ -169,8 +156,7 @@ jffs2_switch(int argc, char **argv)
                return ret;
        }
 
-       v = volume_find("rootfs_data");
-       mp = find_mount_point(v->blk, NULL);
+       mp = find_mount_point(v->blk, 0);
        if (mp) {
                fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
                return -1;
@@ -182,7 +168,7 @@ jffs2_switch(int argc, char **argv)
                /* fall through */
 
        case FS_DEADCODE:
-               ret = switch2jffs();
+               ret = switch2jffs(v);
                if (!ret) {
                        fprintf(stderr, "doing fo cleanup\n");
                        umount2("/tmp/root", MNT_DETACH);
@@ -199,29 +185,40 @@ jffs2_switch(int argc, char **argv)
                        ret = -1;
                }
                break;
-       }
 
+       case FS_UBIFS:
+               ret = overlay_mount(v, "ubifs");
+               if (ret)
+                       break;
+               if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
+                       fprintf(stderr, "switching to ubifs failed\n");
+                       ret = -1;
+               }
+               break;
+       }
        return ret;
 }
 
-static int overlay_mount_fs(void)
+static int overlay_mount_fs(struct volume *v)
 {
-       struct volume *v;
+       char *fstype;
 
        if (mkdir("/tmp/overlay", 0755)) {
                fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
                return -1;
        }
 
-       v = volume_find("rootfs_data");
-       if (!v) {
-               fprintf(stderr, "rootfs_data does not exist\n");
-               return -1;
+       fstype = "jffs2";
+
+       switch (volume_identify(v)) {
+       case FS_UBIFS:
+               fstype = "ubifs";
+               break;
        }
 
-       if (mount(v->blk, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
-               fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n",
-                               v->blk, strerror(errno));
+       if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
+               fprintf(stderr, "failed to mount -t %s %s /tmp/overlay: %s\n",
+                               fstype, v->blk, strerror(errno));
                return -1;
        }
 
@@ -230,29 +227,28 @@ static int overlay_mount_fs(void)
        return -1;
 }
 
-static int overlay_mount(void)
+int mount_overlay(struct volume *v)
 {
-       struct volume *v = volume_find("rootfs_data");;
        char *mp;
 
        if (!v)
                return -1;
 
-       mp = find_mount_point(v->blk, NULL);
+       mp = find_mount_point(v->blk, 0);
        if (mp) {
                fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
                return -1;
        }
 
-       overlay_mount_fs();
+       overlay_mount_fs(v);
 
        extroot_prefix = "/tmp/overlay";
-       if (!backend_mount("extroot")) {
-               fprintf(stderr, "fs-state: switched to extroot\n");
+       if (!mount_extroot()) {
+               fprintf(stderr, "switched to extroot\n");
                return 0;
        }
 
-       fprintf(stderr, "switching to jffs2\n");
+       fprintf(stderr, "switching to overlay\n");
        if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
                fprintf(stderr, "switching to jffs2 failed - fallback to ramoverlay\n");
                return ramoverlay();
@@ -260,9 +256,3 @@ static int overlay_mount(void)
 
        return -1;
 }
-
-static struct backend overlay_backend = {
-       .name = "overlay",
-       .mount = overlay_mount,
-};
-BACKEND(overlay_backend);