2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <sys/types.h>
16 #include <sys/mount.h>
18 #include <asm/byteorder.h>
30 #include "libfstools.h"
33 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
36 foreachdir(const char *dir, int (*cb)(const char*))
42 if (dir[strlen(dir) - 1] == '/')
43 snprintf(globdir, 256, "%s*", dir);
45 snprintf(globdir, 256, "%s/*", dir); /**/
47 if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
48 for (j = 0; j < gl.gl_pathc; j++)
49 foreachdir(gl.gl_pathv[j], cb);
55 overlay_mount(struct volume *v, char *fs)
57 if (mkdir("/tmp/overlay", 0755)) {
58 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
62 if (mount(v->blk, "/tmp/overlay", fs, MS_NOATIME, NULL)) {
63 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n", fs, v->blk, strerror(errno));
67 return volume_init(v);
71 switch2jffs(struct volume *v)
76 if (!stat(SWITCH_JFFS2, &s)) {
77 ULOG_ERR("jffs2 switch already running\n");
81 creat("/tmp/.switch_jffs2", 0600);
82 ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
83 unlink("/tmp/.switch_jffs2");
85 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v->blk, strerror(errno));
89 if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
90 ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno));
94 system("cp -a /tmp/root/* /rom/overlay"); /**/
96 if (pivot("/rom", "/mnt")) {
97 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno));
101 if (mount_move("/mnt", "/tmp/root", "")) {
102 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
106 return fopivot("/overlay", "/rom");
110 handle_whiteout(const char *dir)
115 struct dirent **namelist;
118 n = scandir(dir, &namelist, NULL, NULL);
126 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
127 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
128 sz = readlink(file, link, sizeof(link) - 1);
133 orig = strstr(&file[1], "/");
134 if (orig && !strcmp(link, "(overlay-whiteout)"))
146 jffs2_switch(struct volume *v)
151 if (find_overlay_mount("overlayfs:/tmp/root"))
154 if (find_filesystem("overlay")) {
155 ULOG_ERR("overlayfs not supported by kernel\n");
159 mp = find_mount_point(v->blk, 0);
161 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
165 switch (volume_identify(v)) {
167 ULOG_ERR("no jffs2 marker found\n");
171 ret = switch2jffs(v);
173 ULOG_INFO("performing overlay whiteout\n");
174 umount2("/tmp/root", MNT_DETACH);
175 foreachdir("/overlay/", handle_whiteout);
180 ret = overlay_mount(v, "jffs2");
183 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
184 ULOG_ERR("switching to jffs2 failed\n");
190 ret = overlay_mount(v, "ubifs");
193 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
194 ULOG_ERR("switching to ubifs failed\n");
202 static int overlay_mount_fs(struct volume *v)
206 if (mkdir("/tmp/overlay", 0755)) {
207 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
213 switch (volume_identify(v)) {
219 if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
220 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
221 fstype, v->blk, strerror(errno));
230 int mount_overlay(struct volume *v)
237 mp = find_mount_point(v->blk, 0);
239 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
245 extroot_prefix = "/tmp/overlay";
246 if (!mount_extroot()) {
247 ULOG_INFO("switched to extroot\n");
251 ULOG_INFO("switching to jffs2 overlay\n");
252 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
253 ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");