4cc319e759a31680db883cae031b8b1fdb9b66f6
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"
34 #define GLOB_ONLYDIR 0x100
37 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
38 #define OVERLAYDIR "/rom/overlay"
40 static bool keep_sysupgrade
;
43 handle_rmdir(const char *dir
)
56 while ((dt
= readdir(d
)) != NULL
) {
57 if (fstatat(fd
, dt
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) || S_ISDIR(st
.st_mode
))
60 if (keep_sysupgrade
&& !strcmp(dt
->d_name
, "sysupgrade.tgz"))
63 unlinkat(fd
, dt
->d_name
, 0);
73 foreachdir(const char *dir
, int (*cb
)(const char*))
75 static char *globdir
= NULL
;
76 static size_t globdirlen
= 0;
77 struct stat s
= { 0 };
78 size_t dirlen
= strlen(dir
);
82 if (dirlen
+ sizeof("/*") > globdirlen
) {
83 /* Alloc extra 256 B to avoid too many reallocs */
84 size_t len
= dirlen
+ sizeof("/*") + 256;
87 tmp
= realloc(globdir
, len
);
94 sprintf(globdir
, "%s/*", dir
);
96 /* Include GLOB_MARK as callbacks expect a trailing slash */
97 if (!glob(globdir
, GLOB_NOESCAPE
| GLOB_MARK
| GLOB_ONLYDIR
, NULL
, &gl
))
98 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
99 char *dir
= gl
.gl_pathv
[j
];
100 int len
= strlen(gl
.gl_pathv
[j
]);
103 /* Quick way of skipping files */
104 if (dir
[len
- 1] != '/')
107 /* lstat needs path without a trailing slash */
110 err
= lstat(gl
.gl_pathv
[j
], &s
);
114 if (!err
&& !S_ISLNK(s
.st_mode
))
115 foreachdir(gl
.gl_pathv
[j
], cb
);
120 static void foreach_mount(int (*cb
)(const char *, const char *))
122 FILE *fp
= fopen("/proc/mounts", "r");
123 static char line
[256];
128 while (fgets(line
, sizeof(line
), fp
)) {
129 char device
[32], mount_point
[32];
131 if (sscanf(line
, "%31s %31s %*s %*s %*u %*u", device
, mount_point
) == 2)
132 cb(device
, mount_point
);
139 overlay_delete(const char *dir
, bool _keep_sysupgrade
)
141 keep_sysupgrade
= _keep_sysupgrade
;
142 foreachdir(dir
, handle_rmdir
);
146 overlay_mount(struct volume
*v
, char *fs
)
148 if (mkdir("/tmp/overlay", 0755)) {
149 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
153 if (mount(v
->blk
, "/tmp/overlay", fs
, MS_NOATIME
, NULL
)) {
154 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n", fs
, v
->blk
);
162 * move_mount - move mount point to the new root
164 static int move_mount(const char *device
, const char *mount_point
)
166 static const char *prefix
= "/tmp/root/";
168 if (strncmp(mount_point
, prefix
, strlen(prefix
)))
171 return mount_move(prefix
, "/", mount_point
+ strlen(prefix
));
175 switch2jffs(struct volume
*v
)
180 if (!stat(SWITCH_JFFS2
, &s
)) {
181 ULOG_ERR("jffs2 switch already running\n");
185 fd
= creat(SWITCH_JFFS2
, 0600);
187 ULOG_ERR("failed - cannot create jffs2 switch mark: %m\n");
192 ret
= mount(v
->blk
, OVERLAYDIR
, "jffs2", MS_NOATIME
, NULL
);
193 unlink(SWITCH_JFFS2
);
195 ULOG_ERR("failed - mount -t jffs2 %s %s: %m\n", v
->blk
, OVERLAYDIR
);
199 if (mount("none", "/", NULL
, MS_NOATIME
| MS_REMOUNT
, 0)) {
200 ULOG_ERR("failed - mount -o remount,ro none: %m\n");
204 if (system("cp -a /tmp/root/* /rom/overlay")) {
205 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %m\n");
209 if (pivot("/rom", "/mnt")) {
210 ULOG_ERR("failed - pivot /rom /mnt: %m\n");
214 if (mount_move("/mnt", "/tmp/root", "")) {
215 ULOG_ERR("failed - mount -o move /mnt /tmp/root %m\n");
219 ret
= fopivot("/overlay", "/rom");
222 * Besides copying overlay data from "tmpfs" to "jffs2" we should also
223 * move mount points that user could create during JFFS2 formatting.
224 * This has to happen after fopivot call because:
225 * 1) It's trivial to find mount points to move then (/tmp/root/...).
226 * 2) We can't do that earlier using /rom/overlay/upper/ as overlay(fs)
227 * doesn't support mounts. Mounting to upper dir don't make overlay
228 * /propagate/ files to the target dir.
230 foreach_mount(move_mount
);
236 handle_whiteout(const char *dir
)
241 struct dirent
**namelist
;
244 n
= scandir(dir
, &namelist
, NULL
, NULL
);
252 snprintf(file
, sizeof(file
), "%s%s", dir
, namelist
[n
]->d_name
);
253 if (!lstat(file
, &s
) && S_ISLNK(s
.st_mode
)) {
254 sz
= readlink(file
, link
, sizeof(link
) - 1);
259 orig
= strstr(&file
[1], "/");
260 if (orig
&& !strcmp(link
, "(overlay-whiteout)"))
271 static char *overlay_fs_name(int type
)
287 jffs2_switch(struct volume
*v
)
292 if (find_overlay_mount("overlayfs:/tmp/root"))
295 if (find_filesystem("overlay")) {
296 ULOG_ERR("overlayfs not supported by kernel\n");
301 mp
= find_mount_point(v
->blk
, 0);
303 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
307 type
= volume_identify(v
);
308 fs_name
= overlay_fs_name(type
);
312 ULOG_ERR("no jffs2 marker found\n");
319 ULOG_INFO("performing overlay whiteout\n");
320 umount2("/tmp/root", MNT_DETACH
);
321 foreachdir("/overlay/", handle_whiteout
);
323 /* try hard to be in sync */
324 ULOG_INFO("synchronizing overlay\n");
325 if (system("cp -a /tmp/root/upper/* / 2>/dev/null"))
326 ULOG_ERR("failed to sync jffs2 overlay\n");
332 if (overlay_mount(v
, fs_name
))
334 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
335 ULOG_ERR("switching to %s failed\n", fs_name
);
342 fs_state_set("/overlay", FS_STATE_READY
);
346 static int overlay_mount_fs(struct volume
*v
, const char *overlay_mp
)
348 char *fstype
= overlay_fs_name(volume_identify(v
));
350 if (mkdir(overlay_mp
, 0755)) {
351 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
355 if (mount(v
->blk
, overlay_mp
, fstype
,
356 #ifdef OVL_MOUNT_FULL_ACCESS_TIME
361 #ifdef OVL_MOUNT_COMPRESS_ZLIB
367 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n",
375 enum fs_state
fs_state_get(const char *dir
)
382 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
383 sprintf(path
, "%s/.fs_state", dir
);
384 len
= readlink(path
, valstr
, sizeof(valstr
) - 1);
386 return FS_STATE_UNKNOWN
;
391 if (val
> __FS_STATE_LAST
)
392 return FS_STATE_UNKNOWN
;
398 int fs_state_set(const char *dir
, enum fs_state state
)
403 if (fs_state_get(dir
) == state
)
406 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
407 sprintf(path
, "%s/.fs_state", dir
);
409 snprintf(valstr
, sizeof(valstr
), "%d", state
);
411 return symlink(valstr
, path
);
415 int mount_overlay(struct volume
*v
)
417 const char *overlay_mp
= "/tmp/overlay";
424 mp
= find_mount_point(v
->blk
, 0);
426 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
430 err
= overlay_mount_fs(v
, overlay_mp
);
435 * Check for extroot config in overlay (rootfs_data) and if present then
436 * prefer it over rootfs_data.
438 if (!mount_extroot(overlay_mp
)) {
439 ULOG_INFO("switched to extroot\n");
443 switch (fs_state_get(overlay_mp
)) {
444 case FS_STATE_UNKNOWN
:
445 fs_state_set(overlay_mp
, FS_STATE_PENDING
);
446 if (fs_state_get(overlay_mp
) != FS_STATE_PENDING
) {
447 ULOG_ERR("unable to set filesystem state\n");
450 case FS_STATE_PENDING
:
451 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
452 overlay_delete(overlay_mp
, true);
458 fs_name
= overlay_fs_name(volume_identify(v
));
459 ULOG_INFO("switching to %s overlay\n", fs_name
);
460 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
461 ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name
);