bind-dirs.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash -e
  2. # vim: set ts=4 sw=4 sts=4 et :
  3. #
  4. # bind-dirs
  5. # Binds directories which allows changes in TemplateBasedVM to persist.
  6. # https://www.qubes-os.org/doc/bind-dirs/
  7. #
  8. # To umount all bind-dirs, just pass any arg in $1, like umount
  9. #
  10. # Copyright (C) 2014 - 2015 Jason Mehring <nrgaway@gmail.com>
  11. # Copyright (C) 2014 - 2015 Patrick Schleizer <adrelanos@riseup.net>
  12. # License: GPL-2+
  13. #
  14. # This program is free software; you can redistribute it and/or
  15. # modify it under the terms of the GNU General Public License
  16. # as published by the Free Software Foundation; either version 2
  17. # of the License, or (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. # Source Qubes library.
  27. # shellcheck source=init/functions
  28. source /usr/lib/qubes/init/functions
  29. prerequisite() {
  30. if ! is_rwonly_persistent ; then
  31. true "No TemplateBasedVM detected. Exiting."
  32. exit 0
  33. fi
  34. }
  35. init() {
  36. [ -n "$rw_dest_dir" ] || rw_dest_dir="/rw/bind-dirs"
  37. [ -n "$symlink_level_max" ] || symlink_level_max="10"
  38. mkdir --parents "$rw_dest_dir"
  39. shopt -s nullglob
  40. shopt -s dotglob
  41. }
  42. legacy() {
  43. ## The legacy function gets overwritten by Whonix:
  44. ## https://github.com/Whonix/qubes-whonix/blob/master/usr/lib/qubes-bind-dirs.d/41_qubes-whonix-legacy.conf
  45. ## Please do not remove this legacy function without coordination with Whonix.
  46. true
  47. }
  48. bind_dirs() {
  49. ## legend
  50. ## fso: file system object
  51. ## ro: read-only
  52. ## rw: read-write
  53. for fso_ro in "${binds[@]}"; do
  54. local symlink_level_counter
  55. symlink_level_counter="0"
  56. while true; do
  57. if [ -h "$fso_ro" ]; then
  58. ## Resolving where there symlink points to, and using the result
  59. ## for bind mount instead.
  60. symlink_level_counter="$(( symlink_level_counter + 1 ))"
  61. true "$fso_ro is a symlink"
  62. fso_real_location="$(realpath "$fso_ro")"
  63. fso_ro="$fso_real_location"
  64. else
  65. true "$fso_ro is not a symlink"
  66. break
  67. fi
  68. if [ "$symlink_level_counter" -ge "$symlink_level_max" ]; then
  69. break
  70. fi
  71. done
  72. true "fso_ro: $fso_ro"
  73. fso_rw="${rw_dest_dir}${fso_ro}"
  74. # Make sure fso_ro is not mounted.
  75. umount "$fso_ro" 2> /dev/null || true
  76. if [ -n "$1" ]; then
  77. true "Umounting $1 only..."
  78. continue
  79. fi
  80. if [ -d "$fso_rw" ] || [ -f "$fso_rw" ]; then
  81. if [ ! -e "$fso_ro" ]; then
  82. ## Create empty file or directory if path exists in /rw to allow to bind mount none existing files/dirs.
  83. test -d "$fso_rw" && mkdir --parents "$fso_ro"
  84. test -f "$fso_rw" && touch "$fso_ro"
  85. fi
  86. else
  87. if [ -d "$fso_ro" ] || [ -f "$fso_ro" ]; then
  88. ## Initially copy over data directories to /rw if rw directory does not exist.
  89. echo "Initializing $rw_dest_dir with files from $fso_ro" >&2
  90. cp --archive --recursive --parents "$fso_ro" "$rw_dest_dir"
  91. else
  92. true "$fso_ro is neither a directory nor a file and the path does not exist below /rw, skipping."
  93. continue
  94. fi
  95. fi
  96. # Bind the fso.
  97. echo "Bind mounting $fso_rw onto $fso_ro" >&2
  98. mount --bind "$fso_rw" "$fso_ro"
  99. done
  100. }
  101. main() {
  102. prerequisite "$@"
  103. init "$@"
  104. legacy "$@"
  105. bind_dirs "$@"
  106. }
  107. binds=()
  108. for source_folder in /usr/lib/qubes-bind-dirs.d /etc/qubes-bind-dirs.d /rw/config/qubes-bind-dirs.d ; do
  109. true "source_folder: $source_folder"
  110. if [ ! -d "$source_folder" ]; then
  111. continue
  112. fi
  113. for file_name in "$source_folder/"*".conf" ; do
  114. bash -n "$file_name"
  115. # shellcheck source=/dev/null
  116. source "$file_name"
  117. done
  118. done
  119. main "$@"
  120. true "OK: END."