fstools: support for ext4fs overlay
[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;
31         struct volume *data = volume_find("rootfs_data");
32
33         if (!getenv("PREINIT"))
34                 return -1;
35
36         if (!data) {
37                 root = volume_find("rootfs");
38                 volume_init(root);
39                 ULOG_NOTE("mounting /dev/root\n");
40                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
41         }
42
43         /*
44          * Before trying to mount and use "rootfs_data" let's check if there is
45          * extroot configured. Following call will handle reading config from
46          * the "rootfs_data" on its own.
47          */
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         switch (volume_identify(data)) {
56         case FS_NONE:
57                 ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
58                 return ramoverlay();
59
60         case FS_DEADCODE:
61                 /*
62                  * Filesystem isn't ready yet and we are in the preinit, so we
63                  * can't afford waiting for it. Use tmpfs for now and handle it
64                  * properly in the "done" call.
65                  */
66                 ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
67                 return ramoverlay();
68
69         case FS_JFFS2:
70         case FS_UBIFS:
71         case FS_EXT4FS:
72                 mount_overlay(data);
73                 break;
74
75         case FS_SNAPSHOT:
76                 mount_snapshot(data);
77                 break;
78         }
79
80         return 0;
81 }
82
83 static int
84 stop(int argc, char *argv[1])
85 {
86         if (!getenv("SHUTDOWN"))
87                 return -1;
88
89         return 0;
90 }
91
92 /*
93  * Called at the end of init, it can wait for filesystem if needed.
94  */
95 static int
96 done(int argc, char *argv[1])
97 {
98         struct volume *v = volume_find("rootfs_data");
99
100         if (!v)
101                 return -1;
102
103         switch (volume_identify(v)) {
104         case FS_NONE:
105         case FS_DEADCODE:
106                 return jffs2_switch(v);
107
108         case FS_JFFS2:
109         case FS_UBIFS:
110         case FS_EXT4FS:
111                 fs_state_set("/overlay", FS_STATE_READY);
112                 break;
113         }
114
115         return 0;
116 }
117
118 int main(int argc, char **argv)
119 {
120         if (argc < 2)
121                 return start(argc, argv);
122         if (!strcmp(argv[1], "ram"))
123                 return ramoverlay();
124         if (!strcmp(argv[1], "stop"))
125                 return stop(argc, argv);
126         if (!strcmp(argv[1], "done"))
127                 return done(argc, argv);
128         return -1;
129 }