OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.8 KiB

  1. # --- T2-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # T2 SDE: package/.../embutils/x-switch_root.patch
  5. # Copyright (C) 2004 - 2006 The T2 SDE Project
  6. #
  7. # More information can be found in the files COPYING and README.
  8. #
  9. # This patch file is dual-licensed. It is available under the license the
  10. # patched project is licensed under, as long as it is an OpenSource license
  11. # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
  12. # of the GNU General Public License as published by the Free Software
  13. # Foundation; either version 2 of the License, or (at your option) any later
  14. # version.
  15. # --- T2-COPYRIGHT-NOTE-END ---
  16. pivot_root'ing rootfs does uncover kernel inf. loops, thus I extraced some
  17. bits form the Fedora Core's nash to trick around with moving the mount point
  18. and chroot to move the real root device to / - yuck.
  19. - Rene Rebe <rene@exactcode.de>
  20. --- embutils-0.17/switch_root.c 1970-01-01 00:00:00.000000000 +0000
  21. +++ embutils-0.17-more/switch_root.c 2005-08-27 09:05:22.000000000 +0000
  22. @@ -0,0 +1,151 @@
  23. +
  24. +/* inspired by Fedora Core's mkinitrd/nash beast
  25. + - to pivot_root alike functionality with initramfs
  26. +
  27. + 2005 by Rene Rebe
  28. + */
  29. +
  30. +#include <sys/types.h>
  31. +#include <sys/stat.h>
  32. +#include <fcntl.h>
  33. +#include <dirent.h>
  34. +#include <stdio.h>
  35. +#include <errno.h>
  36. +
  37. +#ifndef MS_REMOUNT
  38. +#define MS_REMOUNT 32
  39. +#endif
  40. +
  41. +#ifndef MS_BIND
  42. +#define MS_BIND 4096
  43. +#endif
  44. +
  45. +#ifndef MS_MOVE
  46. +#define MS_MOVE 8192
  47. +#endif
  48. +
  49. +/* remove all files/directories below dirName -- don't cross mountpoints */
  50. +int recursiveRemove(char * dirName) {
  51. + struct stat sb,rb;
  52. + DIR * dir;
  53. + struct dirent * d;
  54. + char * strBuf = alloca(strlen(dirName) + 1024);
  55. +
  56. + if (!(dir = opendir(dirName))) {
  57. + fprintf(stderr, "error opening %s: %d\n", dirName, errno);
  58. + return 0;
  59. + }
  60. +
  61. + if (fstat(dirfd(dir),&rb)) {
  62. + fprintf(stderr, "unable to stat %s: %d\n", dirName, errno);
  63. + return 0;
  64. + }
  65. +
  66. + errno = 0;
  67. + while ((d = readdir(dir))) {
  68. + errno = 0;
  69. +
  70. + if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
  71. + errno = 0;
  72. + continue;
  73. + }
  74. +
  75. + strcpy(strBuf, dirName);
  76. + strcat(strBuf, "/");
  77. + strcat(strBuf, d->d_name);
  78. +
  79. + if (lstat(strBuf, &sb)) {
  80. + fprintf(stderr, "failed to stat %s: %d\n", strBuf, errno);
  81. + errno = 0;
  82. + continue;
  83. + }
  84. +
  85. + /* only descend into subdirectories if device is same as dir */
  86. + if (S_ISDIR(sb.st_mode)) {
  87. + if (sb.st_dev == rb.st_dev) {
  88. + recursiveRemove(strBuf);
  89. + if (rmdir(strBuf))
  90. + fprintf(stderr, "failed to rmdir %s: %d\n", strBuf, errno);
  91. + }
  92. + errno = 0;
  93. + continue;
  94. + }
  95. +
  96. + if (unlink(strBuf)) {
  97. + fprintf(stderr, "failed to remove %s: %d\n", strBuf, errno);
  98. + errno = 0;
  99. + continue;
  100. + }
  101. + }
  102. +
  103. + if (errno) {
  104. + closedir(dir);
  105. + printf("error reading from %s: %d\n", dirName, errno);
  106. + return 1;
  107. + }
  108. +
  109. + closedir(dir);
  110. +
  111. + return 0;
  112. +}
  113. +
  114. +/* 2.6 magic not-pivot-root but kind of similar stuff.
  115. + * This is based on code from klibc/utils/run_init.c
  116. + */
  117. +int main (int argc, char* argv[]) {
  118. + char * new;
  119. +
  120. + if (!*++argv) {
  121. + printf("switch_root: new root mount point expected\n");
  122. + return 1;
  123. + }
  124. + new = *argv;
  125. +
  126. + if (!*++argv) {
  127. + printf("switch_root: new init expected\n");
  128. + return 1;
  129. + }
  130. +
  131. + int fd, i = 0;
  132. +
  133. + if (chdir(new)) {
  134. + printf("switch_root: chdir(%s) failed: %d\n", new, errno);
  135. + return 1;
  136. + }
  137. +
  138. + if ((fd = open("./dev/console", O_RDWR)) < 0) {
  139. + printf("ERROR opening /dev/console!!!!: %d\n", errno);
  140. + fd = 0;
  141. + }
  142. +
  143. + if (dup2(fd, 0) != 0) printf("error dup2'ing fd of %d to 0\n", fd);
  144. + if (dup2(fd, 1) != 1) printf("error dup2'ing fd of %d to 1\n", fd);
  145. + if (dup2(fd, 2) != 2) printf("error dup2'ing fd of %d to 2\n", fd);
  146. + if (fd > 2)
  147. + close(fd);
  148. +
  149. +// recursiveRemove("/");
  150. +
  151. + fd = open("/", O_RDONLY);
  152. +
  153. + if (mount(".", "/", NULL, MS_MOVE, NULL)) {
  154. + printf("switch_root: mount failed: %d\n", errno);
  155. + close(fd);
  156. + return 1;
  157. + }
  158. +
  159. + if (chroot(".") || chdir("/")) {
  160. + printf("switch_root: chroot() failed: %d\n", errno);
  161. + close(fd);
  162. + return 1;
  163. + }
  164. +
  165. + /* release the old "/" */
  166. + close(fd);
  167. +
  168. + /* exec the init */
  169. + fd = execvp (argv[0], argv);
  170. + printf ("switch_root: execvp failed: %d\n", fd);
  171. + return fd;
  172. +}
  173. +
  174. --- embutils-0.17/Makefile 2005-08-27 18:34:54.000000000 +0000
  175. +++ embutils-0.17-more/Makefile 2005-08-27 18:36:59.000000000 +0000
  176. @@ -11,7 +11,7 @@
  177. install sosrm soscp sosmv sosln soslns md5sum sleep2 allinone kill uniq \
  178. dd tr mesg write touch du tail uuencode uudecode nohup nice cmp mktemp \
  179. truncate test date mount printenv umount pivot_root insmod rmmod \
  180. -lsmod free losetup
  181. +lsmod free losetup switch_root
  182. OBJDIR:=bin
  183. TARGETS=$(patsubst %,$(OBJDIR)/%,$(PRGS))