1*cf5a6c84SAndroid Build Coastguard Worker /* switch_root.c - Switch from rootfs/initramfs to another filesystem
2*cf5a6c84SAndroid Build Coastguard Worker *
3*cf5a6c84SAndroid Build Coastguard Worker * Copyright 2005 Rob Landley <[email protected]>
4*cf5a6c84SAndroid Build Coastguard Worker
5*cf5a6c84SAndroid Build Coastguard Worker USE_SWITCH_ROOT(NEWTOY(switch_root, "<2c:h", TOYFLAG_SBIN))
6*cf5a6c84SAndroid Build Coastguard Worker
7*cf5a6c84SAndroid Build Coastguard Worker config SWITCH_ROOT
8*cf5a6c84SAndroid Build Coastguard Worker bool "switch_root"
9*cf5a6c84SAndroid Build Coastguard Worker default y
10*cf5a6c84SAndroid Build Coastguard Worker help
11*cf5a6c84SAndroid Build Coastguard Worker usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT...
12*cf5a6c84SAndroid Build Coastguard Worker
13*cf5a6c84SAndroid Build Coastguard Worker Use from PID 1 under initramfs to free initramfs, chroot to NEW_ROOT,
14*cf5a6c84SAndroid Build Coastguard Worker and exec NEW_INIT.
15*cf5a6c84SAndroid Build Coastguard Worker
16*cf5a6c84SAndroid Build Coastguard Worker -c Redirect console to device in NEW_ROOT
17*cf5a6c84SAndroid Build Coastguard Worker -h Hang instead of exiting on failure (avoids kernel panic)
18*cf5a6c84SAndroid Build Coastguard Worker */
19*cf5a6c84SAndroid Build Coastguard Worker
20*cf5a6c84SAndroid Build Coastguard Worker #define FOR_switch_root
21*cf5a6c84SAndroid Build Coastguard Worker #include "toys.h"
22*cf5a6c84SAndroid Build Coastguard Worker
GLOBALS(char * c;struct stat new;dev_t rootdev;)23*cf5a6c84SAndroid Build Coastguard Worker GLOBALS(
24*cf5a6c84SAndroid Build Coastguard Worker char *c;
25*cf5a6c84SAndroid Build Coastguard Worker
26*cf5a6c84SAndroid Build Coastguard Worker struct stat new;
27*cf5a6c84SAndroid Build Coastguard Worker dev_t rootdev;
28*cf5a6c84SAndroid Build Coastguard Worker )
29*cf5a6c84SAndroid Build Coastguard Worker
30*cf5a6c84SAndroid Build Coastguard Worker static int del_node(struct dirtree *node)
31*cf5a6c84SAndroid Build Coastguard Worker {
32*cf5a6c84SAndroid Build Coastguard Worker int flag = 0;
33*cf5a6c84SAndroid Build Coastguard Worker
34*cf5a6c84SAndroid Build Coastguard Worker if (same_file(&TT.new, &node->st) || !dirtree_notdotdot(node)) return 0;
35*cf5a6c84SAndroid Build Coastguard Worker
36*cf5a6c84SAndroid Build Coastguard Worker if (node->st.st_dev != TT.rootdev) {
37*cf5a6c84SAndroid Build Coastguard Worker char *s = dirtree_path(node, 0);
38*cf5a6c84SAndroid Build Coastguard Worker
39*cf5a6c84SAndroid Build Coastguard Worker if (mount(s, s+1, "", MS_MOVE, "")) perror_msg("Failed to move %s", s);
40*cf5a6c84SAndroid Build Coastguard Worker // TODO: handle undermounts
41*cf5a6c84SAndroid Build Coastguard Worker rmdir(s);
42*cf5a6c84SAndroid Build Coastguard Worker free(s);
43*cf5a6c84SAndroid Build Coastguard Worker
44*cf5a6c84SAndroid Build Coastguard Worker return 0;
45*cf5a6c84SAndroid Build Coastguard Worker }
46*cf5a6c84SAndroid Build Coastguard Worker
47*cf5a6c84SAndroid Build Coastguard Worker if (S_ISDIR(node->st.st_mode)) {
48*cf5a6c84SAndroid Build Coastguard Worker if (!node->again) return DIRTREE_COMEAGAIN;
49*cf5a6c84SAndroid Build Coastguard Worker flag = AT_REMOVEDIR;
50*cf5a6c84SAndroid Build Coastguard Worker }
51*cf5a6c84SAndroid Build Coastguard Worker unlinkat(dirtree_parentfd(node), node->name, flag);
52*cf5a6c84SAndroid Build Coastguard Worker
53*cf5a6c84SAndroid Build Coastguard Worker return 0;
54*cf5a6c84SAndroid Build Coastguard Worker }
55*cf5a6c84SAndroid Build Coastguard Worker
switch_root_main(void)56*cf5a6c84SAndroid Build Coastguard Worker void switch_root_main(void)
57*cf5a6c84SAndroid Build Coastguard Worker {
58*cf5a6c84SAndroid Build Coastguard Worker char *newroot = *toys.optargs, **cmdline = toys.optargs+1;
59*cf5a6c84SAndroid Build Coastguard Worker struct stat st;
60*cf5a6c84SAndroid Build Coastguard Worker struct statfs stfs;
61*cf5a6c84SAndroid Build Coastguard Worker int ii, console QUIET;
62*cf5a6c84SAndroid Build Coastguard Worker
63*cf5a6c84SAndroid Build Coastguard Worker // Must be root on a ramfs or tmpfs instance
64*cf5a6c84SAndroid Build Coastguard Worker if (getpid() != 1) error_exit("not pid 1");
65*cf5a6c84SAndroid Build Coastguard Worker if (statfs("/", &stfs) ||
66*cf5a6c84SAndroid Build Coastguard Worker (stfs.f_type != 0x858458f6 && stfs.f_type != 0x01021994))
67*cf5a6c84SAndroid Build Coastguard Worker {
68*cf5a6c84SAndroid Build Coastguard Worker error_msg("not ramfs");
69*cf5a6c84SAndroid Build Coastguard Worker goto panic;
70*cf5a6c84SAndroid Build Coastguard Worker }
71*cf5a6c84SAndroid Build Coastguard Worker
72*cf5a6c84SAndroid Build Coastguard Worker // New directory must be different filesystem instance
73*cf5a6c84SAndroid Build Coastguard Worker if (chdir(newroot) || stat(".", &TT.new) || stat("/", &st) ||
74*cf5a6c84SAndroid Build Coastguard Worker same_file(&TT.new, &st))
75*cf5a6c84SAndroid Build Coastguard Worker {
76*cf5a6c84SAndroid Build Coastguard Worker error_msg("bad newroot '%s'", newroot);
77*cf5a6c84SAndroid Build Coastguard Worker goto panic;
78*cf5a6c84SAndroid Build Coastguard Worker }
79*cf5a6c84SAndroid Build Coastguard Worker TT.rootdev = st.st_dev;
80*cf5a6c84SAndroid Build Coastguard Worker
81*cf5a6c84SAndroid Build Coastguard Worker // trim any / characters from the init cmdline, as we want to test it with
82*cf5a6c84SAndroid Build Coastguard Worker // stat(), relative to newroot. *cmdline is also used below, but by that
83*cf5a6c84SAndroid Build Coastguard Worker // point we are in the chroot, so a relative path is still OK.
84*cf5a6c84SAndroid Build Coastguard Worker while (**cmdline == '/') (*cmdline)++;
85*cf5a6c84SAndroid Build Coastguard Worker
86*cf5a6c84SAndroid Build Coastguard Worker // init program must exist and be an executable file
87*cf5a6c84SAndroid Build Coastguard Worker if (stat(*cmdline, &st) || !S_ISREG(st.st_mode) || !(st.st_mode&0100)) {
88*cf5a6c84SAndroid Build Coastguard Worker error_msg("bad init");
89*cf5a6c84SAndroid Build Coastguard Worker goto panic;
90*cf5a6c84SAndroid Build Coastguard Worker }
91*cf5a6c84SAndroid Build Coastguard Worker
92*cf5a6c84SAndroid Build Coastguard Worker if (TT.c && -1 == (console = open(TT.c, O_RDWR))) {
93*cf5a6c84SAndroid Build Coastguard Worker perror_msg("bad console '%s'", TT.c);
94*cf5a6c84SAndroid Build Coastguard Worker goto panic;
95*cf5a6c84SAndroid Build Coastguard Worker }
96*cf5a6c84SAndroid Build Coastguard Worker
97*cf5a6c84SAndroid Build Coastguard Worker // Ok, enough safety checks: wipe root partition.
98*cf5a6c84SAndroid Build Coastguard Worker dirtree_read("/", del_node);
99*cf5a6c84SAndroid Build Coastguard Worker
100*cf5a6c84SAndroid Build Coastguard Worker // Enter the new root before starting init
101*cf5a6c84SAndroid Build Coastguard Worker if (chroot(".")) {
102*cf5a6c84SAndroid Build Coastguard Worker perror_msg("chroot");
103*cf5a6c84SAndroid Build Coastguard Worker goto panic;
104*cf5a6c84SAndroid Build Coastguard Worker }
105*cf5a6c84SAndroid Build Coastguard Worker
106*cf5a6c84SAndroid Build Coastguard Worker // Make sure cwd does not point outside of the chroot
107*cf5a6c84SAndroid Build Coastguard Worker if (chdir("/")) {
108*cf5a6c84SAndroid Build Coastguard Worker perror_msg("chdir");
109*cf5a6c84SAndroid Build Coastguard Worker goto panic;
110*cf5a6c84SAndroid Build Coastguard Worker }
111*cf5a6c84SAndroid Build Coastguard Worker
112*cf5a6c84SAndroid Build Coastguard Worker if (TT.c) {
113*cf5a6c84SAndroid Build Coastguard Worker for (ii = 0; ii<3; ii++) dup2(console, ii);
114*cf5a6c84SAndroid Build Coastguard Worker if (console>2) close(console);
115*cf5a6c84SAndroid Build Coastguard Worker }
116*cf5a6c84SAndroid Build Coastguard Worker execv(*cmdline, cmdline);
117*cf5a6c84SAndroid Build Coastguard Worker perror_msg("Failed to exec '%s'", *cmdline);
118*cf5a6c84SAndroid Build Coastguard Worker panic:
119*cf5a6c84SAndroid Build Coastguard Worker if (FLAG(h)) for (;;) wait(NULL);
120*cf5a6c84SAndroid Build Coastguard Worker }
121