10f66f451Sopenharmony_ci/* switch_root.c - Switch from rootfs/initramfs to another filesystem
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2005 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_SWITCH_ROOT(NEWTOY(switch_root, "<2c:h", TOYFLAG_SBIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig SWITCH_ROOT
80f66f451Sopenharmony_ci  bool "switch_root"
90f66f451Sopenharmony_ci  default y
100f66f451Sopenharmony_ci  help
110f66f451Sopenharmony_ci    usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT...
120f66f451Sopenharmony_ci
130f66f451Sopenharmony_ci    Use from PID 1 under initramfs to free initramfs, chroot to NEW_ROOT,
140f66f451Sopenharmony_ci    and exec NEW_INIT.
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci    -c	Redirect console to device in NEW_ROOT
170f66f451Sopenharmony_ci    -h	Hang instead of exiting on failure (avoids kernel panic)
180f66f451Sopenharmony_ci*/
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci#define FOR_switch_root
210f66f451Sopenharmony_ci#include "toys.h"
220f66f451Sopenharmony_ci#include <sys/vfs.h>
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_ciGLOBALS(
250f66f451Sopenharmony_ci  char *c;
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_ci  dev_t rootdev;
280f66f451Sopenharmony_ci)
290f66f451Sopenharmony_ci
300f66f451Sopenharmony_cistatic int del_node(struct dirtree *node)
310f66f451Sopenharmony_ci{
320f66f451Sopenharmony_ci  if (node->st.st_dev == TT.rootdev && dirtree_notdotdot(node)) {
330f66f451Sopenharmony_ci    int flag = 0;
340f66f451Sopenharmony_ci    if (S_ISDIR(node->st.st_mode)) {
350f66f451Sopenharmony_ci      if (!node->again) return DIRTREE_COMEAGAIN;
360f66f451Sopenharmony_ci      flag = AT_REMOVEDIR;
370f66f451Sopenharmony_ci    }
380f66f451Sopenharmony_ci    unlinkat(dirtree_parentfd(node), node->name, flag);
390f66f451Sopenharmony_ci  }
400f66f451Sopenharmony_ci
410f66f451Sopenharmony_ci  return 0;
420f66f451Sopenharmony_ci}
430f66f451Sopenharmony_ci
440f66f451Sopenharmony_civoid switch_root_main(void)
450f66f451Sopenharmony_ci{
460f66f451Sopenharmony_ci  char *newroot = *toys.optargs, **cmdline = toys.optargs+1;
470f66f451Sopenharmony_ci  struct stat st1, st2;
480f66f451Sopenharmony_ci  struct statfs stfs;
490f66f451Sopenharmony_ci  int console QUIET;
500f66f451Sopenharmony_ci
510f66f451Sopenharmony_ci  if (getpid() != 1) error_exit("not pid 1");
520f66f451Sopenharmony_ci
530f66f451Sopenharmony_ci  // Root filesystem we're leaving must be ramfs or tmpfs
540f66f451Sopenharmony_ci  if (statfs("/", &stfs) ||
550f66f451Sopenharmony_ci    (stfs.f_type != 0x858458f6 && stfs.f_type != 0x01021994))
560f66f451Sopenharmony_ci  {
570f66f451Sopenharmony_ci    error_msg("not ramfs");
580f66f451Sopenharmony_ci    goto panic;
590f66f451Sopenharmony_ci  }
600f66f451Sopenharmony_ci
610f66f451Sopenharmony_ci  // New directory must be different filesystem instance
620f66f451Sopenharmony_ci  if (chdir(newroot) || stat(".", &st1) || stat("/", &st2) ||
630f66f451Sopenharmony_ci    st1.st_dev == st2.st_dev)
640f66f451Sopenharmony_ci  {
650f66f451Sopenharmony_ci    error_msg("bad newroot '%s'", newroot);
660f66f451Sopenharmony_ci    goto panic;
670f66f451Sopenharmony_ci  }
680f66f451Sopenharmony_ci  TT.rootdev=st2.st_dev;
690f66f451Sopenharmony_ci
700f66f451Sopenharmony_ci  // trim any / characters from the init cmdline, as we want to test it with
710f66f451Sopenharmony_ci  // stat(), relative to newroot. *cmdline is also used below, but by that
720f66f451Sopenharmony_ci  // point we are in the chroot, so a relative path is still OK.
730f66f451Sopenharmony_ci  while (**cmdline == '/') (*cmdline)++;
740f66f451Sopenharmony_ci
750f66f451Sopenharmony_ci  // init program must exist and be an executable file
760f66f451Sopenharmony_ci  if (stat(*cmdline, &st1) || !S_ISREG(st1.st_mode) || !(st1.st_mode&0100)) {
770f66f451Sopenharmony_ci    error_msg("bad init");
780f66f451Sopenharmony_ci    goto panic;
790f66f451Sopenharmony_ci  }
800f66f451Sopenharmony_ci
810f66f451Sopenharmony_ci  if (TT.c && -1 == (console = open(TT.c, O_RDWR))) {
820f66f451Sopenharmony_ci    perror_msg("bad console '%s'", TT.c);
830f66f451Sopenharmony_ci    goto panic;
840f66f451Sopenharmony_ci  }
850f66f451Sopenharmony_ci
860f66f451Sopenharmony_ci  // Ok, enough safety checks: wipe root partition.
870f66f451Sopenharmony_ci  dirtree_read("/", del_node);
880f66f451Sopenharmony_ci
890f66f451Sopenharmony_ci  // Fix the appearance of the mount table in the newroot chroot
900f66f451Sopenharmony_ci  if (mount(".", "/", NULL, MS_MOVE, NULL)) {
910f66f451Sopenharmony_ci    perror_msg("mount");
920f66f451Sopenharmony_ci    goto panic;
930f66f451Sopenharmony_ci  }
940f66f451Sopenharmony_ci
950f66f451Sopenharmony_ci  // Enter the new root before starting init
960f66f451Sopenharmony_ci  if (chroot(".")) {
970f66f451Sopenharmony_ci    perror_msg("chroot");
980f66f451Sopenharmony_ci    goto panic;
990f66f451Sopenharmony_ci  }
1000f66f451Sopenharmony_ci
1010f66f451Sopenharmony_ci  // Make sure cwd does not point outside of the chroot
1020f66f451Sopenharmony_ci  if (chdir("/")) {
1030f66f451Sopenharmony_ci    perror_msg("chdir");
1040f66f451Sopenharmony_ci    goto panic;
1050f66f451Sopenharmony_ci  }
1060f66f451Sopenharmony_ci
1070f66f451Sopenharmony_ci  if (TT.c) {
1080f66f451Sopenharmony_ci    int i;
1090f66f451Sopenharmony_ci    for (i=0; i<3; i++) if (console != i) dup2(console, i);
1100f66f451Sopenharmony_ci    if (console>2) close(console);
1110f66f451Sopenharmony_ci  }
1120f66f451Sopenharmony_ci  execv(*cmdline, cmdline);
1130f66f451Sopenharmony_ci  perror_msg("Failed to exec '%s'", *cmdline);
1140f66f451Sopenharmony_cipanic:
1150f66f451Sopenharmony_ci  if (toys.optflags & FLAG_h) for (;;) wait(NULL);
1160f66f451Sopenharmony_ci}
117