annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
20 #include "toys.h"
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
21 #include <sys/vfs.h>
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
22
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
23 DEFINE_GLOBALS(
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
24 char *console;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
25
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
26 dev_t rootdev;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
27 )
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 #define TT this.switch_root
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
30
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
31 #define FLAG_h (1<<0)
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
32 #define FLAG_c (1<<1)
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
33
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
34 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
35 {
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
36 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
37 int flag = 0;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
38 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
39 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
40 flag = AT_REMOVEDIR;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
42 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
43 }
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
44
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
45 return 0;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
46 }
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
47
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
48 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
49 {
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
50 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
51 struct stat st1, st2;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
52 struct statfs stfs;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
53 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
54
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
55 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
56
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
57 // 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
58 if (statfs("/", &stfs) ||
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
59 (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
60 {
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
61 error_msg("not ramfs");
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
62 goto panic;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
63 }
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 // 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
66 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
67 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
68 {
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
69 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
70 goto panic;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
71 }
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
72 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
73
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
74 // 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
75 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
76 error_msg("bad init");
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
77 goto panic;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
78 }
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 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
81 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
82 goto panic;
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
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
85 // 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
86 dirtree_read("/", del_node);
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
87
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
88 if (TT.console) {
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
89 int i;
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
90 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
91 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
92 }
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
93 execv(*cmdline, cmdline);
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
94 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
95 panic:
c60ac785784f Add switch_root and fix infrastructure to understand name "switch_root".
Rob Landley <rob@landley.net>
parents:
diff changeset
96 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
97 }