Skip to content
Closed
6 changes: 5 additions & 1 deletion bin/omarchy-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ create)
# nothing has ever been captured.
if (( ${#CONFIGS[@]} == 0 )); then
echo -e "\e[33mNo Snapper configs found, so no snapshot was created.\e[0m" >&2
echo "Configure Snapper with: sudo bash -euo pipefail \"$OMARCHY_PATH/install/config/snapper.sh\"" >&2
if [[ $(findmnt -n -o FSTYPE /) == "btrfs" ]]; then
echo "Configure Snapper with: sudo bash -euo pipefail \"$OMARCHY_PATH/install/config/snapper.sh\"" >&2
else
echo "Omarchy's automatic Snapper setup requires a Btrfs root filesystem." >&2
fi
exit 1
fi

Expand Down
86 changes: 72 additions & 14 deletions install/config/snapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,81 @@ SNAPPER_CONFIG_PATH="${OMARCHY_SNAPPER_CONFIG_PATH:-/etc/snapper/configs/root}"
SNAPPER_CONF_PATH="${OMARCHY_SNAPPER_CONF_PATH:-/etc/conf.d/snapper}"
template="${OMARCHY_SNAPPER_TEMPLATE:-${OMARCHY_PATH:-/usr/share/omarchy}/default/snapper/root}"

echo "Configuring Omarchy Snapper snapshot retention"
echo "Normalizing Omarchy Snapper snapshot retention"

if [[ ! -f $SNAPPER_CONFIG_PATH ]]; then
mkdir -p "$(dirname "$SNAPPER_CONFIG_PATH")"
root_fstype=$(findmnt -no FSTYPE /)

if [[ ${OMARCHY_SNAPPER_CONFIGURE_TEST:-0} == "1" ]]; then
: >"$SNAPPER_CONFIG_PATH"
else
snapper --no-dbus -c root create-config / >/dev/null 2>&1 || snapper -c root create-config / >/dev/null
if [[ $root_fstype == "btrfs" ]]; then
if [[ ! -f $SNAPPER_CONFIG_PATH ]]; then
mkdir -p "$(dirname "$SNAPPER_CONFIG_PATH")"

if [[ ${OMARCHY_SNAPPER_CONFIGURE_TEST:-0} == "1" ]]; then
: >"$SNAPPER_CONFIG_PATH"
else
snapper --no-dbus -c root create-config / >/dev/null 2>&1 || snapper -c root create-config / >/dev/null
fi
fi
fi

install -m 0644 "$template" "$SNAPPER_CONFIG_PATH"
install -m 0644 "$template" "$SNAPPER_CONFIG_PATH"

mkdir -p "$(dirname "$SNAPPER_CONF_PATH")"
printf '%s\n' 'SNAPPER_CONFIGS="root"' >"$SNAPPER_CONF_PATH"
chmod 0644 "$SNAPPER_CONF_PATH"

systemctl disable --now snapper-timeline.timer >/dev/null 2>&1 || true
systemctl enable --now snapper-cleanup.timer limine-snapper-sync.service >/dev/null 2>&1 || true
else
config_dir=$(dirname "$SNAPPER_CONFIG_PATH")

if [[ -f $SNAPPER_CONFIG_PATH ]] &&
grep -qFx 'SUBVOLUME="/"' "$SNAPPER_CONFIG_PATH" &&
grep -qFx 'FSTYPE="btrfs"' "$SNAPPER_CONFIG_PATH"; then
Comment thread
yashranaway marked this conversation as resolved.
Resolved
rm -f "$SNAPPER_CONFIG_PATH"
fi

mkdir -p "$(dirname "$SNAPPER_CONF_PATH")"
printf '%s\n' 'SNAPPER_CONFIGS="root"' >"$SNAPPER_CONF_PATH"
chmod 0644 "$SNAPPER_CONF_PATH"
snapper_configs=""
if [[ -f $SNAPPER_CONF_PATH ]]; then
snapper_configs=$(awk -v single="'" '
/^[[:space:]]*SNAPPER_CONFIGS[[:space:]]*=/ {
value = $0
sub(/^[^=]*=[[:space:]]*/, "", value)
quote = substr(value, 1, 1)
if (quote == "\"" || quote == single) {
value = substr(value, 2)
value = substr(value, 1, index(value, quote) - 1)
} else {
sub(/[[:space:]]*#.*/, "", value)
Comment thread
yashranaway marked this conversation as resolved.
Resolved
sub(/[[:space:]]*$/, "", value)
}
}
END { print value }
' "$SNAPPER_CONF_PATH")
if [[ ! -e $SNAPPER_CONFIG_PATH && " $snapper_configs " == *" root "* ]]; then
remaining_configs=""
for config in $snapper_configs; do
[[ $config == "root" ]] || remaining_configs+="${remaining_configs:+ }$config"
done
tmp=$(mktemp "$SNAPPER_CONF_PATH.XXXXXX")
awk -v configs="$remaining_configs" '
/^[[:space:]]*SNAPPER_CONFIGS[[:space:]]*=/ { print "SNAPPER_CONFIGS=\"" configs "\""; next }
{ print }
' "$SNAPPER_CONF_PATH" >"$tmp"
cat "$tmp" >"$SNAPPER_CONF_PATH"
rm -f "$tmp"
snapper_configs=$remaining_configs
fi
fi

systemctl disable --now snapper-timeline.timer >/dev/null 2>&1 || true
systemctl enable --now snapper-cleanup.timer limine-snapper-sync.service >/dev/null 2>&1 || true
has_configs=false
for config in $snapper_configs; do
if [[ -f $config_dir/$config ]]; then
has_configs=true
break
fi
done
if ! $has_configs; then
systemctl disable --now snapper-cleanup.timer >/dev/null 2>&1 || true
fi

systemctl disable --now limine-snapper-sync.service >/dev/null 2>&1 || true
fi
11 changes: 11 additions & 0 deletions migrations/1786517763.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
echo "Remove Btrfs snapshot setup from non-Btrfs roots"

[[ $(findmnt -no FSTYPE /) != "btrfs" ]] || exit 0

snapper_config_script="$OMARCHY_PATH/install/config/snapper.sh"

if (( EUID == 0 )); then
env OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$snapper_config_script"
else
sudo env OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$snapper_config_script"
fi
105 changes: 105 additions & 0 deletions test/shell.d/snapper-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ printf 'systemctl %s\n' "$*" >>"$TEST_LOG"
STUB
chmod +x "$fake_bin/systemctl"

cat >"$fake_bin/findmnt" <<'STUB'
#!/bin/bash
echo "${TEST_ROOT_FSTYPE:-btrfs}"
STUB
chmod +x "$fake_bin/findmnt"

cat >"$fake_bin/sudo" <<'STUB'
#!/bin/bash
"$@"
STUB
chmod +x "$fake_bin/sudo"

notification_migration=$(grep -rl 'Disable Limine Snapper warning notifier' "$ROOT/migrations" | head -n 1 || true)
[[ -n $notification_migration ]] || fail "Limine Snapper warning notifier migration exists"
grep -F 'limine-snapper-notify.desktop' "$notification_migration" >/dev/null
Expand Down Expand Up @@ -71,6 +83,42 @@ grep -Fx 'systemctl disable --now snapper-timeline.timer' "$test_tmp/calls.log"
grep -Fx 'systemctl enable --now snapper-cleanup.timer limine-snapper-sync.service' "$test_tmp/calls.log" >/dev/null || fail "snapshot configure enables cleanup and Limine snapshot sync"
pass "snapshot configure normalizes Snapper policy and services"

: >"$test_tmp/calls.log"
TEST_ROOT_FSTYPE=ext4 \
TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$ROOT" \
OMARCHY_SNAPPER_CONFIG_PATH="$test_tmp/etc/snapper/configs/root" \
OMARCHY_SNAPPER_CONF_PATH="$test_tmp/etc/conf.d/snapper" \
bash -euo pipefail "$ROOT/install/config/snapper.sh" >/dev/null

[[ ! -e $test_tmp/etc/snapper/configs/root ]] || fail "snapshot configure removes the invalid Btrfs root config"
grep -Fx 'SNAPPER_CONFIGS=""' "$test_tmp/etc/conf.d/snapper" >/dev/null || fail "snapshot configure drops the invalid root config from snapper defaults"
grep -Fx 'systemctl disable --now snapper-cleanup.timer' "$test_tmp/calls.log" >/dev/null || fail "snapshot configure disables cleanup without snapshot configs"
grep -Fx 'systemctl disable --now limine-snapper-sync.service' "$test_tmp/calls.log" >/dev/null || fail "snapshot configure disables Limine snapshot sync on non-Btrfs roots"
! grep -F 'systemctl enable' "$test_tmp/calls.log" >/dev/null || fail "snapshot configure does not enable snapshot services on non-Btrfs roots"
pass "snapshot configure removes Btrfs-only setup from non-Btrfs roots"

other_config_root="$test_tmp/other/etc/snapper/configs"
mkdir -p "$other_config_root" "$test_tmp/other/etc/conf.d"
cp "$template" "$other_config_root/root"
: >"$other_config_root/home"
printf '%s\n' 'SNAPPER_CONFIGS="root home"' >"$test_tmp/other/etc/conf.d/snapper"
: >"$test_tmp/calls.log"

TEST_ROOT_FSTYPE=ext4 \
TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$ROOT" \
OMARCHY_SNAPPER_CONFIG_PATH="$other_config_root/root" \
OMARCHY_SNAPPER_CONF_PATH="$test_tmp/other/etc/conf.d/snapper" \
bash -euo pipefail "$ROOT/install/config/snapper.sh" >/dev/null

[[ -f $other_config_root/home ]] || fail "snapshot configure preserves other snapshot configs"
grep -Fx 'SNAPPER_CONFIGS="home"' "$test_tmp/other/etc/conf.d/snapper" >/dev/null || fail "snapshot configure keeps other configs registered"
! grep -Fx 'systemctl disable --now snapper-cleanup.timer' "$test_tmp/calls.log" >/dev/null || fail "snapshot configure keeps cleanup available for other configs"
pass "snapshot configure preserves cleanup for other snapshot configs"

setup_system="$ROOT/bin/omarchy-apply-system"
grep -F 'config/all.sh' "$setup_system" >/dev/null ||
fail "system setup runs the config phase"
Expand All @@ -87,6 +135,33 @@ grep -F 'as_root env OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$snapper_c
! grep -F 'NUMBER_LIMIT="5"' "$migration" >/dev/null || fail "Snapper service migration does not overwrite working custom retention"
pass "Snapper service migration only repairs broken services idempotently"

non_btrfs_migration="$ROOT/migrations/1786517763.sh"
grep -Fx '[[ $(findmnt -no FSTYPE /) != "btrfs" ]] || exit 0' "$non_btrfs_migration" >/dev/null
grep -F 'bash -euo pipefail "$snapper_config_script"' "$non_btrfs_migration" >/dev/null

migration_root="$test_tmp/migration-root"
mkdir -p "$migration_root/install/config"
cat >"$migration_root/install/config/snapper.sh" <<'STUB'
printf '%s\n' snapper-config >>"$TEST_LOG"
STUB

: >"$test_tmp/calls.log"
TEST_ROOT_FSTYPE=ext4 \
TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$migration_root" \
bash -euo pipefail "$non_btrfs_migration" >/dev/null
grep -Fx 'snapper-config' "$test_tmp/calls.log" >/dev/null || fail "non-Btrfs migration runs the guarded snapshot configuration"

: >"$test_tmp/calls.log"
TEST_ROOT_FSTYPE=btrfs \
TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$migration_root" \
bash -euo pipefail "$non_btrfs_migration" >/dev/null
[[ ! -s $test_tmp/calls.log ]] || fail "non-Btrfs migration leaves Btrfs roots alone"
pass "non-Btrfs migration reuses the guarded snapshot configuration"

# Checkouts differ per machine, so allow an explicit pointer at the sibling repo.
# Accepts either the omarchy-pkgs checkout or its pkgbuilds/ directory.
find_omarchy_pks_root() {
Expand Down Expand Up @@ -155,3 +230,33 @@ if [[ -f $phases && -f $manifest ]]; then
! grep -F '/etc/systemd/system/timers.target.wants/snapper-timeline.timer' "$manifest" >/dev/null || fail "fresh ISO manifest does not enable snapper timeline timer"
fi
pass "omarchy-iso delegates Snapper setup to packaged system setup"

: >"$test_tmp/calls.log"
partial="$test_tmp/partial"
mkdir -p "$partial/configs/unrelated-directory"
printf '# keep my settings\nSNAPPER_CONFIGS="root"\nSNAPPER_DEBUG="yes"\n' >"$partial/snapper"
printf 'unrelated note\n' >"$partial/configs/README"
TEST_ROOT_FSTYPE=ext4 TEST_LOG="$test_tmp/calls.log" PATH="$fake_bin:$PATH" \
OMARCHY_SNAPPER_CONFIG_PATH="$partial/configs/root" OMARCHY_SNAPPER_CONF_PATH="$partial/snapper" \
bash -euo pipefail "$ROOT/install/config/snapper.sh" >/dev/null
grep -Fxq 'SNAPPER_CONFIGS=""' "$partial/snapper" || fail "partial cleanup removes stale root registration"
grep -Fxq '# keep my settings' "$partial/snapper" || fail "shared Snapper comments survive"
grep -Fxq 'SNAPPER_DEBUG="yes"' "$partial/snapper" || fail "unrelated Snapper settings survive"
grep -Fxq 'systemctl disable --now snapper-cleanup.timer' "$test_tmp/calls.log" || fail "unregistered files and directories do not keep cleanup enabled"
pass "partial non-Btrfs repair preserves shared settings and ignores unregistered files"

for assignment in "SNAPPER_CONFIGS='home'" 'SNAPPER_CONFIGS=home' ' SNAPPER_CONFIGS = "home" # retain cleanup'; do
printf '%s\n' "$assignment" >"$partial/snapper"
: >"$partial/configs/home"
: >"$test_tmp/calls.log"
TEST_ROOT_FSTYPE=ext4 TEST_LOG="$test_tmp/calls.log" PATH="$fake_bin:$PATH" \
OMARCHY_SNAPPER_CONFIG_PATH="$partial/configs/root" OMARCHY_SNAPPER_CONF_PATH="$partial/snapper" \
bash -euo pipefail "$ROOT/install/config/snapper.sh" >/dev/null
! grep -Fxq 'systemctl disable --now snapper-cleanup.timer' "$test_tmp/calls.log" || fail "valid quoted and unquoted registration keeps cleanup available"
done
printf "SNAPPER_CONFIGS='root home'\n" >"$partial/snapper"
TEST_ROOT_FSTYPE=ext4 TEST_LOG="$test_tmp/calls.log" PATH="$fake_bin:$PATH" \
OMARCHY_SNAPPER_CONFIG_PATH="$partial/configs/root" OMARCHY_SNAPPER_CONF_PATH="$partial/snapper" \
bash -euo pipefail "$ROOT/install/config/snapper.sh" >/dev/null
grep -Fxq 'SNAPPER_CONFIGS="home"' "$partial/snapper" || fail "single-quoted registration removes only the retired root"
pass "literal Snapper registration forms preserve valid cleanup configs"
19 changes: 19 additions & 0 deletions test/shell.d/snapshot-create-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ fi
STUB
chmod +x "$fake_bin/snapper"

cat >"$fake_bin/findmnt" <<'STUB'
#!/bin/bash
echo "${TEST_ROOT_FSTYPE:-btrfs}"
STUB
chmod +x "$fake_bin/findmnt"
export OMARCHY_PATH="$ROOT"

# A snapshot that silently creates nothing reads as a successful snapshot, so
# an unconfigured Snapper has to fail loudly instead of passing for a backup.
: >"$test_tmp/calls.log"
Expand All @@ -55,6 +62,18 @@ grep -qF 'No Snapper configs found' <<<"$stderr" ||
! grep -q '^snapper -c .* create ' "$test_tmp/calls.log" ||
fail "snapshot create does not invent a config to snapshot"
pass "snapshot create fails loudly when Snapper is installed but unconfigured"
grep -qF 'Configure Snapper with:' <<<"$stderr" || fail "Btrfs users receive setup guidance"

set +e
stderr=$(TEST_ROOT_FSTYPE=ext4 TEST_LOG="$test_tmp/calls.log" PATH="$fake_bin:$PATH" \
bash "$snapshot" create 2>&1 >/dev/null)
status=$?
set -e
(( status != 0 )) || fail "unconfigured ext4 snapshots still report failure"
grep -qF 'requires a Btrfs root filesystem' <<<"$stderr" || fail "ext4 users receive filesystem guidance"
! grep -qF 'Configure Snapper with:' <<<"$stderr" || fail "ext4 users are not sent to a setup that does nothing"
pass "unconfigured non-Btrfs systems receive accurate snapshot guidance"


cat >"$fake_bin/snapper" <<'STUB'
#!/bin/bash
Expand Down