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.
14 #include <sys/mount.h>
15 #include <sys/types.h>
21 #include <libubox/ulog.h>
23 #include "libfstools/libfstools.h"
24 #include "libfstools/volume.h"
27 * Called in the early (PREINIT) stage, when we immediately need some writable
31 start(int argc
, char *argv
[3])
34 struct volume
*data
= volume_find("rootfs_data");
37 if (!getenv("PREINIT") && stat("/tmp/.preinit", &s
))
41 root
= volume_find("rootfs");
44 ULOG_NOTE("mounting /dev/root\n");
46 ULOG_NOTE("mounting /dev/root with options %s\n", argv
[2]);
49 * If present, mount rootfs with passed options.
50 * Example F2FS filesystem with compress_algorithm option.
52 mount("/dev/root", "/", NULL
, MS_NOATIME
| MS_REMOUNT
,
53 argc
< 3 ? 0 : argv
[2]);
56 /* Check for extroot config in rootfs before even trying rootfs_data */
57 if (!mount_extroot("")) {
58 ULOG_NOTE("switched to extroot\n");
62 /* There isn't extroot, so just try to mount "rootfs_data" */
64 switch (volume_identify(data
)) {
66 ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
71 * Filesystem isn't ready yet and we are in the preinit, so we
72 * can't afford waiting for it. Use tmpfs for now and handle it
73 * properly in the "done" call.
75 ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
94 stop(int argc
, char *argv
[1])
96 if (!getenv("SHUTDOWN"))
103 * Called at the end of init, it can wait for filesystem if needed.
106 done(int argc
, char *argv
[1])
108 struct volume
*v
= volume_find("rootfs_data");
113 switch (volume_identify(v
)) {
116 return jffs2_switch(v
);
122 fs_state_set("/overlay", FS_STATE_READY
);
129 int main(int argc
, char **argv
)
131 if (argc
< 2 || !strcmp(argv
[1], "start"))
132 return start(argc
, argv
);
133 if (!strcmp(argv
[1], "ram"))
135 if (!strcmp(argv
[1], "stop"))
136 return stop(argc
, argv
);
137 if (!strcmp(argv
[1], "done"))
138 return done(argc
, argv
);