Skip to content

Instantly share code, notes, and snippets.

@nikp123
Last active July 4, 2024 15:38
Show Gist options
  • Save nikp123/d56da1c17e29f60a584f50671b30e551 to your computer and use it in GitHub Desktop.
Save nikp123/d56da1c17e29f60a584f50671b30e551 to your computer and use it in GitHub Desktop.
Show *hypo*thetical filesystem changes by a command ;) before running it on a live system
#!/usr/bin/env sh
#
# Script by: github.com/nikp123
#
# Distributed under the terms of the MIT license
#
# The command requires ROOT despite the usage of fuse-overlayfs
# I look forward to fixing this in the future.
# You NEED the following commands already installed on your system:
# https://github.com/kmxz/overlayfs-tools/tree/master
# https://github.com/containers/fuse-overlayfs
#
if [ $# -lt 2 ]; then
echo "Usage: $0 directory_to_sandbox command"
exit -1
fi
source_dir="$1"
mkdir "$source_dir.up" "$source_dir.work"
fuse-overlayfs -o lowerdir="$source_dir",upperdir="$source_dir.up",workdir="$source_dir.work" "$source_dir"
${@:2}
echo -n "Waiting for unmount"
while ! umount "$source_dir" &> /dev/null; do
echo -n "."
sleep .1
done
echo ""
overlay diff -l "$source_dir" -u "$source_dir.up"
# Prompt the user with a yes/no question
echo -n "Apply changes? (y/N): "
read answer
# Check the user's response
case "$answer" in
[yY]|[yY][eE][sS]) # If answer is yes or y (case insensitive)
echo "Meging changes..."
overlay merge -l "$source_dir" -u "$source_dir.up"
./overlay-tools-*
rm overlay-tools-*
;;
*)
# NOOP
;;
esac
rm -rf "$source_dir.up" "$source_dir.work"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment