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>
24 #include "libfstools.h"
26 char const *extroot_prefix = NULL;
29 * This will execute "block extroot" and make use of mounted extroot or return
32 int mount_extroot(void)
43 /* try finding the library directory */
44 snprintf(ldlib_path, sizeof(ldlib_path), "%s/upper/lib", extroot_prefix);
46 if (stat(ldlib_path, &s) || !S_ISDIR(s.st_mode))
47 snprintf(ldlib_path, sizeof(ldlib_path), "%s/lib", extroot_prefix);
49 /* try finding the block executable */
50 snprintf(block_path, sizeof(block_path), "%s/upper/sbin/block", extroot_prefix);
52 if (stat(block_path, &s) || !S_ISREG(s.st_mode))
53 snprintf(block_path, sizeof(block_path), "%s/sbin/block", extroot_prefix);
55 if (stat(block_path, &s) || !S_ISREG(s.st_mode))
56 snprintf(block_path, sizeof(block_path), "/sbin/block");
58 if (stat(block_path, &s) || !S_ISREG(s.st_mode))
61 /* set LD_LIBRARY_PATH env var and load kmods from overlay if we found a lib directory there */
62 if (!stat(ldlib_path, &s) && S_ISDIR(s.st_mode)) {
63 ULOG_INFO("loading kmods from internal overlay\n");
64 setenv("LD_LIBRARY_PATH", ldlib_path, 1);
65 snprintf(kmod_loader, sizeof(kmod_loader),
66 "/sbin/kmodloader %s/etc/modules-boot.d/", dirname(ldlib_path));
67 if (system(kmod_loader))
68 ULOG_ERR("failed to launch kmodloader from internal overlay\n");
73 mkdir("/tmp/extroot", 0755);
74 execl(block_path, block_path, "extroot", NULL);
79 waitpid(pid, &status, 0);
80 if (!WEXITSTATUS(status)) {
81 if (find_mount("/tmp/extroot/mnt")) {
82 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);
84 mkdir("/tmp/extroot/mnt/proc", 0755);
85 mkdir("/tmp/extroot/mnt/dev", 0755);
86 mkdir("/tmp/extroot/mnt/sys", 0755);
87 mkdir("/tmp/extroot/mnt/tmp", 0755);
88 mkdir("/tmp/extroot/mnt/rom", 0755);
90 if (mount_move("/tmp/extroot", "", "/mnt")) {
91 ULOG_ERR("moving pivotroot failed - continue normal boot\n");
92 umount("/tmp/extroot/mnt");
93 } else if (pivot("/mnt", "/rom")) {
94 ULOG_ERR("switching to pivotroot failed - continue normal boot\n");
97 umount("/tmp/overlay");
98 rmdir("/tmp/overlay");
99 rmdir("/tmp/extroot/mnt");
100 rmdir("/tmp/extroot");
103 } else if (find_mount("/tmp/extroot/overlay")) {
104 if (mount_move("/tmp/extroot", "", "/overlay")) {
105 ULOG_ERR("moving extroot failed - continue normal boot\n");
106 umount("/tmp/extroot/overlay");
107 } else if (fopivot("/overlay", "/rom")) {
108 ULOG_ERR("switching to extroot failed - continue normal boot\n");
111 umount("/tmp/overlay");
112 rmdir("/tmp/overlay");
113 rmdir("/tmp/extroot/overlay");
114 rmdir("/tmp/extroot");