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.

188 lines
4.9 KiB

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