Skip to content

Commit f3db79e

Browse files
committed
finalize-staged: Ensure /boot automount doesn't expire
If `/boot` is an automount, then the unit will be stopped as soon as the automount expires. That's would defeat the purpose of using systemd to delay finalizing the deployment until shutdown. This is not uncommon as `systemd-gpt-auto-generator` will create an automount unit for `/boot` when it's the EFI System Partition and there's no fstab entry. To ensure that systemd doesn't stop the service early when the `/boot` automount expires, introduce a new unit that holds `/boot` open until it's sent `SIGTERM`. This uses a new `--hold` option for `finalize-staged` that loads but doesn't lock the sysroot. A separate unit is used since we want the process to remain active throughout the finalization run in `ExecStop`. That wouldn't work if it was specified in `ExecStart` in the same unit since it would be killed before the `ExecStop` action was run. Fixes: #2543
1 parent e30a3b6 commit f3db79e

6 files changed

Lines changed: 193 additions & 7 deletions

File tree

‎Makefile-boot.am‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ systemdsystemunit_DATA = src/boot/ostree-prepare-root.service \
4141
src/boot/ostree-boot-complete.service \
4242
src/boot/ostree-finalize-staged.service \
4343
src/boot/ostree-finalize-staged.path \
44+
src/boot/ostree-finalize-staged-hold.service \
4445
$(NULL)
4546
systemdtmpfilesdir = $(prefix)/lib/tmpfiles.d
4647
dist_systemdtmpfiles_DATA = src/boot/ostree-tmpfiles.conf
@@ -70,6 +71,7 @@ EXTRA_DIST += src/boot/dracut/module-setup.sh \
7071
src/boot/ostree-finalize-staged.path \
7172
src/boot/ostree-remount.service \
7273
src/boot/ostree-finalize-staged.service \
74+
src/boot/ostree-finalize-staged-hold.service \
7375
src/boot/grub2/grub2-15_ostree \
7476
src/boot/grub2/ostree-grub-generator \
7577
$(NULL)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (C) 2018 Red Hat, Inc.
2+
# Copyright (C) 2022 Endless OS Foundation LLC
3+
#
4+
# This library is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU Lesser General Public
6+
# License as published by the Free Software Foundation; either
7+
# version 2 of the License, or (at your option) any later version.
8+
#
9+
# This library is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# Lesser General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU Lesser General Public
15+
# License along with this library. If not, see <https://www.gnu.org/licenses/>.
16+
17+
# See https://github.com/ostreedev/ostree/pull/2543 for background.
18+
[Unit]
19+
Description=Hold /boot Open for OSTree Finalize Staged Deployment
20+
Documentation=man:ostree(1)
21+
ConditionPathExists=/run/ostree-booted
22+
DefaultDependencies=no
23+
24+
RequiresMountsFor=/sysroot /boot
25+
After=local-fs.target
26+
Before=basic.target final.target
27+
28+
[Service]
29+
Type=exec
30+
31+
# This is explicitly run in the root namespace to ensure an automounted
32+
# /boot doesn't time out since autofs doesn't handle mount namespaces.
33+
#
34+
# https://bugzilla.redhat.com/show_bug.cgi?id=2056090
35+
ExecStart=+/usr/bin/ostree admin finalize-staged --hold

‎src/boot/ostree-finalize-staged.service‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Before=basic.target final.target
2929
After=systemd-journal-flush.service
3030
Conflicts=final.target
3131

32+
# Start the hold unit and ensure it stays active throughout this
33+
# service.
34+
Wants=ostree-finalize-staged-hold.service
35+
After=ostree-finalize-staged-hold.service
36+
3237
[Service]
3338
Type=oneshot
3439
RemainAfterExit=yes

‎src/ostree/ot-admin-builtin-finalize-staged.c‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (C) 2018 Red Hat, Inc.
3+
* Copyright (C) 2022 Endless OS Foundation LLC
34
*
45
* SPDX-License-Identifier: LGPL-2.0+
56
*
@@ -19,8 +20,9 @@
1920

2021
#include "config.h"
2122

22-
#include "config.h"
23-
23+
#include <glib-unix.h>
24+
#include <sched.h>
25+
#include <signal.h>
2426
#include <stdlib.h>
2527

2628
#include "ot-main.h"
@@ -32,10 +34,23 @@
3234
#include "ostree-cmd-private.h"
3335
#include "ostree.h"
3436

