Skip to content

Commit b4f7305

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 b4f7305

File tree

7 files changed

+228
-6
lines changed

7 files changed

+228
-6
lines changed

crates/lib/src/cli.rs

Lines changed: 89 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 can perform a soft reboot
758+
fn can_perform_soft_reboot(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, performing 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 can_perform_soft_reboot(host.status.staged.as_ref()) {
911+
soft_reboot_staged(sysroot)?;
912+
}
913+
}
855914
if opts.apply {
856915
crate::reboot::reboot()?;
857916
}
@@ -873,6 +932,12 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
873932
if changed {
874933
sysroot.update_mtime()?;
875934

935+
if opts.queue_soft_reboot {
936+
if can_perform_soft_reboot(host.status.staged.as_ref()) {
937+
soft_reboot_staged(sysroot)?;
938+
}
939+
}
940+
876941
if opts.apply {
877942
crate::reboot::reboot()?;
878943
}
@@ -948,6 +1013,12 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
9481013

9491014
sysroot.update_mtime()?;
9501015

1016+
if opts.queue_soft_reboot {
1017+
if can_perform_soft_reboot(host.status.staged.as_ref()) {
1018+
soft_reboot_staged(sysroot)?;
1019+
}
1020+
}
1021+
9511022
if opts.apply {
9521023
crate::reboot::reboot()?;
9531024
}
@@ -961,6 +1032,22 @@ async fn rollback(opts: RollbackOpts) -> Result<()> {
9611032
let sysroot = &get_storage().await?;
9621033
crate::deploy::rollback(sysroot).await?;
9631034

1035+
if opts.queue_soft_reboot {
1036+
// Get status before rollback to check soft-reboot capability
1037+
let host = crate::status::get_status_require_booted(sysroot)?.2;
1038+
1039+
if can_perform_soft_reboot(host.status.rollback.as_ref()) {
1040+
println!("Rollback deployment is soft-reboot capable, preparing for soft-reboot...");
1041+
1042+
let deployments_list = sysroot.deployments();
1043+
let target_deployment = deployments_list
1044+
.first()
1045+
.ok_or_else(|| anyhow::anyhow!("No deployments found after rollback"))?;
1046+
1047+
prepare_soft_reboot(sysroot, target_deployment)?;
1048+
}
1049+
}
1050+
9641051
if opts.apply {
9651052
crate::reboot::reboot()?;
9661053
}

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,14 @@ fn boot_entry_from_deployment(
144144
(None, CachedImageStatus::default(), false)
145145
};
146146

147+
let soft_reboot_capable =
148+
ostree_ext::systemd_has_soft_reboot() && sysroot.deployment_can_soft_reboot(deployment);
149+
147150
let r = BootEntry {
148151
image,
149152
cached_update,
150153
incompatible,
154+
soft_reboot_capable,
151155
store,
152156
pinned: deployment.is_pinned(),
153157
ostree: Some(crate::spec::BootEntryOstree {
@@ -463,6 +467,18 @@ fn human_render_slot(
463467
}
464468
}
465469
}
470+
471+
// Show soft-reboot capability
472+
write_row_name(&mut out, "Soft-reboot", prefix_len)?;
473+
writeln!(
474+
out,
475+
"{}",
476+
if entry.soft_reboot_capable {
477+
"yes"
478+
} else {
479+
"no"
480+
}
481+
)?;
466482
}
467483

468484
tracing::debug!("pinned={}", entry.pinned);
@@ -500,6 +516,18 @@ fn human_render_slot_ostree(
500516
if let Some(ostree) = &entry.ostree {
501517
render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
502518
}
519+
520+
// Show soft-reboot capability
521+
write_row_name(&mut out, "Soft-reboot", prefix_len)?;
522+
writeln!(
523+
out,
524+
"{}",
525+
if entry.soft_reboot_capable {
526+
"yes"
527+
} else {
528+
"no"
529+
}
530+
)?;
503531
}
504532

505533
tracing::debug!("pinned={}", entry.pinned);
@@ -721,5 +749,6 @@ mod tests {
721749
assert!(w.contains("Deploy serial:"));
722750
assert!(w.contains("Staged:"));
723751
assert!(w.contains("Commit:"));
752+
assert!(w.contains("Soft-reboot:"));
724753
}
725754
}

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
@@ -37,6 +37,14 @@ execute:
3737
- /tmt/tests/bootc-install-provision
3838
- /tmt/tests/test-21-logically-bound-switch
3939

40+
/test-21-soft-reboot:
41+
summary: Soft reboot support
42+
discover:
43+
how: fmf
44+
test:
45+
- /tmt/tests/bootc-install-provision
46+
- /tmt/tests/test-21-soft-reboot
47+
4048
/test-22-logically-bound-install:
4149
summary: Execute logically bound images tests for switching images
4250
environment+:
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.soft_reboot_capable) == true
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 ($st.status.staged.soft_reboot_capable) == false
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+
}

0 commit comments

Comments
 (0)