ad1de363544afa813d95b1a1fb87321e75003566
[project/ubox.git] / mount_root.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <getopt.h>
18 #include <syslog.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <libgen.h>
24 #include <glob.h>
25 #include <dirent.h>
26
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/mount.h>
30 #include <sys/wait.h>
31
32 #include <asm/byteorder.h>
33
34 #include <mtd/mtd-user.h>
35
36 #define DEBUG(level, fmt, ...) do { \
37         if (debug >= level) \
38                 fprintf(stderr, "%s %s(%d): " fmt, argv0, __func__, __LINE__, ## __VA_ARGS__); \
39         } while (0)
40
41 #define LOG(fmt, ...) do { \
42                 syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
43                 fprintf(stderr, "%s: "fmt, argv0, ## __VA_ARGS__); \
44         } while (0)
45
46 #define ERROR(fmt, ...) do { \
47                 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
48                 fprintf(stderr, "%s: "fmt, argv0, ## __VA_ARGS__); \
49         } while (0)
50
51 enum {
52         FS_NONE,
53         FS_JFFS2,
54         FS_DEADCODE,
55 };
56
57 static const char *argv0;
58
59 /* this is a raw syscall - man 2 pivot_root */
60 extern int pivot_root(const char *new_root, const char *put_old);
61
62 static int debug = 0;
63
64 static void foreachdir(const char *dir, int (*cb)(const char*))
65 {
66         char globdir[256];
67         glob_t gl;
68         int j;
69
70         if (dir[strlen(dir) - 1] == '/')
71                 snprintf(globdir, 256, "%s*", dir);
72         else
73                 snprintf(globdir, 256, "%s/*", dir);
74
75         if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
76                 for (j = 0; j < gl.gl_pathc; j++)
77                         foreachdir(gl.gl_pathv[j], cb);
78
79         cb(dir);
80 }
81
82 static int find_overlay_mount(char *overlay)
83 {
84         FILE *fp = fopen("/proc/mounts", "r");
85         static char line[256];
86         int ret = -1;
87
88         if(!fp)
89                 return ret;
90
91         while (ret && fgets(line, sizeof(line), fp))
92                 if (!strncmp(line, overlay, strlen(overlay)))
93                         ret = 0;
94
95         fclose(fp);
96
97         return ret;
98 }
99
100 static char* find_mount(char *mp)
101 {
102         FILE *fp = fopen("/proc/mounts", "r");
103         static char line[256];
104         char *point = NULL;
105
106         if(!fp)
107                 return NULL;
108
109         while (fgets(line, sizeof(line), fp)) {
110                 char *s, *t = strstr(line, " ");
111
112                 if (!t)
113                         return NULL;
114                 t++;
115                 s = strstr(t, " ");
116                 if (!s)
117                         return NULL;
118                 *s = '\0';
119
120                 if (!strcmp(t, mp)) {
121                         fclose(fp);
122                         return t;
123                 }
124         }
125
126         fclose(fp);
127
128         return point;
129 }
130
131 static char* find_mount_point(char *block, char *fs)
132 {
133         FILE *fp = fopen("/proc/mounts", "r");
134         static char line[256];
135         int len = strlen(block);
136         char *point = NULL;
137
138         if(!fp)
139                 return NULL;
140
141         while (fgets(line, sizeof(line), fp)) {
142                 if (!strncmp(line, block, len)) {
143                         char *p = &line[len + 1];
144                         char *t = strstr(p, " ");
145
146                         if (!t)
147                                 return NULL;
148
149                         *t = '\0';
150                         t++;
151
152                         if (fs && strncmp(t, fs, strlen(fs))) {
153                                 ERROR("block is mounted with wrong fs\n");
154                                 return NULL;
155                         }
156                         point = p;
157                         break;
158                 }
159         }
160
161         fclose(fp);
162
163         return point;
164 }
165
166 static char* find_mtd_index(char *name)
167 {
168         FILE *fp = fopen("/proc/mtd", "r");
169         static char line[256];
170         char *index = NULL;
171
172         if(!fp)
173                 return index;
174
175         while (!index && fgets(line, sizeof(line), fp)) {
176                 if (strstr(line, name)) {
177                         char *eol = strstr(line, ":");
178
179                         if (!eol)
180                                 continue;
181
182                         *eol = '\0';
183                         index = &line[3];
184                         DEBUG(1, "found %s -> index:%s\n", name, index);
185                 }
186         }
187
188         fclose(fp);
189
190         return index;
191 }
192
193 static int find_mtd_block(char *name, char *part, int plen)
194 {
195         char *index = find_mtd_index(name);
196
197         if (!index)
198                 return -1;
199
200         snprintf(part, plen, "/dev/mtdblock%s", index);
201         DEBUG(1, "found %s -> %s\n", name, part);
202
203         return 0;
204 }
205
206 static int find_mtd_char(char *name, char *part, int plen)
207 {
208         char *index = find_mtd_index(name);
209
210         if (!index)
211                 return -1;
212
213         snprintf(part, plen, "/dev/mtd%s", index);
214         DEBUG(1, "found %s -> %s\n", name, part);
215
216         return 0;
217 }
218
219 static int mtd_unlock(char *mtd)
220 {
221         struct erase_info_user mtdlock;
222         struct mtd_info_user mtdinfo;
223         int fd = open(mtd, O_RDWR | O_SYNC);
224         int ret = -1;
225
226         DEBUG(1, "%s\n", mtd);
227
228         if (!fd) {
229                 ERROR("failed to open %s: %s\n", mtd, strerror(errno));
230                 return -1;
231         }
232
233         ret = ioctl(fd, MEMGETINFO, &mtdinfo);
234         if (ret) {
235                 ERROR("ioctl(%s, MEMGETINFO) failed: %s\n", mtd, strerror(errno));
236                 goto err_out;
237         }
238
239         mtdlock.start = 0;
240         mtdlock.length = mtdinfo.size;
241         ioctl(fd, MEMUNLOCK, &mtdlock);
242
243 err_out:
244         close(fd);
245
246         return ret;
247 }
248
249 static int mtd_mount_jffs2(void)
250 {
251         char rootfs_data[32];
252
253         if (mkdir("/tmp/overlay", 0755)) {
254                 ERROR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
255                 return -1;
256         }
257
258         if (find_mtd_block("rootfs_data", rootfs_data, sizeof(rootfs_data))) {
259                 ERROR("rootfs_data does not exist\n");
260                 return -1;
261         }
262
263         if (mount(rootfs_data, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
264                 ERROR("failed to mount -t jffs2 %s /tmp/overlay: %s\n", rootfs_data, strerror(errno));
265                 return -1;
266         }
267
268         find_mtd_char("rootfs_data", rootfs_data, sizeof(rootfs_data));
269
270         return mtd_unlock(rootfs_data);
271 }
272
273 static int jffs2_ready(char *mtd)
274 {
275         FILE *fp = fopen(mtd, "r");
276         __u32 deadc0de;
277         __u16 jffs2;
278         size_t sz;
279
280         if (!fp) {
281                 ERROR("reading %s failed\n", mtd);
282                 exit(-1);
283         }
284
285         sz = fread(&deadc0de, sizeof(deadc0de), 1, fp);
286         fclose(fp);
287
288         if (sz != 1) {
289                 ERROR("reading %s failed: %s\n", mtd, strerror(errno));
290                 exit(-1);
291         }
292
293         deadc0de = __be32_to_cpu(deadc0de);
294         jffs2 = __be16_to_cpu(deadc0de >> 16);
295
296         if (jffs2 == 0x1985) {
297                 LOG("jffs2 is ready\n");
298                 return FS_JFFS2;
299         }
300
301         if (deadc0de == 0xdeadc0de) {
302                 LOG("jffs2 is not ready - marker found\n");
303                 return FS_DEADCODE;
304         }
305
306         ERROR("No jffs2 marker was found\n");
307
308         return FS_NONE;
309 }
310
311 static int check_fs_exists(char *fs)
312 {
313         FILE *fp = fopen("/proc/filesystems", "r");
314         static char line[256];
315         int ret = -1;
316
317         DEBUG(2, "%s\n", fs);
318
319         if (!fp) {
320                 ERROR("opening /proc/filesystems failed: %s\n", strerror(errno));
321                 goto out;
322         }
323
324         while (ret && fgets(line, sizeof(line), fp))
325                 if (strstr(line, fs))
326                         ret = 0;
327
328         fclose(fp);
329
330 out:
331         return ret;
332 }
333
334 static int mount_move(char *oldroot, char *newroot, char *dir)
335 {
336 #ifndef MS_MOVE
337 #define MS_MOVE (1 << 13)
338 #endif
339         struct stat s;
340         char olddir[64];
341         char newdir[64];
342         int ret;
343
344         DEBUG(2, "%s %s %s\n", oldroot, newroot, dir);
345
346         snprintf(olddir, sizeof(olddir), "%s%s", oldroot, dir);
347         snprintf(newdir, sizeof(newdir), "%s%s", newroot, dir);
348
349         if (stat(olddir, &s) || !S_ISDIR(s.st_mode))
350                 return -1;
351
352         if (stat(newdir, &s) || !S_ISDIR(s.st_mode))
353                 return -1;
354
355         ret = mount(olddir, newdir, NULL, MS_NOATIME | MS_MOVE, NULL);
356
357         if (ret)
358                 DEBUG(1, "failed %s %s: %s\n", olddir, newdir, strerror(errno));
359
360         return ret;
361 }
362
363 static int pivot(char *new, char *old)
364 {
365         char pivotdir[64];
366         int ret;
367
368         DEBUG(2, "%s %s\n", new, old);
369
370         if (mount_move("", new, "/proc"))
371                 return -1;
372
373         snprintf(pivotdir, sizeof(pivotdir), "%s%s", new, old);
374
375         ret = pivot_root(new, pivotdir);
376
377         if (ret < 0) {
378                 ERROR("pivot_root failed %s %s: %s\n", new, pivotdir, strerror(errno));
379                 return -1;
380         }
381
382         mount_move(old, "", "/dev");
383         mount_move(old, "", "/tmp");
384         mount_move(old, "", "/sys");
385         mount_move(old, "", "/overlay");
386
387         return 0;
388 }
389
390 static int fopivot(char *rw_root, char *ro_root)
391 {
392         char overlay[64], lowerdir[64];
393
394         DEBUG(2, "%s %s\n", rw_root, ro_root);
395
396         if (check_fs_exists("overlay")) {
397                 ERROR("BUG: no suitable fs found\n");
398                 return -1;
399         }
400
401         snprintf(overlay, sizeof(overlay), "overlayfs:%s", rw_root);
402         snprintf(lowerdir, sizeof(lowerdir), "lowerdir=/,upperdir=%s", rw_root);
403
404         if (mount(overlay, "/mnt", "overlayfs", MS_NOATIME, lowerdir)) {
405                 ERROR("mount failed: %s\n", strerror(errno));
406                 return -1;
407         }
408
409         return pivot("/mnt", ro_root);
410 }
411
412 static int ramoverlay(void)
413 {
414         DEBUG(2, "\n");
415
416         mkdir("/tmp/root", 0755);
417         mount("tmpfs", "/tmp/root", "tmpfs", MS_NOATIME, "mode=0755");
418
419         return fopivot("/tmp/root", "/rom");
420 }
421
422 static int switch2jffs(void)
423 {
424         char mtd[32];
425
426         if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
427                 ERROR("no rootfs_data was found\n");
428                 return -1;
429         }
430
431         if (mount(mtd, "/rom/overlay", "jffs2", MS_NOATIME, NULL)) {
432                 ERROR("failed - mount -t jffs2 %s /rom/overlay: %s\n", mtd, strerror(errno));
433                 return -1;
434         }
435
436         if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
437                 ERROR("failed - mount -o remount,ro none: %s\n", strerror(errno));
438                 return -1;
439         }
440
441         system("cp -a /tmp/root/* /rom/overlay");
442
443         if (pivot("/rom", "/mnt")) {
444                 ERROR("failed - pivot /rom /mnt: %s\n", strerror(errno));
445                 return -1;
446         }
447
448         if (mount_move("/mnt", "/tmp/root", "")) {
449                 ERROR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
450                 return -1;
451         }
452
453         return fopivot("/overlay", "/rom");
454 }
455
456 static int handle_whiteout(const char *dir)
457 {
458         struct stat s;
459         char link[256];
460         ssize_t sz;
461         struct dirent **namelist;
462         int n;
463
464         n = scandir(dir, &namelist, NULL, NULL);
465
466         if (n < 1)
467                 return -1;
468
469         while (n--) {
470                 char file[256];
471
472                 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
473                 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
474                         sz = readlink(file, link, sizeof(link) - 1);
475                         if (sz > 0) {
476                                 char *orig;
477
478                                 link[sz] = '\0';
479                                 orig = strstr(&file[1], "/");
480                                 if (orig && !strcmp(link, "(overlay-whiteout)")) {
481                                         DEBUG(1, "unlinking %s\n", orig);
482                                         unlink(orig);
483                                 }
484                         }
485                 }
486                 free(namelist[n]);
487         }
488         free(namelist);
489
490         return 0;
491 }
492
493 static int main_switch2jffs(int argc, char **argv)
494 {
495         char mtd[32];
496         char *mp;
497         int ret = -1;
498
499         if (find_overlay_mount("overlayfs:/tmp/root"))
500                 return -1;
501
502         if (check_fs_exists("overlay")) {
503                 ERROR("overlayfs not found\n");
504                 return ret;
505         }
506
507         find_mtd_block("rootfs_data", mtd, sizeof(mtd));
508         mp = find_mount_point(mtd, NULL);
509         if (mp) {
510                 LOG("rootfs_data:%s is already mounted as %s\n", mtd, mp);
511                 return -1;
512         }
513
514         if (find_mtd_char("rootfs_data", mtd, sizeof(mtd))) {
515                 ERROR("no rootfs_data was found\n");
516                 return ret;
517         }
518
519         switch (jffs2_ready(mtd)) {
520         case FS_NONE:
521                 ERROR("no jffs2 marker found\n");
522                 /* fall through */
523
524         case FS_DEADCODE:
525                 ret = switch2jffs();
526                 if (!ret) {
527                         DEBUG(1, "doing fo cleanup\n");
528                         umount2("/tmp/root", MNT_DETACH);
529                         foreachdir("/overlay/", handle_whiteout);
530                 }
531                 break;
532
533         case FS_JFFS2:
534                 ret = mtd_mount_jffs2();
535                 if (ret)
536                         break;
537                 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
538                         ERROR("switching to jffs2 failed\n");
539                         ret = -1;
540                 }
541                 break;
542         }
543
544         return ret;
545 }
546
547 static int extroot(void)
548 {
549         struct stat s;
550         pid_t pid;
551
552         if (stat("/sbin/block", &s))
553                 return -1;
554
555         pid = fork();
556         if (!pid) {
557                 mkdir("/tmp/extroot", 0755);
558                 execl("/sbin/block", "/sbin/block", "extroot", NULL);
559                 exit(-1);
560         } else if (pid > 0) {
561                 int status;
562
563                 waitpid(pid, &status, 0);
564                 if (!WEXITSTATUS(status)) {
565                         if (find_mount("/tmp/mnt")) {
566                                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);
567
568                                 mkdir("/tmp/mnt/proc", 0755);
569                                 mkdir("/tmp/mnt/dev", 0755);
570                                 mkdir("/tmp/mnt/sys", 0755);
571                                 mkdir("/tmp/mnt/tmp", 0755);
572                                 mkdir("/tmp/mnt/rom", 0755);
573
574                                 if (mount_move("/tmp", "", "/mnt")) {
575                                         ERROR("moving pivotroot failed - continue normal boot\n");
576                                         umount("/tmp/mnt");
577                                 } else if (pivot("/mnt", "/rom")) {
578                                         ERROR("switching to pivotroot failed - continue normal boot\n");
579                                         umount("/mnt");
580                                 } else {
581                                         return 0;
582                                 }
583                         } else if (find_mount("/tmp/overlay")) {
584                                 if (mount_move("/tmp", "", "/overlay")) {
585                                         ERROR("moving extroot failed - continue normal boot\n");
586                                         umount("/tmp/overlay");
587                                 } else if (fopivot("/overlay", "/rom")) {
588                                         ERROR("switching to extroot failed - continue normal boot\n");
589                                         umount("/overlay");
590                                 } else {
591                                         return 0;
592                                 }
593                         }
594                 }
595         }
596         return -1;
597 }
598
599 int main(int argc, char **argv)
600 {
601         char *mp;
602         char mtd[32];
603
604         argv0 = basename(*argv);
605
606         if (!strcmp(basename(*argv), "switch2jffs"))
607                 return main_switch2jffs(argc, argv);
608
609         if (!getenv("PREINIT"))
610                 return -1;
611
612         if (find_mtd_char("rootfs_data", mtd, sizeof(mtd))) {
613                 if (!find_mtd_char("rootfs", mtd, sizeof(mtd)))
614                         mtd_unlock(mtd);
615                 LOG("mounting /dev/root\n");
616                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
617         } else {
618                 if (!extroot()) {
619                         fprintf(stderr, "mount_root: switched to extroot\n");
620                         return 0;
621                 }
622
623                 switch (jffs2_ready(mtd)) {
624                 case FS_NONE:
625                 case FS_DEADCODE:
626                         return ramoverlay();
627
628                 case FS_JFFS2:
629                         find_mtd_block("rootfs_data", mtd, sizeof(mtd));
630                         mp = find_mount_point(mtd, NULL);
631                         if (mp) {
632                                 LOG("rootfs_data:%s is already mounted as %s\n", mtd, mp);
633                                 return -1;
634                         }
635
636                         mtd_mount_jffs2();
637                         DEBUG(1, "switching to jffs2\n");
638                         if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
639                                 ERROR("switching to jffs2 failed - fallback to ramoverlay\n");
640                                 return ramoverlay();
641                         }
642                 }
643         }
644
645         return 0;
646 }