ca2c31c3a9ed70fb22d90b5af92d59a9f406c392
[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 /* Check for extroot config in rootfs before even trying rootfs_data */
48 extroot_prefix = "";
49 if (!mount_extroot()) {
50 ULOG_NOTE("switched to extroot\n");
51 return 0;
52 }
53
54 /* There isn't extroot, so just try to mount "rootfs_data" */
55 volume_init(data);
56 switch (volume_identify(data)) {
57 case FS_NONE:
58 ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
59 return ramoverlay();
60
61 case FS_DEADCODE:
62 /*
63 * Filesystem isn't ready yet and we are in the preinit, so we
64 * can't afford waiting for it. Use tmpfs for now and handle it
65 * properly in the "done" call.
66 */
67 ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
68 return ramoverlay();
69
70 case FS_EXT4:
71 case FS_F2FS:
72 case FS_JFFS2:
73 case FS_UBIFS:
74 mount_overlay(data);
75 break;
76
77 case FS_SNAPSHOT:
78 mount_snapshot(data);
79 break;
80 }
81
82 return 0;
83 }
84
85 static int
86 stop(int argc, char *argv[1])
87 {
88 if (!getenv("SHUTDOWN"))
89 return -1;
90
91 return 0;
92 }
93
94 /*
95 * Called at the end of init, it can wait for filesystem if needed.
96 */
97 static int
98 done(int argc, char *argv[1])
99 {
100 struct volume *v = volume_find("rootfs_data");
101
102 if (!v)
103 return -1;
104
105 switch (volume_identify(v)) {
106 case FS_NONE:
107 case FS_DEADCODE:
108 return jffs2_switch(v);
109
110 case FS_EXT4:
111 case FS_F2FS:
112 case FS_JFFS2:
113 case FS_UBIFS:
114 fs_state_set("/overlay", FS_STATE_READY);
115 break;
116 }
117
118 return 0;
119 }
120
121 int main(int argc, char **argv)
122 {
123 if (argc < 2)
124 return start(argc, argv);
125 if (!strcmp(argv[1], "ram"))
126 return ramoverlay();
127 if (!strcmp(argv[1], "stop"))
128 return stop(argc, argv);
129 if (!strcmp(argv[1], "done"))
130 return done(argc, argv);
131 return -1;
132 }