Skip to content

Commit 9d5eb1a

Browse files
committed
chore: hide grub and bios, systemd-boot behind feature flags
1 parent 3af4186 commit 9d5eb1a

File tree

10 files changed

+185
-75
lines changed

10 files changed

+185
-75
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ tier = "2"
1919
name = "bootupd"
2020
path = "src/main.rs"
2121

22+
[features]
23+
default = ["systemd-boot"]
24+
grub = []
25+
systemd-boot = []
26+
2227
[dependencies]
2328
anyhow = "1.0"
2429
bincode = "1.3.2"

src/blockdev.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub fn find_colocated_esps(devices: &Vec<String>) -> Result<Option<Vec<String>>>
6969
}
7070

7171
/// Find bios_boot partition on the same device
72+
#[cfg(feature = "grub")]
7273
pub fn get_bios_boot_partition(device: &str) -> Result<Option<String>> {
7374
const BIOS_BOOT_TYPE_GUID: &str = "21686148-6449-6E6F-744E-656564454649";
7475
let device_info = bootc_internal_blockdev::partitions_of(Utf8Path::new(device))?;
@@ -83,6 +84,7 @@ pub fn get_bios_boot_partition(device: &str) -> Result<Option<String>> {
8384
}
8485

8586
/// Find all bios_boot partitions on the devices
87+
#[cfg(feature = "grub")]
8688
pub fn find_colocated_bios_boot(devices: &Vec<String>) -> Result<Option<Vec<String>>> {
8789
// look for all bios_boot parts on those devices
8890
let mut bios_boots = Vec::new();

src/bootupd.rs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,65 @@
1-
#[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))]
1+
#[cfg(all(
2+
any(target_arch = "x86_64", target_arch = "powerpc64"),
3+
feature = "grub"
4+
))]
25
use crate::bios;
36
use crate::component;
4-
use crate::component::{Component, ValidationResult};
7+
use crate::component::Component;
8+
#[cfg(feature = "grub")]
9+
use crate::component::ValidationResult;
510
use crate::coreos;
611
#[cfg(any(
712
target_arch = "x86_64",
813
target_arch = "aarch64",
914
target_arch = "riscv64"
1015
))]
1116
use crate::efi;
17+
18+
#[cfg(feature = "grub")]
1219
use crate::freezethaw::fsfreeze_thaw_cycle;
1320
use crate::model::{ComponentStatus, ComponentUpdatable, ContentMetadata, SavedState, Status};
14-
use crate::{ostreeutil, util};
21+
#[cfg(feature = "grub")]
22+
use crate::ostreeutil;
23+
use crate::util;
1524
use anyhow::{anyhow, bail, Context, Result};
1625
use camino::{Utf8Path, Utf8PathBuf};
26+
#[cfg(feature = "grub")]
1727
use clap::crate_version;
28+
#[cfg(feature = "grub")]
1829
use fn_error_context::context;
30+
#[cfg(feature = "grub")]
1931
use libc::mode_t;
32+
#[cfg(feature = "grub")]
2033
use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWUSR};
2134
use serde::{Deserialize, Serialize};
2235
use std::borrow::Cow;
2336
use std::collections::BTreeMap;
37+
38+
#[cfg(feature = "grub")]
2439
use std::fs::{self, File};
25-
use std::io::{BufRead, BufReader, BufWriter, Write};
26-
use std::path::{Path, PathBuf};
40+
#[cfg(feature = "grub")]
41+
use std::io::{BufRead, BufWriter, Write};
42+
43+
#[cfg(feature = "grub")]
44+
use std::io::BufReader;
45+
use std::path::Path;
46+
#[cfg(feature = "grub")]
47+
use std::path::PathBuf;
2748

2849
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2950
pub(crate) enum ConfigMode {
3051
None,
3152
Static,
3253
WithUUID,
33-
SystemdBoot,
3454
}
3555

