Skip to content

Commit 885e55e

Browse files
committed
cli: add support for soft-reboots
This commit adds --queue-soft-reboot which uses the ostree api's to setup soft-reboots during switch, update and rollback operations. Co-authored-by: Colin Walters <[email protected]> Signed-off-by: Joseph Marrero Corchado <[email protected]> Signed-off-by: Colin Walters <[email protected]>
1 parent b89d977 commit 885e55e

File tree

8 files changed

+244
-6
lines changed

8 files changed

+244
-6
lines changed

crates/lib/src/cli.rs

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ pub(crate) struct UpgradeOpts {
7979
#[clap(long, conflicts_with = "check")]
8080
pub(crate) apply: bool,
8181

82+
/// Queue a soft reboot after staging the deployment.
83+
///
84+
/// This will prepare the system for a soft reboot instead of a full reboot,
85+
/// which allows userspace to restart without rebooting the kernel.
86+
#[clap(long, conflicts_with = "check")]
87+
pub(crate) queue_soft_reboot: bool,
88+
8289
#[clap(flatten)]
8390
pub(crate) progress: ProgressOptions,
8491
}
@@ -98,6 +105,13 @@ pub(crate) struct SwitchOpts {
98105
#[clap(long)]
99106
pub(crate) apply: bool,
100107

108+
/// Queue a soft reboot after staging the deployment.
109+
///
110+
/// This will prepare the system for a soft reboot instead of a full reboot,
111+
/// which allows userspace to restart without rebooting the kernel.
112+
#[clap(long)]
113+
pub(crate) queue_soft_reboot: bool,
114+
101115
/// The transport; e.g. oci, oci-archive, containers-storage. Defaults to `registry`.
102116
#[clap(long, default_value = "registry")]
103117
pub(crate) transport: String,
@@ -141,6 +155,13 @@ pub(crate) struct RollbackOpts {
141155
/// a userspace-only restart.
142156
#[clap(long)]
143157
pub(crate) apply: bool,
158+
159+
/// Queue a soft reboot after performing the rollback.
160+
///
161+
/// This will prepare the system for a soft reboot instead of a full reboot,
162+
/// which allows userspace to restart without rebooting the kernel.
163+
#[clap(long)]
164+
pub(crate) queue_soft_reboot: bool,
144165
}
145166

146167
/// Perform an edit operation
@@ -554,7 +575,7 @@ pub(crate) enum Opt {
554575
Note on Rollbacks and the `/etc` Directory:
555576
556577
When you perform a rollback (e.g., with `bootc rollback`), any
557-
changes made to files in the `/etc` directory wont carry over
578+
changes made to files in the `/etc` directory won't carry over
558579
to the rolled-back deployment. The `/etc` files will revert
559580
to their state from that previous deployment instead.
560581
@@ -733,6 +754,40 @@ pub(crate) fn require_root(is_container: bool) -> Result<()> {
733754
Ok(())
734755
}
735756

757+
/// Check if a deployment has soft reboot capability
758+
fn has_soft_reboot_capability(deployment: Option<&crate::spec::BootEntry>) -> bool {
759+
deployment.map(|d| d.soft_reboot_capable).unwrap_or(false)
760+
}
761+
762+
/// Prepare a soft reboot for the given deployment
763+
#[context("Preparing soft reboot")]
764+
fn prepare_soft_reboot(
765+
sysroot: &crate::store::Storage,
766+
deployment: &ostree::Deployment,
767+
) -> Result<()> {
768+
let cancellable = ostree::gio::Cancellable::NONE;
769+
sysroot
770+
.sysroot
771+
.deployment_set_soft_reboot(deployment, false, cancellable)
772+
.context("Failed to prepare soft-reboot")?;
773+
Ok(())
774+
}
775+
776+
/// Perform a soft reboot for a staged deployment
777+
#[context("Soft reboot staged deployment")]
778+
fn soft_reboot_staged(sysroot: &crate::store::Storage) -> Result<()> {
779+
println!("Staged deployment is soft-reboot capable, preparing for soft-reboot...");
780+
781+
let deployments_list = sysroot.deployments();
782+
let staged_deployment = deployments_list
783+
.iter()
784+
.find(|d| d.is_staged())
785+
.ok_or_else(|| anyhow::anyhow!("Failed to find staged deployment"))?;
786+
787+
prepare_soft_reboot(sysroot, staged_deployment)?;
788+
Ok(())
789+
}
790+
736791
/// A few process changes that need to be made for writing.
737792
/// IMPORTANT: This may end up re-executing the current process,
738793
/// so anything that happens before this should be idempotent.
@@ -851,7 +906,11 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
851906
.unwrap_or_default();
852907
if staged_unchanged {
853908
println!("Staged update present, not changed.");
854-
909+
if opts.queue_soft_reboot {
910+
if has_soft_reboot_capability(host.status.staged.as_ref()) {
911+
soft_reboot_staged(sysroot)?;
912+
}
913+
}
855914
if opts.apply {
856915
crate::reboot::reboot()?;
857916
}
@@ -873,6 +932,15 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
873932
if changed {
874933
sysroot.update_mtime()?;
875934

935+
if opts.queue_soft_reboot {
936+
// At this point we have new staged deployment and the host definition has changed.
937+
// We need the updated host status before we check if we can prepare the soft-reboot.
938+
let updated_host = crate::status::get_status(sysroot, Some(&booted_deployment))?.1;
939+
if has_soft_reboot_capability(updated_host.status.staged.as_ref()) {
940+
soft_reboot_staged(sysroot)?;
941+
}
942+
}
943+
876944
if opts.apply {
877945
crate::reboot::reboot()?;
878946
}
@@ -948,6 +1016,15 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
9481016

9491017
sysroot.update_mtime()?;
9501018

1019+
if opts.queue_soft_reboot {
1020+
// At this point we have staged the deployment and the host definition has changed.
1021+
// We need the updated host status before we check if we can prepare the soft-reboot.
1022+
let updated_host = crate::status::get_status(sysroot, Some(&booted_deployment))?.1;
1023+
if has_soft_reboot_capability(updated_host.status.staged.as_ref()) {
1024+
soft_reboot_staged(sysroot)?;
1025+
}
1026+
}
1027+
9511028
if opts.apply {
9521029
crate::reboot::reboot()?;
9531030
}
@@ -961,6 +1038,22 @@ async fn rollback(opts: RollbackOpts) -> Result<()> {
9611038
let sysroot = &get_storage().await?;
9621039
crate::deploy::rollback(sysroot).await?;
9631040

1041+
if opts.queue_soft_reboot {
1042+
// Get status of rollback deployment to check soft-reboot capability
1043+
let host = crate::status::get_status_require_booted(sysroot)?.2;
1044+
1045+
if has_soft_reboot_capability(host.status.rollback.as_ref()) {
1046+
println!("Rollback deployment is soft-reboot capable, preparing for soft-reboot...");
1047+
1048+
let deployments_list = sysroot.deployments();
1049+
let target_deployment = deployments_list
1050+
.first()
1051+
.ok_or_else(|| anyhow::anyhow!("No rollback deployment found!"))?;
1052+
1053+
prepare_soft_reboot(sysroot, target_deployment)?;
1054+
}
1055+
}
1056+
9641057
if opts.apply {
9651058
crate::reboot::reboot()?;
9661059
}

crates/lib/src/deploy.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,6 @@ async fn deploy(
577577
&opts,
578578
Some(cancellable),
579579
)?;
580-
tracing::debug!(
581-
"Soft reboot capable: {:?}",
582-
sysroot.deployment_can_soft_reboot(&d)
583-
);
584580
Ok(d.index())
585581
}),
586582
)

crates/lib/src/spec.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ pub struct BootEntry {
176176
pub incompatible: bool,
177177
/// Whether this entry will be subject to garbage collection
178178
pub pinned: bool,
179+
/// This is true if (relative to the booted system) this is a possible target for a soft reboot
180+
#[serde(default)]
181+
pub soft_reboot_capable: bool,
179182
/// The container storage backend
180183
#[serde(default)]
181184
pub store: Option<Store>,
@@ -517,6 +520,7 @@ mod tests {
517520
image: None,
518521
cached_update: None,
519522
incompatible: false,
523+
soft_reboot_capable: false,
520524
pinned: false,
521525
store: None,
522526
ostree: None,

crates/lib/src/status.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ impl From<ImageReference> for OstreeImageReference {
8686
}
8787
}
8888

89+
/// Check if a deployment has soft reboot capability
90+
fn has_soft_reboot_capability(sysroot: &Storage, deployment: &ostree::Deployment) -> bool {
91+
ostree_ext::systemd_has_soft_reboot() && sysroot.deployment_can_soft_reboot(deployment)
92+
}
93+
8994
/// Parse an ostree origin file (a keyfile) and extract the targeted
9095
/// container image reference.
9196
fn get_image_origin(origin: &glib::KeyFile) -> Result<Option<OstreeImageReference>> {
@@ -144,10 +149,13 @@ fn boot_entry_from_deployment(
144149
(None, CachedImageStatus::default(), false)
145150
};
146151

152+
let soft_reboot_capable = has_soft_reboot_capability(sysroot, deployment);
153+
147154
let r = BootEntry {
148155
image,
149156
cached_update,
150157
incompatible,
158+
soft_reboot_capable,
151159
store,
152160
pinned: deployment.is_pinned(),
153161
ostree: Some(crate::spec::BootEntryOstree {
@@ -381,6 +389,27 @@ fn render_verbose_ostree_info(
381389
Ok(())
382390
}
383391

392+
/// Helper function to render if soft-reboot capable
393+
fn write_soft_reboot(
394+
mut out: impl Write,
395+
entry: &crate::spec::BootEntry,
396+
prefix_len: usize,
397+
) -> Result<()> {
398+
// Show soft-reboot capability
399+
write_row_name(&mut out, "Soft-reboot", prefix_len)?;
400+
writeln!(
401+
out,
402+
"{}",
403+
if entry.soft_reboot_capable {
404+
"yes"
405+
} else {
406+
"no"
407+
}
408+
)?;
409+
410+
Ok(())
411+
}
412+
384413
/// Write the data for a container image based status.
385414
fn human_render_slot(
386415
mut out: impl Write,
@@ -463,6 +492,9 @@ fn human_render_slot(
463492
}
464493
}
465494
}
495+
496+
// Show soft-reboot capability
497+
write_soft_reboot(&mut out, entry, prefix_len)?;
466498
}
467499

468500
tracing::debug!("pinned={}", entry.pinned);
@@ -500,6 +532,9 @@ fn human_render_slot_ostree(
500532
if let Some(ostree) = &entry.ostree {
501533
render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
502534
}
535+
536+
// Show soft-reboot capability
537+
write_soft_reboot(&mut out, entry, prefix_len)?;
503538
}
504539

505540
tracing::debug!("pinned={}", entry.pinned);
@@ -721,5 +756,6 @@ mod tests {
721756
assert!(w.contains("Deploy serial:"));
722757
assert!(w.contains("Staged:"));
723758
assert!(w.contains("Commit:"));
759+
assert!(w.contains("Soft-reboot:"));
724760
}
725761
}

crates/ostree-ext/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,12 @@ pub mod prelude {
7878
pub mod fixture;
7979
#[cfg(feature = "internal-testing-api")]
8080
pub mod integrationtest;
81+
82+
/// Check if the system has the soft reboot target, which signals
83+
/// systemd support for soft reboots.
84+
pub fn systemd_has_soft_reboot() -> bool {
85+
const UNIT: &str = "/usr/lib/systemd/system/soft-reboot.target";
86+
use std::sync::OnceLock;
87+
static EXISTS: OnceLock<bool> = OnceLock::new();
88+
*EXISTS.get_or_init(|| std::path::Path::new(UNIT).exists())
89+
}

tmt/plans/integration.fmf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ execute:
6262
test:
6363
- /tmt/tests/bootc-install-provision
6464
- /tmt/tests/test-24-local-upgrade-reboot
65+
66+
/test-25-soft-reboot:
67+
summary: Soft reboot support
68+
discover:
69+
how: fmf
70+
test:
71+
- /tmt/tests/bootc-install-provision
72+
- /tmt/tests/test-25-soft-reboot

tmt/tests/booted/test-soft-reboot.nu

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Verify that soft reboot works (on by default)
2+
use std assert
3+
use tap.nu
4+
5+
let soft_reboot_capable = "/usr/lib/systemd/system/soft-reboot.target" | path exists
6+
if not $soft_reboot_capable {
7+
echo "Skipping, system is not soft reboot capable"
8+
return
9+
}
10+
11+
# This code runs on *each* boot.
12+
# Here we just capture information.
13+
bootc status
14+
let st = bootc status --json | from json
15+
let booted = $st.status.booted.image
16+
17+
# Run on the first boot
18+
def initial_build [] {
19+
tap begin "local image push + pull + upgrade"
20+
21+
let td = mktemp -d
22+
cd $td
23+
24+
bootc image copy-to-storage
25+
26+
# A simple derived container that adds a file, but also injects some kargs
27+
"FROM localhost/bootc
28+
RUN echo test content > /usr/share/testfile-for-soft-reboot.txt
29+
" | save Dockerfile
30+
# Build it
31+
podman build -t localhost/bootc-derived .
32+
33+
bootc switch --queue-soft-reboot --transport containers-storage localhost/bootc-derived
34+
let st = bootc status --json | from json
35+
assert $st.status.staged.softRebootCapable
36+
37+
# And reboot into it
38+
tmt-reboot
39+
}
40+
41+
# The second boot; verify we're in the derived image
42+
def second_boot [] {
43+
assert ("/usr/share/testfile-for-soft-reboot.txt" | path exists)
44+
45+
assert equal (systemctl show -P SoftRebootsCount) "1"
46+
}
47+
48+
# Run on the second boot
49+
def second_build [] {
50+
tap begin "local image push + pull + upgrade"
51+
52+
let td = mktemp -d
53+
cd $td
54+
55+
bootc image copy-to-storage
56+
57+
# A new derived with new kargs which should stop the soft reboot.
58+
"FROM localhost/bootc
59+
RUN echo test content > /usr/share/testfile-for-soft-reboot.txt
60+
RUN echo 'kargs = ["foo1=bar2"]' | tee /usr/lib/bootc/kargs.d/00-foo1bar2.toml > /dev/null
61+
" | save Dockerfile
62+
# Build it
63+
podman build -t localhost/bootc-derived .
64+
65+
bootc update --queue-soft-reboot --transport containers-storage
66+
let st = bootc status --json | from json
67+
assert (not $st.status.staged.softRebootCapable)
68+
69+
# And reboot into it
70+
tmt-reboot
71+
}
72+
73+
# The third boot; verify we're in the derived image
74+
def third_boot [] {
75+
assert ("/usr/lib/bootc/kargs.d/00-foo1bar2.toml" | path exists)
76+
77+
assert equal (systemctl show -P SoftRebootsCount) "0"
78+
}
79+
80+
def main [] {
81+
# See https://tmt.readthedocs.io/en/stable/stories/features.html#reboot-during-test
82+
match $env.TMT_REBOOT_COUNT? {
83+
null | "0" => initial_build,
84+
"1" => second_boot,
85+
"2" => second_build,
86+
"3" => third_boot,
87+
$o => { error make { msg: $"Invalid TMT_REBOOT_COUNT ($o)" } },
88+
}
89+
}

tmt/tests/test-25-soft-reboot.fmf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
summary: Execute soft reboot test
2+
test: nu booted/test-soft-reboot.nu
3+
duration: 30m

0 commit comments

Comments
 (0)