Skip to content

Commit 3d63b81

Browse files
lib/composefs: Centralize constants
Centralize all constants in a separate file Signed-off-by: Johan-Liebert1 <[email protected]>
1 parent 39f952e commit 3d63b81

File tree

8 files changed

+93
-55
lines changed

8 files changed

+93
-55
lines changed

crates/lib/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ostree_container::store::PrepareResult;
2020
use ostree_ext::composefs::fsverity;
2121
use ostree_ext::composefs::fsverity::FsVerityHashValue;
2222
use ostree_ext::container as ostree_container;
23-
use ostree_ext::container_utils::{composefs_booted, ostree_booted};
23+
use ostree_ext::container_utils::ostree_booted;
2424
use ostree_ext::keyfileext::KeyFileExt;
2525
use ostree_ext::ostree;
2626
use schemars::schema_for;
@@ -36,7 +36,7 @@ use crate::progress_jsonl::{ProgressWriter, RawProgressFd};
3636
use crate::spec::Host;
3737
use crate::spec::ImageReference;
3838
use crate::status::composefs_deployment_status;
39-
use crate::utils::sigpolicy_from_opt;
39+
use crate::utils::{composefs_booted, sigpolicy_from_opt};
4040

4141
/// Shared progress options
4242
#[derive(Debug, Parser, PartialEq, Eq)]

crates/lib/src/composefs_consts.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// composefs= paramter in kernel cmdline
2+
pub const COMPOSEFS_CMDLINE: &str = "composefs=";
3+
/// composefs=? paramter in kernel cmdline. The `?` signifies that the fs-verity validation is
4+
/// optional in case the filesystem doesn't support it.
5+
pub const COMPOSEFS_INSECURE_CMDLINE: &str = "composefs=?";
6+
7+
/// Directory to store transient state, such as staged deployemnts etc
8+
pub(crate) const COMPOSEFS_TRANSIENT_STATE_DIR: &str = "/run/composefs";
9+
/// File created in /run/composefs to record a staged-deployment
10+
pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";
11+
12+
/// Absolute path to composefs-native state directory
13+
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
14+
/// Relative path to composefs-native state directory. Relative to /sysroot
15+
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
16+
17+
/// Section in .origin file to store boot related metadata
18+
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";
19+
/// Whether the deployment was booted with BLS or UKI
20+
pub(crate) const ORIGIN_KEY_BOOT_TYPE: &str = "boot_type";
21+
/// Key to store the SHA256 sum of vmlinuz + initrd for a deployment
22+
pub(crate) const ORIGIN_KEY_BOOT_DIGEST: &str = "digest";
23+
24+
/// Filename for `loader/entries`
25+
pub(crate) const BOOT_LOADER_ENTRIES: &str = "entries";
26+
/// Filename for staged boot loader entries
27+
pub(crate) const STAGED_BOOT_LOADER_ENTRIES: &str = "entries.staged";
28+
/// Filename for rollback boot loader entries
29+
pub(crate) const ROLLBACK_BOOT_LOADER_ENTRIES: &str = STAGED_BOOT_LOADER_ENTRIES;
30+
31+
/// Filename for grub user config
32+
pub(crate) const USER_CFG: &str = "user.cfg";
33+
/// Filename for staged grub user config
34+
pub(crate) const USER_CFG_STAGED: &str = "user.cfg.staged";
35+
/// Filename for rollback grub user config
36+
pub(crate) const USER_CFG_ROLLBACK: &str = USER_CFG_STAGED;

crates/lib/src/deploy.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ use ostree_ext::tokio_util::spawn_blocking_cancellable_flatten;
2626
use rustix::fs::{fsync, renameat_with, AtFlags, RenameFlags};
2727

2828
use crate::bls_config::{parse_bls_config, BLSConfig};
29+
use crate::composefs_consts::{
30+
BOOT_LOADER_ENTRIES, ROLLBACK_BOOT_LOADER_ENTRIES, USER_CFG, USER_CFG_ROLLBACK,
31+
};
2932
use crate::install::{get_efi_uuid_source, BootType};
3033
use crate::parsers::grub_menuconfig::{parse_grub_menuentry_file, MenuEntry};
3134
use crate::progress_jsonl::{Event, ProgressWriter, SubTaskBytes, SubTaskStep};
@@ -744,11 +747,6 @@ pub(crate) async fn stage(
744747
Ok(())
745748
}
746749

