Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions agents/skills/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ prompt themselves. Migrations must be idempotent: if one user already applied a
machine-wide repair, the same migration running for another user should detect
that and no-op.

The marker is in the home directory, which a snapshot restore of the root filesystem leaves alone, while much of what migrations change goes back with the root. So the runner keeps two more empty files for each migration it completes:

```text
/var/lib/omarchy/migration-records/<uid>/<migration filename> (root record, on the root filesystem)
~/.local/state/omarchy/migrations-receipts/<machine-id>/<migration filename> (receipt: a root record was written on this machine)
```

A migration whose receipt for this machine is there while its root record is gone has been rolled back: the root filesystem was restored to a point before it ran. It is pending again, `omarchy-migrate --pending` lists it, and the next run announces it as running again after a snapshot restore. A marker without a receipt is trusted as before, which covers every marker written before the records existed, the markers seeded from `/etc/skel`, and a home directory carried to another machine or a reinstall.

The records directory belongs to the user but sits under root-owned directories, so creating it takes root once. The runner creates it only in a process that inherited the update lock, which is what `omarchy update` gives it along with its sudo authorization, and it uses `sudo -n`, so it never asks for a password. Any other run writes records when the directory is there and otherwise completes without them. Records and receipts are best effort: failing to write one never fails a migration, it only means a later restore will not be noticed for that migration. This is one more reason migrations must be idempotent: a rolled-back migration runs a second time against a home directory that already has its changes.

## When migrations run

