feat(rollback): add slot selector and manager

This commit is contained in:
blazblue547-afk committed 2026-06-18 08:21:18 -04:00
1 parent 23681b9f48
commit f445a4ce10
12 files changed
+1421 -71

No files matched your search

+197 -14
View File
@@ -11,6 +11,7 @@ normal_rootfs_matches() {
[[ -d "$ROOTFS_STAGING_DIR" ]] || return 1
[[ -f "$ROOTFS_STAGING_DIR/etc/forgeos-nix" ]] || return 1
[[ -x "$ROOTFS_STAGING_DIR/usr/bin/forgeos-app" ]] || return 1
[[ -x "$ROOTFS_STAGING_DIR/usr/sbin/forgeos-rollback" ]] || return 1
[[ -f "$ROOTFS_STAGING_DIR/etc/machine-id" ]] || return 1
[[ ! -f "$ROOTFS_STAGING_DIR/etc/fstab" ]] || return 1
@@ -45,6 +46,11 @@ ROOT_IMAGE="$OUT_DIR/forgeos-root.img"
APP_IMAGE="$OUT_DIR/forgeos-apps.img"
IMAGE_ROOTFS_DIR="$ROOTFS_STAGING_DIR"
APP_IMAGE_NEEDED=0
ROOT_SLOT_COUNT=1
ROOT_PART_START=2
APP_PART_NUM=
declare -a ROOT_OFFSETS=()
declare -a ROOT_SIZES=()
partition_field() {
local partition=$1
@@ -57,6 +63,77 @@ partition_field() {
'
}
validate_rollback_slots() {
[[ "$ROLLBACK_SLOTS" =~ ^[0-9]+$ ]] || die "ROLLBACK_SLOTS must be a positive integer"
(( ROLLBACK_SLOTS >= 1 )) || die "ROLLBACK_SLOTS must be at least 1"
}
root_slot_partlabel() {
local slot=$1
if (( slot == 0 )); then
printf 'root\n'
elif (( slot == 1 )); then
printf 'root-rollback\n'
else
printf 'root-rollback-%s\n' "$slot"
fi
}
root_slot_fs_label() {
local slot=$1
if (( slot == 0 )); then
printf '%s\n' "$ROOT_LABEL"
else
printf '%s_%s\n' "$ROOT_LABEL" "$slot"
fi
}
root_slot_image() {
local slot=$1
if (( slot == 0 )); then
printf '%s\n' "$ROOT_IMAGE"
else
printf '%s/forgeos-root-%s.img\n' "$OUT_DIR" "$slot"
fi
}
root_slot_title() {
local slot=$1
if (( slot == 0 )); then
printf 'current base\n'
elif (( slot == 1 )); then
printf 'rollback base\n'
else
printf 'rollback base %s\n' "$slot"
fi
}
configure_root_slots() {
local slot
local fs_label
local partlabel
validate_rollback_slots
if atomic_base_enabled; then
ROOT_SLOT_COUNT=$ROLLBACK_SLOTS
else
ROOT_SLOT_COUNT=1
fi
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
fs_label=$(root_slot_fs_label "$slot")
(( ${#fs_label} <= 16 )) || die "ext4 label '$fs_label' is too long; shorten ROOT_LABEL or reduce rollback slots"
partlabel=$(root_slot_partlabel "$slot")
[[ "$partlabel" != *[[:space:]]* ]] || die "root partition label must not contain whitespace: $partlabel"
done
}
reset_staging_tree() {
local path=$1
@@ -183,13 +260,88 @@ prepare_atomic_image_trees() {
APP_IMAGE_NEEDED=1
}
write_grub_root_slots() {
local linux_args=$1
local slot
local partlabel
partlabel=$(root_slot_partlabel 0)
printf 'set linux_common="root=PARTLABEL=%s forgeos.root=PARTLABEL=%s %s"\n' "$partlabel" "$partlabel" "$linux_args" >> "$GRUB_CFG"
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
partlabel=$(root_slot_partlabel "$slot")
printf 'set linux_slot_%s="root=PARTLABEL=%s forgeos.root=PARTLABEL=%s %s"\n' "$slot" "$partlabel" "$partlabel" "$linux_args" >> "$GRUB_CFG"
done
printf '\n' >> "$GRUB_CFG"
}
write_grub_rollback_menu() {
local slot
local title
local partlabel
(( ROOT_SLOT_COUNT > 1 )) || return 0
cat >> "$GRUB_CFG" <<'EOF'
submenu "ForgeOS rollback selector" {
EOF
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
title=$(root_slot_title "$slot")
partlabel=$(root_slot_partlabel "$slot")
if [[ "$DESKTOP" != "gnome" ]] && ! truthy "$ENABLE_DESKTOP"; then
printf ' menuentry "ForgeOS - %s (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG"
printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG"
printf ' set gfxmode=1024x768\n' >> "$GRUB_CFG"
printf ' set gfxpayload=keep\n' >> "$GRUB_CFG"
printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s $handoff_common nomodeset video=efifb:1024x768 fbcon=map:0 fbcon=font:VGA8x16\n' "$slot" >> "$GRUB_CFG"
printf ' initrd ($esp)/EFI/BOOT/rootfs.cpio.gz\n' >> "$GRUB_CFG"
printf ' }\n' >> "$GRUB_CFG"
printf '\n' >> "$GRUB_CFG"
printf ' menuentry "ForgeOS - %s normal display (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG"
printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG"
printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s $handoff_common i915.enable_psr=0 i915.fastboot=0\n' "$slot" >> "$GRUB_CFG"
printf ' initrd ($esp)/EFI/BOOT/rootfs.cpio.gz\n' >> "$GRUB_CFG"
printf ' }\n' >> "$GRUB_CFG"
else
printf ' menuentry "ForgeOS - %s (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG"
printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG"
printf ' set gfxmode=1024x768\n' >> "$GRUB_CFG"
printf ' set gfxpayload=keep\n' >> "$GRUB_CFG"
printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s nomodeset video=efifb:1024x768 fbcon=map:0 fbcon=font:VGA8x16\n' "$slot" >> "$GRUB_CFG"
printf ' }\n' >> "$GRUB_CFG"
fi
printf '\n' >> "$GRUB_CFG"
printf ' menuentry "ForgeOS - %s direct kernel (%s)" {\n' "$title" "$partlabel" >> "$GRUB_CFG"
printf ' search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI\n' >> "$GRUB_CFG"
printf ' linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_slot_%s i915.enable_psr=0 i915.fastboot=0\n' "$slot" >> "$GRUB_CFG"
printf ' }\n' >> "$GRUB_CFG"
if (( slot + 1 < ROOT_SLOT_COUNT )); then
printf '\n' >> "$GRUB_CFG"
fi
done
cat >> "$GRUB_CFG" <<'EOF'
}
EOF
}
write_grub_config() {
local root_mode=rw
local linux_args
if atomic_base_enabled; then
root_mode=ro
fi
linux_args="rootfstype=ext4 rootwait $root_mode console=tty0 loglevel=7 ignore_loglevel earlyprintk=efi,keep vt.global_cursor_default=1"
cat > "$GRUB_CFG" <<'EOF'
set timeout=10
set default=0
@@ -201,10 +353,13 @@ set gfxpayload=keep
insmod all_video
insmod gfxterm
terminal_output gfxterm console
set linux_common="__LINUX_COMMON__"
set initrd_common="rdinit=/sbin/init"
set handoff_common="rdinit=/init forgeos.switch_root=1"
EOF
write_grub_root_slots "$linux_args"
cat >> "$GRUB_CFG" <<'EOF'
menuentry "ForgeOS - Dell Precision 7720 default" {
search --no-floppy --file --set=esp /EFI/BOOT/FORGEOS.EFI
linux ($esp)/EFI/BOOT/FORGEOS.EFI $linux_common i915.enable_psr=0 i915.fastboot=0
@@ -226,7 +381,7 @@ menuentry "ForgeOS - firmware framebuffer safe mode" {
}
EOF
sed -i "s|__LINUX_COMMON__|root=PARTLABEL=root rootfstype=ext4 rootwait $root_mode console=tty0 loglevel=7 ignore_loglevel earlyprintk=efi,keep vt.global_cursor_default=1|" "$GRUB_CFG"
write_grub_rollback_menu
if [[ "$DESKTOP" != "gnome" ]] && ! truthy "$ENABLE_DESKTOP"; then
cat >> "$GRUB_CFG" <<'EOF'
@@ -302,6 +457,9 @@ EOF
prepare_efi_loader() {
case "$BOOTLOADER" in
stub)
if ! grep -qx 'CONFIG_CMDLINE_BOOL=y' "$OUT_DIR/kernel.config" 2>/dev/null; then
die "BOOTLOADER=stub requires a kernel with CONFIG_CMDLINE_BOOL=y or firmware-provided EFI load options; use BOOTLOADER=grub for the default rollback-capable image"
fi
"$ROOT_DIR/scripts/sign-efi-loader.sh" "$OUT_DIR/bzImage" "$EFI_LOADER"
install -m 0644 "$OUT_DIR/bzImage" "$EFI_KERNEL"
;;
@@ -325,6 +483,8 @@ prepare_efi_loader() {
esac
}
configure_root_slots
msg "preparing EFI loader"
prepare_efi_loader
@@ -338,23 +498,32 @@ if atomic_base_enabled; then
BASE_SIZE_MIB=${BASE_SIZE_MIB:-$((BASE_USED_MIB + BASE_HEADROOM_MIB))}
BASE_MIN_MIB=$((BASE_USED_MIB + 64))
APP_MIN_MIB=$((APP_USED_MIB + APP_HEADROOM_MIB))
BASE_END_MIB=$((ESP_END_MIB + BASE_SIZE_MIB))
BASE_END_MIB=$((ESP_END_MIB + (BASE_SIZE_MIB * ROOT_SLOT_COUNT)))
APP_SIZE_MIB=$((IMAGE_SIZE_MIB - BASE_END_MIB))
(( BASE_SIZE_MIB >= BASE_MIN_MIB )) || die "BASE_SIZE_MIB=$BASE_SIZE_MIB is too small; need at least ${BASE_MIN_MIB}MiB"
(( APP_SIZE_MIB >= APP_MIN_MIB )) || die "IMAGE_SIZE_MIB=$IMAGE_SIZE_MIB leaves ${APP_SIZE_MIB}MiB for apps; need at least ${APP_MIN_MIB}MiB"
msg "atomic layout: ${BASE_SIZE_MIB}MiB base root, ${APP_SIZE_MIB}MiB writable app/state layer"
msg "atomic layout: ${ROOT_SLOT_COUNT} base slot(s) at ${BASE_SIZE_MIB}MiB each, ${APP_SIZE_MIB}MiB writable app/state layer"
fi
msg "creating raw image $IMAGE_PATH"
rm -f "$IMAGE_PATH" "$ESP_IMAGE" "$ROOT_IMAGE" "$APP_IMAGE"
rm -f "$IMAGE_PATH" "$ESP_IMAGE" "$APP_IMAGE"
rm -f "$OUT_DIR"/forgeos-root-[0-9]*.img
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
rm -f "$(root_slot_image "$slot")"
done
qemu-img create -f raw "$IMAGE_PATH" "${IMAGE_SIZE_MIB}M" >/dev/null
parted -s "$IMAGE_PATH" mklabel gpt
parted -s "$IMAGE_PATH" mkpart ESP fat32 1MiB "${ESP_END_MIB}MiB"
parted -s "$IMAGE_PATH" set 1 esp on
if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then
parted -s "$IMAGE_PATH" mkpart root ext4 "${ESP_END_MIB}MiB" "${BASE_END_MIB}MiB"
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
ROOT_START_MIB=$((ESP_END_MIB + (slot * BASE_SIZE_MIB)))
ROOT_END_MIB=$((ROOT_START_MIB + BASE_SIZE_MIB))
parted -s "$IMAGE_PATH" mkpart "$(root_slot_partlabel "$slot")" ext4 "${ROOT_START_MIB}MiB" "${ROOT_END_MIB}MiB"
done
APP_PART_NUM=$((ROOT_PART_START + ROOT_SLOT_COUNT))
parted -s "$IMAGE_PATH" mkpart apps ext4 "${BASE_END_MIB}MiB" 100%
else
parted -s "$IMAGE_PATH" mkpart root ext4 "${ESP_END_MIB}MiB" 100%
@@ -362,17 +531,27 @@ fi
ESP_OFFSET=$(partition_field 1 2)
ESP_SIZE=$(partition_field 1 4)
ROOT_OFFSET=$(partition_field 2 2)
ROOT_SIZE=$(partition_field 2 4)
ROOT_SIZE=
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
ROOT_PART_NUM=$((ROOT_PART_START + slot))
ROOT_OFFSETS[$slot]=$(partition_field "$ROOT_PART_NUM" 2)
ROOT_SIZES[$slot]=$(partition_field "$ROOT_PART_NUM" 4)
[[ -n "${ROOT_OFFSETS[$slot]}" && -n "${ROOT_SIZES[$slot]}" ]] || die "failed to parse root slot $slot partition layout"
if [[ -z "$ROOT_SIZE" ]]; then
ROOT_SIZE=${ROOT_SIZES[$slot]}
elif [[ "$ROOT_SIZE" != "${ROOT_SIZES[$slot]}" ]]; then
die "root slot $slot size differs from the primary root slot"
fi
done
if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then
APP_OFFSET=$(partition_field 3 2)
APP_SIZE=$(partition_field 3 4)
APP_OFFSET=$(partition_field "$APP_PART_NUM" 2)
APP_SIZE=$(partition_field "$APP_PART_NUM" 4)
else
APP_OFFSET=
APP_SIZE=
fi
[[ -n "$ESP_OFFSET" && -n "$ESP_SIZE" && -n "$ROOT_OFFSET" && -n "$ROOT_SIZE" ]] || die "failed to parse partition layout"
[[ -n "$ESP_OFFSET" && -n "$ESP_SIZE" && -n "$ROOT_SIZE" ]] || die "failed to parse partition layout"
if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then
[[ -n "$APP_OFFSET" && -n "$APP_SIZE" ]] || die "failed to parse app partition layout"
fi
@@ -388,8 +567,10 @@ if [[ "$DESKTOP" != "gnome" ]] && ! truthy "$ENABLE_DESKTOP"; then
mcopy -i "$ESP_IMAGE" "$EFI_INITRD" ::/EFI/BOOT/rootfs.cpio.gz
fi
msg "building ext4 root partition image"
build_ext4_image "$ROOT_LABEL" "$IMAGE_ROOTFS_DIR" "$ROOT_IMAGE" "$ROOT_SIZE"
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
msg "building ext4 $(root_slot_title "$slot") partition image"
build_ext4_image "$(root_slot_fs_label "$slot")" "$IMAGE_ROOTFS_DIR" "$(root_slot_image "$slot")" "${ROOT_SIZES[$slot]}"
done
if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then
msg "building ext4 app/state partition image"
@@ -398,7 +579,9 @@ fi
msg "embedding partitions into raw disk image"
dd if="$ESP_IMAGE" of="$IMAGE_PATH" bs=4M seek="$ESP_OFFSET" oflag=seek_bytes conv=notrunc status=none
dd if="$ROOT_IMAGE" of="$IMAGE_PATH" bs=4M seek="$ROOT_OFFSET" oflag=seek_bytes conv=notrunc status=none
for ((slot=0; slot<ROOT_SLOT_COUNT; slot++)); do
dd if="$(root_slot_image "$slot")" of="$IMAGE_PATH" bs=4M seek="${ROOT_OFFSETS[$slot]}" oflag=seek_bytes conv=notrunc status=none
done
if [[ "$APP_IMAGE_NEEDED" -eq 1 ]]; then
dd if="$APP_IMAGE" of="$IMAGE_PATH" bs=4M seek="$APP_OFFSET" oflag=seek_bytes conv=notrunc status=none
fi
-4
View File
@@ -15,10 +15,6 @@ mkdir -p "$KERNEL_BUILD_DIR" "$MODULES_STAGING_DIR"
make -C "$KERNEL_SRC_DIR" O="$KERNEL_BUILD_DIR" ARCH="$ARCH" x86_64_defconfig
apply_kconfig_fragment "$KERNEL_BUILD_DIR/.config" "$ROOT_DIR/config/kernel.fragment"
cmdline_fragment=$(mktemp)
printf 'CONFIG_CMDLINE="root=PARTLABEL=root rootfstype=ext4 rootwait ro console=tty0 console=ttyS0,115200n8 loglevel=7 ignore_loglevel i915.enable_psr=0 i915.fastboot=0"\n' > "$cmdline_fragment"
apply_kconfig_fragment "$KERNEL_BUILD_DIR/.config" "$cmdline_fragment"
rm -f "$cmdline_fragment"
make -C "$KERNEL_SRC_DIR" O="$KERNEL_BUILD_DIR" ARCH="$ARCH" olddefconfig
msg "building Linux ${KERNEL_VERSION}"
+1
View File
@@ -168,6 +168,7 @@ chmod 755 \
"$ROOTFS_STAGING_DIR/sbin/forgeos-switch-root" \
"$ROOTFS_STAGING_DIR/usr/lib/forgeos/nix-bootstrap" \
"$ROOTFS_STAGING_DIR/usr/bin/forgeos-app" \
"$ROOTFS_STAGING_DIR/usr/sbin/forgeos-rollback" \
"$ROOTFS_STAGING_DIR/usr/bin/neofetch" \
"$ROOTFS_STAGING_DIR/usr/share/udhcpc/default.script"
chmod 700 "$ROOTFS_STAGING_DIR/root" "$ROOTFS_STAGING_DIR/home/forge"
+1
View File
@@ -24,6 +24,7 @@ ROOT_LABEL=${ROOT_LABEL:-FORGE_ROOT}
EFI_LABEL=${EFI_LABEL:-FORGE_EFI}
APP_LABEL=${APP_LABEL:-FORGE_APPS}
ATOMIC_BASE=${ATOMIC_BASE:-1}
ROLLBACK_SLOTS=${ROLLBACK_SLOTS:-2}
case "$DESKTOP" in
console|gnome) ;;
*)
+56 -18
View File
@@ -231,14 +231,57 @@ partition_fstype() {
lsblk -dnro FSTYPE "$1" 2>/dev/null | head -n1
}
writable_partition_number() {
local apps_part
partition_partlabel() {
lsblk -dnro PARTLABEL "$1" 2>/dev/null | head -n1
}
apps_part=$(partition_path "$DEVICE" 3)
if [[ -b "$apps_part" ]]; then
printf '3\n'
partition_label() {
lsblk -dnro LABEL "$1" 2>/dev/null | head -n1
}
partition_number() {
lsblk -dnro PARTN "$1" 2>/dev/null | head -n1
}
device_partitions() {
lsblk -lnpo NAME "$DEVICE" | awk 'NR > 1 { print $1 }'
}
writable_partition_path() {
local part
local partlabel
local fs_label
local last_ext4=
while IFS= read -r part; do
[[ -b "$part" ]] || continue
partlabel=$(partition_partlabel "$part")
fs_label=$(partition_label "$part")
if [[ "$partlabel" == "apps" || "$fs_label" == "$APP_LABEL" ]]; then
printf '%s\n' "$part"
return 0
fi
if [[ "$(partition_fstype "$part")" == "ext4" ]]; then
last_ext4=$part
fi
done < <(device_partitions)
[[ -n "$last_ext4" ]] || return 1
printf '%s\n' "$last_ext4"
}
writable_partition_name() {
local part=$1
local partlabel
local fs_label
partlabel=$(partition_partlabel "$part")
fs_label=$(partition_label "$part")
if [[ "$partlabel" == "apps" || "$fs_label" == "$APP_LABEL" ]]; then
printf 'app/state\n'
else
printf '2\n'
printf 'root\n'
fi
}
@@ -251,13 +294,10 @@ expand_writable_partition() {
image_size=$(stat -c %s "$IMAGE_PATH")
device_size=$(device_size_bytes "$DEVICE")
part_num=$(writable_partition_number)
target_part=$(partition_path "$DEVICE" "$part_num")
if [[ "$part_num" == "3" ]]; then
target_name="app/state"
else
target_name="root"
fi
target_part=$(writable_partition_path) || die "could not locate writable ForgeOS partition on $DEVICE"
part_num=$(partition_number "$target_part")
[[ -n "$part_num" ]] || die "could not determine partition number for $target_part"
target_name=$(writable_partition_name "$target_part")
wait_for_block "$target_part"
@@ -280,21 +320,19 @@ expand_writable_partition() {
}
randomize_ext4_uuids() {
local part_num
local part
local fs_type
[[ "$RANDOMIZE_FS_UUIDS" -eq 1 ]] || return 0
for part_num in 2 3; do
part=$(partition_path "$DEVICE" "$part_num")
while IFS= read -r part; do
[[ -b "$part" ]] || continue
fs_type=$(partition_fstype "$part")
[[ "$fs_type" == "ext4" ]] || continue
msg "randomizing ext4 filesystem UUID on $part"
tune2fs -U random "$part" >/dev/null
done
done < <(device_partitions)
}
print_summary() {
@@ -352,7 +390,7 @@ fi
[[ -n "$DEVICE" ]] || die "missing required --device argument"
require_cmd lsblk findmnt parted partprobe dd blockdev e2fsck resize2fs tune2fs udevadm flock
require_cmd lsblk findmnt parted partprobe dd blockdev e2fsck resize2fs tune2fs udevadm flock awk
require_root
ensure_dirs