guix-pack-relocatable.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2018, 2019, 2020 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. run_without_store "$test_directory/Bin/sed" --version > "$test_directory/output"
  76. grep 'GNU sed' "$test_directory/output"
  77. # Same with an explicit engine.
  78. run_without_store GUIX_EXECUTION_ENGINE="userns" \
  79. "$test_directory/Bin/sed" --version > "$test_directory/output"
  80. grep 'GNU sed' "$test_directory/output"
  81. # Check whether the exit code is preserved.
  82. ! run_without_store "$test_directory/Bin/sed" --does-not-exist
  83. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  84. else
  85. echo "'userns' execution tests skipped" >&2
  86. fi
  87. case "`uname -m`" in
  88. x86_64|i?86)
  89. # Try '-RR' and PRoot.
  90. tarball="`guix pack -RR -S /Bin=bin sed`"
  91. tar tvf "$tarball" | grep /bin/proot
  92. (cd "$test_directory"; tar xf "$tarball")
  93. run_without_store GUIX_EXECUTION_ENGINE="proot" \
  94. "$test_directory/Bin/sed" --version > "$test_directory/output"
  95. grep 'GNU sed' "$test_directory/output"
  96. # Now with fakechroot.
  97. run_without_store GUIX_EXECUTION_ENGINE="fakechroot" \
  98. "$test_directory/Bin/sed" --version > "$test_directory/output"
  99. grep 'GNU sed' "$test_directory/output"
  100. unset GUIX_EXECUTION_ENGINE
  101. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  102. if unshare -r true
  103. then
  104. # Check whether the store contains everything it should. Check
  105. # once when erasing $STORE_PARENT ("/gnu") and once when erasing
  106. # $NIX_STORE_DIR ("/gnu/store").
  107. tarball="`guix pack -RR -S /bin=bin bash-minimal`"
  108. (cd "$test_directory"; tar xf "$tarball")
  109. STORE_PARENT="`dirname $NIX_STORE_DIR`"
  110. export STORE_PARENT
  111. for engine in userns proot fakechroot
  112. do
  113. for i in $(guix gc -R $(guix build bash-minimal | grep -v -e '-doc$'))
  114. do
  115. 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)
  116. 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)
  117. done
  118. done
  119. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  120. fi
  121. ;;
  122. *)
  123. echo "skipping PRoot and Fakechroot tests" >&2
  124. ;;
  125. esac
  126. if unshare -r true
  127. then
  128. # Check what happens if the wrapped binary forks and leaves child
  129. # processes behind, like a daemon. The root file system should remain
  130. # available to those child processes. See <https://bugs.gnu.org/44261>.
  131. cat > "$test_directory/manifest.scm" <<EOF
  132. (use-modules (guix))
  133. (define daemon
  134. (program-file "daemon"
  135. #~(begin
  136. (use-modules (ice-9 match)
  137. (ice-9 ftw))
  138. (call-with-output-file "parent-store"
  139. (lambda (port)
  140. (write (scandir (ungexp (%store-prefix)))
  141. port)))
  142. (match (primitive-fork)
  143. (0 (sigaction SIGHUP (const #t))
  144. (call-with-output-file "pid"
  145. (lambda (port)
  146. (display (getpid) port)))
  147. (pause)
  148. (call-with-output-file "child-store"
  149. (lambda (port)
  150. (write (scandir (ungexp (%store-prefix)))
  151. port))))
  152. (_ #t)))))
  153. (define package
  154. (computed-file "package"
  155. #~(let ((out (ungexp output)))
  156. (mkdir out)
  157. (mkdir (string-append out "/bin"))
  158. (symlink (ungexp daemon)
  159. (string-append out "/bin/daemon")))))
  160. (manifest (list (manifest-entry
  161. (name "daemon")
  162. (version "0")
  163. (item package))))
  164. EOF
  165. tarball="$(guix pack -S /bin=bin -R -m "$test_directory/manifest.scm")"
  166. (cd "$test_directory"; tar xf "$tarball")
  167. # Run '/bin/daemon', which forks, then wait for the child, send it SIGHUP
  168. # so that it dumps its view of the store, and make sure the child and
  169. # parent both see the same store contents.
  170. (cd "$test_directory"; run_without_store ./bin/daemon)
  171. wait_for_file "$test_directory/pid"
  172. kill -HUP $(cat "$test_directory/pid")
  173. wait_for_file "$test_directory/child-store"
  174. diff -u "$test_directory/parent-store" "$test_directory/child-store"
  175. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  176. fi
  177. # Ensure '-R' works with outputs other than "out".
  178. tarball="`guix pack -R -S /share=share groff:doc`"
  179. (cd "$test_directory"; tar xf "$tarball")
  180. test -d "$test_directory/share/doc/groff/html"
  181. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  182. # Ensure '-R' applies to propagated inputs. Failing to do that, it would fail
  183. # with a profile collision error in this case because 'python-scipy'
  184. # propagates 'python-numpy'. See <https://bugs.gnu.org/42510>.
  185. guix pack -RR python-numpy python-scipy --no-grafts -n
  186. # Check that packages that mix executable and support files (e.g. git) in the
  187. # "binary" directories still work after wrapped.
  188. cat >"$test_directory/manifest.scm" <<'EOF'
  189. (use-modules (guix) (guix profiles) (guix search-paths)
  190. (gnu packages bootstrap))
  191. (manifest
  192. (list (manifest-entry
  193. (name "test") (version "0")
  194. (item (file-union "test"
  195. `(("bin/hello"
  196. ,(program-file
  197. "hello"
  198. #~(begin
  199. (add-to-load-path (getenv "HELLO_EXEC_PATH"))
  200. (display (load-from-path "msg"))(newline))
  201. #:guile %bootstrap-guile))
  202. ("libexec/hello/msg"
  203. ,(plain-file "msg" "42")))))
  204. (search-paths
  205. (list (search-path-specification
  206. (variable "HELLO_EXEC_PATH")
  207. (files '("libexec/hello"))
  208. (separator #f)))))))
  209. EOF
  210. tarball="`guix pack -RR -S /opt= -m $test_directory/manifest.scm`"
  211. (cd "$test_directory"; tar xvf "$tarball")
  212. ( export GUIX_PROFILE=$test_directory/opt
  213. . $GUIX_PROFILE/etc/profile
  214. run_without_store "$test_directory/opt/bin/hello" > "$test_directory/output" )
  215. cat "$test_directory/output"
  216. test "`cat $test_directory/output`" = "42"