libfstools: extroot: simplify kmodloader invocation
[project/fstools.git] / libfstools / extroot.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 <unistd.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/mount.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22 #include <libgen.h>
23
24 #include "libfstools.h"
25
26 char const *extroot_prefix = NULL;
27
28 /*
29 * This will execute "block extroot" and make use of mounted extroot or return
30 * an error.
31 */
32 int mount_extroot(void)
33 {
34 char ldlib_path[32];
35 char block_path[32];
36 char kmod_loader[64];
37 struct stat s;
38 pid_t pid;
39
40 if (!extroot_prefix)
41 return -1;
42
43 /* try finding the library directory */
44 snprintf(ldlib_path, sizeof(ldlib_path), "%s/upper/lib", extroot_prefix);
45
46 if (stat(ldlib_path, &s) || !S_ISDIR(s.st_mode))
47 snprintf(ldlib_path, sizeof(ldlib_path), "%s/lib", extroot_prefix);
48
49 /* try finding the block executable */
50 snprintf(block_path, sizeof(block_path), "%s/upper/sbin/block", extroot_prefix);
51
52 if (stat(block_path, &s) || !S_ISREG(s.st_mode))
53 snprintf(block_path, sizeof(block_path), "%s/sbin/block", extroot_prefix);
54
55 if (stat(block_path, &s) || !S_ISREG(s.st_mode))
56 snprintf(block_path, sizeof(block_path), "/sbin/block");
57
58 if (stat(block_path, &s) || !S_ISREG(s.st_mode))
59 return -1;
60
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 setenv("LD_LIBRARY_PATH", ldlib_path, 1);
64 snprintf(kmod_loader, sizeof(kmod_loader),
65 "/sbin/kmodloader %s/etc/modules-boot.d/", dirname(ldlib_path));
66 system(kmod_loader);
67 }
68
69 pid = fork();
70 if (!pid) {
71 mkdir("/tmp/extroot", 0755);
72 execl(block_path, block_path, "extroot", NULL);
73 exit(-1);
74 } else if (pid > 0) {
75 int status;
76
77 waitpid(pid, &status, 0);
78 if (!WEXITSTATUS(status)) {
79 if (find_mount("/tmp/extroot/mnt")) {
80 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);
81
82 mkdir("/tmp/extroot/mnt/proc", 0755);
83 mkdir("/tmp/extroot/mnt/dev", 0755);
84 mkdir("/tmp/extroot/mnt/sys", 0755);
85 mkdir("/tmp/extroot/mnt/tmp", 0755);
86 mkdir("/tmp/extroot/mnt/rom", 0755);
87
88 if (mount_move("/tmp/extroot", "", "/mnt")) {
89 fprintf(stderr, "moving pivotroot failed - continue normal boot\n");
90 umount("/tmp/extroot/mnt");
91 } else if (pivot("/mnt", "/rom")) {
92 fprintf(stderr, "switching to pivotroot failed - continue normal boot\n");
93 umount("/mnt");
94 } else {
95 umount("/tmp/overlay");
96 rmdir("/tmp/overlay");
97 rmdir("/tmp/extroot/mnt");
98 rmdir("/tmp/extroot");
99 return 0;
100 }
101 } else if (find_mount("/tmp/extroot/overlay")) {
102 if (mount_move("/tmp/extroot", "", "/overlay")) {
103 fprintf(stderr, "moving extroot failed - continue normal boot\n");
104 umount("/tmp/extroot/overlay");
105 } else if (fopivot("/overlay", "/rom")) {
106 fprintf(stderr, "switching to extroot failed - continue normal boot\n");
107 umount("/overlay");
108 } else {
109 umount("/tmp/overlay");
110 rmdir("/tmp/overlay");
111 rmdir("/tmp/extroot/overlay");
112 rmdir("/tmp/extroot");
113 return 0;
114 }
115 }
116 }
117 }
118 return -1;
119 }