7c9b480ba963818a70c30048ce4e1a1a181a2793
[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 system("cp -a /tmp/root/* /rom/overlay"); /**/
134
135 if (pivot("/rom", "/mnt")) {
136 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno));
137 return -1;
138 }
139
140 if (mount_move("/mnt", "/tmp/root", "")) {
141 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
142 return -1;
143 }
144
145 return fopivot("/overlay", "/rom");
146 }
147
148 int
149 handle_whiteout(const char *dir)
150 {
151 struct stat s;
152 char link[256];
153 ssize_t sz;
154 struct dirent **namelist;
155 int n;
156
157 n = scandir(dir, &namelist, NULL, NULL);
158
159 if (n < 1)
160 return -1;
161
162 while (n--) {
163 char file[256];
164
165 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
166 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
167 sz = readlink(file, link, sizeof(link) - 1);
168 if (sz > 0) {
169 char *orig;
170
171 link[sz] = '\0';
172 orig = strstr(&file[1], "/");
173 if (orig && !strcmp(link, "(overlay-whiteout)"))
174 unlink(orig);
175 }
176 }
177 free(namelist[n]);
178 }
179 free(namelist);
180
181 return 0;
182 }
183
184 int
185 jffs2_switch(struct volume *v)
186 {
187 char *mp;
188 int ret = -1;
189
190 if (find_overlay_mount("overlayfs:/tmp/root"))
191 return -1;
192
193 if (find_filesystem("overlay")) {
194 ULOG_ERR("overlayfs not supported by kernel\n");
195 return ret;
196 }
197
198 mp = find_mount_point(v->blk, 0);
199 if (mp) {
200 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
201 return -1;
202 }
203
204 switch (volume_identify(v)) {
205 case FS_NONE:
206 ULOG_ERR("no jffs2 marker found\n");
207 /* fall through */
208
209 case FS_DEADCODE:
210 ret = switch2jffs(v);
211 if (!ret) {
212 ULOG_INFO("performing overlay whiteout\n");
213 umount2("/tmp/root", MNT_DETACH);
214 foreachdir("/overlay/", handle_whiteout);
215 }
216 break;
217
218 case FS_JFFS2:
219 ret = overlay_mount(v, "jffs2");
220 if (ret)
221 break;
222 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
223 ULOG_ERR("switching to jffs2 failed\n");
224 ret = -1;
225 }
226 break;
227
228 case FS_UBIFS:
229 ret = overlay_mount(v, "ubifs");
230 if (ret)
231 break;
232 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
233 ULOG_ERR("switching to ubifs failed\n");
234 ret = -1;
235 }
236 break;
237 }
238 return ret;
239 }
240
241 static int overlay_mount_fs(struct volume *v)
242 {
243 char *fstype;
244
245 if (mkdir("/tmp/overlay", 0755)) {
246 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
247 return -1;
248 }
249
250 fstype = "jffs2";
251
252 switch (volume_identify(v)) {
253 case FS_UBIFS:
254 fstype = "ubifs";
255 break;
256 }
257
258 volume_init(v);
259
260 if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
261 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
262 fstype, v->blk, strerror(errno));
263 return -1;
264 }
265
266 return -1;
267 }
268
269 int mount_overlay(struct volume *v)
270 {
271 char *mp;
272
273 if (!v)
274 return -1;
275
276 mp = find_mount_point(v->blk, 0);
277 if (mp) {
278 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
279 return -1;
280 }
281
282 overlay_mount_fs(v);
283
284 extroot_prefix = "/tmp/overlay";
285 if (!mount_extroot()) {
286 ULOG_INFO("switched to extroot\n");
287 return 0;
288 }
289
290 ULOG_INFO("switching to jffs2 overlay\n");
291 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
292 ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");
293 return ramoverlay();
294 }
295
296 return -1;
297 }