Skip to content

Commit c23039f

Browse files
committed
Use mount namespace to simplify code
With mount namespace we don't need to remount `/boot` read only before exit. Also do not try to parse /proc/self/mountinfo, just use '-w' test flag. If a command is called directly without systemd, reexec ourselves with `unshare -m`.
1 parent 6ccf8e9 commit c23039f

4 files changed

Lines changed: 27 additions & 76 deletions

File tree

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
#!/bin/bash
2-
set -eo pipefail
3-
4-
# Boolean variable to track if /boot was initially mounted as read-only
5-
# Ensure compatibility with rpm-ostree where /boot is rw but in bootc /boot is ro
6-
boot_was_ro=false
7-
8-
# Remount /boot as read-only if it was mounted as read-only ealier
9-
function remount_boot_ro {
10-
if $boot_was_ro; then
11-
mount -o remount,bind,ro /boot || exit 13
12-
fi
13-
return
14-
}
15-
16-
# Remount /boot as read-write if it was mounted as read-only
17-
function remount_boot_rw {
18-
if grep -q " /boot .* ro," /proc/mounts; then
19-
mount -o remount,rw /boot || exit 13
20-
boot_was_ro=true
21-
fi
22-
return
23-
}
1+
# skip if /boot is already writable
2+
if [ ! -w /boot ]; then
3+
if [[ "$(readlink /proc/1/ns/mnt)" == "$(readlink /proc/self/ns/mnt)" ]]; then
4+
# reexec ourselves to unshare the mount namespace
5+
exec unshare -m $0 $@
6+
else
7+
# we are in a separate mount namespace, we can remount
8+
# without impacting the rest of the system
9+
mount -o remount,rw /boot
10+
fi
11+
fi

usr/libexec/greenboot/greenboot-grub2-set-counter

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,8 @@ else
1717
max_boot_attempts=3 # default to 3 attempts
1818
fi
1919

20+
/usr/bin/grub2-editenv - set boot_counter="$max_boot_attempts"
2021

21-
remount_boot_rw
22-
23-
if ! /usr/bin/grub2-editenv - set boot_counter="$max_boot_attempts"; then
24-
# If the first command fails, remount /boot as read-only and exit with failure
25-
remount_boot_ro
26-
exit 1
27-
fi
28-
29-
if ! /usr/bin/grub2-editenv /boot/grub2/grubenv set boot_success=0; then
30-
# If the first command fails, remount /boot as read-only and exit with failure
31-
remount_boot_ro
32-
exit 1
33-
fi
34-
35-
# Revert /boot as read-only
36-
remount_boot_ro
22+
/usr/bin/grub2-editenv /boot/grub2/grubenv set boot_success=0
3723

3824
echo "<3>GRUB2 environment variables have been set for system upgrade. Max boot attempts is $max_boot_attempts"
Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
#!/bin/bash
2-
32
set -eo pipefail
43

54
source /usr/libexec/greenboot/greenboot-boot-remount
6-
remount_boot_rw
75

86
# Run the grub2-editenv commands
9-
if ! /usr/bin/grub2-editenv /boot/grub2/grubenv set boot_success=1; then
10-
# If the first command fails, remount /boot as read-only and exit with failure
11-
remount_boot_ro
12-
exit 1
13-
fi
14-
15-
if ! /usr/bin/grub2-editenv /boot/grub2/grubenv unset boot_counter; then
16-
# If the second command fails, remount /boot as read-only and exit with failure
17-
remount_boot_ro
18-
exit 1
19-
fi
20-
21-
# Remount /boot as read-only if it was mounted as read-write
22-
remount_boot_ro
7+
/usr/bin/grub2-editenv /boot/grub2/grubenv set boot_success=1
238

24-
# If everything succeeded, exit with success
25-
exit 0
9+
/usr/bin/grub2-editenv /boot/grub2/grubenv unset boot_counter
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
# Determine if the current boot is a fallback boot, if not exit early
5+
if ! (grub2-editenv list | grep -q "^boot_counter=-1$"); then
6+
exit 0
7+
fi
8+
49
source /usr/libexec/greenboot/greenboot-boot-remount
510

611
function attempt_rollback {
@@ -25,25 +30,13 @@ function attempt_rollback {
2530
return
2631
}
2732

28-
# Determine if the current boot is a fallback boot
2933
# If booted into fallback deployment, clean up bootloader entries (rollback)
30-
if grub2-editenv list | grep -q "^boot_counter=-1$"; then
31-
# Logs from previous boot may be unavailable on systems without internal RTC; defaulting to empty string
32-
prev_logs="$(journalctl -u greenboot-healthcheck.service -p 2 -b -1 -o cat)" || true
33-
attempt_rollback
34-
if [ -n "$prev_logs" ]; then
35-
echo "<3>Health check logs from previous boot:"
36-
echo "<3>$prev_logs"
37-
fi
38-
39-
remount_boot_rw
40-
41-
if ! /usr/bin/grub2-editenv - unset boot_counter; then
42-
# If the above command fails, remount /boot as read-only and exit with failure
43-
remount_boot_ro
44-
exit 1
45-
fi
34+
# Logs from previous boot may be unavailable on systems without internal RTC; defaulting to empty string
35+
prev_logs="$(journalctl -u greenboot-healthcheck.service -p 2 -b -1 -o cat)" || true
36+
attempt_rollback
37+
if [ -n "$prev_logs" ]; then
38+
echo "<3>Health check logs from previous boot:"
39+
echo "<3>$prev_logs"
4640
fi
4741

48-
# Remount /boot as read-only if it was mounted as read-write
49-
remount_boot_ro
42+
/usr/bin/grub2-editenv - unset boot_counter

0 commit comments

Comments
 (0)