2015-11-30 18:20:22 +01:00
|
|
|
#!/bin/bash -e
|
2018-02-08 14:21:01 +01:00
|
|
|
shopt -s nullglob dotglob
|
2015-11-30 18:20:22 +01:00
|
|
|
# vim: set ts=4 sw=4 sts=4 et :
|
|
|
|
#
|
|
|
|
# bind-dirs
|
|
|
|
# Binds directories which allows changes in TemplateBasedVM to persist.
|
2016-12-21 00:15:12 +01:00
|
|
|
# https://www.qubes-os.org/doc/bind-dirs/
|
2015-11-30 18:20:22 +01:00
|
|
|
#
|
|
|
|
# To umount all bind-dirs, just pass any arg in $1, like umount
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 - 2015 Jason Mehring <nrgaway@gmail.com>
|
|
|
|
# Copyright (C) 2014 - 2015 Patrick Schleizer <adrelanos@riseup.net>
|
|
|
|
# License: GPL-2+
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-10-22 17:43:16 +02:00
|
|
|
# Source Qubes library.
|
2017-09-30 04:56:02 +02:00
|
|
|
# shellcheck source=init/functions
|
2017-03-14 20:13:23 +01:00
|
|
|
source /usr/lib/qubes/init/functions
|
2015-11-30 18:20:22 +01:00
|
|
|
|
|
|
|
prerequisite() {
|
2020-09-15 12:52:59 +02:00
|
|
|
if is_fully_persistent ; then
|
|
|
|
echo "No TemplateBasedVM/DisposableVM detected. Exiting."
|
2015-11-30 18:20:22 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
[ -n "$rw_dest_dir" ] || rw_dest_dir="/rw/bind-dirs"
|
2015-12-18 01:15:51 +01:00
|
|
|
[ -n "$symlink_level_max" ] || symlink_level_max="10"
|
2015-11-30 18:20:22 +01:00
|
|
|
mkdir --parents "$rw_dest_dir"
|
|
|
|
}
|
|
|
|
|
|
|
|
legacy() {
|
2016-07-27 15:07:32 +02:00
|
|
|
## The legacy function gets overwritten by Whonix:
|
|
|
|
## https://github.com/Whonix/qubes-whonix/blob/master/usr/lib/qubes-bind-dirs.d/41_qubes-whonix-legacy.conf
|
|
|
|
## Please do not remove this legacy function without coordination with Whonix.
|
2016-07-24 02:09:11 +02:00
|
|
|
true
|
2015-11-30 18:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bind_dirs() {
|
2015-12-18 00:11:27 +01:00
|
|
|
## legend
|
2015-11-30 18:20:22 +01:00
|
|
|
## fso: file system object
|
|
|
|
## ro: read-only
|
|
|
|
## rw: read-write
|
2015-12-18 00:11:27 +01:00
|
|
|
|
2017-03-14 20:10:26 +01:00
|
|
|
for fso_ro in "${binds[@]}"; do
|
2015-12-18 01:15:51 +01:00
|
|
|
local symlink_level_counter
|
|
|
|
symlink_level_counter="0"
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
if [ -h "$fso_ro" ]; then
|
2016-01-06 21:46:38 +01:00
|
|
|
## Resolving where there symlink points to, and using the result
|
|
|
|
## for bind mount instead.
|
2015-12-18 01:15:51 +01:00
|
|
|
symlink_level_counter="$(( symlink_level_counter + 1 ))"
|
|
|
|
true "$fso_ro is a symlink"
|
|
|
|
fso_real_location="$(realpath "$fso_ro")"
|
|
|
|
fso_ro="$fso_real_location"
|
|
|
|
else
|
2018-10-01 23:07:01 +02:00
|
|
|
echo "$fso_ro is not a symlink"
|
2015-12-18 01:15:51 +01:00
|
|
|
break
|
|
|
|
fi
|
2016-01-06 21:46:38 +01:00
|
|
|
if [ "$symlink_level_counter" -ge "$symlink_level_max" ]; then
|
2015-12-18 01:15:51 +01:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
true "fso_ro: $fso_ro"
|
2015-11-30 18:20:22 +01:00
|
|
|
fso_rw="${rw_dest_dir}${fso_ro}"
|
|
|
|
|
2015-12-18 00:11:27 +01:00
|
|
|
# Make sure fso_ro is not mounted.
|
2015-11-30 18:20:22 +01:00
|
|
|
umount "$fso_ro" 2> /dev/null || true
|
|
|
|
|
|
|
|
if [ -n "$1" ]; then
|
2015-12-18 00:11:27 +01:00
|
|
|
true "Umounting $1 only..."
|
2015-11-30 18:20:22 +01:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2017-03-14 20:31:28 +01:00
|
|
|
if [ -d "$fso_rw" ] || [ -f "$fso_rw" ]; then
|
|
|
|
if [ ! -e "$fso_ro" ]; then
|
|
|
|
## Create empty file or directory if path exists in /rw to allow to bind mount none existing files/dirs.
|
|
|
|
test -d "$fso_rw" && mkdir --parents "$fso_ro"
|
|
|
|
test -f "$fso_rw" && touch "$fso_ro"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ -d "$fso_ro" ] || [ -f "$fso_ro" ]; then
|
|
|
|
## Initially copy over data directories to /rw if rw directory does not exist.
|
2016-10-22 17:43:16 +02:00
|
|
|
echo "Initializing $rw_dest_dir with files from $fso_ro" >&2
|
|
|
|
cp --archive --recursive --parents "$fso_ro" "$rw_dest_dir"
|
2017-03-14 20:31:28 +01:00
|
|
|
else
|
2018-10-01 23:07:01 +02:00
|
|
|
echo "$fso_ro is neither a directory nor a file and the path does not exist below /rw, skipping."
|
2017-03-14 20:31:28 +01:00
|
|
|
continue
|
2016-08-28 18:35:03 +02:00
|
|
|
fi
|
2015-11-30 18:20:22 +01:00
|
|
|
fi
|
|
|
|
|
2015-12-18 00:11:27 +01:00
|
|
|
# Bind the fso.
|
2016-10-22 17:43:16 +02:00
|
|
|
echo "Bind mounting $fso_rw onto $fso_ro" >&2
|
2015-11-30 18:20:22 +01:00
|
|
|
mount --bind "$fso_rw" "$fso_ro"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2015-12-18 01:15:51 +01:00
|
|
|
prerequisite "$@"
|
|
|
|
init "$@"
|
|
|
|
legacy "$@"
|
|
|
|
bind_dirs "$@"
|
2015-11-30 18:20:22 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:13:23 +01:00
|
|
|
binds=()
|
2015-12-18 00:11:27 +01:00
|
|
|
for source_folder in /usr/lib/qubes-bind-dirs.d /etc/qubes-bind-dirs.d /rw/config/qubes-bind-dirs.d ; do
|
|
|
|
true "source_folder: $source_folder"
|
|
|
|
if [ ! -d "$source_folder" ]; then
|
2015-11-30 18:20:22 +01:00
|
|
|
continue
|
|
|
|
fi
|
2015-12-18 00:11:27 +01:00
|
|
|
for file_name in "$source_folder/"*".conf" ; do
|
2015-11-30 18:20:22 +01:00
|
|
|
bash -n "$file_name"
|
2017-03-14 20:13:23 +01:00
|
|
|
# shellcheck source=/dev/null
|
2015-11-30 18:20:22 +01:00
|
|
|
source "$file_name"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2015-12-18 01:15:51 +01:00
|
|
|
main "$@"
|
2016-05-03 15:16:59 +02:00
|
|
|
|
|
|
|
true "OK: END."
|