preinit 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh -e
  2. # This script is spawned by s6-overlay-suexec, as the
  3. # first thing in the userland boot process.
  4. # It is run as root even with a USER directive present.
  5. # If USER, then UID and GID hold the uid and gid of the user
  6. # (this is used by s6-chown -U).
  7. prog=/package/admin/s6-overlay/libexec/preinit
  8. checknoexec () {
  9. IFS=,
  10. set -- $1
  11. IFS=
  12. while test "$#" -gt 0 ; do
  13. if test "$1" = noexec ; then
  14. return 0
  15. fi
  16. shift
  17. done
  18. return 1
  19. }
  20. # Ensure /run is writable
  21. if test "0$S6_READ_ONLY_ROOT" -ne 0 ; then
  22. echo "$prog: info: read-only root" 1>&2
  23. if ! test -d /run ; then
  24. echo "$prog: fatal: /run is missing or not a directory" 1>&2
  25. exit 100
  26. fi
  27. if : > '/run/test of writability' 2>/dev/null ; then
  28. echo "$prog: info: writable /run. Checking for executability." 1>&2
  29. s6-rmrf '/run/test of writability'
  30. if ! s6-mount -o remount,rw,exec tmpfs /run 2>/dev/null ; then
  31. notfound=true
  32. while read these filesystem type options please ; do
  33. if test $filesystem = /run ; then
  34. notfound=false
  35. if checknoexec "$options" ; then
  36. echo "$prog: warning: your container manager pre-mounts run with \
  37. the incorrect noexec option, which s6-overlay cannot work with; expect /init \
  38. to crash soon. To fix the issue, either pre-mount /run with the exec option, \
  39. or as a workaround give this container the CAP_SYS_ADMIN capability so \
  40. s6-overlay can fix it at run time."
  41. fi
  42. break
  43. fi
  44. done < /proc/mounts
  45. if $notfound ; then
  46. echo "$prog: warning: unable to find /run in /proc/mounts, check that \
  47. your container manager pre-mounts /proc, and that /run is a tmpfs. The container \
  48. is likely to crash soon, if /run is (incorrectly) mounted noexec."
  49. fi
  50. fi
  51. else
  52. echo "$prog: info: creating a tmpfs on /run" 1>&2
  53. s6-mount -wt tmpfs -o exec,mode=0755 tmpfs /run
  54. fi
  55. else
  56. s6-mkdir -p -m 0755 /run
  57. fi
  58. if test "0$UID" -ne 0 ; then
  59. s6-chown -U -- /run
  60. fi
  61. # Ensure /var/run is a symlink to /run
  62. if test -L /var/run && test "`s6-linkname -f /var/run`" = /run ; then : ; else
  63. echo "$prog: notice: /var/run is not a symlink to /run, fixing it" 1>&2
  64. s6-rmrf /var/run
  65. s6-ln -s /run /var/run
  66. fi
  67. # Clean up in case /run hasn't been wiped or USER has changed
  68. s6-rmrf /run/s6 /run/service /run/uncaught-logs /run/s6-rc*
  69. s6-mkdir -m 0755 /run/s6 /run/service
  70. if test "0$UID" -ne 0 ; then
  71. s6-chown -U -- /run/s6
  72. s6-chown -U -- /run/service
  73. fi