77d683ba30bc028fd4c22c971181ecc6988226da
[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 <stdio.h>
16 #include <stdlib.h>
17
18 #include <libubox/ulog.h>
19
20 #include "libfstools/libfstools.h"
21 #include "libfstools/volume.h"
22
23 /*
24  * Called in the early (PREINIT) stage, when we immediately need some writable
25  * filesystem.
26  */
27 static int
28 start(int argc, char *argv[1])
29 {
30         struct volume *root = volume_find("rootfs");
31         struct volume *data = volume_find("rootfs_data");
32
33         if (data && find_mount_point(data->blk, 0))
34                 return -1;
35
36         if (root && find_mount_point(root->blk, 0))
37                 return -1;
38
39         if (!data) {
40                 volume_init(root);
41                 ULOG_NOTE("mounting /dev/root\n");
42                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
43         }
44
45         /*
46          * Before trying to mount and use "rootfs_data" let's check if there is
47          * extroot configured. Following call will handle reading config from
48          * the "rootfs_data" on its own.
49          */
50         extroot_prefix = "";
51         if (!mount_extroot()) {
52                 ULOG_NOTE("switched to extroot\n");
53                 return 0;
54         }
55
56         /* There isn't extroot, so just try to mount "rootfs_data" */
57         switch (volume_identify(data)) {
58         case FS_NONE:
59                 ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
60                 return ramoverlay();
61
62         case FS_DEADCODE:
63                 /*
64                  * Filesystem isn't ready yet and we are in the preinit, so we
65                  * can't afford waiting for it. Use tmpfs for now and handle it
66                  * properly in the "done" call.
67                  */
68                 ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
69                 return ramoverlay();
70
71         case FS_JFFS2:
72         case FS_UBIFS:
73         case FS_EXT4FS:
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_JFFS2:
111         case FS_UBIFS:
112         case FS_EXT4FS:
113                 fs_state_set("/overlay", FS_STATE_READY);
114                 break;
115         }
116
117         return 0;
118 }
119
120 int main(int argc, char **argv)
121 {
122         if (argc < 2)
123                 return start(argc, argv);
124         if (!strcmp(argv[1], "ram"))
125                 return ramoverlay();
126         if (!strcmp(argv[1], "stop"))
127                 return stop(argc, argv);
128         if (!strcmp(argv[1], "done"))
129                 return done(argc, argv);
130         return -1;
131 }