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