switch_root.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "busybox.h"
  9. #include <sys/vfs.h>
  10. // Make up for header deficiencies.
  11. #ifndef RAMFS_MAGIC
  12. #define RAMFS_MAGIC 0x858458f6
  13. #endif
  14. #ifndef TMPFS_MAGIC
  15. #define TMPFS_MAGIC 0x01021994
  16. #endif
  17. #ifndef MS_MOVE
  18. #define MS_MOVE 8192
  19. #endif
  20. dev_t rootdev;
  21. // Recursively delete contents of rootfs.
  22. static void delete_contents(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. if((dir = opendir(directory))) {
  32. while ((d = readdir(dir))) {
  33. char *newdir=d->d_name;
  34. // Skip . and ..
  35. if(*newdir=='.' && (!newdir[1] || (newdir[1]=='.' && !newdir[2])))
  36. continue;
  37. // Recurse to delete contents
  38. newdir = alloca(strlen(directory) + strlen(d->d_name) + 2);
  39. sprintf(newdir, "%s/%s", directory, d->d_name);
  40. delete_contents(newdir);
  41. }
  42. closedir(dir);
  43. // Directory should now be empty. Zap it.
  44. rmdir(directory);
  45. }
  46. // It wasn't a directory. Zap it.
  47. } else unlink(directory);
  48. }
  49. int switch_root_main(int argc, char *argv[])
  50. {
  51. char *newroot, *console=NULL;
  52. struct stat st1, st2;
  53. struct statfs stfs;
  54. // Parse args (-c console)
  55. opt_complementary = "-2";
  56. getopt32(argc, argv, "c:", &console);
  57. // Change to new root directory and verify it's a different fs.
  58. newroot=argv[optind++];
  59. if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) ||
  60. st1.st_dev == st2.st_dev)
  61. {
  62. bb_error_msg_and_die("bad newroot %s", newroot);
  63. }
  64. rootdev=st2.st_dev;
  65. // Additional sanity checks: we're about to rm -rf /, so be REALLY SURE
  66. // we mean it. (I could make this a CONFIG option, but I would get email
  67. // from all the people who WILL eat their filesystemss.)
  68. if (lstat("/init", &st1) || !S_ISREG(st1.st_mode) || statfs("/", &stfs) ||
  69. (stfs.f_type != RAMFS_MAGIC && stfs.f_type != TMPFS_MAGIC) ||
  70. getpid() != 1)
  71. {
  72. bb_error_msg_and_die("not rootfs");
  73. }
  74. // Zap everything out of rootdev
  75. delete_contents("/");
  76. // Overmount / with newdir and chroot into it. The chdir is needed to
  77. // recalculate "." and ".." links.
  78. if (mount(".", "/", NULL, MS_MOVE, NULL) || chroot(".") || chdir("/"))
  79. bb_error_msg_and_die("moving root");
  80. // If a new console specified, redirect stdin/stdout/stderr to that.
  81. if (console) {
  82. close(0);
  83. if(open(console, O_RDWR) < 0)
  84. bb_error_msg_and_die("bad console '%s'", console);
  85. dup2(0, 1);
  86. dup2(0, 2);
  87. }
  88. // Exec real init. (This is why we must be pid 1.)
  89. execv(argv[optind], argv+optind);
  90. bb_error_msg_and_die("bad init '%s'", argv[optind]);
  91. }