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"
35 static bool keep_sysupgrade
;
38 handle_rmdir(const char *dir
)
51 while ((dt
= readdir(d
)) != NULL
) {
52 if (fstatat(fd
, dt
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) || S_ISDIR(st
.st_mode
))
55 if (keep_sysupgrade
&& !strcmp(dt
->d_name
, "sysupgrade.tgz"))
58 unlinkat(fd
, dt
->d_name
, 0);
68 foreachdir(const char *dir
, int (*cb
)(const char*))
70 struct stat s
= { 0 };
75 if (dir
[strlen(dir
) - 1] == '/')
76 snprintf(globdir
, 256, "%s*", dir
);
78 snprintf(globdir
, 256, "%s/*", dir
); /**/
80 if (!glob(globdir
, GLOB_NOESCAPE
| GLOB_MARK
| GLOB_ONLYDIR
, NULL
, &gl
))
81 for (j
= 0; j
< gl
.gl_pathc
; j
++)
82 if (!stat(gl
.gl_pathv
[j
], &s
) && !S_ISLNK(s
.st_mode
))
83 foreachdir(gl
.gl_pathv
[j
], cb
);
89 overlay_delete(const char *dir
, bool _keep_sysupgrade
)
91 keep_sysupgrade
= _keep_sysupgrade
;
92 foreachdir(dir
, handle_rmdir
);
96 overlay_mount(struct volume
*v
, char *fs
)
98 if (mkdir("/tmp/overlay", 0755)) {
99 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno
));
103 if (mount(v
->blk
, "/tmp/overlay", fs
, MS_NOATIME
, NULL
)) {
104 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n", fs
, v
->blk
, strerror(errno
));
108 return volume_init(v
);
112 switch2jffs(struct volume
*v
)
117 if (!stat(SWITCH_JFFS2
, &s
)) {
118 ULOG_ERR("jffs2 switch already running\n");
122 creat("/tmp/.switch_jffs2", 0600);
123 ret
= mount(v
->blk
, "/rom/overlay", "jffs2", MS_NOATIME
, NULL
);
124 unlink("/tmp/.switch_jffs2");
126 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v
->blk
, strerror(errno
));
130 if (mount("none", "/", NULL
, MS_NOATIME
| MS_REMOUNT
, 0)) {
131 ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno
));
135 if (system("cp -a /tmp/root/* /rom/overlay")) {
136 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %s\n", strerror(errno
));
140 if (pivot("/rom", "/mnt")) {
141 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno
));
145 if (mount_move("/mnt", "/tmp/root", "")) {
146 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno
));
150 return fopivot("/overlay", "/rom");
154 handle_whiteout(const char *dir
)
159 struct dirent
**namelist
;
162 n
= scandir(dir
, &namelist
, NULL
, NULL
);
170 snprintf(file
, sizeof(file
), "%s%s", dir
, namelist
[n
]->d_name
);
171 if (!lstat(file
, &s
) && S_ISLNK(s
.st_mode
)) {
172 sz
= readlink(file
, link
, sizeof(link
) - 1);
177 orig
= strstr(&file
[1], "/");
178 if (orig
&& !strcmp(link
, "(overlay-whiteout)"))
190 jffs2_switch(struct volume
*v
)
195 if (find_overlay_mount("overlayfs:/tmp/root"))
198 if (find_filesystem("overlay")) {
199 ULOG_ERR("overlayfs not supported by kernel\n");
203 mp
= find_mount_point(v
->blk
, 0);
205 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
209 switch (volume_identify(v
)) {
211 ULOG_ERR("no jffs2 marker found\n");
215 ret
= switch2jffs(v
);
217 ULOG_INFO("performing overlay whiteout\n");
218 umount2("/tmp/root", MNT_DETACH
);
219 foreachdir("/overlay/", handle_whiteout
);
224 ret
= overlay_mount(v
, "jffs2");
227 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
228 ULOG_ERR("switching to jffs2 failed\n");
234 ret
= overlay_mount(v
, "ubifs");
237 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
238 ULOG_ERR("switching to ubifs failed\n");
248 fs_state_set("/overlay", FS_STATE_READY
);
252 static int overlay_mount_fs(struct volume
*v
)
256 if (mkdir("/tmp/overlay", 0755)) {
257 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno
));
263 switch (volume_identify(v
)) {
271 if (mount(v
->blk
, "/tmp/overlay", fstype
, MS_NOATIME
, NULL
)) {
272 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
273 fstype
, v
->blk
, strerror(errno
));
280 enum fs_state
fs_state_get(const char *dir
)
287 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
288 sprintf(path
, "%s/.fs_state", dir
);
289 len
= readlink(path
, valstr
, sizeof(valstr
) - 1);
291 return FS_STATE_UNKNOWN
;
296 if (val
> __FS_STATE_LAST
)
297 return FS_STATE_UNKNOWN
;
303 int fs_state_set(const char *dir
, enum fs_state state
)
308 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
309 sprintf(path
, "%s/.fs_state", dir
);
311 snprintf(valstr
, sizeof(valstr
), "%d", state
);
313 return symlink(valstr
, path
);
317 int mount_overlay(struct volume
*v
)
324 mp
= find_mount_point(v
->blk
, 0);
326 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
332 extroot_prefix
= "/tmp/overlay";
333 if (!mount_extroot()) {
334 ULOG_INFO("switched to extroot\n");
338 switch(fs_state_get("/tmp/overlay")) {
339 case FS_STATE_UNKNOWN
:
340 fs_state_set("/tmp/overlay", FS_STATE_PENDING
);
341 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING
) {
342 ULOG_ERR("unable to set filesystem state\n");
345 case FS_STATE_PENDING
:
346 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
347 overlay_delete("/tmp/overlay", true);
353 ULOG_INFO("switching to jffs2 overlay\n");
354 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
355 ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");