85237e44922ed01b4f7c5edb2126e384ade2c653
[project/fstools.git] / libfstools / overlay.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/stat.h>
15 #include <sys/types.h>
16 #include <sys/mount.h>
17
18 #include <asm/byteorder.h>
19
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <glob.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29
30 #include "../fs-state.h"
31 #include "volume.h"
32
33 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
34
35 void
36 foreachdir(const char *dir, int (*cb)(const char*))
37 {
38 char globdir[256];
39 glob_t gl;
40 int j;
41
42 if (dir[strlen(dir) - 1] == '/')
43 snprintf(globdir, 256, "%s*", dir);
44 else
45 snprintf(globdir, 256, "%s/*", dir);
46
47 if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
48 for (j = 0; j < gl.gl_pathc; j++)
49 foreachdir(gl.gl_pathv[j], cb);
50
51 cb(dir);
52 }
53
54 static int
55 overlay_mount(struct volume *v, char *fs)
56 {
57 if (mkdir("/tmp/overlay", 0755)) {
58 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
59 return -1;
60 }
61
62 if (mount(v->blk, "/tmp/overlay", fs, MS_NOATIME, NULL)) {
63 fprintf(stderr, "failed to mount -t %s %s /tmp/overlay: %s\n", fs, v->blk, strerror(errno));
64 return -1;
65 }
66
67 return volume_init(v);
68 }
69
70 static int
71 switch2jffs(struct volume *v)
72 {
73 struct stat s;
74 int ret;
75
76 if (!stat(SWITCH_JFFS2, &s)) {
77 fprintf(stderr, "jffs2 switch already running\n");
78 return -1;
79 }
80
81 creat("/tmp/.switch_jffs2", 0600);
82 ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
83 unlink("/tmp/.switch_jffs2");
84 if (ret) {
85 fprintf(stderr, "failed - mount -t jffs2 %s /rom/overlay: %s\n", v->blk, strerror(errno));
86 return -1;
87 }
88
89 if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
90 fprintf(stderr, "failed - mount -o remount,ro none: %s\n", strerror(errno));
91 return -1;
92 }
93
94 system("cp -a /tmp/root/* /rom/overlay");
95
96 if (pivot("/rom", "/mnt")) {
97 fprintf(stderr, "failed - pivot /rom /mnt: %s\n", strerror(errno));
98 return -1;
99 }
100
101 if (mount_move("/mnt", "/tmp/root", "")) {
102 fprintf(stderr, "failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
103 return -1;
104 }
105
106 return fopivot("/overlay", "/rom");
107 }
108
109 int
110 handle_whiteout(const char *dir)
111 {
112 struct stat s;
113 char link[256];
114 ssize_t sz;
115 struct dirent **namelist;
116 int n;
117
118 n = scandir(dir, &namelist, NULL, NULL);
119
120 if (n < 1)
121 return -1;
122
123 while (n--) {
124 char file[256];
125
126 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
127 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
128 sz = readlink(file, link, sizeof(link) - 1);
129 if (sz > 0) {
130 char *orig;
131
132 link[sz] = '\0';
133 orig = strstr(&file[1], "/");
134 if (orig && !strcmp(link, "(overlay-whiteout)"))
135 unlink(orig);
136 }
137 }
138 free(namelist[n]);
139 }
140 free(namelist);
141
142 return 0;
143 }
144
145 static int
146 ask_user(int argc, char **argv)
147 {
148 if ((argc < 2) || strcmp(argv[1], "-y")) {
149 fprintf(stderr, "This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
150 if (getchar() != 'y')
151 return -1;
152 }
153 return 0;
154
155 }
156
157 int
158 jffs2_switch(int argc, char **argv)
159 {
160 struct volume *v;
161 char *mp;
162 int ret = -1;
163
164 if (find_overlay_mount("overlayfs:/tmp/root"))
165 return -1;
166
167 if (find_filesystem("overlay")) {
168 fprintf(stderr, "overlayfs not found\n");
169 return ret;
170 }
171
172 v = volume_find("rootfs_data");
173 mp = find_mount_point(v->blk, NULL);
174 if (mp) {
175 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
176 return -1;
177 }
178
179 switch (volume_identify(v)) {
180 case FS_NONE:
181 fprintf(stderr, "no jffs2 marker found\n");
182 /* fall through */
183
184 case FS_DEADCODE:
185 ret = switch2jffs();
186 if (!ret) {
187 fprintf(stderr, "doing fo cleanup\n");
188 umount2("/tmp/root", MNT_DETACH);
189 foreachdir("/overlay/", handle_whiteout);
190 }
191 break;
192
193 case FS_JFFS2:
194 ret = overlay_mount(v, "jffs2");
195 if (ret)
196 break;
197 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
198 fprintf(stderr, "switching to jffs2 failed\n");
199 ret = -1;
200 }
201 break;
202 }
203
204 return ret;
205 }
206
207 static int overlay_mount_fs(void)
208 {
209 struct volume *v;
210
211 if (mkdir("/tmp/overlay", 0755)) {
212 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
213 return -1;
214 }
215
216 v = volume_find("rootfs_data");
217 if (!v) {
218 fprintf(stderr, "rootfs_data does not exist\n");
219 return -1;
220 }
221
222 if (mount(v->blk, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
223 fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n",
224 v->blk, strerror(errno));
225 return -1;
226 }
227
228 volume_init(v);
229
230 return -1;
231 }
232
233 static int overlay_mount(void)
234 {
235 struct volume *v = volume_find("rootfs_data");;
236 char *mp;
237
238 if (!v)
239 return -1;
240
241 mp = find_mount_point(v->blk, NULL);
242 if (mp) {
243 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
244 return -1;
245 }
246
247 overlay_mount_fs();
248
249 extroot_prefix = "/tmp/overlay";
250 if (!backend_mount("extroot")) {
251 fprintf(stderr, "fs-state: switched to extroot\n");
252 return 0;
253 }
254
255 fprintf(stderr, "switching to jffs2\n");
256 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
257 fprintf(stderr, "switching to jffs2 failed - fallback to ramoverlay\n");
258 return ramoverlay();
259 }
260
261 return -1;
262 }
263
264 static struct backend overlay_backend = {
265 .name = "overlay",
266 .mount = overlay_mount,
267 };
268 BACKEND(overlay_backend);