switch_root.c 3.0 KB

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