mount_root: use symlink instead of xattr to store initialization state
[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 "libfstools.h"
31 #include "volume.h"
32
33 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
34
35 static bool keep_sysupgrade;
36
37 static int
38 handle_rmdir(const char *dir)
39 {
40 struct dirent *dt;
41 struct stat st;
42 DIR *d;
43 int fd;
44
45 d = opendir(dir);
46 if (!d)
47 return -1;
48
49 fd = dirfd(d);
50
51 while ((dt = readdir(d)) != NULL) {
52 if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
53 continue;
54
55 if (keep_sysupgrade && !strcmp(dt->d_name, "sysupgrade.tgz"))
56 continue;
57
58 unlinkat(fd, dt->d_name, 0);
59 }
60
61 closedir(d);
62 rmdir(dir);
63
64 return 0;
65 }
66
67 void
68 foreachdir(const char *dir, int (*cb)(const char*))
69 {
70 char globdir[256];
71 glob_t gl;
72 int j;
73
74 if (dir[strlen(dir) - 1] == '/')
75 snprintf(globdir, 256, "%s*", dir);
76 else
77 snprintf(globdir, 256, "%s/*", dir); /**/
78
79 if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
80 for (j = 0; j < gl.gl_pathc; j++)
81 foreachdir(gl.gl_pathv[j], cb);
82
83 cb(dir);
84 }
85
86 void
87 overlay_delete(const char *dir, bool _keep_sysupgrade)
88 {
89 keep_sysupgrade = _keep_sysupgrade;
90 foreachdir(dir, handle_rmdir);
91 }
92
93 static int
94 overlay_mount(struct volume *v, char *fs)
95 {
96 if (mkdir("/tmp/overlay", 0755)) {
97 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
98 return -1;
99 }
100
101 if (mount(v->blk, "/tmp/overlay", fs, MS_NOATIME, NULL)) {
102 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n", fs, v->blk, strerror(errno));
103 return -1;
104 }
105
106 return volume_init(v);
107 }
108
109 static int
110 switch2jffs(struct volume *v)
111 {
112 struct stat s;
113 int ret;
114
115 if (!stat(SWITCH_JFFS2, &s)) {
116 ULOG_ERR("jffs2 switch already running\n");
117 return -1;
118 }
119
120 creat("/tmp/.switch_jffs2", 0600);
121 ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
122 unlink("/tmp/.switch_jffs2");
123 if (ret) {
124 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v->blk, strerror(errno));
125 return -1;
126 }
127
128 if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
129 ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno));
130 return -1;
131 }
132
133 if (system("cp -a /tmp/root/* /rom/overlay")) {
134 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %s\n", strerror(errno));
135 return -1;
136 }
137
138 if (pivot("/rom", "/mnt")) {
139 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno));
140 return -1;
141 }
142
143 if (mount_move("/mnt", "/tmp/root", "")) {
144 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
145 return -1;
146 }
147
148 return fopivot("/overlay", "/rom");
149 }
150
151 int
152 handle_whiteout(const char *dir)
153 {
154 struct stat s;
155 char link[256];
156 ssize_t sz;
157 struct dirent **namelist;
158 int n;
159
160 n = scandir(dir, &namelist, NULL, NULL);
161
162 if (n < 1)
163 return -1;
164
165 while (n--) {
166 char file[256];
167
168 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
169 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
170 sz = readlink(file, link, sizeof(link) - 1);
171 if (sz > 0) {
172 char *orig;
173
174 link[sz] = '\0';
175 orig = strstr(&file[1], "/");
176 if (orig && !strcmp(link, "(overlay-whiteout)"))
177 unlink(orig);
178 }
179 }
180 free(namelist[n]);
181 }
182 free(namelist);
183
184 return 0;
185 }
186
187 int
188 jffs2_switch(struct volume *v)
189 {
190 char *mp;
191 int ret = -1;
192
193 if (find_overlay_mount("overlayfs:/tmp/root"))
194 return -1;
195
196 if (find_filesystem("overlay")) {
197 ULOG_ERR("overlayfs not supported by kernel\n");
198 return ret;
199 }
200
201 mp = find_mount_point(v->blk, 0);
202 if (mp) {
203 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
204 return -1;
205 }
206
207 switch (volume_identify(v)) {
208 case FS_NONE:
209 ULOG_ERR("no jffs2 marker found\n");
210 /* fall through */
211
212 case FS_DEADCODE:
213 ret = switch2jffs(v);
214 if (!ret) {
215 ULOG_INFO("performing overlay whiteout\n");
216 umount2("/tmp/root", MNT_DETACH);
217 foreachdir("/overlay/", handle_whiteout);
218 }
219 break;
220
221 case FS_JFFS2:
222 ret = overlay_mount(v, "jffs2");
223 if (ret)
224 break;
225 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
226 ULOG_ERR("switching to jffs2 failed\n");
227 ret = -1;
228 }
229 break;
230
231 case FS_UBIFS:
232 ret = overlay_mount(v, "ubifs");
233 if (ret)
234 break;
235 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
236 ULOG_ERR("switching to ubifs failed\n");
237 ret = -1;
238 }
239 break;
240 }
241
242 if (ret)
243 return ret;
244
245 sync();
246 fs_state_set("/overlay", FS_STATE_READY);
247 return 0;
248 }
249
250 static int overlay_mount_fs(struct volume *v)
251 {
252 char *fstype;
253
254 if (mkdir("/tmp/overlay", 0755)) {
255 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
256 return -1;
257 }
258
259 fstype = "jffs2";
260
261 switch (volume_identify(v)) {
262 case FS_UBIFS:
263 fstype = "ubifs";
264 break;
265 }
266
267 volume_init(v);
268
269 if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
270 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
271 fstype, v->blk, strerror(errno));
272 return -1;
273 }
274
275 return -1;
276 }
277
278 enum fs_state fs_state_get(const char *dir)
279 {
280 char *path;
281 char valstr[16];
282 uint32_t val;
283 ssize_t len;
284
285 path = alloca(strlen(dir) + 1 + sizeof("/.fs_state"));
286 sprintf(path, "%s/.fs_state", dir);
287 len = readlink(path, valstr, sizeof(valstr) - 1);
288 if (len < 0)
289 return FS_STATE_UNKNOWN;
290
291 valstr[len] = 0;
292 val = atoi(valstr);
293
294 if (val > __FS_STATE_LAST)
295 return FS_STATE_UNKNOWN;
296
297 return val;
298 }
299
300
301 int fs_state_set(const char *dir, enum fs_state state)
302 {
303 char valstr[16];
304 char *path;
305
306 path = alloca(strlen(dir) + 1 + sizeof("/.fs_state"));
307 sprintf(path, "%s/.fs_state", dir);
308 unlink(path);
309 snprintf(valstr, sizeof(valstr), "%d", state);
310
311 return symlink(valstr, path);
312 }
313
314
315 int mount_overlay(struct volume *v)
316 {
317 char *mp;
318
319 if (!v)
320 return -1;
321
322 mp = find_mount_point(v->blk, 0);
323 if (mp) {
324 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
325 return -1;
326 }
327
328 overlay_mount_fs(v);
329
330 extroot_prefix = "/tmp/overlay";
331 if (!mount_extroot()) {
332 ULOG_INFO("switched to extroot\n");
333 return 0;
334 }
335
336 switch(fs_state_get("/tmp/overlay")) {
337 case FS_STATE_UNKNOWN:
338 fs_state_set("/tmp/overlay", FS_STATE_PENDING);
339 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING) {
340 ULOG_ERR("unable to set filesystem state\n");
341 break;
342 }
343 case FS_STATE_PENDING:
344 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
345 overlay_delete("/tmp/overlay", true);
346 break;
347 case FS_STATE_READY:
348 break;
349 }
350
351 ULOG_INFO("switching to jffs2 overlay\n");
352 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
353 ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");
354 return ramoverlay();
355 }
356
357 return -1;
358 }