56+
#[cfg(feature = "grub")]
3657
impl ConfigMode {
3758
pub(crate) fn enabled_with_uuid(&self) -> Option<bool> {
3859
match self {
3960
ConfigMode::None => None,
4061
ConfigMode::Static => Some(false),
4162
ConfigMode::WithUUID => Some(true),
42-
ConfigMode::SystemdBoot => Some(false),
4363
}
4464
}
4565
}
@@ -48,7 +68,8 @@ pub(crate) fn install(
4868
source_root: &str,
4969
dest_root: &str,
5070
device: Option<&str>,
51-
configs: ConfigMode,
71+
#[cfg(feature = "grub")] configs: ConfigMode,
72+
#[cfg(not(feature = "grub"))] _configs: ConfigMode,
5273
update_firmware: bool,
5374
target_components: Option<&[String]>,
5475
auto_components: bool,
@@ -85,6 +106,7 @@ pub(crate) fn install(
85106
}
86107

87108
let mut state = SavedState::default();
109+
#[cfg(feature = "grub")]
88110
let mut installed_efi_vendor = None;
89111
for &component in target_components.iter() {
90112
// skip for BIOS if device is empty
@@ -101,18 +123,18 @@ pub(crate) fn install(
101123
.with_context(|| format!("installing component {}", component.name()))?;
102124
log::info!("Installed {} {}", component.name(), meta.meta.version);
103125
state.installed.insert(component.name().into(), meta);
104-
// If not systemd-boot, try to get the efi vendor
105-
if configs != ConfigMode::SystemdBoot {
106-
// Yes this is a hack...the Component thing just turns out to be too generic.
107-
if let Some(vendor) = component.get_efi_vendor(&source_root)? {
108-
assert!(installed_efi_vendor.is_none());
109-
installed_efi_vendor = Some(vendor);
110-
}
126+
127+
// Yes this is a hack...the Component thing just turns out to be too generic.
128+
#[cfg(feature = "grub")]
129+
if let Some(vendor) = component.get_efi_vendor(&source_root)? {
130+
assert!(installed_efi_vendor.is_none());
131+
installed_efi_vendor = Some(vendor);
111132
}
112133
}
113134

114135
let sysroot = &openat::Dir::open(dest_root)?;
115136

137+
#[cfg(feature = "grub")]
116138
match configs.enabled_with_uuid() {
117139
Some(uuid) => {
118140
let meta = get_static_config_meta()?;
@@ -129,7 +151,8 @@ pub(crate) fn install(
129151
None => {}
130152
}
131153

132-
if configs == ConfigMode::SystemdBoot {
154+
#[cfg(feature = "systemd-boot")]
155+
{
133156
let efi = crate::efi::Efi::default();
134157
log::warn!("Installing systemd-boot entries");
135158
if let Ok(Some(mnt)) = efi.get_mounted_esp(Path::new(dest_root)) {
@@ -153,6 +176,7 @@ pub(crate) fn install(
153176
}
154177

155178
#[context("Get static config metadata")]
179+
#[cfg(feature = "grub")]
156180
fn get_static_config_meta() -> Result<ContentMetadata> {
157181
let self_bin_meta = std::fs::metadata("/proc/self/exe").context("Querying self meta")?;
158182
let self_meta = ContentMetadata {
@@ -185,9 +209,11 @@ pub(crate) fn get_components_impl(auto: bool) -> Components {
185209
if is_efi_booted {
186210
insert_component(&mut components, Box::new(efi::Efi::default()));
187211
} else {
212+
#[cfg(feature = "grub")]
188213
insert_component(&mut components, Box::new(bios::Bios::default()));
189214
}
190215
} else {
216+
#[cfg(feature = "grub")]
191217
insert_component(&mut components, Box::new(bios::Bios::default()));
192218
insert_component(&mut components, Box::new(efi::Efi::default()));
193219
}
@@ -281,6 +307,7 @@ pub(crate) fn update(name: &str, rootcxt: &RootContext) -> Result<ComponentUpdat
281307
}
282308

283309
/// daemon implementation of component adoption
310+
#[cfg(feature = "grub")]
284311
pub(crate) fn adopt_and_update(
285312
name: &str,
286313
rootcxt: &RootContext,
@@ -327,6 +354,7 @@ pub(crate) fn adopt_and_update(
327354
}
328355

329356
/// daemon implementation of component validate
357+
#[cfg(feature = "grub")]
330358
pub(crate) fn validate(name: &str) -> Result<ValidationResult> {
331359
let state = SavedState::load_from_disk("/")?.unwrap_or_default();
332360
let component = component::new_from_name(name)?;
@@ -532,6 +560,8 @@ pub(crate) fn client_run_update() -> Result<()> {
532560
}
533561
updated = true;
534562
}
563+
564+
#[cfg(feature = "grub")]
535565
for (name, adoptable) in status.adoptable.iter() {
536566
if adoptable.confident {
537567
if let Some(r) = adopt_and_update(name, &rootcxt, false)? {
@@ -548,6 +578,7 @@ pub(crate) fn client_run_update() -> Result<()> {
548578
Ok(())
549579
}
550580

581+
#[cfg(feature = "grub")]
551582
pub(crate) fn client_run_adopt_and_update(with_static_config: bool) -> Result<()> {
552583
let rootcxt = prep_before_update()?;
553584
let status: Status = status()?;
@@ -563,6 +594,7 @@ pub(crate) fn client_run_adopt_and_update(with_static_config: bool) -> Result<()
563594
Ok(())
564595
}
565596

597+
#[cfg(feature = "grub")]
566598
pub(crate) fn client_run_validate() -> Result<()> {
567599
let status: Status = status()?;
568600
if status.components.is_empty() {
@@ -593,6 +625,7 @@ pub(crate) fn client_run_validate() -> Result<()> {
593625
}
594626

595627
#[context("Migrating to a static GRUB config")]
628+
#[cfg(feature = "grub")]
596629
pub(crate) fn client_run_migrate_static_grub_config() -> Result<()> {
597630
// Did we already complete the migration?
598631
// We need to migrate if bootloader is not none (or not set)
@@ -679,6 +712,7 @@ pub(crate) fn client_run_migrate_static_grub_config() -> Result<()> {
679712

680713
/// Writes a stripped GRUB config to `stripped_config_name`, removing lines between
681714
/// `### BEGIN /etc/grub.d/15_ostree ###` and `### END /etc/grub.d/15_ostree ###`.
715+
#[cfg(feature = "grub")]
682716
fn strip_grub_config_file(
683717
current_config_content: impl BufRead,
684718
dirfd: &openat::Dir,
@@ -739,6 +773,7 @@ mod tests {
739773
}
740774

741775
#[test]
776+
#[cfg(feature = "grub")]
742777
fn test_strip_grub_config_file() -> Result<()> {
743778
let root: &tempfile::TempDir = &tempfile::tempdir()?;
744779
let root_path = root.path();

src/cli/bootupctl.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ pub enum CtlVerb {
5555
Status(StatusOpts),
5656
#[clap(name = "update", about = "Update all components")]
5757
Update,
58+
#[cfg(feature = "grub")]
5859
#[clap(name = "adopt-and-update", about = "Update all adoptable components")]
5960
AdoptAndUpdate(AdoptAndUpdateOpts),
61+
#[cfg(feature = "grub")]
6062
#[clap(name = "validate", about = "Validate system state")]
6163
Validate,
64+
#[cfg(feature = "grub")]
6265
#[clap(
6366
name = "migrate-static-grub-config",
6467
hide = true,
@@ -89,6 +92,7 @@ pub struct StatusOpts {
8992
}
9093

9194
#[derive(Debug, Parser)]
95+
#[cfg(feature = "grub")]
9296
pub struct AdoptAndUpdateOpts {
9397
/// Install the static GRUB config files
9498
#[clap(long, action)]
@@ -101,14 +105,17 @@ impl CtlCommand {
101105
match self.cmd {
102106
CtlVerb::Status(opts) => Self::run_status(opts),
103107
CtlVerb::Update => Self::run_update(),
108+
#[cfg(feature = "grub")]
104109
CtlVerb::AdoptAndUpdate(opts) => Self::run_adopt_and_update(opts),
110+
#[cfg(feature = "grub")]
105111
CtlVerb::Validate => Self::run_validate(),
106112
CtlVerb::Backend(CtlBackend::Generate(opts)) => {
107113
super::bootupd::DCommand::run_generate_meta(opts)
108114
}
109115
CtlVerb::Backend(CtlBackend::Install(opts)) => {
110116
super::bootupd::DCommand::run_install(opts)
111117
}
118+
#[cfg(feature = "grub")]
112119
CtlVerb::MigrateStaticGrubConfig => Self::run_migrate_static_grub_config(),
113120
}
114121
}
@@ -140,18 +147,21 @@ impl CtlCommand {
140147
}
141148

142149
/// Runner for `update` verb.
150+
#[cfg(feature = "grub")]
143151
fn run_adopt_and_update(opts: AdoptAndUpdateOpts) -> Result<()> {
144152
ensure_running_in_systemd()?;
145153
bootupd::client_run_adopt_and_update(opts.with_static_config)
146154
}
147155

148156
/// Runner for `validate` verb.
157+
#[cfg(feature = "grub")]
149158
fn run_validate() -> Result<()> {
150159
ensure_running_in_systemd()?;
151160
bootupd::client_run_validate()
152161
}
153162

154163
/// Runner for `migrate-static-grub-config` verb.
164+
#[cfg(feature = "grub")]
155165
fn run_migrate_static_grub_config() -> Result<()> {
156166
ensure_running_in_systemd()?;
157167
bootupd::client_run_migrate_static_grub_config()

src/cli/bootupd.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ pub struct InstallOpts {
7373
/// then only enable installation to the ESP.
7474
#[clap(long)]
7575
auto: bool,
76-
77-
/// The bootloader to use
78-
#[clap(long, default_value = "systemd-boot")]
79-
bootloader: Option<String>,
8076
}
8177

8278
#[derive(Debug, Parser)]
@@ -107,18 +103,14 @@ impl DCommand {
107103

108104
/// Runner for `install` verb.
109105
pub(crate) fn run_install(opts: InstallOpts) -> Result<()> {
110-
let mut configmode = if opts.write_uuid {
106+
let configmode = if opts.write_uuid {
111107
ConfigMode::WithUUID
112108
} else if opts.with_static_configs {
113109
ConfigMode::Static
114110
} else {
115111
ConfigMode::None
116112
};
117113

118-
if opts.bootloader.as_deref() == Some("systemd-boot") {
119-
configmode = ConfigMode::SystemdBoot;
120-
}
121-
122114
bootupd::install(
123115
&opts.src_root,
124116
&opts.dest_root,

src/component.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ pub(crate) trait Component {
2929
/// In an operating system whose initially booted disk image is not
3030
/// using bootupd, detect whether it looks like the component exists
3131
/// and "synthesize" content metadata from it.
32+
#[cfg(feature = "grub")]
3233
fn query_adopt(&self, devices: &Option<Vec<String>>) -> Result<Option<Adoptable>>;
3334

3435
// Backup the current grub config, and install static grub config from tree
36+
#[cfg(feature = "grub")]
3537
fn migrate_static_grub_config(&self, sysroot_path: &str, destdir: &openat::Dir) -> Result<()>;
3638

3739
/// Given an adoptable system and an update, perform the update.
40+
#[cfg(feature = "grub")]
3841
fn adopt_update(
3942
&self,
4043
rootcxt: &RootContext,
@@ -75,9 +78,11 @@ pub(crate) trait Component {
7578
) -> Result<InstalledContent>;
7679

7780
/// Used on the client to validate an installed version.
81+
#[cfg(feature = "grub")]
7882
fn validate(&self, current: &InstalledContent) -> Result<ValidationResult>;
7983

8084
/// Locating efi vendor dir
85+
#[cfg(feature = "grub")]
8186
fn get_efi_vendor(&self, sysroot: &openat::Dir) -> Result<Option<String>>;
8287
}
8388

@@ -91,7 +96,10 @@ pub(crate) fn new_from_name(name: &str) -> Result<Box<dyn Component>> {
9196
))]
9297
#[allow(clippy::box_default)]
9398
"EFI" => Box::new(crate::efi::Efi::default()),
94-
#[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))]
99+
#[cfg(all(
100+
any(target_arch = "x86_64", target_arch = "powerpc64"),
101+
feature = "grub"
102+
))]
95103
#[allow(clippy::box_default)]
96104
"BIOS" => Box::new(crate::bios::Bios::default()),
97105
_ => anyhow::bail!("No component {}", name),
@@ -117,6 +125,7 @@ pub(crate) fn component_updatedirname(component: &dyn Component) -> PathBuf {
117125
target_arch = "aarch64",
118126
target_arch = "riscv64"
119127
))]
128+
#[cfg(feature = "grub")]
120129
pub(crate) fn component_updatedir(sysroot: &str, component: &dyn Component) -> PathBuf {
121130
Path::new(sysroot).join(component_updatedirname(component))
122131
}
@@ -194,9 +203,11 @@ pub(crate) fn query_adopt_state() -> Result<Option<Adoptable>> {
194203

195204
#[cfg(test)]
196205
mod tests {
206+
#[cfg(feature = "grub")]
197207
use super::*;
198208

199209
#[test]
210+
#[cfg(feature = "grub")]
200211
fn test_get_efi_vendor() -> Result<()> {
201212
let td = tempfile::tempdir()?;
202213
let tdp = td.path();

0 commit comments

Comments
 (0)