Skip to content

Commit

Permalink
Merge pull request #22 from binpash/fix-mounting
Browse files Browse the repository at this point in the history
Several bugfixes and improvements
  • Loading branch information
angelhof committed Jun 20, 2023
2 parents 15a0795 + 5022d8e commit 3d5af35
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,38 @@ try() {
START_DIR="$PWD"

[ "$SANDBOX_DIR" ] || SANDBOX_DIR=$(mktemp -d)
export SANDBOX_DIR
mkdir -p "$SANDBOX_DIR/upperdir" "$SANDBOX_DIR/workdir" "$SANDBOX_DIR/temproot"

# we will overlay-mount each root directory separately (instead of all at once) because some directories cannot be overlayed
# so we set up the mount points now
#
# TODO can we avoid warnings?
# TODO can we make this more uniform?
ls / | xargs -I '{}' mkdir "$SANDBOX_DIR"/temproot/'{}' "$SANDBOX_DIR"/workdir/'{}' "$SANDBOX_DIR"/upperdir/'{}'
for top_dir in $(ls /)
do
top_dir_abs="/$top_dir"
## Only make the directory if the original is a directory too
if [ -d "$top_dir_abs" ]; then
mkdir "$SANDBOX_DIR"/upperdir/"$top_dir" "$SANDBOX_DIR"/workdir/"$top_dir" "$SANDBOX_DIR"/temproot/"$top_dir"
fi
done

mount_and_execute=$(mktemp)
chroot_executable=$(mktemp)
cat >"$mount_and_execute" <<EOF
export chroot_executable=$(mktemp)
cat >"$mount_and_execute" <<"EOF"
#!/bin/sh
# actually mount the overlays
# TODO we may not need to ignore ALL of these (paltform dependent?)
ls / | grep -v -e proc -e dev -e proj -e run -e sys -e snap -e swap.img | xargs -I '{}' mount -t overlay overlay -o lowerdir=/'{}',upperdir="$SANDBOX_DIR"/upperdir/'{}',workdir="$SANDBOX_DIR"/workdir/'{}' "$SANDBOX_DIR"/temproot/'{}'
for top_dir in $(ls /)
do
top_dir_abs="/$top_dir"
## If the directory is not a mountpoint
if [ -d "$top_dir_abs" ] && ! mountpoint -q "$top_dir_abs"; then
## TODO: The
mount -t overlay overlay -o lowerdir=/"$top_dir",upperdir="$SANDBOX_DIR"/upperdir/"$top_dir",workdir="$SANDBOX_DIR"/workdir/"$top_dir" "$SANDBOX_DIR"/temproot/"$top_dir" 2> /tmp/try.log || echo "Warning: Failed mounting $top_dir_abs as an overlay..." 1>&2
fi
done
## Bind the udev mount so that the containerized process has access to /dev
## KK 2023-05-06 Are there any secutiry/safety implications by binding the whole /dev?
## KK 2023-05-06 Are there any security/safety implications by binding the whole /dev?
## Maybe we just want to bind a few files in it like /dev/null, /dev/zero?
mount --rbind /dev "$SANDBOX_DIR/temproot/dev"
mount --rbind --read-only /run "$SANDBOX_DIR/temproot/run"
Expand Down Expand Up @@ -97,20 +109,36 @@ summary() {
exit 1
fi

# TODO let people control what's ignored
# We don't include directories here since that would be too verbose for the summary.
changed_files=$(find "$SANDBOX_DIR/upperdir/" -type f -or \( -type c -size 0 \) | grep -v -e .rkr -e Rikerfile)
# We don't include directories here (like in commit) since that would be too verbose for the summary.
changed_files=$(find "$SANDBOX_DIR/upperdir/" -type f -or \( -type c -size 0 \) | ignore_changes)

if [ "$changed_files" ]
if [ -z "$changed_files" ];
then
echo
echo "Changes detected in the following files:"
echo
echo "$changed_files"
return 0
else
return 1
fi

echo
echo "Changes detected in the following files:"
echo
while IFS= read -r changed_file; do
local_file="${changed_file#$SANDBOX_DIR/upperdir}"
## KK 2023-06-20 Could print local_file instead of changed file for
## cleaner output.
if [ -d "$changed_file" ] && ! [ -d "${local_file}" ]
then # new directory
## KK 2023-06-20 This is not reachable since the `type -d` option is not given to find above.
echo "$changed_file (created)"
elif [ -c "$changed_file" ] && ! [ -s "$changed_file" ]
then # whiteout file
echo "$changed_file (deleted)"
elif [ -f "$changed_file" ]
then # normal file
echo "$changed_file (modified/added)"
fi
done <<EOF
$changed_files
EOF
return 0
}

################################################################################
Expand All @@ -121,7 +149,7 @@ commit() {
# This is different from the one in summary because it also includes all directories.
# TODO: Could be made more efficient by only appending directories to the already computed
# changed_files from summary.
changed_files=$(find "$SANDBOX_DIR/upperdir/" -type f -o \( -type c -size 0 \) -o -type d | grep -v -e .rkr -e Rikerfile)
changed_files=$(find "$SANDBOX_DIR/upperdir/" -type f -o \( -type c -size 0 \) -o -type d | ignore_changes)

while IFS= read -r changed_file; do
local_file="${changed_file#$SANDBOX_DIR/upperdir}"
Expand All @@ -148,6 +176,14 @@ $changed_files
EOF
}


## Defines which changes we want to ignore in the summary and commit
## TODO: Make this be parametrizable, through a file for example
ignore_changes() {
grep -v -e .rkr -e Rikerfile
}


################################################################################
# Argument parsing
################################################################################
Expand Down

0 comments on commit 3d5af35

Please sign in to comment.