747-
/// Filename for `loader/entries`
748-
pub(crate) const USER_CFG: &str = "user.cfg";
749-
pub(crate) const USER_CFG_STAGED: &str = "user.cfg.staged";
750-
pub(crate) const USER_CFG_ROLLBACK: &str = USER_CFG_STAGED;
751-
752750
#[context("Rolling back UKI")]
753751
pub(crate) fn rollback_composefs_uki() -> Result<()> {
754752
let user_cfg_path = PathBuf::from("/sysroot/boot/grub2");
@@ -802,14 +800,9 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
802800
Ok(())
803801
}
804802

805-
/// Filename for `loader/entries`
806-
const CURRENT_ENTRIES: &str = "entries";
807-
const STAGED_ENTRIES: &str = "entries.staged";
808-
const ROLLBACK_ENTRIES: &str = STAGED_ENTRIES;
809-
810803
// Need str to store lifetime
811804
pub(crate) fn get_sorted_uki_boot_entries<'a>(str: &'a mut String) -> Result<Vec<MenuEntry<'a>>> {
812-
let mut file = std::fs::File::open("/sysroot/boot/grub2/user.cfg")?;
805+
let mut file = std::fs::File::open(format!("/sysroot/boot/grub2/{USER_CFG}"))?;
813806
file.read_to_string(str)?;
814807
parse_grub_menuentry_file(str)
815808
}
@@ -818,7 +811,7 @@ pub(crate) fn get_sorted_uki_boot_entries<'a>(str: &'a mut String) -> Result<Vec
818811
pub(crate) fn get_sorted_bls_boot_entries(ascending: bool) -> Result<Vec<BLSConfig>> {
819812
let mut all_configs = vec![];
820813

821-
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{CURRENT_ENTRIES}"))? {
814+
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{BOOT_LOADER_ENTRIES}"))? {
822815
let entry = entry?;
823816

824817
let file_name = entry.file_name();
@@ -861,7 +854,9 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
861854
assert!(all_configs.len() == 2);
862855

863856
// Write these
864-
let dir_path = PathBuf::from(format!("/sysroot/boot/loader/{ROLLBACK_ENTRIES}"));
857+
let dir_path = PathBuf::from(format!(
858+
"/sysroot/boot/loader/{ROLLBACK_BOOT_LOADER_ENTRIES}"
859+
));
865860
create_dir_all(&dir_path).with_context(|| format!("Failed to create dir: {dir_path:?}"))?;
866861

867862
let rollback_entries_dir =
@@ -889,18 +884,21 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
889884
let dir = Dir::open_ambient_dir("/sysroot/boot/loader", cap_std::ambient_authority())
890885
.context("Opening loader dir")?;
891886

892-
tracing::debug!("Atomically exchanging for {ROLLBACK_ENTRIES} and {CURRENT_ENTRIES}");
887+
tracing::debug!(
888+
"Atomically exchanging for {ROLLBACK_BOOT_LOADER_ENTRIES} and {BOOT_LOADER_ENTRIES}"
889+
);
893890
renameat_with(
894891
&dir,
895-
ROLLBACK_ENTRIES,
892+
ROLLBACK_BOOT_LOADER_ENTRIES,
896893
&dir,
897-
CURRENT_ENTRIES,
894+
BOOT_LOADER_ENTRIES,
898895
RenameFlags::EXCHANGE,
899896
)
900897
.context("renameat")?;
901898

902-
tracing::debug!("Removing {ROLLBACK_ENTRIES}");
903-
rustix::fs::unlinkat(&dir, ROLLBACK_ENTRIES, AtFlags::REMOVEDIR).context("unlinkat")?;
899+
tracing::debug!("Removing {ROLLBACK_BOOT_LOADER_ENTRIES}");
900+
rustix::fs::unlinkat(&dir, ROLLBACK_BOOT_LOADER_ENTRIES, AtFlags::REMOVEDIR)
901+
.context("unlinkat")?;
904902

905903
tracing::debug!("Syncing to disk");
906904
fsync(

crates/lib/src/install.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ use serde::{Deserialize, Serialize};
7676
use self::baseline::InstallBlockDeviceOpts;
7777
use crate::bls_config::{parse_bls_config, BLSConfig};
7878
use crate::boundimage::{BoundImage, ResolvedBoundImage};
79+
use crate::composefs_consts::{
80+
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, COMPOSEFS_INSECURE_CMDLINE,
81+
COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, ORIGIN_KEY_BOOT,
82+
ORIGIN_KEY_BOOT_DIGEST, ORIGIN_KEY_BOOT_TYPE, STAGED_BOOT_LOADER_ENTRIES, STATE_DIR_ABS,
83+
STATE_DIR_RELATIVE, USER_CFG, USER_CFG_STAGED,
84+
};
7985
use crate::containerenv::ContainerExecutionInfo;
8086
use crate::deploy::{
8187
get_sorted_uki_boot_entries, prepare_for_pull, pull_from_prepared, PreparedImportMeta,
82-
PreparedPullResult, USER_CFG, USER_CFG_STAGED,
88+
PreparedPullResult,
8389
};
8490
use crate::lsm;
8591
use crate::parsers::grub_menuconfig::MenuEntry;
@@ -1534,7 +1540,7 @@ async fn initialize_composefs_repository(
15341540

15351541
rootfs_dir
15361542
.create_dir_all("composefs")
1537-
.context("Creating dir 'composefs'")?;
1543+
.context("Creating dir composefs")?;
15381544

15391545
let repo = open_composefs_repo(rootfs_dir)?;
15401546

@@ -1549,7 +1555,10 @@ async fn initialize_composefs_repository(
15491555

15501556
fn get_booted_bls() -> Result<BLSConfig> {
15511557
let cmdline = crate::kernel::parse_cmdline()?;
1552-
let booted = cmdline.iter().find_map(|x| x.strip_prefix("composefs="));
1558+
let booted = cmdline.iter().find_map(|x| {
1559+
x.strip_prefix(COMPOSEFS_INSECURE_CMDLINE)
1560+
.or_else(|| x.strip_prefix(COMPOSEFS_CMDLINE))
1561+
});
15531562

15541563
let Some(booted) = booted else {
15551564
anyhow::bail!("Failed to find composefs parameter in kernel cmdline");
@@ -1741,10 +1750,10 @@ pub(crate) fn setup_composefs_bls_boot(
17411750

17421751
match &state.composefs_options {
17431752
Some(opt) if opt.insecure => {
1744-
cmdline_options.push_str(&format!(" composefs=?{id_hex}"));
1753+
cmdline_options.push_str(&format!(" {COMPOSEFS_INSECURE_CMDLINE}{id_hex}"));
17451754
}
17461755
None | Some(..) => {
1747-
cmdline_options.push_str(&format!(" composefs={id_hex}"));
1756+
cmdline_options.push_str(&format!(" {COMPOSEFS_CMDLINE}{id_hex}"));
17481757
}
17491758
};
17501759

@@ -1756,7 +1765,7 @@ pub(crate) fn setup_composefs_bls_boot(
17561765
vec![
17571766
format!("root=UUID={DPS_UUID}"),
17581767
RW_KARG.to_string(),
1759-
format!("composefs={id_hex}"),
1768+
format!("{COMPOSEFS_CMDLINE}{id_hex}"),
17601769
]
17611770
.join(" "),
17621771
),
@@ -1800,9 +1809,12 @@ pub(crate) fn setup_composefs_bls_boot(
18001809
booted_bls.version = 0; // entries are sorted by their filename in reverse order
18011810

18021811
// This will be atomically renamed to 'loader/entries' on shutdown/reboot
1803-
(boot_dir.join("loader/entries.staged"), Some(booted_bls))
1812+
(
1813+
boot_dir.join(format!("loader/{STAGED_BOOT_LOADER_ENTRIES}")),
1814+
Some(booted_bls),
1815+
)
18041816
} else {
1805-
(boot_dir.join("loader/entries"), None)
1817+
(boot_dir.join(format!("loader/{BOOT_LOADER_ENTRIES}")), None)
18061818
};
18071819

18081820
create_dir_all(&entries_path).with_context(|| format!("Creating {:?}", entries_path))?;
@@ -2162,20 +2174,6 @@ fn setup_composefs_boot(root_setup: &RootSetup, state: &State, image_id: &str) -
21622174
Ok(())
21632175
}
21642176

2165-
pub(crate) const COMPOSEFS_TRANSIENT_STATE_DIR: &str = "/run/composefs";
2166-
/// File created in /run/composefs to record a staged-deployment
2167-
pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";
2168-
2169-
/// Absolute path to composefs-native state directory
2170-
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
2171-
/// Relative path to composefs-native state directory. Relative to /sysroot
2172-
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
2173-
2174-
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";
2175-
pub(crate) const ORIGIN_KEY_BOOT_TYPE: &str = "boot_type";
2176-
/// Key to store the SHA256 sum of vmlinuz + initrd for a deployment
2177-
pub(crate) const ORIGIN_KEY_BOOT_DIGEST: &str = "digest";
2178-
21792177
/// Creates and populates /sysroot/state/deploy/image_id
21802178
#[context("Writing composefs state")]
21812179
pub(crate) fn write_composefs_state(

crates/lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
mod bls_config;
88
mod boundimage;
99
pub mod cli;
10+
mod composefs_consts;
1011
pub(crate) mod deploy;
1112
pub(crate) mod fsck;
1213
pub(crate) mod generator;

crates/lib/src/status.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use ostree::glib;
1414
use ostree_container::OstreeImageReference;
1515
use ostree_ext::container as ostree_container;
1616
use ostree_ext::container::deploy::ORIGIN_CONTAINER;
17-
use ostree_ext::container_utils::composefs_booted;
1817
use ostree_ext::container_utils::ostree_booted;
1918
use ostree_ext::containers_image_proxy;
2019
use ostree_ext::keyfileext::KeyFileExt;
@@ -24,18 +23,18 @@ use ostree_ext::ostree;
2423
use tokio::io::AsyncReadExt;
2524

2625
use crate::cli::OutputFormat;
26+
use crate::composefs_consts::{
27+
COMPOSEFS_CMDLINE, COMPOSEFS_INSECURE_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME,
28+
COMPOSEFS_TRANSIENT_STATE_DIR, ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_TYPE, STATE_DIR_RELATIVE,
29+
};
2730
use crate::deploy::get_sorted_bls_boot_entries;
2831
use crate::deploy::get_sorted_uki_boot_entries;
2932
use crate::install::BootType;
30-
use crate::install::ORIGIN_KEY_BOOT;
31-
use crate::install::ORIGIN_KEY_BOOT_TYPE;
32-
use crate::install::{
33-
COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, STATE_DIR_RELATIVE,
34-
};
3533
use crate::spec::ImageStatus;
3634
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType};
3735
use crate::spec::{ImageReference, ImageSignature};
3836
use crate::store::{CachedImageStatus, ContainerImageStore, Storage};
37+
use crate::utils::composefs_booted;
3938

4039
impl From<ostree_container::SignatureSource> for ImageSignature {
4140
fn from(sig: ostree_container::SignatureSource) -> Self {
@@ -395,7 +394,11 @@ async fn boot_entry_from_composefs_deployment(
395394
#[context("Getting composefs deployment status")]
396395
pub(crate) async fn composefs_deployment_status() -> Result<Host> {
397396
let cmdline = crate::kernel::parse_cmdline()?;
398-
let booted_image_verity = cmdline.iter().find_map(|x| x.strip_prefix("composefs="));
397+
398+
let booted_image_verity = cmdline.iter().find_map(|x| {
399+
x.strip_prefix(COMPOSEFS_INSECURE_CMDLINE)
400+
.or_else(|| x.strip_prefix(COMPOSEFS_CMDLINE))
401+
});
399402

400403
let Some(booted_image_verity) = booted_image_verity else {
401404
anyhow::bail!("Failed to find composefs parameter in kernel cmdline");

crates/lib/src/utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ use ostree::glib;
1717
use ostree_ext::container::SignatureSource;
1818
use ostree_ext::ostree;
1919

20+
use crate::composefs_consts::{COMPOSEFS_CMDLINE, COMPOSEFS_INSECURE_CMDLINE};
21+
22+
/// Returns true if the system appears to have been booted with composefs without ostree.
23+
pub fn composefs_booted() -> std::io::Result<bool> {
24+
let cmdline = std::fs::read_to_string("/proc/cmdline")?;
25+
Ok(cmdline.contains(COMPOSEFS_CMDLINE) || cmdline.contains(COMPOSEFS_INSECURE_CMDLINE))
26+
}
27+
2028
/// Try to look for keys injected by e.g. rpm-ostree requesting machine-local
2129
/// changes; if any are present, return `true`.
2230
pub(crate) fn origin_has_rpmostree_stuff(kf: &glib::KeyFile) -> bool {

crates/ostree-ext/src/container_utils.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ pub fn ostree_booted() -> io::Result<bool> {
7777
Path::new(&format!("/{OSTREE_BOOTED}")).try_exists()
7878
}
7979

80-
/// Returns true if the system appears to have been booted with composefs without ostree.
81-
pub fn composefs_booted() -> io::Result<bool> {
82-
let cmdline = std::fs::read_to_string("/proc/cmdline")?;
83-
Ok(cmdline.contains("composefs="))
84-
}
85-
8680
/// Returns true if the target root appears to have been booted via ostree.
8781
pub fn is_ostree_booted_in(rootfs: &Dir) -> io::Result<bool> {
8882
rootfs.try_exists(OSTREE_BOOTED)

0 commit comments

Comments
 (0)