Skip to content

Commit

Permalink
support for ro /boot
Browse files Browse the repository at this point in the history
Support is added for greenboot  to work with read-only /boot.
Its uses PrivateMounts=yes to isolate the system and remounts
boot as rw followed by setting the grub paremeters as per healthcheck.

Following services are affected:

* greenboot-grub2-set-success.service
* greenboot-grub2-set-counter.service
* greenboot-rpm-ostree-check-fallback.service

The grub2-editenv command is decoupled from greenboot-grub2-set-success.service
and is part of a seperate script.

Signed-off-by: saypaul <paul.sayan@gmail.com>
  • Loading branch information
say-paul committed Aug 8, 2024
1 parent d64f4e9 commit a5670bd
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 5 deletions.
1 change: 1 addition & 0 deletions greenboot.spec
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ install -DpZm 0644 etc/greenboot/greenboot.conf %{buildroot}%{_sysconfdir}/%{nam
%{_prefix}/lib/bootupd/grub2-static/configs.d/*.cfg
%{_unitdir}/greenboot-status.service
%{_libexecdir}/%{name}/greenboot-grub2-set-counter
%{_libexecdir}/%{name}/greenboot-grub2-set-success
%{_unitdir}/greenboot-grub2-set-success.service
%{_unitdir}/greenboot-grub2-set-counter.service
%{_libexecdir}/%{name}/greenboot-rpm-ostree-grub2-check-fallback
Expand Down
1 change: 1 addition & 0 deletions usr/lib/systemd/system/greenboot-grub2-set-counter.service
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RequiresMountsFor=/boot
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/libexec/greenboot/greenboot-grub2-set-counter
PrivateMounts=yes

[Install]
RequiredBy=ostree-finalize-staged.service
4 changes: 2 additions & 2 deletions usr/lib/systemd/system/greenboot-grub2-set-success.service
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ After=boot-complete.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/grub2-editenv - set boot_success=1
ExecStart=/usr/bin/grub2-editenv - unset boot_counter
ExecStart=/usr/libexec/greenboot/greenboot-grub2-set-success
PrivateMounts=yes

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Before=greenboot-grub2-set-success.service
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/libexec/greenboot/greenboot-rpm-ostree-grub2-check-fallback
PrivateMounts=yes

[Install]
RequiredBy=greenboot-healthcheck.service
28 changes: 26 additions & 2 deletions usr/libexec/greenboot/greenboot-grub2-set-counter
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ elif [ -n "$GREENBOOT_MAX_BOOT_ATTEMPTS" ]; then
else
max_boot_attempts=3 # default to 3 attempts
fi
grub2-editenv - set boot_counter="$max_boot_attempts"
grub2-editenv - set boot_success=0

# Remount /boot as read-only if it was mounted as read-write
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi

if ! /usr/bin/grub2-editenv - set boot_counter="$max_boot_attempts"; then
# If the first command fails, remount /boot as read-only and exit with failure
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi
exit 1
fi

if ! /usr/bin/grub2-editenv /boot/grub2/grubenv set boot_success=0; then
# If the first command fails, remount /boot as read-only and exit with failure
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi
exit 1
fi

# Remount /boot as read-only if it was mounted as read-write
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi

echo "<3>GRUB2 environment variables have been set for system upgrade. Max boot attempts is $max_boot_attempts"
33 changes: 33 additions & 0 deletions usr/libexec/greenboot/greenboot-grub2-set-success
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

set -eo pipefail

# Check if /boot is mounted as read-only, and remount as read-write if necessary
if grep -q " /boot .* ro," /proc/mounts; then
mount -o remount,rw /boot || exit 1
fi

# Run the grub2-editenv commands
if ! /usr/bin/grub2-editenv /boot/grub2/grubenv set boot_success=1; then
# If the first command fails, remount /boot as read-only and exit with failure
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi
exit 1
fi

if ! /usr/bin/grub2-editenv /boot/grubenv unset boot_counter; then
# If the second command fails, remount /boot as read-only and exit with failure
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi
exit 1
fi

# Remount /boot as read-only if it was mounted as read-write
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi

# If everything succeeded, exit with success
exit 0
20 changes: 19 additions & 1 deletion usr/libexec/greenboot/greenboot-rpm-ostree-grub2-check-fallback
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
set -euo pipefail

# Check if /boot is mounted as read-only, and remount as read-write if necessary
if grep -q " /boot .* ro," /proc/mounts; then
mount -o remount,rw /boot || exit 1
fi

# Determine if the current boot is a fallback boot
# If booted into fallback deployment, clean up bootloader entries (rollback)
if grub2-editenv list | grep -q "^boot_counter=-1$"; then
Expand All @@ -12,5 +17,18 @@ if grub2-editenv list | grep -q "^boot_counter=-1$"; then
echo "<3>Health check logs from previous boot:"
echo "<3>$prev_logs"
fi
grub2-editenv - unset boot_counter

if ! /usr/bin/grub2-editenv - unset boot_counter; then
# If the above command fails, remount /boot as read-only and exit with failure
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi
exit 1
fi

fi

# Remount /boot as read-only if it was mounted as read-write
if grep -q " /boot .* rw," /proc/mounts; then
mount -o remount,ro /boot || exit 1
fi

0 comments on commit a5670bd

Please sign in to comment.