libfstools: add f2fs filesystem type and simplify fs type code
[project/fstools.git] / mount_root.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include <sys/mount.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include <libubox/ulog.h>
22
23 #include "libfstools/libfstools.h"
24 #include "libfstools/volume.h"
25
26 /*
27 * Called in the early (PREINIT) stage, when we immediately need some writable
28 * filesystem.
29 */
30 static int
31 start(int argc, char *argv[1])
32 {
33 struct volume *root;
34 struct volume *data = volume_find("rootfs_data");
35 struct stat s;
36
37 if (!getenv("PREINIT") && stat("/tmp/.preinit", &s))
38 return -1;
39
40 if (!data) {
41 root = volume_find("rootfs");
42 volume_init(root);
43 ULOG_NOTE("mounting /dev/root\n");
44 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
45 }
46
47 /*
48 * Before trying to mount and use "rootfs_data" let's check if there is
49 * extroot configured. Following call will handle reading config from
50 * the "rootfs_data" on its own.
51 */
52 extroot_prefix = "";
53 if (!mount_extroot()) {
54 ULOG_NOTE("switched to extroot\n");
55 return 0;
56 }
57
58 /* There isn't extroot, so just try to mount "rootfs_data" */
59 volume_init(data);
60 switch (volume_identify(data)) {
61 case FS_NONE:
62 ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
63 return ramoverlay();
64
65 case FS_DEADCODE:
66 /*
67 * Filesystem isn't ready yet and we are in the preinit, so we
68 * can't afford waiting for it. Use tmpfs for now and handle it
69 * properly in the "done" call.
70 */
71 ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
72 return ramoverlay();
73
74 case FS_F2FS:
75 case FS_JFFS2:
76 case FS_UBIFS:
77 mount_overlay(data);
78 break;
79
80 case FS_SNAPSHOT:
81 mount_snapshot(data);
82 break;
83 }
84
85 return 0;
86 }
87
88 static int
89 stop(int argc, char *argv[1])
90 {
91 if (!getenv("SHUTDOWN"))
92 return -1;
93
94 return 0;
95 }
96
97 /*
98 * Called at the end of init, it can wait for filesystem if needed.
99 */
100 static int
101 done(int argc, char *argv[1])
102 {
103 struct volume *v = volume_find("rootfs_data");
104
105 if (!v)
106 return -1;
107
108 switch (volume_identify(v)) {
109 case FS_NONE:
110 case FS_DEADCODE:
111 return jffs2_switch(v);
112
113 case FS_F2FS:
114 case FS_JFFS2:
115 case FS_UBIFS:
116 fs_state_set("/overlay", FS_STATE_READY);
117 break;
118 }
119
120 return 0;
121 }
122
123 int main(int argc, char **argv)
124 {
125 if (argc < 2)
126 return start(argc, argv);
127 if (!strcmp(argv[1], "ram"))
128 return ramoverlay();
129 if (!strcmp(argv[1], "stop"))
130 return stop(argc, argv);
131 if (!strcmp(argv[1], "done"))
132 return done(argc, argv);
133 return -1;
134 }