block: support builtin fstab config
[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 "libfstools/libfstools.h"
19 #include "libfstools/volume.h"
20
21 /*
22  * Called in the early (PREINIT) stage, when we immediately need some writable
23  * filesystem.
24  */
25 static int
26 start(int argc, char *argv[1])
27 {
28         struct volume *root;
29         struct volume *data = volume_find("rootfs_data");
30
31         if (!getenv("PREINIT"))
32                 return -1;
33
34         if (!data) {
35                 root = volume_find("rootfs");
36                 volume_init(root);
37                 fprintf(stderr, "mounting /dev/root\n");
38                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
39         }
40
41         /*
42          * Before trying to mount and use "rootfs_data" let's check if there is
43          * extroot configured. Following call will handle reading config from
44          * the "rootfs_data" on its own.
45          */
46         extroot_prefix = "";
47         if (!mount_extroot()) {
48                 fprintf(stderr, "fs-state: switched to extroot\n");
49                 return 0;
50         }
51
52         /* There isn't extroot, so just try to mount "rootfs_data" */
53         switch (volume_identify(data)) {
54         case FS_NONE:
55         case FS_DEADCODE:
56                 /*
57                  * Filesystem isn't ready yet and we are in the preinit, so we
58                  * can't afford waiting for it. Use tmpfs for now and handle it
59                  * properly in the "done" call.
60                  */
61                 return ramoverlay();
62
63         case FS_JFFS2:
64         case FS_UBIFS:
65                 mount_overlay(data);
66                 break;
67
68         case FS_SNAPSHOT:
69                 mount_snapshot(data);
70                 break;
71         }
72
73         return 0;
74 }
75
76 static int
77 stop(int argc, char *argv[1])
78 {
79         if (!getenv("SHUTDOWN"))
80                 return -1;
81
82         return 0;
83 }
84
85 /*
86  * Called at the end of init, it can wait for filesystem if needed.
87  */
88 static int
89 done(int argc, char *argv[1])
90 {
91         struct volume *v = volume_find("rootfs_data");
92
93         if (!v)
94                 return -1;
95
96         switch (volume_identify(v)) {
97         case FS_NONE:
98         case FS_DEADCODE:
99                 return jffs2_switch(v);
100         }
101
102         return 0;
103 }
104
105 int main(int argc, char **argv)
106 {
107         if (argc < 2)
108                 return start(argc, argv);
109         if (!strcmp(argv[1], "stop"))
110                 return stop(argc, argv);
111         if (!strcmp(argv[1], "done"))
112                 return done(argc, argv);
113         return -1;
114 }