switch_root.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* switch_root.c - Switch from rootfs/initramfs to another filesystem
  2. *
  3. * Copyright 2005 Rob Landley <rob@landley.net>
  4. USE_SWITCH_ROOT(NEWTOY(switch_root, "<2c:h", TOYFLAG_SBIN))
  5. config SWITCH_ROOT
  6. bool "switch_root"
  7. default y
  8. help
  9. usage: switch_root [-c /dev/console] NEW_ROOT NEW_INIT...
  10. Use from PID 1 under initramfs to free initramfs, chroot to NEW_ROOT,
  11. and exec NEW_INIT.
  12. -c Redirect console to device in NEW_ROOT
  13. -h Hang instead of exiting on failure (avoids kernel panic)
  14. */
  15. #define FOR_switch_root
  16. #include "toys.h"
  17. #include <sys/vfs.h>
  18. GLOBALS(
  19. char *c;
  20. dev_t rootdev;
  21. )
  22. static int del_node(struct dirtree *node)
  23. {
  24. if (node->st.st_dev == TT.rootdev && dirtree_notdotdot(node)) {
  25. int flag = 0;
  26. if (S_ISDIR(node->st.st_mode)) {
  27. if (!node->again) return DIRTREE_COMEAGAIN;
  28. flag = AT_REMOVEDIR;
  29. }
  30. unlinkat(dirtree_parentfd(node), node->name, flag);
  31. }
  32. return 0;
  33. }
  34. void switch_root_main(void)
  35. {
  36. char *newroot = *toys.optargs, **cmdline = toys.optargs+1;
  37. struct stat st1, st2;
  38. struct statfs stfs;
  39. int console QUIET;
  40. if (getpid() != 1) error_exit("not pid 1");
  41. // Root filesystem we're leaving must be ramfs or tmpfs
  42. if (statfs("/", &stfs) ||
  43. (stfs.f_type != 0x858458f6 && stfs.f_type != 0x01021994))
  44. {
  45. error_msg("not ramfs");
  46. goto panic;
  47. }
  48. // New directory must be different filesystem instance
  49. if (chdir(newroot) || stat(".", &st1) || stat("/", &st2) ||
  50. st1.st_dev == st2.st_dev)
  51. {
  52. error_msg("bad newroot '%s'", newroot);
  53. goto panic;
  54. }
  55. TT.rootdev=st2.st_dev;
  56. // trim any / characters from the init cmdline, as we want to test it with
  57. // stat(), relative to newroot. *cmdline is also used below, but by that
  58. // point we are in the chroot, so a relative path is still OK.
  59. while (**cmdline == '/') (*cmdline)++;
  60. // init program must exist and be an executable file
  61. if (stat(*cmdline, &st1) || !S_ISREG(st1.st_mode) || !(st1.st_mode&0100)) {
  62. error_msg("bad init");
  63. goto panic;
  64. }
  65. if (TT.c && -1 == (console = open(TT.c, O_RDWR))) {
  66. perror_msg("bad console '%s'", TT.c);
  67. goto panic;
  68. }
  69. // Ok, enough safety checks: wipe root partition.
  70. dirtree_read("/", del_node);
  71. // Fix the appearance of the mount table in the newroot chroot
  72. if (mount(".", "/", NULL, MS_MOVE, NULL)) {
  73. perror_msg("mount");
  74. goto panic;
  75. }
  76. // Enter the new root before starting init
  77. if (chroot(".")) {
  78. perror_msg("chroot");
  79. goto panic;
  80. }
  81. // Make sure cwd does not point outside of the chroot
  82. if (chdir("/")) {
  83. perror_msg("chdir");
  84. goto panic;
  85. }
  86. if (TT.c) {
  87. int i;
  88. for (i=0; i<3; i++) if (console != i) dup2(console, i);
  89. if (console>2) close(console);
  90. }
  91. execv(*cmdline, cmdline);
  92. perror_msg("Failed to exec '%s'", *cmdline);
  93. panic:
  94. if (toys.optflags & FLAG_h) for (;;) wait(NULL);
  95. }