Skip to content

Commit 009ee03

Browse files
Johan-Liebert1cgwalters
authored andcommitted
lib/composefs: Centralize constants
Centralize all constants in a separate file Signed-off-by: Johan-Liebert1 <[email protected]>
1 parent 805a42d commit 009ee03

File tree

5 files changed

+71
-46
lines changed

5 files changed

+71
-46
lines changed

crates/lib/src/composefs_consts.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// composefs= paramter in kernel cmdline
2+
pub const COMPOSEFS_CMDLINE: &str = "composefs";
3+
4+
/// Directory to store transient state, such as staged deployemnts etc
5+
pub(crate) const COMPOSEFS_TRANSIENT_STATE_DIR: &str = "/run/composefs";
6+
/// File created in /run/composefs to record a staged-deployment
7+
pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";
8+
9+
/// Absolute path to composefs-native state directory
10+
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
11+
/// Relative path to composefs-native state directory. Relative to /sysroot
12+
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
13+
14+
/// Section in .origin file to store boot related metadata
15+
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";
16+
/// Whether the deployment was booted with BLS or UKI
17+
pub(crate) const ORIGIN_KEY_BOOT_TYPE: &str = "boot_type";
18+
/// Key to store the SHA256 sum of vmlinuz + initrd for a deployment
19+
pub(crate) const ORIGIN_KEY_BOOT_DIGEST: &str = "digest";
20+
21+
/// Filename for `loader/entries`
22+
pub(crate) const BOOT_LOADER_ENTRIES: &str = "entries";
23+
/// Filename for staged boot loader entries
24+
pub(crate) const STAGED_BOOT_LOADER_ENTRIES: &str = "entries.staged";
25+
/// Filename for rollback boot loader entries
26+
pub(crate) const ROLLBACK_BOOT_LOADER_ENTRIES: &str = STAGED_BOOT_LOADER_ENTRIES;
27+
28+
/// Filename for grub user config
29+
pub(crate) const USER_CFG: &str = "user.cfg";
30+
/// Filename for staged grub user config
31+
pub(crate) const USER_CFG_STAGED: &str = "user.cfg.staged";
32+
/// Filename for rollback grub user config
33+
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");
@@ -801,14 +799,9 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
801799
Ok(())
802800
}
803801

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

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

823816
let file_name = entry.file_name();
@@ -860,7 +853,9 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
860853
assert!(all_configs.len() == 2);
861854

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

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

891-
tracing::debug!("Atomically exchanging for {ROLLBACK_ENTRIES} and {CURRENT_ENTRIES}");
886+
tracing::debug!(
887+
"Atomically exchanging for {ROLLBACK_BOOT_LOADER_ENTRIES} and {BOOT_LOADER_ENTRIES}"
888+
);
892889
renameat_with(
893890
&dir,
894-
ROLLBACK_ENTRIES,
891+
ROLLBACK_BOOT_LOADER_ENTRIES,
895892
&dir,
896-
CURRENT_ENTRIES,
893+
BOOT_LOADER_ENTRIES,
897894
RenameFlags::EXCHANGE,
898895
)
899896
.context("renameat")?;
900897

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

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

