switch_root.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* vi: set sw=4 ts=4: */
  2. /* Copyright 2005 Rob Landley <rob@landley.net>
  3. *
  4. * Switch from rootfs to another filesystem as the root of the mount tree.
  5. *
  6. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  7. */
  8. #include "libbb.h"
  9. #include <sys/vfs.h>
  10. // Make up for header deficiencies
  11. #ifndef RAMFS_MAGIC
  12. #define RAMFS_MAGIC ((unsigned)0x858458f6)
  13. #endif
  14. #ifndef TMPFS_MAGIC
  15. #define TMPFS_MAGIC ((unsigned)0x01021994)
  16. #endif
  17. #ifndef MS_MOVE
  18. #define MS_MOVE 8192
  19. #endif
  20. // Recursively delete contents of rootfs
  21. static void delete_contents(const char *directory, dev_t rootdev)
  22. {
  23. DIR *dir;
  24. struct dirent *d;
  25. struct stat st;
  26. // Don't descend into other filesystems
  27. if (lstat(directory, &st) || st.st_dev != rootdev)
  28. return;
  29. // Recursively delete the contents of directories
  30. if (S_ISDIR(st.st_mode)) {
  31. dir = opendir(directory);
  32. if (dir) {
  33. while ((d = readdir(dir))) {
  34. char *newdir = d->d_name;
  35. // Skip . and ..
  36. if (DOT_OR_DOTDOT(newdir))
  37. continue;
  38. // Recurse to delete contents
  39. newdir = concat_path_file(directory, newdir);
  40. delete_contents(newdir, rootdev);
  41. free(newdir);
  42. }
  43. closedir(dir);
  44. // Directory should now be empty, zap it
  45. rmdir(directory);
  46. }
  47. } else {
  48. // It wasn't a directory, zap it
  49. unlink(directory);
  50. }
  51. }
  52. int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  53. int switch_root_main(int argc UNUSED_PARAM, char **argv)
  54. {
  55. char *newroot, *console = NULL;
  56. struct stat st;
  57. struct statfs stfs;
  58. dev_t rootdev;
  59. // Parse args (-c console)
  60. opt_complementary = "-2"; // minimum 2 params
  61. getopt32(argv, "+c:", &console); // '+': stop at first non-option
  62. argv += optind;
  63. newroot = *argv++;
  64. // Change to new root directory and verify it's a different fs
  65. xchdir(newroot);
  66. xstat("/", &st);
  67. rootdev = st.st_dev;
  68. xstat(".", &st);
  69. if (st.st_dev == rootdev || getpid() != 1) {
  70. // Show usage, it says new root must be a mountpoint
  71. // and we must be PID 1
  72. bb_show_usage();
  73. }
  74. // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
  75. // we mean it. I could make this a CONFIG option, but I would get email
  76. // from all the people who WILL destroy their filesystems.
  77. statfs("/", &stfs); // this never fails
  78. if (lstat("/init", &st) != 0 || !S_ISREG(st.st_mode)
  79. || ((unsigned)stfs.f_type != RAMFS_MAGIC
  80. && (unsigned)stfs.f_type != TMPFS_MAGIC)
  81. ) {
  82. bb_error_msg_and_die("not rootfs");
  83. }
  84. // Zap everything out of rootdev
  85. delete_contents("/", rootdev);
  86. // Overmount / with newdir and chroot into it
  87. if (mount(".", "/", NULL, MS_MOVE, NULL)) {
  88. // For example, fails when newroot is not a mountpoint
  89. bb_perror_msg_and_die("error moving root");
  90. }
  91. // The chdir is needed to recalculate "." and ".." links
  92. xchroot(".");
  93. xchdir("/");
  94. // If a new console specified, redirect stdin/stdout/stderr to it
  95. if (console) {
  96. close(0);
  97. xopen(console, O_RDWR);
  98. xdup2(0, 1);
  99. xdup2(0, 2);
  100. }
  101. // Exec real init
  102. execv(argv[0], argv);
  103. bb_perror_msg_and_die("can't execute '%s'", argv[0]);
  104. }