guix-pack-relocatable.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2018, 2019, 2020, 2023 Ludovic Courtès <ludo@gnu.org>
  3. # Copyright © 2020 Eric Bavier <bavier@posteo.net>
  4. #
  5. # This file is part of GNU Guix.
  6. #
  7. # GNU Guix is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or (at
  10. # your option) any later version.
  11. #
  12. # GNU Guix is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # Test the 'guix pack --relocatable' using the external store, if any.
  21. #
  22. guix pack --version
  23. # 'guix pack --relocatable' requires a C compiler and libc.a, which our
  24. # bootstrap binaries don't provide. To make the test relatively inexpensive,
  25. # run it on the user's global store if possible, on the grounds that binaries
  26. # may already be there or can be built or downloaded inexpensively.
  27. storedir="`guile -c '(use-modules (guix config))(display %storedir)'`"
  28. localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`"
  29. NIX_STORE_DIR="$storedir"
  30. GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
  31. export NIX_STORE_DIR GUIX_DAEMON_SOCKET
  32. if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
  33. then
  34. exit 77
  35. fi
  36. # Attempt to run the given command in a namespace where the store is
  37. # invisible. This makes sure the presence of the store does not hide
  38. # problems.
  39. run_without_store ()
  40. {
  41. if unshare -r true # Are user namespaces supported?
  42. then
  43. # Run that relocatable executable in a user namespace where we "erase"
  44. # the store by mounting an empty file system on top of it. That way,
  45. # we exercise the wrapper code that creates the user namespace and
  46. # bind-mounts the store.
  47. unshare -mrf sh -c 'mount -t tmpfs -o ro none "$NIX_STORE_DIR"; '"$*"
  48. else
  49. # Run the relocatable program in the current namespaces. This is a
  50. # weak test because we're going to access store items from the host
  51. # store.
  52. sh -c "$*"
  53. fi
  54. }
  55. # Wait for the given file to show up. Error out if it doesn't show up in a
  56. # timely fashion.
  57. wait_for_file ()
  58. {
  59. i=0
  60. while ! test -f "$1" && test $i -lt 20
  61. do
  62. sleep 0.3
  63. i=`expr $i + 1`
  64. done
  65. test -f "$1"
  66. }
  67. test_directory="`mktemp -d`"
  68. export test_directory
  69. trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
  70. if unshare -r true
  71. then
  72. # Test the 'userns' execution engine.
  73. tarball="`guix pack -R -S /Bin=bin sed`"
  74. (cd "$test_directory"; tar xvf "$tarball")
  75. chmod +w "$test_directory"
  76. run_without_store "$test_directory/Bin/sed" --version > "$test_directory/output"
  77. grep 'GNU sed' "$test_directory/output"
  78. # Same with an explicit engine.
  79. run_without_store GUIX_EXECUTION_ENGINE="userns" \
  80. "$test_directory/Bin/sed" --version > "$test_directory/output"
  81. grep 'GNU sed' "$test_directory/output"
  82. # Check whether the exit code is preserved.
  83. run_without_store "$test_directory/Bin/sed" --does-not-exist && false
  84. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  85. else
  86. echo "'userns' execution tests skipped" >&2
  87. fi
  88. case "`uname -m`" in
  89. x86_64|i?86)
  90. # Try '-RR' and PRoot.
  91. tarball="`guix pack -RR -S /Bin=bin sed`"
  92. tar tvf "$tarball" | grep /bin/proot
  93. (cd "$test_directory"; tar xf "$tarball")
  94. chmod +w "$test_directory"
  95. run_without_store GUIX_EXECUTION_ENGINE="proot" \
  96. "$test_directory/Bin/sed" --version > "$test_directory/output"
  97. grep 'GNU sed' "$test_directory/output"
  98. # Now with fakechroot.
  99. run_without_store GUIX_EXECUTION_ENGINE="fakechroot" \
  100. "$test_directory/Bin/sed" --version > "$test_directory/output"
  101. grep 'GNU sed' "$test_directory/output"
  102. unset GUIX_EXECUTION_ENGINE
  103. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  104. if unshare -r true
  105. then
  106. # Check whether the store contains everything it should. Check
  107. # once when erasing $STORE_PARENT ("/gnu") and once when erasing
  108. # $NIX_STORE_DIR ("/gnu/store").
  109. tarball="`guix pack -RR -S /bin=bin bash-minimal`"
  110. (cd "$test_directory"; tar xf "$tarball")
  111. STORE_PARENT="`dirname $NIX_STORE_DIR`"
  112. export STORE_PARENT
  113. for engine in userns proot fakechroot
  114. do
  115. for i in $(guix gc -R $(guix build bash-minimal | grep -v -e '-doc$'))
  116. do
  117. unshare -mrf sh -c "mount -t tmpfs none \"$NIX_STORE_DIR\"; GUIX_EXECUTION_ENGINE=$engine $test_directory/bin/sh -c 'echo $NIX_STORE_DIR/*'" | grep $(basename $i)
  118. unshare -mrf sh -c "mount -t tmpfs none \"$STORE_PARENT\"; GUIX_EXECUTION_ENGINE=$engine $test_directory/bin/sh -c 'echo $NIX_STORE_DIR/*'" | grep $(basename $i)
  119. done
  120. done
  121. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  122. fi
  123. ;;
  124. *)
  125. echo "skipping PRoot and Fakechroot tests" >&2
  126. ;;
  127. esac
  128. if unshare -r true
  129. then
  130. # Check what happens if the wrapped binary forks and leaves child
  131. # processes behind, like a daemon. The root file system should remain
  132. # available to those child processes. See <https://bugs.gnu.org/44261>.
  133. cat > "$test_directory/manifest.scm" <<EOF
  134. (use-modules (guix))
  135. (define daemon
  136. (program-file "daemon"
  137. #~(begin
  138. (use-modules (ice-9 match)
  139. (ice-9 ftw))
  140. (call-with-output-file "parent-store"
  141. (lambda (port)
  142. (write (scandir (ungexp (%store-prefix)))
  143. port)))
  144. (match (primitive-fork)
  145. (0 (sigaction SIGHUP (const #t))
  146. (call-with-output-file "pid"
  147. (lambda (port)
  148. (display (getpid) port)))
  149. (pause)
  150. (call-with-output-file "child-store"
  151. (lambda (port)
  152. (write (scandir (ungexp (%store-prefix)))
  153. port))))
  154. (_ #t)))))
  155. (define package
  156. (computed-file "package"
  157. #~(let ((out (ungexp output)))
  158. (mkdir out)
  159. (mkdir (string-append out "/bin"))
  160. (symlink (ungexp daemon)
  161. (string-append out "/bin/daemon")))))
  162. (manifest (list (manifest-entry
  163. (name "daemon")
  164. (version "0")
  165. (item package))))
  166. EOF
  167. tarball="$(guix pack -S /bin=bin -R -m "$test_directory/manifest.scm")"
  168. (cd "$test_directory"; tar xf "$tarball")
  169. # Run '/bin/daemon', which forks, then wait for the child, send it SIGHUP
  170. # so that it dumps its view of the store, and make sure the child and
  171. # parent both see the same store contents.
  172. chmod +w "$test_directory"
  173. (cd "$test_directory"; run_without_store ./bin/daemon)
  174. wait_for_file "$test_directory/pid"
  175. kill -HUP $(cat "$test_directory/pid")
  176. wait_for_file "$test_directory/child-store"
  177. diff -u "$test_directory/parent-store" "$test_directory/child-store"
  178. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  179. fi
  180. # Ensure '-R' works with outputs other than "out".
  181. tarball="`guix pack -R -S /share=share groff:doc`"
  182. (cd "$test_directory"; tar xf "$tarball")
  183. test -d "$test_directory/share/doc/groff/html"
  184. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  185. # Ensure '-R' applies to propagated inputs. Failing to do that, it would fail
  186. # with a profile collision error in this case because 'python-scipy'
  187. # propagates 'python-numpy'. See <https://bugs.gnu.org/42510>.
  188. guix pack -RR python-numpy python-scipy --no-grafts -n
  189. # Check that packages that mix executable and support files (e.g. git) in the
  190. # "binary" directories still work after wrapped.
  191. cat >"$test_directory/manifest.scm" <<'EOF'
  192. (use-modules (guix) (guix profiles) (guix search-paths)
  193. (gnu packages bootstrap))
  194. (manifest
  195. (list (manifest-entry
  196. (name "test") (version "0")
  197. (item (file-union "test"
  198. `(("bin/hello"
  199. ,(program-file
  200. "hello"
  201. #~(begin
  202. (add-to-load-path (getenv "HELLO_EXEC_PATH"))
  203. (display (load-from-path "msg"))(newline))
  204. #:guile %bootstrap-guile))
  205. ("libexec/hello/msg"
  206. ,(plain-file "msg" "42")))))
  207. (search-paths
  208. (list (search-path-specification
  209. (variable "HELLO_EXEC_PATH")
  210. (files '("libexec/hello"))
  211. (separator #f)))))))
  212. EOF
  213. tarball="`guix pack -RR -S /opt= -m $test_directory/manifest.scm`"
  214. (cd "$test_directory"; tar xvf "$tarball")
  215. chmod +w "$test_directory"
  216. ( export GUIX_PROFILE=$test_directory/opt
  217. . $GUIX_PROFILE/etc/profile
  218. run_without_store "$test_directory/opt/bin/hello" > "$test_directory/output" )
  219. cat "$test_directory/output"
  220. test "`cat $test_directory/output`" = "42"