crates/lib/src/install.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ use serde::{Deserialize, Serialize};
7575
use self::baseline::InstallBlockDeviceOpts;
7676
use crate::bls_config::{parse_bls_config, BLSConfig};
7777
use crate::boundimage::{BoundImage, ResolvedBoundImage};
78+
use crate::composefs_consts::{
79+
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME,
80+
COMPOSEFS_TRANSIENT_STATE_DIR, ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_DIGEST, ORIGIN_KEY_BOOT_TYPE,
81+
STAGED_BOOT_LOADER_ENTRIES, STATE_DIR_ABS, STATE_DIR_RELATIVE, USER_CFG, USER_CFG_STAGED,
82+
};
7883
use crate::containerenv::ContainerExecutionInfo;
7984
use crate::deploy::{
8085
get_sorted_uki_boot_entries, prepare_for_pull, pull_from_prepared, PreparedImportMeta,
81-
PreparedPullResult, USER_CFG, USER_CFG_STAGED,
86+
PreparedPullResult,
8287
};
8388
use crate::kernel_cmdline::Cmdline;
8489
use crate::lsm;
@@ -1548,7 +1553,7 @@ async fn initialize_composefs_repository(
15481553

15491554
rootfs_dir
15501555
.create_dir_all("composefs")
1551-
.context("Creating dir 'composefs'")?;
1556+
.context("Creating dir composefs")?;
15521557

15531558
let repo = open_composefs_repo(rootfs_dir)?;
15541559

@@ -1564,7 +1569,7 @@ async fn initialize_composefs_repository(
15641569
fn get_booted_bls() -> Result<BLSConfig> {
15651570
let cmdline = crate::kernel_cmdline::Cmdline::from_proc()?;
15661571
let booted = cmdline
1567-
.find_str("composefs")
1572+
.find_str(COMPOSEFS_CMDLINE)
15681573
.ok_or_else(|| anyhow::anyhow!("Failed to find composefs parameter in kernel cmdline"))?;
15691574

15701575
for entry in std::fs::read_dir("/sysroot/boot/loader/entries")? {
@@ -1754,10 +1759,10 @@ pub(crate) fn setup_composefs_bls_boot(
17541759

17551760
match &state.composefs_options {
17561761
Some(opt) if opt.insecure => {
1757-
cmdline_options.push_str(&format!(" composefs=?{id_hex}"));
1762+
cmdline_options.push_str(&format!(" {COMPOSEFS_CMDLINE}=?{id_hex}"));
17581763
}
17591764
None | Some(..) => {
1760-
cmdline_options.push_str(&format!(" composefs={id_hex}"));
1765+
cmdline_options.push_str(&format!(" {COMPOSEFS_CMDLINE}={id_hex}"));
17611766
}
17621767
};
17631768

@@ -1769,7 +1774,7 @@ pub(crate) fn setup_composefs_bls_boot(
17691774
vec![
17701775
format!("root=UUID={DPS_UUID}"),
17711776
RW_KARG.to_string(),
1772-
format!("composefs={id_hex}"),
1777+
format!("{COMPOSEFS_CMDLINE}={id_hex}"),
17731778
]
17741779
.join(" "),
17751780
),
@@ -1812,9 +1817,12 @@ pub(crate) fn setup_composefs_bls_boot(
18121817
booted_bls.version = 0; // entries are sorted by their filename in reverse order
18131818

18141819
// This will be atomically renamed to 'loader/entries' on shutdown/reboot
1815-
(boot_dir.join("loader/entries.staged"), Some(booted_bls))
1820+
(
1821+
boot_dir.join(format!("loader/{STAGED_BOOT_LOADER_ENTRIES}")),
1822+
Some(booted_bls),
1823+
)
18161824
} else {
1817-
(boot_dir.join("loader/entries"), None)
1825+
(boot_dir.join(format!("loader/{BOOT_LOADER_ENTRIES}")), None)
18181826
};
18191827

18201828
create_dir_all(&entries_path).with_context(|| format!("Creating {:?}", entries_path))?;
@@ -2175,20 +2183,6 @@ fn setup_composefs_boot(root_setup: &RootSetup, state: &State, image_id: &str) -
21752183
Ok(())
21762184
}
21772185

2178-
pub(crate) const COMPOSEFS_TRANSIENT_STATE_DIR: &str = "/run/composefs";
2179-
/// File created in /run/composefs to record a staged-deployment
2180-
pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";
2181-
2182-
/// Absolute path to composefs-native state directory
2183-
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
2184-
/// Relative path to composefs-native state directory. Relative to /sysroot
2185-
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
2186-
2187-
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";
2188-
pub(crate) const ORIGIN_KEY_BOOT_TYPE: &str = "boot_type";
2189-
/// Key to store the SHA256 sum of vmlinuz + initrd for a deployment
2190-
pub(crate) const ORIGIN_KEY_BOOT_DIGEST: &str = "digest";
2191-
21922186
/// Creates and populates /sysroot/state/deploy/image_id
21932187
#[context("Writing composefs state")]
21942188
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
@@ -9,6 +9,7 @@ pub(crate) mod bootc_kargs;
99
mod boundimage;
1010
mod cfsctl;
1111
pub mod cli;
12+
mod composefs_consts;
1213
pub(crate) mod deploy;
1314
pub(crate) mod fsck;
1415
pub(crate) mod generator;

crates/lib/src/status.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ use ostree_ext::ostree;
2424
use tokio::io::AsyncReadExt;
2525

2626
use crate::cli::OutputFormat;
27+
use crate::composefs_consts::{
28+
COMPOSEFS_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR,
29+
ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_TYPE, STATE_DIR_RELATIVE,
30+
};
2731
use crate::deploy::get_sorted_bls_boot_entries;
2832
use crate::deploy::get_sorted_uki_boot_entries;
2933
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-
};
3534
use crate::spec::ImageStatus;
3635
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType};
3736
use crate::spec::{ImageReference, ImageSignature};
@@ -411,7 +410,7 @@ async fn boot_entry_from_composefs_deployment(
411410
pub(crate) async fn composefs_deployment_status() -> Result<Host> {
412411
let cmdline = crate::kernel_cmdline::Cmdline::from_proc()?;
413412
let composefs_arg = cmdline
414-
.find_str("composefs")
413+
.find_str(COMPOSEFS_CMDLINE)
415414
.ok_or_else(|| anyhow::anyhow!("Failed to find composefs parameter in kernel cmdline"))?;
416415
let booted_image_verity = composefs_arg
417416
.value

0 commit comments

Comments
 (0)