bind-dirs.sh 4.0 KB

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