ca38ce132b8857c6295d8836fe2785eb20d13a23
[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
23 #include "libfstools.h"
24
25 char const *extroot_prefix = NULL;
26
27 /*
28  * This will execute "block extroot" and make use of mounted extroot or return
29  * an error.
30  */
31 int mount_extroot(void)
32 {
33         char ldlib_path[32];
34         char block_path[32];
35         char kmod_loader[64];
36         struct stat s;
37         pid_t pid;
38
39         if (!extroot_prefix)
40                 return -1;
41
42         sprintf(ldlib_path, "%s/lib", extroot_prefix);
43         sprintf(block_path, "%s/sbin/block", extroot_prefix);
44
45         if (stat(block_path, &s)) {
46                 sprintf(block_path, "/sbin/block");
47                 if (stat(block_path, &s))
48                         return -1;
49         }
50
51         sprintf(kmod_loader, "/sbin/kmodloader %s/etc/modules-boot.d/ %s", extroot_prefix, extroot_prefix);
52         system(kmod_loader);
53
54         pid = fork();
55         if (!pid) {
56                 mkdir("/tmp/extroot", 0755);
57                 setenv("LD_LIBRARY_PATH", ldlib_path, 1);
58                 execl(block_path, block_path, "extroot", NULL);
59                 exit(-1);
60         } else if (pid > 0) {
61                 int status;
62
63                 waitpid(pid, &status, 0);
64                 if (!WEXITSTATUS(status)) {
65                         if (find_mount("/tmp/extroot/mnt")) {
66                                 mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT | MS_RDONLY, 0);
67
68                                 mkdir("/tmp/extroot/mnt/proc", 0755);
69                                 mkdir("/tmp/extroot/mnt/dev", 0755);
70                                 mkdir("/tmp/extroot/mnt/sys", 0755);
71                                 mkdir("/tmp/extroot/mnt/tmp", 0755);
72                                 mkdir("/tmp/extroot/mnt/rom", 0755);
73
74                                 if (mount_move("/tmp/extroot", "", "/mnt")) {
75                                         fprintf(stderr, "moving pivotroot failed - continue normal boot\n");
76                                         umount("/tmp/extroot/mnt");
77                                 } else if (pivot("/mnt", "/rom")) {
78                                         fprintf(stderr, "switching to pivotroot failed - continue normal boot\n");
79                                         umount("/mnt");
80                                 } else {
81                                         umount("/tmp/overlay");
82                                         rmdir("/tmp/overlay");
83                                         rmdir("/tmp/extroot/mnt");
84                                         rmdir("/tmp/extroot");
85                                         return 0;
86                                 }
87                         } else if (find_mount("/tmp/extroot/overlay")) {
88                                 if (mount_move("/tmp/extroot", "", "/overlay")) {
89                                         fprintf(stderr, "moving extroot failed - continue normal boot\n");
90                                         umount("/tmp/extroot/overlay");
91                                 } else if (fopivot("/overlay", "/rom")) {
92                                         fprintf(stderr, "switching to extroot failed - continue normal boot\n");
93                                         umount("/overlay");
94                                 } else {
95                                         umount("/tmp/overlay");
96                                         rmdir("/tmp/overlay");
97                                         rmdir("/tmp/extroot/overlay");
98                                         rmdir("/tmp/extroot");
99                                         return 0;
100                                 }
101                         }
102                 }
103         }
104         return -1;
105 }