Skip to content

Commit cd8fa59

Browse files
cgwaltersjeckersb
authored andcommitted
generator: Conditinally enable bootc-status units
Right now this service fails in `bcvk run-ephemeral`, but also likely fails in any non-bootc system that has `subscription-manager` installed. A problem is that dependencies of units are started even if the dependee has a condition that disables it. This basically the target and path depend on `/run/ostree-booted` being present (which yes, won't work for composefs...) Tests: Covered by extant `012-test-unit-status.nu` Signed-off-by: Colin Walters <[email protected]>
1 parent 87d1d0a commit cd8fa59

File tree

7 files changed

+109
-25
lines changed

7 files changed

+109
-25
lines changed

Makefile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ install:
4848
install -D -m 0644 -t $(DESTDIR)$(prefix)/share/man/man5 target/man/*.5; \
4949
install -D -m 0644 -t $(DESTDIR)$(prefix)/share/man/man8 target/man/*.8; \
5050
install -D -m 0644 -t $(DESTDIR)/$(prefix)/lib/systemd/system systemd/*.service systemd/*.timer systemd/*.path systemd/*.target
51-
install -d -m 0755 $(DESTDIR)/$(prefix)/lib/systemd/system/multi-user.target.wants
52-
ln -s ../bootc-status-updated.path $(DESTDIR)/$(prefix)/lib/systemd/system/multi-user.target.wants/bootc-status-updated.path
53-
ln -s ../bootc-status-updated-onboot.target $(DESTDIR)/$(prefix)/lib/systemd/system/multi-user.target.wants/bootc-status-updated-onboot.target
5451
install -D -m 0644 -t $(DESTDIR)/$(prefix)/share/doc/bootc/baseimage/base/usr/lib/ostree/ baseimage/base/usr/lib/ostree/prepare-root.conf
5552
install -d -m 755 $(DESTDIR)/$(prefix)/share/doc/bootc/baseimage/base/sysroot
5653
cp -PfT baseimage/base/ostree $(DESTDIR)/$(prefix)/share/doc/bootc/baseimage/base/ostree
@@ -60,13 +57,6 @@ install:
6057
# Copy dracut and systemd config files
6158
cp -Prf baseimage/dracut $(DESTDIR)$(prefix)/share/doc/bootc/baseimage/dracut
6259
cp -Prf baseimage/systemd $(DESTDIR)$(prefix)/share/doc/bootc/baseimage/systemd
63-
# Install fedora-bootc-destructive-cleanup in fedora derivatives
64-
ID=$$(. /usr/lib/os-release && echo $$ID); \
65-
ID_LIKE=$$(. /usr/lib/os-release && echo $$ID_LIKE); \
66-
if [ "$$ID" = "fedora" ] || [[ "$$ID_LIKE" == *"fedora"* ]]; then \
67-
ln -s ../bootc-destructive-cleanup.service $(DESTDIR)/$(prefix)/lib/systemd/system/multi-user.target.wants/bootc-destructive-cleanup.service; \
68-
install -D -m 0755 -t $(DESTDIR)/$(prefix)/lib/bootc contrib/scripts/fedora-bootc-destructive-cleanup; \
69-
fi
7060

7161
# Run this to also take over the functionality of `ostree container` for example.
7262
# Only needed for OS/distros that have callers invoking `ostree container` and not bootc.

crates/lib/src/generator.rs

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
use std::io::BufRead;
22

33
use anyhow::{Context, Result};
4+
use camino::Utf8PathBuf;
45
use cap_std::fs::Dir;
56
use cap_std_ext::{cap_std, dirext::CapStdExtDirExt};
67
use fn_error_context::context;
7-
use ostree_ext::container_utils::is_ostree_booted_in;
8+
use ostree_ext::container_utils::{is_ostree_booted_in, OSTREE_BOOTED};
89
use rustix::{fd::AsFd, fs::StatVfsMountFlags};
910

11+
use crate::install::DESTRUCTIVE_CLEANUP;
12+
13+
const STATUS_ONBOOT_UNIT: &str = "bootc-status-updated-onboot.target";
14+
const STATUS_PATH_UNIT: &str = "bootc-status-updated.path";
15+
const CLEANUP_UNIT: &str = "bootc-destructive-cleanup.service";
16+
const MULTI_USER_TARGET: &str = "multi-user.target";
1017
const EDIT_UNIT: &str = "bootc-fstab-edit.service";
1118
const FSTAB_ANACONDA_STAMP: &str = "Created by anaconda";
1219
pub(crate) const BOOTC_EDITED_STAMP: &str = "Updated by bootc-fstab-edit.service";
@@ -47,9 +54,50 @@ pub(crate) fn fstab_generator_impl(root: &Dir, unit_dir: &Dir) -> Result<bool> {
4754
Ok(false)
4855
}
4956

57+
pub(crate) fn enable_unit(unitdir: &Dir, name: &str, target: &str) -> Result<()> {
58+
let wants = Utf8PathBuf::from(format!("{target}.wants"));
59+
unitdir
60+
.create_dir_all(&wants)
61+
.with_context(|| format!("Creating {wants}"))?;
62+
let source = format!("/usr/lib/systemd/system/{name}");
63+
let target = wants.join(name);
64+
unitdir.remove_file_optional(&target)?;
65+
unitdir
66+
.symlink_contents(&source, &target)
67+
.with_context(|| format!("Writing {name}"))?;
68+
Ok(())
69+
}
70+
71+
/// Enable our units
72+
pub(crate) fn unit_enablement_impl(sysroot: &Dir, unit_dir: &Dir) -> Result<()> {
73+
for unit in [STATUS_ONBOOT_UNIT, STATUS_PATH_UNIT] {
74+
enable_unit(unit_dir, unit, MULTI_USER_TARGET)?;
75+
}
76+
77+
if sysroot.try_exists(DESTRUCTIVE_CLEANUP)? {
78+
tracing::debug!("Found {DESTRUCTIVE_CLEANUP}");
79+
enable_unit(unit_dir, CLEANUP_UNIT, MULTI_USER_TARGET)?;
80+
} else {
81+
tracing::debug!("Didn't find {DESTRUCTIVE_CLEANUP}");
82+
}
83+
84+
Ok(())
85+
}
86+
5087
/// Main entrypoint for the generator
5188
pub(crate) fn generator(root: &Dir, unit_dir: &Dir) -> Result<()> {
52-
// Right now we only do something if the root is a read-only overlayfs (a composefs really)
89+
// Only run on ostree systems
90+
if !root.try_exists(OSTREE_BOOTED)? {
91+
return Ok(());
92+
}
93+
94+
let Some(ref sysroot) = root.open_dir_optional("sysroot")? else {
95+
return Ok(());
96+
};
97+
98+
unit_enablement_impl(sysroot, unit_dir)?;
99+
100+
// Also only run if the root is a read-only overlayfs (a composefs really)
53101
let st = rustix::fs::fstatfs(root.as_fd())?;
54102
if st.f_type != libc::OVERLAYFS_SUPER_MAGIC {
55103
tracing::trace!("Root is not overlayfs");
@@ -62,6 +110,7 @@ pub(crate) fn generator(root: &Dir, unit_dir: &Dir) -> Result<()> {
62110
}
63111
let updated = fstab_generator_impl(root, unit_dir)?;
64112
tracing::trace!("Generated fstab: {updated}");
113+
65114
Ok(())
66115
}
67116

@@ -89,12 +138,16 @@ ExecStart=bootc internals fixup-etc-fstab\n\
89138

90139
#[cfg(test)]
91140
mod tests {
141+
use camino::Utf8Path;
142+
use cap_std_ext::cmdext::CapStdExtCommandExt as _;
143+
92144
use super::*;
93145

94146
fn fixture() -> Result<cap_std_ext::cap_tempfile::TempDir> {
95147
let tempdir = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
96148
tempdir.create_dir("etc")?;
97149
tempdir.create_dir("run")?;
150+
tempdir.create_dir("sysroot")?;
98151
tempdir.create_dir_all("run/systemd/system")?;
99152
Ok(tempdir)
100153
}
@@ -109,6 +162,53 @@ mod tests {
109162
Ok(())
110163
}
111164

165+
#[test]
166+
fn test_units() -> Result<()> {
167+
let tempdir = &fixture()?;
168+
let sysroot = &tempdir.open_dir("sysroot").unwrap();
169+
let unit_dir = &tempdir.open_dir("run/systemd/system")?;
170+
171+
let verify = |wantsdir: &Dir, n: u32| -> Result<()> {
172+
assert_eq!(unit_dir.entries()?.count(), 1);
173+
let r = wantsdir.read_link_contents(STATUS_ONBOOT_UNIT)?;
174+
let r: Utf8PathBuf = r.try_into().unwrap();
175+
assert_eq!(r, format!("/usr/lib/systemd/system/{STATUS_ONBOOT_UNIT}"));
176+
assert_eq!(wantsdir.entries()?.count(), n as usize);
177+
anyhow::Ok(())
178+
};
179+
180+
// Explicitly run this twice to test idempotency
181+
182+
unit_enablement_impl(sysroot, &unit_dir).unwrap();
183+
unit_enablement_impl(sysroot, &unit_dir).unwrap();
184+
let wantsdir = &unit_dir.open_dir("multi-user.target.wants")?;
185+
verify(wantsdir, 2)?;
186+
assert!(wantsdir
187+
.symlink_metadata_optional(CLEANUP_UNIT)
188+
.unwrap()
189+
.is_none());
190+
191+
// Now create sysroot and rerun the generator
192+
unit_enablement_impl(sysroot, &unit_dir).unwrap();
193+
verify(wantsdir, 2)?;
194+
195+
// Create the destructive stamp
196+
sysroot
197+
.create_dir_all(Utf8Path::new(DESTRUCTIVE_CLEANUP).parent().unwrap())
198+
.unwrap();
199+
sysroot.atomic_write(DESTRUCTIVE_CLEANUP, b"").unwrap();
200+
unit_enablement_impl(sysroot, unit_dir).unwrap();
201+
verify(wantsdir, 3)?;
202+
203+
// And now the unit should be enabled
204+
assert!(wantsdir
205+
.symlink_metadata(CLEANUP_UNIT)
206+
.unwrap()
207+
.is_symlink());
208+
209+
Ok(())
210+
}
211+
112212
#[cfg(test)]
113213
mod test {
114214
use super::*;

crates/lib/src/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const RUN_BOOTC: &str = "/run/bootc";
7777
/// The default path for the host rootfs
7878
const ALONGSIDE_ROOT_MOUNT: &str = "/target";
7979
/// Global flag to signal the booted system was provisioned via an alongside bootc install
80-
const DESTRUCTIVE_CLEANUP: &str = "bootc-destructive-cleanup";
80+
pub(crate) const DESTRUCTIVE_CLEANUP: &str = "etc/bootc-destructive-cleanup";
8181
/// This is an ext4 special directory we need to ignore.
8282
const LOST_AND_FOUND: &str = "lost+found";
8383
/// The filename of the composefs EROFS superblock; TODO move this into ostree
@@ -1494,7 +1494,7 @@ async fn ostree_install(state: &State, rootfs: &RootSetup, cleanup: Cleanup) ->
14941494
if matches!(cleanup, Cleanup::TriggerOnNextBoot) {
14951495
let sysroot_dir = crate::utils::sysroot_dir(ostree)?;
14961496
tracing::debug!("Writing {DESTRUCTIVE_CLEANUP}");
1497-
sysroot_dir.atomic_write(format!("etc/{}", DESTRUCTIVE_CLEANUP), b"")?;
1497+
sysroot_dir.atomic_write(DESTRUCTIVE_CLEANUP, b"")?;
14981498
}
14991499

15001500
// We must drop the sysroot here in order to close any open file

crates/tests-integration/src/system_reinstall.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ pub(crate) fn run(image: &str, testargs: libtest_mimic::Arguments) -> Result<()>
9696
let files = [
9797
"usr/lib/bootc/fedora-bootc-destructive-cleanup",
9898
"usr/lib/systemd/system/bootc-destructive-cleanup.service",
99-
"usr/lib/systemd/system/multi-user.target.wants/bootc-destructive-cleanup.service",
10099
"etc/tmpfiles.d/bootc-root-ssh.conf",
101100
];
102101

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
[Unit]
22
Description=Cleanup previous the installation after an alongside installation
33
Documentation=man:bootc(8)
4-
ConditionPathExists=/sysroot/etc/bootc-destructive-cleanup
54

65
[Service]
76
Type=oneshot
87
ExecStart=/usr/lib/bootc/fedora-bootc-destructive-cleanup
98
PrivateMounts=true
109

11-
[Install]
12-
WantedBy=multi-user.target
10+
# No [Install] section, this is enabled via generator
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[Unit]
2-
Description=Target for bootc status changes on boot
2+
Description=Bootc status trigger state sync
33
Documentation=man:bootc-status-updated.target(8)
4-
ConditionPathExists=/run/ostree-booted
4+
After=sysinit.target
55

6-
[Install]
7-
WantedBy=multi-user.target
6+
# No [Install] section, this is enabled via generator

systemd/bootc-status-updated.path

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[Unit]
22
Description=Monitor bootc for status changes
33
Documentation=man:bootc-status-updated.path(8)
4-
ConditionPathExists=/run/ostree-booted
54

65
[Path]
76
PathChanged=/ostree/bootc
87
Unit=bootc-status-updated.target
98

10-
[Install]
11-
WantedBy=multi-user.target
9+
# No [Install] section, this is enabled via generator

0 commit comments

Comments
 (0)