Mercurial > hg > toybox
comparison toys/other/switch_root.c @ 664:c60ac785784f
Add switch_root and fix infrastructure to understand name "switch_root".
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 08 Sep 2012 01:27:54 -0500 |
parents | |
children | 7e846e281e38 |
comparison
equal
deleted
inserted
replaced
663:60cbc87c4314 | 664:c60ac785784f |
---|---|
1 /* switch_root.c - Switch from rootfs/initramfs to another filesystem | |
2 * | |
3 * Copyright 2005 Rob Landley <rob@landley.net> | |
4 | |
5 USE_SWITCH_ROOT(NEWTOY(switch_root, "<2c:h", TOYFLAG_SBIN)) | |
6 | |
7 config SWITCH_ROOT | |
8 bool "switch_root" | |
9 default y | |
10 help | |
11 usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT... | |
12 | |
13 Use from PID 1 under initramfs to free initramfs, chroot to NEW_ROOT, | |
14 and exec NEW_INIT. | |
15 | |
16 -c Redirect console to device in NEW_ROOT | |
17 -h Hang instead of exiting on failure (avoids kernel panic) | |
18 */ | |
19 | |
20 #include "toys.h" | |
21 #include <sys/vfs.h> | |
22 | |
23 DEFINE_GLOBALS( | |
24 char *console; | |
25 | |
26 dev_t rootdev; | |
27 ) | |
28 | |
29 #define TT this.switch_root | |
30 | |
31 #define FLAG_h (1<<0) | |
32 #define FLAG_c (1<<1) | |
33 | |
34 static int del_node(struct dirtree *node) | |
35 { | |
36 if (node->st.st_dev == TT.rootdev && dirtree_notdotdot(node)) { | |
37 int flag = 0; | |
38 if (S_ISDIR(node->st.st_mode)) { | |
39 if (node->data != -1) return DIRTREE_COMEAGAIN; | |
40 flag = AT_REMOVEDIR; | |
41 } | |
42 unlinkat(dirtree_parentfd(node), node->name, flag); | |
43 } | |
44 | |
45 return 0; | |
46 } | |
47 | |
48 void switch_root_main(void) | |
49 { | |
50 char *newroot = *toys.optargs, **cmdline = toys.optargs+1; | |
51 struct stat st1, st2; | |
52 struct statfs stfs; | |
53 int console = console; // gcc's "may be used" warnings are broken. | |
54 | |
55 if (getpid() != 1) error_exit("not pid 1"); | |
56 | |
57 // Root filesystem we're leaving must be ramfs or tmpfs | |
58 if (statfs("/", &stfs) || | |
59 (stfs.f_type != 0x858458f6 && stfs.f_type != 0x01021994)) | |
60 { | |
61 error_msg("not ramfs"); | |
62 goto panic; | |
63 } | |
64 | |
65 // New directory must be different filesystem instance | |
66 if (chdir(newroot) || stat(".", &st1) || stat("/", &st2) || | |
67 st1.st_dev == st2.st_dev) | |
68 { | |
69 error_msg("bad newroot '%s'", newroot); | |
70 goto panic; | |
71 } | |
72 TT.rootdev=st2.st_dev; | |
73 | |
74 // init program must exist and be an executable file | |
75 if (stat("init", &st1) || !S_ISREG(st1.st_mode) || !(st1.st_mode&0100)) { | |
76 error_msg("bad init"); | |
77 goto panic; | |
78 } | |
79 | |
80 if (TT.console && -1 == (console = open(TT.console, O_RDWR))) { | |
81 perror_msg("bad console '%s'", TT.console); | |
82 goto panic; | |
83 } | |
84 | |
85 // Ok, enough safety checks: wipe root partition. | |
86 dirtree_read("/", del_node); | |
87 | |
88 if (TT.console) { | |
89 int i; | |
90 for (i=0; i<3; i++) if (console != i) dup2(console, i); | |
91 if (console>2) close(console); | |
92 } | |
93 execv(*cmdline, cmdline); | |
94 perror_msg("Failed to exec '%s'", *cmdline); | |
95 panic: | |
96 if (toys.optflags & FLAG_h) for (;;) wait(NULL); | |
97 } |