switch_root.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // It wasn't a directory. Zap it.
  48. } else unlink(directory);
  49. }
  50. int switch_root_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  51. int switch_root_main(int argc UNUSED_PARAM, char **argv)
  52. {
  53. char *newroot, *console = NULL;
  54. struct stat st1, st2;
  55. struct statfs stfs;
  56. dev_t rootdev;
  57. // Parse args (-c console)
  58. opt_complementary = "-2"; // minimum 2 params
  59. getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option
  60. argv += optind;
  61. // Change to new root directory and verify it's a different fs.
  62. newroot = *argv++;
  63. xchdir(newroot);
  64. if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
  65. bb_error_msg_and_die("bad newroot %s", newroot);
  66. }
  67. rootdev = st2.st_dev;
  68. // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
  69. // we mean it. (I could make this a CONFIG option, but I would get email
  70. // from all the people who WILL eat their filesystems.)
  71. if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs)
  72. || (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC))
  73. || (getpid() != 1)
  74. ) {
  75. bb_error_msg_and_die("not rootfs");
  76. }
  77. // Zap everything out of rootdev
  78. delete_contents("/", rootdev);
  79. // Overmount / with newdir and chroot into it. The chdir is needed to
  80. // recalculate "." and ".." links.
  81. if (mount(".", "/", NULL, MS_MOVE, NULL))
  82. bb_error_msg_and_die("error moving root");
  83. xchroot(".");
  84. xchdir("/");
  85. // If a new console specified, redirect stdin/stdout/stderr to that.
  86. if (console) {
  87. close(0);
  88. xopen(console, O_RDWR);
  89. xdup2(0, 1);
  90. xdup2(0, 2);
  91. }
  92. // Exec real init. (This is why we must be pid 1.)
  93. execv(argv[0], argv);
  94. bb_perror_msg_and_die("bad init %s", argv[0]);
  95. }