2023-06-26 19:52:01 +02:00
|
|
|
#!/usr/bin/env bash
|
2023-06-26 17:04:28 +02:00
|
|
|
|
2023-06-29 14:02:50 +02:00
|
|
|
# used to work around cases where stdin is unavailable or not a terminal
|
|
|
|
if bash -c ': > /dev/tty' > /dev/null 2>&1; then
|
|
|
|
TTY_OR_CONSOLE="/dev/tty"
|
|
|
|
else
|
|
|
|
TTY_OR_CONSOLE="/dev/console"
|
|
|
|
fi
|
|
|
|
|
2023-07-21 18:44:09 +02:00
|
|
|
function echo_tty {
|
|
|
|
if [ "$1" = "-n" ]; then
|
|
|
|
shift
|
|
|
|
echo -n "$@" > $TTY_OR_CONSOLE
|
|
|
|
else
|
|
|
|
echo "$@" > $TTY_OR_CONSOLE
|
|
|
|
fi
|
|
|
|
}
|
2023-06-29 14:02:50 +02:00
|
|
|
|
2023-06-26 17:04:28 +02:00
|
|
|
function yesno {
|
|
|
|
unset DISCARD_ME
|
|
|
|
while [ -z "$DISCARD_ME" ]; do
|
2023-07-21 18:44:09 +02:00
|
|
|
read -p "[y/n] " -n1 DISCARD_ME < $TTY_OR_CONSOLE > $TTY_OR_CONSOLE
|
2023-06-26 17:04:28 +02:00
|
|
|
case "$DISCARD_ME" in
|
|
|
|
y)
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty
|
2023-06-26 17:04:28 +02:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
n)
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty
|
2023-06-26 17:04:28 +02:00
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
*)
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty " Please enter y for yes or n for no."
|
2023-06-26 17:04:28 +02:00
|
|
|
unset DISCARD_ME
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#TODO: refuse to run if system is running
|
|
|
|
|
2023-07-21 18:44:09 +02:00
|
|
|
#TODO: add a way to force a new system image without asking (for example a file in /run/void-usb)
|
2023-06-26 17:04:28 +02:00
|
|
|
|
2023-06-29 20:13:18 +02:00
|
|
|
#TODO: colors (bright white / light gray for readability)
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty "Create a new system image?
|
2023-06-26 17:04:28 +02:00
|
|
|
|
2023-07-14 21:27:57 +02:00
|
|
|
This will replace the existing system image with a snapshot of the currently
|
|
|
|
running system. Choosing no here will discard any newly installed programs,
|
|
|
|
updates, system configuration changes, etc.
|
|
|
|
|
|
|
|
This should not affect your files in /home as they are stored to disk directly.
|
|
|
|
Check /TODO/README.txt for more information.
|
|
|
|
|
|
|
|
Creating a new system image will take some time.
|
2023-06-26 17:04:28 +02:00
|
|
|
"
|
|
|
|
|
|
|
|
if yesno; then
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty -n "Generating exclusion list... "
|
|
|
|
# cutting off the leading slash allows us to just ignore empty lines below
|
|
|
|
readarray -t MOUNT_LIST <<< "$(findmnt --output TARGET --noheadings --raw | sed -e 's|^/||' | sort)"
|
|
|
|
# always ignore /tmp and the package cache
|
|
|
|
EXCLUDE_LIST=("tmp" "var/cache/xbps")
|
|
|
|
|
|
|
|
for I in ${!MOUNT_LIST[@]}; do
|
|
|
|
if [ ! -z "${MOUNT_LIST[$I]}" ]; then
|
|
|
|
FOUND=0
|
|
|
|
for J in ${!EXCLUDE_LIST[@]}; do
|
|
|
|
if grep "${EXCLUDE_LIST[$J]}" > /dev/null <<< "${MOUNT_LIST[$I]}"; then
|
|
|
|
FOUND=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# if no parent dir of ${MOUNT_LIST[$I]} is found in $EXCLUDE_LIST
|
|
|
|
if [ $FOUND -eq 0 ]; then
|
|
|
|
EXCLUDE_LIST+=("${MOUNT_LIST[$I]}")
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
touch /run/void-usb/system-image-excludes
|
|
|
|
touch /run/void-usb/system-image-pseudo
|
|
|
|
for I in ${!EXCLUDE_LIST[@]}; do
|
|
|
|
echo "/${EXCLUDE_LIST[$I]}" >> /run/void-usb/system-image-excludes
|
|
|
|
FILE_PERMS="$(stat -c '%a %u %g' "${EXCLUDE_LIST[$I]}")"
|
|
|
|
echo "\"/${EXCLUDE_LIST[$I]}\" d $FILE_PERMS" >> /run/void-usb/system-image-pseudo
|
|
|
|
done
|
|
|
|
echo_tty "done"
|
|
|
|
|
|
|
|
echo_tty "Building image..."
|
|
|
|
mksquashfs / /run/void-usb/container/new_squashfs.img -b 1M -comp xz -progress -noappend -pf /run/void-usb/system-image-pseudo -ef /run/void-usb/system-image-excludes > $TTY_OR_CONSOLE
|
|
|
|
|
|
|
|
echo_tty -n "Moving new image in place... "
|
2023-06-28 19:55:09 +02:00
|
|
|
mv /run/void-usb/container/new_squashfs.img /run/void-usb/container/squashfs.img
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty "done"
|
2023-06-26 17:04:28 +02:00
|
|
|
else
|
2023-07-21 18:44:09 +02:00
|
|
|
echo_tty "Discarding system changes."
|
2023-06-26 17:04:28 +02:00
|
|
|
fi
|
2023-07-21 18:44:09 +02:00
|
|
|
|
|
|
|
echo_tty -n "Syncing... "
|
|
|
|
sync
|
|
|
|
echo_tty "done"
|