### During `omarchy update`
Expand Down Expand Up @@ -165,6 +176,12 @@ rm ~/.local/state/omarchy/migrations/<migration>.sh
omarchy-migrate
```

To accept a rolled-back migration as done without running it again, remove its receipt and leave its marker:

```bash
rm ~/.local/state/omarchy/migrations-receipts/"$(cat /etc/machine-id)"/<migration>.sh
```

Don't add a test for a migration under `test/`. It runs once per machine and is frozen once it ships, so the test stops guarding anything. If a migration needs logic worth testing, put that logic in a `bin/` command the migration calls and test the command. Tests of `omarchy-migrate`, the login notifier, and `omarchy-upgrade-to-quattro` stay.

Omarchy 4.0 is upgraded through `bin/omarchy-upgrade-to-quattro`, not through the
Expand Down
20 changes: 20 additions & 0 deletions bin/omarchy-boot-sync
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,25 @@ bootable_snapshots() {
done | sort -rn | head -n "$MAX_SNAPSHOTS"
}

# Deleting a snapshot, snapper removes the files it wrote and then the
# directory, which the pin keeps it from doing: the pin stays behind, and the
# image it names with it. Drops the pins of snapshots that are gone, and their
# directories, except the highest-numbered one, which snapper keeps itself so
# a number is never used twice.
drop_deleted_pins() {
local dir num highest=0
for dir in "$(snapshots_dir)"/*/; do
num=$(basename "$dir")
if [[ $num =~ ^[0-9]+$ ]] && ((num > highest)); then highest=$num; fi
done
for dir in "$(snapshots_dir)"/*/; do
num=$(basename "$dir")
[[ $num =~ ^[0-9]+$ && -f $dir/omarchy-boot-image && ! -d $dir/snapshot ]] || continue
rm -f "$dir/omarchy-boot-image"
if ((num != highest)); then rmdir "$dir" 2>/dev/null || true; fi
done
}

snapshot_details() {
local info=$1 desc date
desc=$(info_field "$info" description)
Expand Down Expand Up @@ -452,6 +471,7 @@ cmd_sync() {
rm -rf "$STORE"/tmp/build.*
# Boot images no longer carry a splash; a store from before keeps none.
rm -f "$STORE/splash.bmp"
drop_deleted_pins

local ns preferred pkgbase kver image num dir file id
local -A profiles=() wanted=()
Expand Down
54 changes: 51 additions & 3 deletions bin/omarchy-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ OMARCHY_PATH="${OMARCHY_PATH:-/usr/share/omarchy}"
STATE_DIR="${OMARCHY_MIGRATION_STATE:-$HOME/.local/state/omarchy/migrations}"
MIGRATIONS_DIR="$OMARCHY_PATH/migrations"

# Markers live in $HOME, which a snapshot restore leaves alone, while much of
# what migrations change is on the root filesystem and goes back with it. So a
# completed migration also gets a record on the root filesystem, and a receipt
# next to the markers saying that record was written on this machine. A receipt
# whose record is gone means the root filesystem was restored to a point before
# the migration ran, so the migration is pending again. Markers without a
# receipt (older runs, another machine's home) are trusted as before.
RECORDS_DIR="${OMARCHY_MIGRATION_RECORDS:-/var/lib/omarchy/migration-records/$UID}"
machine_id=""
IFS= read -r machine_id 2>/dev/null <"${OMARCHY_MIGRATION_MACHINE_ID:-/etc/machine-id}" || true
[[ $machine_id =~ ^[0-9a-f]{32}$ ]] || machine_id=""
RECEIPTS_DIR="$STATE_DIR-receipts/$machine_id"

rolled_back() {
[[ -n $machine_id && -f $RECEIPTS_DIR/$1 && ! -f $RECORDS_DIR/$1 ]]
}

# The records directory is on the root filesystem but owned by the user, so it
# takes root once to create and nothing after that. Only a run that inherited
# the update lock creates it, which is what omarchy update gives it along with
# its authorization, and sudo -n never asks. Any other run records when the
# directory is there and otherwise goes on without, which leaves its markers
# trusted as before.
record_on_root() {
[[ -n $machine_id ]] || return 1
if [[ ! -d $RECORDS_DIR ]]; then
omarchy-update-lock held 2>/dev/null || return 1
sudo -n install -d -m 755 -o "$UID" "$RECORDS_DIR" 2>/dev/null || return 1
fi
touch "$RECORDS_DIR/$1" 2>/dev/null && [[ -f $RECORDS_DIR/$1 ]]
}

migration_entries() {
[[ -d $MIGRATIONS_DIR ]] || return 0

Expand All @@ -49,7 +81,7 @@ pending_migrations() {

while IFS=$'\t' read -r name file marker; do
[[ -n $name ]] || continue
if [[ ! -f $marker ]]; then
if [[ ! -f $marker ]] || rolled_back "$name"; then
printf '%s\n' "$name"
fi
done < <(migration_entries)
Expand Down Expand Up @@ -89,8 +121,17 @@ deferred=0
while IFS=$'\t' read -r name file marker <&3; do
[[ -n $name ]] || continue

if [[ ! -f $marker ]]; then
echo -e "\e[32m\n$(omarchy-i18n update.migration "${name%.sh}")\e[0m"
again=0
if [[ -f $marker ]] && rolled_back "$name"; then
again=1
fi

if [[ ! -f $marker ]] || (( again )); then
if (( again )); then
echo -e "\e[32m\n$(omarchy-i18n update.migrationAgain "${name%.sh}")\e[0m"
else
echo -e "\e[32m\n$(omarchy-i18n update.migration "${name%.sh}")\e[0m"
fi
status=0
OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$file" 3<&- || status=$?

Expand All @@ -106,6 +147,13 @@ while IFS=$'\t' read -r name file marker <&3; do
fi

mkdir -p "$(dirname "$marker")"
# Receipts are best effort: one that cannot be written only loses the
# rerun after a restore, and must not fail a migration that succeeded.
if record_on_root "$name"; then
{ mkdir -p "$RECEIPTS_DIR" && touch "$RECEIPTS_DIR/$name"; } 2>/dev/null || true
elif [[ -n $machine_id ]]; then
rm -f "$RECEIPTS_DIR/$name" 2>/dev/null || true
fi
touch "$marker"
fi
done 3< <(migration_entries)
Expand Down
39 changes: 30 additions & 9 deletions bin/omarchy-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,46 @@ create)

root_number=""
for config in "${CONFIGS[@]}"; do
number=$(sudo snapper -c "$config" create -c number -d "$DESC" -p)
[[ $config == "root" ]] && root_number=$number
err=$(mktemp)
if number=$(sudo snapper -c "$config" create -c number -d "$DESC" -p 2>"$err"); then
[[ -s $err ]] && cat "$err" >&2
[[ -n $number ]] && printf '%s\n' "$number"
[[ $config == "root" ]] && root_number=$number
else
status=$?
output=$(cat "$err")
rm -f "$err"
printf '%s\n' "$output" >&2
if [[ $output == *".snapshots is not a btrfs subvolume"* ]]; then
echo "Find the affected SUBVOLUME with: sudo snapper -c \"$config\" get-config" >&2
echo "If its .snapshots is a regular directory, back it up and move it aside, then run:" >&2
echo " sudo btrfs subvolume create <subvolume>/.snapshots" >&2
echo "Replace <subvolume> with SUBVOLUME, restore the directory's ownership and permissions, then retry." >&2
fi
exit "$status"
fi
rm -f "$err"
sudo snapper -c "$config" cleanup number
done

# On systemd-boot the snapshot's boot entry is built by omarchy-boot-sync;
# wait for it, and check it is there, so an update only goes ahead once its
# rollback point boots. Until a switch to systemd-boot finishes, nothing pins
# a snapshot to a boot image (omarchy-boot-systemd's snapper plugin comes
# with the finish): its rollback point is in Limine's menu.
# On systemd-boot the snapshot's boot entry is built by omarchy-boot-sync.
# On Limine the sync normally runs from its own service, but a mismatched
# /etc/default/limine makes it reject every entry while this script kept
# reporting success. Run it here and only claim the snapshot is bootable
# when the sync accepted it. The snapshot exists either way, so a failed
# sync warns without failing the update.
if [[ $backend == "systemd-boot" && -f ${OMARCHY_BOOT_SYSTEMD_MARKER:-/usr/lib/snapper/plugins/20-omarchy-boot} ]]; then
if ! sudo omarchy-boot-sync sync >/dev/null ||
{ [[ -n $root_number ]] && ! grep -q "^snapshot-$root_number"$'\t' "${OMARCHY_BOOT_STORE:-/var/lib/omarchy/boot}/entries"; }; then
echo -e "\e[33m$(omarchy-i18n snapshot.bootEntryFailed)\e[0m" >&2
exit 1
fi
echo "$(omarchy-i18n snapshot.bootSelectable)"
elif sudo limine-snapper-sync; then
echo "Snapshots can be selected during boot."
else
echo -e "\e[33mSnapshot created, but limine-snapper-sync could not add it to the boot menu. Check journalctl -t limine-snapper-sync.\e[0m" >&2
fi

echo "$(omarchy-i18n snapshot.bootSelectable)"
;;
list)
if [[ $backend == systemd-boot ]]; then
Expand Down
8 changes: 8 additions & 0 deletions bin/omarchy-update
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

set -e

# omarchy-update-stay-awake refuses an OMARCHY_PATH that does not name the
# install it runs from, as in a shell that never loaded the session environment.
# Ask the same question of this command before pruning the package cache and
# taking a snapshot, rather than abort with that work already done.
update_entrypoint=$(readlink -e -- "${BASH_SOURCE[0]}")
source "${update_entrypoint%/*}/omarchy-security-functions"
omarchy_security_require_source_root "$0" || exit 1

update_stay_awake_stopped=0
sudo_keepalive_pid=""

Expand Down
39 changes: 37 additions & 2 deletions bin/omarchy-update-requires-free-space
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,42 @@ elif read -r available_bytes < <(df --output=avail --block-size=1 / 2>/dev/null
[[ $available_bytes =~ ^[0-9]+$ ]] &&
(( available_bytes < 10 * 1024 * 1024 * 1024 )); then
echo "$(omarchy-i18n update.needsFreeSpace 10)"
if findmnt -no OPTIONS / 2>/dev/null | grep -q 'subvol=.*\.snapshots/.*/snapshot'; then
echo "It looks like you are booted from a Btrfs snapshot. Reboot into the regular Omarchy entry, then update from there."
fi
exit 1
else
exit 0
fi

# On btrfs, every byte of df's free space can sit inside partly-used data
# chunks, where metadata chunks may not go. Once the whole device is allocated
# to chunks and metadata headroom is nearly gone, an update dies with ENOSPC
# while df still shows gigabytes free (#15528).
if [[ $(findmnt -no FSTYPE / 2>/dev/null) == btrfs ]]; then
btrfs_df=$(btrfs filesystem df --raw / 2>/dev/null) || exit 0
data_total=$(sed -n 's/^Data,.*total=\([0-9]*\),.*/\1/p' <<<"$btrfs_df")
metadata_total=$(sed -n 's/^Metadata,.*total=\([0-9]*\),.*/\1/p' <<<"$btrfs_df")
metadata_used=$(sed -n '/^Metadata,/s/.*used=\([0-9]*\).*/\1/p' <<<"$btrfs_df")
system_total=$(sed -n 's/^System,.*total=\([0-9]*\),.*/\1/p' <<<"$btrfs_df")

root_device=$(findmnt -no SOURCE / 2>/dev/null | sed 's/\[.*//')
device_bytes=$(lsblk -bno SIZE "$root_device" 2>/dev/null | head -n 1)

if [[ -n $data_total && -n $metadata_total && -n $metadata_used && -n $device_bytes ]]; then
allocated=$((data_total + metadata_total + ${system_total:-0}))
unallocated=$((device_bytes - allocated))
Comment thread
chessing234 marked this conversation as resolved.
Resolved
metadata_free=$((metadata_total - metadata_used))

# Allocated beyond the one device we can see means more than one device, or
# accounting this check does not model; either way it says nothing and the
# update goes on.
if (( allocated <= device_bytes )) &&
(( unallocated < 1024 ** 3 )) &&
(( metadata_free < 1024 ** 3 )); then
echo "The btrfs root has almost no unallocated device space left, so no new metadata chunks can be created and the update can fail with ENOSPC even though df still shows space free."
echo "Reclaim partly-used data chunks with: sudo btrfs balance start -dusage=50 /"
Comment thread
chessing234 marked this conversation as resolved.
Resolved
exit 1
fi
fi
fi

exit 0
1 change: 1 addition & 0 deletions default/i18n/en/cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@
"update.migrationsWaiting": "Waiting for pacman transaction to finish before running Omarchy migrations...",
"update.migrationsDeferred": "Pacman transaction is still running; Omarchy migrations will retry on next login.",
"update.migration": "Running migration (%1)",
"update.migrationAgain": "Running migration again after a snapshot restore (%1)",
"update.migrationDeferred": "Migration %1 is waiting for a package update; it and the migrations after it will run on the next update or login.",
"update.orphans": "Orphan system packages",
"update.orphansFound": {
Expand Down
26 changes: 17 additions & 9 deletions default/initcpio/hooks/omarchy-restore
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ omarchy_restore_get() {
sed -n "s/^$1=//p" "$OMARCHY_RESTORE_JOURNAL" | tail -1
}

omarchy_restore_children() {
# The journal separates paths with colons, not spaces within a path.
omarchy_restore_get CHILDREN | tr ':' '\n'
}

omarchy_restore_set() {
sed "/^$1=/d" "$OMARCHY_RESTORE_JOURNAL" >"$OMARCHY_RESTORE_JOURNAL.new" &&
echo "$1=$2" >>"$OMARCHY_RESTORE_JOURNAL.new" &&
Expand All @@ -168,12 +173,11 @@ omarchy_restore_is_subvol() {
}

omarchy_restore_apply() {
local top=$1 staged backup staged_uuid root_uuid child children
local top=$1 staged backup staged_uuid root_uuid child
staged=$(omarchy_restore_get STAGED)
backup=$(omarchy_restore_get BACKUP)
staged_uuid=$(omarchy_restore_get STAGED_UUID)
root_uuid=$(omarchy_restore_get ROOT_UUID)
children=$(omarchy_restore_get CHILDREN | sed 's/:/ /g')
omarchy_restore_set STATE swapping

# 1. The old root steps aside, unless an interrupted run already moved it.
Expand All @@ -193,7 +197,8 @@ omarchy_restore_apply() {
[ "$(omarchy_restore_uuid "$top/@")" = "$staged_uuid" ] || omarchy_restore_fail "@ is not the staged root" || return 1

# 3. Nested subvolumes move over, each onto an empty placeholder.
for child in $children; do
while IFS= read -r child; do
[ -n "$child" ] || continue
if omarchy_restore_is_subvol "$top/@/$child"; then
continue
fi
Expand All @@ -204,26 +209,29 @@ omarchy_restore_apply() {
mkdir -p "$(dirname "$top/@/$child")"
mv "$top/$backup/$child" "$top/@/$child" || omarchy_restore_fail "could not move $child" || return 1
sync
done
done <<EOF
$(omarchy_restore_children)
EOF
return 0
}

# Undoes whatever apply got through, in reverse.
omarchy_restore_rollback() {
local top=$1 staged backup staged_uuid root_uuid child children
local top=$1 staged backup staged_uuid root_uuid child
staged=$(omarchy_restore_get STAGED)
backup=$(omarchy_restore_get BACKUP)
staged_uuid=$(omarchy_restore_get STAGED_UUID)
root_uuid=$(omarchy_restore_get ROOT_UUID)
children=$(omarchy_restore_get CHILDREN | sed 's/:/ /g')

if [ "$(omarchy_restore_uuid "$top/@")" = "$staged_uuid" ]; then
for child in $children; do
while IFS= read -r child; do
[ -n "$child" ] || continue
if omarchy_restore_is_subvol "$top/@/$child" && [ ! -e "$top/$backup/$child" ]; then
mv "$top/@/$child" "$top/$backup/$child"
mkdir -p "$top/@/$child"
fi
done
done <<EOF
$(omarchy_restore_children)
EOF
mv "$top/@" "$top/$staged"
fi
if [ ! -e "$top/@" ] && [ "$(omarchy_restore_uuid "$top/$backup")" = "$root_uuid" ]; then
Expand Down
2 changes: 1 addition & 1 deletion docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ On systemd-boot, every kernel build is a boot image.
- **Published state.** `$STORE/entries` maps profile IDs to entry IDs. The sync sets `loader.conf`'s `default` to the preferred kernel's live entry (`omarchy-boot-sync prefer PKGBASE`, used by `omarchy-kernel set`).
- **Writing and cleanup.** Images are written beside the old file and renamed over it, after checking ESP space. The sync deletes only its own namespace's files, and never the image a pending one-shot boot points at.

A snapshot is pinned to the image that was current when it was taken (`/.snapshots/N/omarchy-boot-image`), not just to a kernel version. Rolling back from a broken kernel, initramfs or command-line change therefore boots the old ones. Images stay while a pin, the current pointer, the backup or a pending restore needs them.
A snapshot is pinned to the image that was current when it was taken (`/.snapshots/N/omarchy-boot-image`), not just to a kernel version. Rolling back from a broken kernel, initramfs or command-line change therefore boots the old ones. Images stay while a pin, the current pointer, the backup or a pending restore needs them. Snapper leaves the pin of a snapshot it deletes behind, since it only removes its own files; the next sync drops it.

The pipeline:

Expand Down
4 changes: 4 additions & 0 deletions docs/update-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The design goal is:
| `/tmp/omarchy-update.log` | user | Transcript of `omarchy update`, used by `omarchy-update-analyze-logs`. |
| `~/.local/state/omarchy/current/` | user | Generated active theme, selected theme name, and current background symlink. |
| `~/.local/state/omarchy/migrations/` | user | Per-user migration markers. |
| `~/.local/state/omarchy/migrations-receipts/<machine-id>/` | user | Per-user receipts: a root record was written for this migration on this machine. |
| `/var/lib/omarchy/migration-records/<uid>/` | user | Root records of completed migrations, on the root filesystem so a snapshot restore takes them back. Created with `sudo -n` by a migration run that inherited the update lock. |
| `~/.local/state/omarchy/reboot-required` | user | Optional reboot marker checked by `omarchy-update-restart`. |
| `~/.local/state/omarchy/restart-*-required` | user | Optional service/app restart markers checked by `omarchy-update-restart`. The shell needs no marker: it is restarted unconditionally after every update. |

Expand Down Expand Up @@ -57,6 +59,8 @@ privileged work should invoke the appropriate helper or privilege prompt.
Migrations must be idempotent; if one user already applied a machine-wide repair,
the migration should no-op for other users.

A snapshot restore of the root filesystem leaves those markers in place, so the runner also keeps a root record of each completed migration and a receipt for it in the home directory. A migration whose receipt is there while its root record is gone was rolled back by a restore and runs again; [`migrations.md`](../agents/skills/migrations.md) has the paths and the rules.

When invoked by the update, migrations share its single sudo authorization. The standalone migration runner has its own security changes in the migration-boundary PR; this update change does not establish that standalone boundary. Historical migrations remain strictly ordered.

For watchers and diagnostics, `omarchy-migrate --pending` prints pending
Expand Down
7 changes: 7 additions & 0 deletions etc/tmpfiles.d/omarchy-btrfs-reclaim.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Let btrfs compact partly used data block groups on its own. Without it, data
# chunks can claim the whole device over time, metadata has no room to grow, and
# writes fail with ENOSPC while df still shows free space. Data only: metadata
# reclaim stays off. Kernels before 6.11 have no such files, so the globs match
# nothing there.
w /sys/fs/btrfs/*/allocation/data/dynamic_reclaim - - - - 1
w /sys/fs/btrfs/*/allocation/data/periodic_reclaim - - - - 1
Loading
Loading