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 static char *globdir
= NULL
;
71 static size_t globdirlen
= 0;
72 struct stat s
= { 0 };
73 size_t dirlen
= strlen(dir
);
77 if (dirlen
+ sizeof("/*") > globdirlen
) {
78 /* Alloc extra 256 B to avoid too many reallocs */
79 size_t len
= dirlen
+ sizeof("/*") + 256;
82 tmp
= realloc(globdir
, len
);
89 sprintf(globdir
, "%s/*", dir
);
91 /* Include GLOB_MARK as callbacks expect a trailing slash */
92 if (!glob(globdir
, GLOB_NOESCAPE
| GLOB_MARK
| GLOB_ONLYDIR
, NULL
, &gl
))
93 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
94 char *dir
= gl
.gl_pathv
[j
];
95 int len
= strlen(gl
.gl_pathv
[j
]);
98 /* Quick way of skipping files */
99 if (dir
[len
- 1] != '/')
102 /* lstat needs path without a trailing slash */
105 err
= lstat(gl
.gl_pathv
[j
], &s
);
109 if (!err
&& !S_ISLNK(s
.st_mode
))
110 foreachdir(gl
.gl_pathv
[j
], cb
);
115 static void foreach_mount(int (*cb
)(const char *, const char *))
117 FILE *fp
= fopen("/proc/mounts", "r");
118 static char line
[256];
123 while (fgets(line
, sizeof(line
), fp
)) {
124 char device
[32], mount_point
[32];
126 if (sscanf(line
, "%31s %31s %*s %*s %*u %*u", device
, mount_point
) == 2)
127 cb(device
, mount_point
);
134 overlay_delete(const char *dir
, bool _keep_sysupgrade
)
136 keep_sysupgrade
= _keep_sysupgrade
;
137 foreachdir(dir
, handle_rmdir
);
141 overlay_mount(struct volume
*v
, char *fs
)
143 if (mkdir("/tmp/overlay", 0755)) {
144 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
148 if (mount(v
->blk
, "/tmp/overlay", fs
, MS_NOATIME
, NULL
)) {
149 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n", fs
, v
->blk
);
157 * move_mount - move mount point to the new root
159 static int move_mount(const char *device
, const char *mount_point
)
161 static const char *prefix
= "/tmp/root/";
163 if (strncmp(mount_point
, prefix
, strlen(prefix
)))
166 return mount_move(prefix
, "/", mount_point
+ strlen(prefix
));
170 switch2jffs(struct volume
*v
)
175 if (!stat(SWITCH_JFFS2
, &s
)) {
176 ULOG_ERR("jffs2 switch already running\n");
180 creat("/tmp/.switch_jffs2", 0600);
181 ret
= mount(v
->blk
, "/rom/overlay", "jffs2", MS_NOATIME
, NULL
);
182 unlink("/tmp/.switch_jffs2");
184 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %m\n", v
->blk
);
188 if (mount("none", "/", NULL
, MS_NOATIME
| MS_REMOUNT
, 0)) {
189 ULOG_ERR("failed - mount -o remount,ro none: %m\n");
193 if (system("cp -a /tmp/root/* /rom/overlay")) {
194 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %m\n");
198 if (pivot("/rom", "/mnt")) {
199 ULOG_ERR("failed - pivot /rom /mnt: %m\n");
203 if (mount_move("/mnt", "/tmp/root", "")) {
204 ULOG_ERR("failed - mount -o move /mnt /tmp/root %m\n");
208 ret
= fopivot("/overlay", "/rom");
211 * Besides copying overlay data from "tmpfs" to "jffs2" we should also
212 * move mount points that user could create during JFFS2 formatting.
213 * This has to happen after fopivot call because:
214 * 1) It's trivial to find mount points to move then (/tmp/root/...).
215 * 2) We can't do that earlier using /rom/overlay/upper/ as overlay(fs)
216 * doesn't support mounts. Mounting to upper dir don't make overlay
217 * /propagate/ files to the target dir.
219 foreach_mount(move_mount
);
225 handle_whiteout(const char *dir
)
230 struct dirent
**namelist
;
233 n
= scandir(dir
, &namelist
, NULL
, NULL
);
241 snprintf(file
, sizeof(file
), "%s%s", dir
, namelist
[n
]->d_name
);
242 if (!lstat(file
, &s
) && S_ISLNK(s
.st_mode
)) {
243 sz
= readlink(file
, link
, sizeof(link
) - 1);
248 orig
= strstr(&file
[1], "/");
249 if (orig
&& !strcmp(link
, "(overlay-whiteout)"))
260 static char *overlay_fs_name(int type
)
276 jffs2_switch(struct volume
*v
)
281 if (find_overlay_mount("overlayfs:/tmp/root"))
284 if (find_filesystem("overlay")) {
285 ULOG_ERR("overlayfs not supported by kernel\n");
290 mp
= find_mount_point(v
->blk
, 0);
292 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
296 type
= volume_identify(v
);
297 fs_name
= overlay_fs_name(type
);
301 ULOG_ERR("no jffs2 marker found\n");
308 ULOG_INFO("performing overlay whiteout\n");
309 umount2("/tmp/root", MNT_DETACH
);
310 foreachdir("/overlay/", handle_whiteout
);
312 /* try hard to be in sync */
313 ULOG_INFO("syncronizing overlay\n");
314 if (system("cp -a /tmp/root/upper/* / 2>/dev/null"))
315 ULOG_ERR("failed to sync jffs2 overlay\n");
321 if (overlay_mount(v
, fs_name
))
323 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
324 ULOG_ERR("switching to %s failed\n", fs_name
);
331 fs_state_set("/overlay", FS_STATE_READY
);
335 static int overlay_mount_fs(struct volume
*v
)
337 char *fstype
= overlay_fs_name(volume_identify(v
));
339 if (mkdir("/tmp/overlay", 0755)) {
340 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
344 if (mount(v
->blk
, "/tmp/overlay", fstype
,
345 #ifdef OVL_MOUNT_FULL_ACCESS_TIME
350 #ifdef OVL_MOUNT_COMPRESS_ZLIB
356 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n",
364 enum fs_state
fs_state_get(const char *dir
)
371 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
372 sprintf(path
, "%s/.fs_state", dir
);
373 len
= readlink(path
, valstr
, sizeof(valstr
) - 1);
375 return FS_STATE_UNKNOWN
;
380 if (val
> __FS_STATE_LAST
)
381 return FS_STATE_UNKNOWN
;
387 int fs_state_set(const char *dir
, enum fs_state state
)
392 if (fs_state_get(dir
) == state
)
395 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
396 sprintf(path
, "%s/.fs_state", dir
);
398 snprintf(valstr
, sizeof(valstr
), "%d", state
);
400 return symlink(valstr
, path
);
404 int mount_overlay(struct volume
*v
)
411 mp
= find_mount_point(v
->blk
, 0);
413 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
419 extroot_prefix
= "/tmp/overlay";
420 if (!mount_extroot()) {
421 ULOG_INFO("switched to extroot\n");
425 switch(fs_state_get("/tmp/overlay")) {
426 case FS_STATE_UNKNOWN
:
427 fs_state_set("/tmp/overlay", FS_STATE_PENDING
);
428 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING
) {
429 ULOG_ERR("unable to set filesystem state\n");
432 case FS_STATE_PENDING
:
433 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
434 overlay_delete("/tmp/overlay", true);
440 fs_name
= overlay_fs_name(volume_identify(v
));
441 ULOG_INFO("switching to %s overlay\n", fs_name
);
442 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
443 ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name
);