fakeroot_tmpfiles.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/sh
  2. #
  3. # The systemd-tmpfiles has the ability to grab information
  4. # from the filesystem (instead from the running system).
  5. #
  6. # tmpfs directories (/tmp, /proc, ...) are skipped since they're not
  7. # relevant for the rootfs image.
  8. #
  9. # However there are a few specifiers that *always* will grab
  10. # information from the running system examples are %a, %b, %m, %H
  11. # (Architecture, Boot UUID, Machine UUID, Hostname).
  12. #
  13. # See [1] for historic information.
  14. #
  15. # This script will (conservatively) skip tmpfiles lines that have
  16. # such an specifier to prevent leaking host information.
  17. #
  18. # shell expansion is critical to be POSIX compliant,
  19. # this script wont work with zsh in its default mode for example.
  20. #
  21. # The script takes several measures to handle more complex stuff
  22. # like passing this correctly:
  23. # f+ "/var/example" - - - - %B\n%o\n%w\n%W%%\n
  24. #
  25. # [1] - https://github.com/systemd/systemd/pull/16187
  26. [ -n "${HOST_SYSTEMD_TMPFILES-}" ] ||
  27. HOST_SYSTEMD_TMPFILES=systemd-tmpfiles
  28. [ -n "${1-}" -a -d "${1-}"/usr/lib/tmpfiles.d ] ||
  29. { echo 1>&2 "$0: need ROOTFS argument"; exit 1; }
  30. ${HOST_SYSTEMD_TMPFILES} --no-pager --cat-config --root="$1" |
  31. sed -e '/^[[:space:]]*#/d' -e 's,^[[:space:]]*,,' -e '/^$/d' |
  32. while read -r line; do
  33. # it is allowed to use quotes around arguments,
  34. # so let the shell pack the arguments
  35. eval "set -- $line"
  36. # dont output warnings for directories we dont process
  37. [ "${2#/dev}" = "${2}" ] && [ "${2#/proc}" = "${2}" ] &&
  38. [ "${2#/run}" = "${2}" ] && [ "${2#/sys}" = "${2}" ] &&
  39. [ "${2#/tmp}" = "${2}" ] && [ "${2#/mnt}" = "${2}" ] ||
  40. continue
  41. # blank out all specs that are ok to use,
  42. # test if some remain. (Specs up to date with v250)
  43. if echo "$2 ${7-}" | sed -e 's,%[%BCEgGhLMosStTuUVwW],,g' | grep -v -q '%'; then
  44. # no "bad" specifiers, pass the line unmodified
  45. eval "printf '%s\n' '$line'"
  46. else
  47. # warn
  48. eval "printf 'ignored spec: %s\n' '$line' 1>&2"
  49. fi
  50. done |
  51. TMPDIR= TEMP= TMP= ${HOST_SYSTEMD_TMPFILES} --create --boot --root="$1" \
  52. --exclude-prefix=/dev --exclude-prefix=/proc --exclude-prefix=/run \
  53. --exclude-prefix=/sys --exclude-prefix=/tmp --exclude-prefix=/mnt \
  54. -