Skip to content

Commit e8c9d82

Browse files
committed
cli: add support for soft-reboots
This commit adds --soft-reboot=required|auto to the cli 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 598d0bd commit e8c9d82

File tree

8 files changed

+297
-6
lines changed

8 files changed

+297
-6
lines changed

crates/lib/src/cli.rs

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

83+
/// Configure soft reboot behavior.
84+
///
85+
/// 'required' will fail if soft reboot is not available.
86+
/// 'auto' will use soft reboot if available, otherwise fall back to regular reboot.
87+
#[clap(long = "soft-reboot", conflicts_with = "check")]
88+
pub(crate) soft_reboot: Option<SoftRebootMode>,
89+
8390
#[clap(flatten)]
8491
pub(crate) progress: ProgressOptions,
8592
}
@@ -99,6 +106,13 @@ pub(crate) struct SwitchOpts {
99106
#[clap(long)]
100107
pub(crate) apply: bool,
101108

109+
/// Configure soft reboot behavior.
110+
///
111+
/// 'required' will fail if soft reboot is not available.
112+
/// 'auto' will use soft reboot if available, otherwise fall back to regular reboot.
113+
#[clap(long = "soft-reboot")]
114+
pub(crate) soft_reboot: Option<SoftRebootMode>,
115+
102116
/// The transport; e.g. oci, oci-archive, containers-storage. Defaults to `registry`.
103117
#[clap(long, default_value = "registry")]
104118
pub(crate) transport: String,
@@ -142,6 +156,13 @@ pub(crate) struct RollbackOpts {
142156
/// a userspace-only restart.
143157
#[clap(long)]
144158
pub(crate) apply: bool,
159+
160+
/// Configure soft reboot behavior.
161+
///
162+
/// 'required' will fail if soft reboot is not available.
163+
/// 'auto' will use soft reboot if available, otherwise fall back to regular reboot.
164+
#[clap(long = "soft-reboot")]
165+
pub(crate) soft_reboot: Option<SoftRebootMode>,
145166
}
146167

147168
/// Perform an edit operation
@@ -167,6 +188,15 @@ pub(crate) enum OutputFormat {
167188
Json,
168189
}
169190

191+
#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
192+
#[clap(rename_all = "lowercase")]
193+
pub(crate) enum SoftRebootMode {
194+
/// Require a soft reboot; fail if not possible
195+
Required,
196+
/// Automatically use soft reboot if possible, otherwise use regular reboot
197+
Auto,
198+
}
199+
170200
/// Perform an status operation
171201
#[derive(Debug, Parser, PartialEq, Eq)]
172202
pub(crate) struct StatusOpts {
@@ -562,7 +592,7 @@ pub(crate) enum Opt {
562592
Note on Rollbacks and the `/etc` Directory:
563593
564594
When you perform a rollback (e.g., with `bootc rollback`), any
565-
changes made to files in the `/etc` directory wont carry over
595+
changes made to files in the `/etc` directory won't carry over
566596
to the rolled-back deployment. The `/etc` files will revert
567597
to their state from that previous deployment instead.
568598
@@ -741,6 +771,76 @@ pub(crate) fn require_root(is_container: bool) -> Result<()> {
741771
Ok(())
742772
}
743773

774+
/// Check if a deployment has soft reboot capability
775+
fn has_soft_reboot_capability(deployment: Option<&crate::spec::BootEntry>) -> bool {
776+
deployment.map(|d| d.soft_reboot_capable).unwrap_or(false)
777+
}
778+
779+
/// Prepare a soft reboot for the given deployment
780+
#[context("Preparing soft reboot")]
781+
fn prepare_soft_reboot(
782+
sysroot: &crate::store::Storage,
783+
deployment: &ostree::Deployment,
784+
) -> Result<()> {
785+
let cancellable = ostree::gio::Cancellable::NONE;
786+
sysroot
787+
.sysroot
788+
.deployment_set_soft_reboot(deployment, false, cancellable)
789+
.context("Failed to prepare soft-reboot")?;
790+
Ok(())
791+
}
792+
793+
/// Handle soft reboot based on the configured mode
794+
#[context("Handling soft reboot")]
795+
fn handle_soft_reboot<F>(
796+
soft_reboot_mode: Option<SoftRebootMode>,
797+
entry: Option<&crate::spec::BootEntry>,
798+
deployment_type: &str,
799+
execute_soft_reboot: F,
800+
) -> Result<()>
801+
where
802+
F: FnOnce() -> Result<()>,
803+
{
804+
let Some(mode) = soft_reboot_mode else {
805+
return Ok(());
806+
};
807+
808+
let can_soft_reboot = has_soft_reboot_capability(entry);
809+
match mode {
810+
SoftRebootMode::Required => {
811+
if can_soft_reboot {
812+
execute_soft_reboot()?;
813+
} else {
814+
anyhow::bail!(
815+
"Soft reboot was required but {} deployment is not soft-reboot capable",
816+
deployment_type
817+
);
818+
}
819+
}
820+
SoftRebootMode::Auto => {
821+
if can_soft_reboot {
822+
execute_soft_reboot()?;
823+
}
824+
}
825+
}
826+
Ok(())
827+
}
828+
829+
/// Perform a soft reboot for a staged deployment
830+
#[context("Soft reboot staged deployment")]
831+
fn soft_reboot_staged(sysroot: &crate::store::Storage) -> Result<()> {
832+
println!("Staged deployment is soft-reboot capable, preparing for soft-reboot...");
833+
834+
let deployments_list = sysroot.deployments();
835+
let staged_deployment = deployments_list
836+
.iter()
837+
.find(|d| d.is_staged())
838+
.ok_or_else(|| anyhow::anyhow!("Failed to find staged deployment"))?;
839+
840+
prepare_soft_reboot(sysroot, staged_deployment)?;
841+
Ok(())
842+
}
843+
744844
/// A few process changes that need to be made for writing.
745845
/// IMPORTANT: This may end up re-executing the current process,
746846
/// so anything that happens before this should be idempotent.
@@ -859,7 +959,12 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
859959
.unwrap_or_default();
860960
if staged_unchanged {
861961
println!("Staged update present, not changed.");
862-
962+
handle_soft_reboot(
963+
opts.soft_reboot,
964+
host.status.staged.as_ref(),
965+
"staged",
966+
|| soft_reboot_staged(sysroot),
967+
)?;
863968
if opts.apply {
864969
crate::reboot::reboot()?;
865970
}
@@ -881,6 +986,18 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
881986
if changed {
882987
sysroot.update_mtime()?;
883988

989+
if opts.soft_reboot.is_some() {
990+
// At this point we have new staged deployment and the host definition has changed.
991+
// We need the updated host status before we check if we can prepare the soft-reboot.
992+
let updated_host = crate::status::get_status(sysroot, Some(&booted_deployment))?.1;
993+
handle_soft_reboot(
994+
opts.soft_reboot,
995+
updated_host.status.staged.as_ref(),
996+
"staged",
997+
|| soft_reboot_staged(sysroot),
998+
)?;
999+
}
1000+
8841001
if opts.apply {
8851002
crate::reboot::reboot()?;
8861003
}
@@ -956,6 +1073,18 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
9561073

9571074
sysroot.update_mtime()?;
9581075

1076+
if opts.soft_reboot.is_some() {
1077+
// At this point we have staged the deployment and the host definition has changed.
1078+
// We need the updated host status before we check if we can prepare the soft-reboot.
1079+
let updated_host = crate::status::get_status(sysroot, Some(&booted_deployment))?.1;
1080+
handle_soft_reboot(
1081+
opts.soft_reboot,
1082+
updated_host.status.staged.as_ref(),
1083+
"staged",
1084+
|| soft_reboot_staged(sysroot),
1085+
)?;
1086+
}
1087+
9591088
if opts.apply {
9601089
crate::reboot::reboot()?;
9611090
}
@@ -969,6 +1098,27 @@ async fn rollback(opts: RollbackOpts) -> Result<()> {
9691098
let sysroot = &get_storage().await?;
9701099
crate::deploy::rollback(sysroot).await?;
9711100

1101+
if opts.soft_reboot.is_some() {
1102+
// Get status of rollback deployment to check soft-reboot capability
1103+
let host = crate::status::get_status_require_booted(sysroot)?.2;
1104+
1105+
handle_soft_reboot(
1106+
opts.soft_reboot,
1107+
host.status.rollback.as_ref(),
1108+
"rollback",
1109+
|| {
1110+
println!(
1111+
"Rollback deployment is soft-reboot capable, preparing for soft-reboot..."
1112+
);
1113+
let deployments_list = sysroot.deployments();
1114+
let target_deployment = deployments_list
1115+
.first()
1116+
.ok_or_else(|| anyhow::anyhow!("No rollback deployment found!"))?;
1117+
prepare_soft_reboot(sysroot, target_deployment)
1118+
},
1119+
)?;
1120+
}
1121+
9721122
if opts.apply {
9731123
crate::reboot::reboot()?;
9741124
}

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

0 commit comments

Comments
 (0)