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