2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
18 #include <sys/mount.h>
19 #include <sys/types.h>
23 #include "libfstools.h"
25 char const *extroot_prefix = NULL;
28 * This will execute "block extroot" and make use of mounted extroot or return
31 int mount_extroot(void)
42 sprintf(ldlib_path, "%s/lib", extroot_prefix);
43 sprintf(block_path, "%s/sbin/block", extroot_prefix);
45 if (stat(block_path, &s))
48 sprintf(kmod_loader, "/sbin/kmodloader %s/etc/modules-boot.d/ %s", extroot_prefix, extroot_prefix);
53 mkdir("/tmp/extroot", 0755);
54 setenv("LD_LIBRARY_PATH", ldlib_path, 1);
55 execl(block_path, block_path, "extroot", NULL);
60 waitpid(pid, &status, 0);
61 if (!WEXITSTATUS(status)) {
62 if (find_mount("/tmp/extroot/mnt")) {
63 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);
65 mkdir("/tmp/extroot/mnt/proc", 0755);
66 mkdir("/tmp/extroot/mnt/dev", 0755);
67 mkdir("/tmp/extroot/mnt/sys", 0755);
68 mkdir("/tmp/extroot/mnt/tmp", 0755);
69 mkdir("/tmp/extroot/mnt/rom", 0755);
71 if (mount_move("/tmp/extroot", "", "/mnt")) {
72 fprintf(stderr, "moving pivotroot failed - continue normal boot\n");
73 umount("/tmp/extroot/mnt");
74 } else if (pivot("/mnt", "/rom")) {
75 fprintf(stderr, "switching to pivotroot failed - continue normal boot\n");
78 umount("/tmp/overlay");
79 rmdir("/tmp/overlay");
80 rmdir("/tmp/extroot/mnt");
81 rmdir("/tmp/extroot");
84 } else if (find_mount("/tmp/extroot/overlay")) {
85 if (mount_move("/tmp/extroot", "", "/overlay")) {
86 fprintf(stderr, "moving extroot failed - continue normal boot\n");
87 umount("/tmp/extroot/overlay");
88 } else if (fopivot("/overlay", "/rom")) {
89 fprintf(stderr, "switching to extroot failed - continue normal boot\n");
92 umount("/tmp/overlay");
93 rmdir("/tmp/overlay");
94 rmdir("/tmp/extroot/overlay");
95 rmdir("/tmp/extroot");