3
0

switch_root.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. static dev_t rootdev;
  21. // Recursively delete contents of rootfs.
  22. static void delete_contents(const char *directory)
  23. {
  24. DIR *dir;
  25. struct dirent *d;
  26. struct stat st;
  27. // Don't descend into other filesystems
  28. if (lstat(directory, &st) || st.st_dev != rootdev) 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 (*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
  37. continue;
  38. // Recurse to delete contents
  39. newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
  40. sprintf(newdir, "%s/%s", directory, d->d_name);
  41. delete_contents(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 ATTRIBUTE_UNUSED, char **argv)
  52. {
  53. char *newroot, *console = NULL;
  54. struct stat st1, st2;
  55. struct statfs stfs;
  56. // Parse args (-c console)
  57. opt_complementary = "-2"; // minimum 2 params
  58. getopt32(argv, "+c:", &console); // '+': stop parsing at first non-option
  59. argv += optind;
  60. // Change to new root directory and verify it's a different fs.
  61. newroot = *argv++;
  62. xchdir(newroot);
  63. if (lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) {
  64. bb_error_msg_and_die("bad newroot %s", newroot);
  65. }
  66. rootdev = st2.st_dev;
  67. // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
  68. // we mean it. (I could make this a CONFIG option, but I would get email
  69. // from all the people who WILL eat their filesystems.)
  70. if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs)
  71. || (((unsigned)stfs.f_type != RAMFS_MAGIC) && ((unsigned)stfs.f_type != TMPFS_MAGIC))
  72. || (getpid() != 1)
  73. ) {
  74. bb_error_msg_and_die("not rootfs");
  75. }
  76. // Zap everything out of rootdev
  77. delete_contents("/");
  78. // Overmount / with newdir and chroot into it. The chdir is needed to
  79. // recalculate "." and ".." links.
  80. if (mount(".", "/", NULL, MS_MOVE, NULL))
  81. bb_error_msg_and_die("error moving root");
  82. xchroot(".");
  83. xchdir("/");
  84. // If a new console specified, redirect stdin/stdout/stderr to that.
  85. if (console) {
  86. close(0);
  87. xopen(console, O_RDWR);
  88. dup2(0, 1);
  89. dup2(0, 2);
  90. }
  91. // Exec real init. (This is why we must be pid 1.)
  92. execv(argv[0], argv);
  93. bb_perror_msg_and_die("bad init %s", argv[0]);
  94. }