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