e281c0b9ee978810612dfef2ba8a3718f13f98cc
[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 static int
22 start(int argc, char *argv[1])
23 {
24         struct volume *v = volume_find("rootfs_data");
25
26         if (!getenv("PREINIT"))
27                 return -1;
28
29         if (!v) {
30                 v = volume_find("rootfs");
31                 volume_init(v);
32                 fprintf(stderr, "mounting /dev/root\n");
33                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
34                 return 0;
35         }
36
37         extroot_prefix = "";
38         if (!mount_extroot()) {
39                 fprintf(stderr, "fs-state: switched to extroot\n");
40                 return 0;
41         }
42
43         switch (volume_identify(v)) {
44         case FS_NONE:
45         case FS_DEADCODE:
46                 return ramoverlay();
47
48         case FS_JFFS2:
49                 mount_overlay();
50                 break;
51
52         case FS_SNAPSHOT:
53                 mount_snapshot();
54                 break;
55         }
56
57         return 0;
58 }
59
60 static int
61 stop(int argc, char *argv[1])
62 {
63         if (!getenv("SHUTDOWN"))
64                 return -1;
65
66         return 0;
67 }
68
69 static int
70 done(int argc, char *argv[1])
71 {
72         struct volume *v = volume_find("rootfs_data");
73
74         if (!v)
75                 return -1;
76
77         switch (volume_identify(v)) {
78         case FS_NONE:
79         case FS_DEADCODE:
80                 return jffs2_switch(argc, argv);
81         }
82
83         return 0;
84 }
85
86 int main(int argc, char **argv)
87 {
88         if (argc < 2)
89                 return start(argc, argv);
90         if (!strcmp(argv[1], "stop"))
91                 return stop(argc, argv);
92         if (!strcmp(argv[1], "done"))
93                 return done(argc, argv);
94         return -1;
95 }