37+
static gboolean opt_hold;
38+
3539
static GOptionEntry options[] = {
40+
{ "hold", 0, 0, G_OPTION_ARG_NONE, &opt_hold, "Hold /boot open during finalization", NULL },
3641
{ NULL }
3742
};
3843

44+
static gboolean
45+
sigterm_cb (gpointer user_data)
46+
{
47+
gboolean *running = user_data;
48+
g_print ("Received SIGTERM, exiting\n");
49+
*running = FALSE;
50+
g_main_context_wakeup (NULL);
51+
return G_SOURCE_REMOVE;
52+
}
53+
3954
/* Called by ostree-finalize-staged.service, and in turn
4055
* invokes a cmdprivate function inside the shared library.
4156
*/
@@ -50,13 +65,56 @@ ot_admin_builtin_finalize_staged (int argc, char **argv, OstreeCommandInvocation
5065

5166
g_autoptr(GOptionContext) context = g_option_context_new ("");
5267
g_autoptr(OstreeSysroot) sysroot = NULL;
68+
69+
/* First parse the args without loading the sysroot to see what options are
70+
* set. */
5371
if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
54-
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
72+
OSTREE_ADMIN_BUILTIN_FLAG_NO_LOAD,
5573
invocation, &sysroot, cancellable, error))
5674
return FALSE;
5775

58-
if (!ostree_cmd__private__()->ostree_finalize_staged (sysroot, cancellable, error))
59-
return FALSE;
76+
if (opt_hold)
77+
{
78+
/* Load the sysroot unlocked so that a separate namespace isn't
79+
* created. */
80+
if (!ostree_admin_sysroot_load (sysroot,
81+
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER | OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED,
82+
cancellable, error))
83+
return FALSE;
84+
85+
/* In case it's an automount, open /boot so that the automount doesn't
86+
* expire until before this process exits. If it did expire and got
87+
* unmounted, the service would be stopped and the deployment would be
88+
* finalized earlier than expected.
89+
*/
90+
int sysroot_fd = ostree_sysroot_get_fd (sysroot);
91+
glnx_autofd int boot_fd = -1;
92+
g_debug ("Opening /boot directory");
93+
if (!glnx_opendirat (sysroot_fd, "boot", TRUE, &boot_fd, error))
94+
return FALSE;
95+
96+
/* We want to keep /boot open until the deployment is finalized during
97+
* system shutdown, so block on SIGTERM under the assumption that it will
98+
* be received when systemd stops the unit.
99+
*/
100+
gboolean running = TRUE;
101+
g_unix_signal_add (SIGTERM, sigterm_cb, &running);
102+
g_print ("Waiting for SIGTERM\n");
103+
while (running)
104+
g_main_context_iteration (NULL, TRUE);
105+
}
106+
else
107+
{
108+
/* Load the sysroot with the normal flags and actually finalize the
109+
* deployment. */
110+
if (!ostree_admin_sysroot_load (sysroot,
111+
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
112+
cancellable, error))
113+
return FALSE;
114+
115+
if (!ostree_cmd__private__()->ostree_finalize_staged (sysroot, cancellable, error))
116+
return FALSE;
117+
}
60118

61119
return TRUE;
62120
}

‎tests/inst/src/destructive.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ fn impl_transaction_test<M: AsRef<str>>(
463463
"
464464
systemctl stop rpm-ostreed
465465
systemctl stop ostree-finalize-staged
466+
systemctl stop ostree-finalize-staged-hold
466467
ostree reset testrepo:${testref} ${booted_commit}
467468
rpm-ostree cleanup -pbrm
468469
",
@@ -504,7 +505,8 @@ fn impl_transaction_test<M: AsRef<str>>(
504505
InterruptStrategy::Force(ForceInterruptStrategy::Kill9) => {
505506
bash!(
506507
"systemctl kill -s KILL rpm-ostreed || true
507-
systemctl kill -s KILL ostree-finalize-staged || true"
508+
systemctl kill -s KILL ostree-finalize-staged || true
509+
systemctl kill -s KILL ostree-finalize-staged-hold || true"
508510
)?;
509511
live_strategy = Some(strategy);
510512
}
@@ -528,7 +530,8 @@ fn impl_transaction_test<M: AsRef<str>>(
528530
InterruptStrategy::Polite(PoliteInterruptStrategy::Stop) => {
529531
bash!(
530532
"systemctl stop rpm-ostreed || true
531-
systemctl stop ostree-finalize-staged || true"
533+
systemctl stop ostree-finalize-staged || true
534+
systemctl stop ostree-finalize-staged-hold || true"
532535
)?;
533536
live_strategy = Some(strategy);
534537
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
# https://github.com/ostreedev/ostree/issues/2543
3+
set -xeuo pipefail
4+
5+
. ${KOLA_EXT_DATA}/libinsttest.sh
6+
7+
case "${AUTOPKGTEST_REBOOT_MARK:-}" in
8+
"")
9+
# Ensure boot is mount point
10+
mountpoint /boot
11+
12+
# Create an automount unit with an extremely short timeout
13+
cat > /etc/systemd/system/boot.automount <<"EOF"
14+
[Automount]
15+
Where=/boot
16+
TimeoutIdleSec=1
17+
18+
[Install]
19+
WantedBy=local-fs.target
20+
EOF
21+
systemctl daemon-reload
22+
systemctl enable boot.automount
23+
24+
# Unmount /boot, start the automount unit, and ensure the units are
25+
# in the correct states.
26+
umount /boot
27+
systemctl start boot.automount
28+
boot_state=$(systemctl show -P ActiveState boot.mount)
29+
boot_auto_state=$(systemctl show -P ActiveState boot.automount)
30+
assert_streq "${boot_state}" inactive
31+
assert_streq "${boot_auto_state}" active
32+
33+
# Trigger a new staged deployment and check that the relevant units
34+
# are enabled.
35+
ostree admin deploy --stage --karg-append=somedummykarg=1 "${host_commit}"
36+
rpm-ostree status --json
37+
deployment_staged=$(rpmostree_query_json '.deployments[0].staged')
38+
assert_streq "${deployment_staged}" true
39+
test -f /run/ostree/staged-deployment
40+
finalize_staged_state=$(systemctl show -P ActiveState ostree-finalize-staged.service)
41+
finalize_staged_hold_state=$(systemctl show -P ActiveState ostree-finalize-staged-hold.service)
42+
assert_streq "${finalize_staged_state}" active
43+
assert_streq "${finalize_staged_hold_state}" active
44+
45+
# Sleep 1 second to ensure the boot automount idle timeout has
46+
# passed and then check that /boot is still mounted.
47+
sleep 1
48+
boot_state=$(systemctl show -P ActiveState boot.mount)
49+
assert_streq "${boot_state}" active
50+
51+
/tmp/autopkgtest-reboot "2"
52+
;;
53+
"2")
54+
rpm-ostree status --json
55+
deployment_staged=$(rpmostree_query_json '.deployments[0].staged')
56+
assert_streq "${deployment_staged}" false
57+
test ! -f /run/ostree/staged-deployment
58+
assert_file_has_content_literal /proc/cmdline somedummykarg=1
59+
60+
# Check that the finalize and hold services succeeded in the
61+
# previous boot. Dump them to the test log to help debugging.
62+
prepare_tmpdir
63+
journalctl -b -1 -o short-monotonic \
64+
-u ostree-finalize-staged.service \
65+
-u ostree-finalize-staged-hold.service \
66+
-u boot.mount \
67+
-u boot.automount \
68+
> logs.txt
69+
cat logs.txt
70+
assert_file_has_content logs.txt 'ostree-finalize-staged.service: \(Succeeded\|Deactivated successfully\)'
71+
assert_file_has_content logs.txt 'ostree-finalize-staged-hold.service: \(Succeeded\|Deactivated successfully\)'
72+
73+
# Check that the hold service remained active and kept /boot mounted until
74+
# the finalize service completed.
75+
finalize_stopped=$(journalctl -b -1 -o json -g Stopped -u ostree-finalize-staged.service | tail -n1 | jq -r .__MONOTONIC_TIMESTAMP)
76+
hold_stopping=$(journalctl -b -1 -o json -g Stopping -u ostree-finalize-staged-hold.service | tail -n1 | jq -r .__MONOTONIC_TIMESTAMP)
77+
hold_stopped=$(journalctl -b -1 -o json -g Stopped -u ostree-finalize-staged-hold.service | tail -n1 | jq -r .__MONOTONIC_TIMESTAMP)
78+
boot_unmounting=$(journalctl -b -1 -o json -g Unmounting -u boot.mount | tail -n1 | jq -r .__MONOTONIC_TIMESTAMP)
79+
test "${finalize_stopped}" -lt "${hold_stopping}"
80+
test "${hold_stopped}" -lt "${boot_unmounting}"
81+
;;
82+
*) fatal "Unexpected AUTOPKGTEST_REBOOT_MARK=${AUTOPKGTEST_REBOOT_MARK}" ;;
83+
esac

0 commit comments

Comments
 (0)