Skip to content

Commit 059335b

Browse files
lib/composefs: Centralize constants
Centralize all constants in a separate file Signed-off-by: Johan-Liebert1 <pragyanpoudyal41999@gmail.com>
1 parent f787575 commit 059335b

5 files changed

Lines changed: 71 additions & 46 deletions

File tree

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};
@@ -747,11 +750,6 @@ pub(crate) async fn stage(
747750
Ok(())
748751
}
749752

750-
/// Filename for `loader/entries`
751-
pub(crate) const USER_CFG: &str = "user.cfg";
752-
pub(crate) const USER_CFG_STAGED: &str = "user.cfg.staged";
753-
pub(crate) const USER_CFG_ROLLBACK: &str = USER_CFG_STAGED;
754-
755753
#[context("Rolling back UKI")]
756754
pub(crate) fn rollback_composefs_uki() -> Result<()> {
757755
let user_cfg_path = PathBuf::from("/sysroot/boot/grub2");
@@ -804,14 +802,9 @@ pub(crate) fn rollback_composefs_uki() -> Result<()> {
804802
Ok(())
805803
}
806804

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

823-
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{CURRENT_ENTRIES}"))? {
816+
for entry in std::fs::read_dir(format!("/sysroot/boot/loader/{BOOT_LOADER_ENTRIES}"))? {
824817
let entry = entry?;
825818

826819
let file_name = entry.file_name();
@@ -863,7 +856,9 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
863856
assert!(all_configs.len() == 2);
864857

865858
// Write these
866-
let dir_path = PathBuf::from(format!("/sysroot/boot/loader/{ROLLBACK_ENTRIES}"));
859+
let dir_path = PathBuf::from(format!(
860+
"/sysroot/boot/loader/{ROLLBACK_BOOT_LOADER_ENTRIES}"
861+
));
867862
create_dir_all(&dir_path).with_context(|| format!("Failed to create dir: {dir_path:?}"))?;
868863

869864
let rollback_entries_dir =
@@ -891,18 +886,21 @@ pub(crate) fn rollback_composefs_bls() -> Result<()> {
891886
let dir = Dir::open_ambient_dir("/sysroot/boot/loader", cap_std::ambient_authority())
892887
.context("Opening loader dir")?;
893888

894-
tracing::debug!("Atomically exchanging for {ROLLBACK_ENTRIES} and {CURRENT_ENTRIES}");
889+
tracing::debug!(
890+
"Atomically exchanging for {ROLLBACK_BOOT_LOADER_ENTRIES} and {BOOT_LOADER_ENTRIES}"
891+
);
895892
renameat_with(
896893
&dir,
897-
ROLLBACK_ENTRIES,
894+
ROLLBACK_BOOT_LOADER_ENTRIES,
898895
&dir,
899-
CURRENT_ENTRIES,
896+
BOOT_LOADER_ENTRIES,
900897
RenameFlags::EXCHANGE,
901898
)
902899
.context("renameat")?;
903900

904-
tracing::debug!("Removing {ROLLBACK_ENTRIES}");
905-
rustix::fs::unlinkat(&dir, ROLLBACK_ENTRIES, AtFlags::empty()).context("unlinkat")?;
901+
tracing::debug!("Removing {ROLLBACK_BOOT_LOADER_ENTRIES}");
902+
rustix::fs::unlinkat(&dir, ROLLBACK_BOOT_LOADER_ENTRIES, AtFlags::empty())
903+
.context("unlinkat")?;
906904

907905
tracing::debug!("Syncing to disk");
908906
fsync(

crates/lib/src/install.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ 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_STAGED_DEPLOYMENT_FNAME,
81+
COMPOSEFS_TRANSIENT_STATE_DIR, ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_DIGEST, ORIGIN_KEY_BOOT_TYPE,
82+
STAGED_BOOT_LOADER_ENTRIES, STATE_DIR_ABS, STATE_DIR_RELATIVE, USER_CFG, USER_CFG_STAGED,
83+
};
7984
use crate::containerenv::ContainerExecutionInfo;
8085
use crate::deploy::{
8186
get_sorted_uki_boot_entries, prepare_for_pull, pull_from_prepared, PreparedImportMeta,
82-
PreparedPullResult, USER_CFG, USER_CFG_STAGED,
87+
PreparedPullResult,
8388
};
8489
use crate::kernel_cmdline::Cmdline;
8590
use crate::lsm;
@@ -1496,7 +1501,7 @@ async fn initialize_composefs_repository(
14961501

14971502
rootfs_dir
14981503
.create_dir_all("composefs")
1499-
.context("Creating dir 'composefs'")?;
1504+
.context("Creating dir composefs")?;
15001505

15011506
let repo = open_composefs_repo(rootfs_dir)?;
15021507

@@ -1518,7 +1523,7 @@ async fn initialize_composefs_repository(
15181523
fn get_booted_bls() -> Result<BLSConfig> {
15191524
let cmdline = crate::kernel_cmdline::Cmdline::from_proc()?;
15201525
let booted = cmdline
1521-
.find_str("composefs")
1526+
.find_str(COMPOSEFS_CMDLINE)
15221527
.ok_or_else(|| anyhow::anyhow!("Failed to find composefs parameter in kernel cmdline"))?;
15231528

15241529
for entry in std::fs::read_dir("/sysroot/boot/loader/entries")? {
@@ -1708,10 +1713,10 @@ pub(crate) fn setup_composefs_bls_boot(
17081713

17091714
match &state.composefs_options {
17101715
Some(opt) if opt.insecure => {
1711-
cmdline_options.push_str(&format!(" composefs=?{id_hex}"));
1716+
cmdline_options.push_str(&format!(" {COMPOSEFS_CMDLINE}=?{id_hex}"));
17121717
}
17131718
None | Some(..) => {
1714-
cmdline_options.push_str(&format!(" composefs={id_hex}"));
1719+
cmdline_options.push_str(&format!(" {COMPOSEFS_CMDLINE}={id_hex}"));
17151720
}
17161721
};
17171722

@@ -1723,7 +1728,7 @@ pub(crate) fn setup_composefs_bls_boot(
17231728
vec![
17241729
format!("root=UUID={DPS_UUID}"),
17251730
RW_KARG.to_string(),
1726-
format!("composefs={id_hex}"),
1731+
format!("{COMPOSEFS_CMDLINE}={id_hex}"),
17271732
]
17281733
.join(" "),
17291734
),
@@ -1766,9 +1771,12 @@ pub(crate) fn setup_composefs_bls_boot(
17661771
booted_bls.version = 0; // entries are sorted by their filename in reverse order
17671772

17681773
// This will be atomically renamed to 'loader/entries' on shutdown/reboot
1769-
(boot_dir.join("loader/entries.staged"), Some(booted_bls))
1774+
(
1775+
boot_dir.join(format!("loader/{STAGED_BOOT_LOADER_ENTRIES}")),
1776+
Some(booted_bls),
1777+
)
17701778
} else {
1771-
(boot_dir.join("loader/entries"), None)
1779+
(boot_dir.join(format!("loader/{BOOT_LOADER_ENTRIES}")), None)
17721780
};
17731781

17741782
create_dir_all(&entries_path).with_context(|| format!("Creating {:?}", entries_path))?;
@@ -2130,20 +2138,6 @@ fn setup_composefs_boot(root_setup: &RootSetup, state: &State, image_id: &str) -
21302138
Ok(())
21312139
}
21322140

2133-
pub(crate) const COMPOSEFS_TRANSIENT_STATE_DIR: &str = "/run/composefs";
2134-
/// File created in /run/composefs to record a staged-deployment
2135-
pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";
2136-
2137-
/// Absolute path to composefs-native state directory
2138-
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
2139-
/// Relative path to composefs-native state directory. Relative to /sysroot
2140-
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
2141-
2142-
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";
2143-
pub(crate) const ORIGIN_KEY_BOOT_TYPE: &str = "boot_type";
2144-
/// Key to store the SHA256 sum of vmlinuz + initrd for a deployment
2145-
pub(crate) const ORIGIN_KEY_BOOT_DIGEST: &str = "digest";
2146-
21472141
/// Creates and populates /sysroot/state/deploy/image_id
21482142
#[context("Writing composefs state")]
21492143
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
@@ -28,14 +28,13 @@ use ostree_ext::ostree;
2828
use tokio::io::AsyncReadExt;
2929

3030
use crate::cli::OutputFormat;
31+
use crate::composefs_consts::{
32+
COMPOSEFS_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR,
33+
ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_TYPE, STATE_DIR_RELATIVE,
34+
};
3135
use crate::deploy::get_sorted_bls_boot_entries;
3236
use crate::deploy::get_sorted_uki_boot_entries;
3337
use crate::install::BootType;
34-
use crate::install::ORIGIN_KEY_BOOT;
35-
use crate::install::ORIGIN_KEY_BOOT_TYPE;
36-
use crate::install::{
37-
COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, STATE_DIR_RELATIVE,
38-
};
3938
use crate::spec::ImageStatus;
4039
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType};
4140
use crate::spec::{ImageReference, ImageSignature};
@@ -463,7 +462,7 @@ async fn boot_entry_from_composefs_deployment(
463462
pub(crate) async fn composefs_deployment_status() -> Result<Host> {
464463
let cmdline = crate::kernel_cmdline::Cmdline::from_proc()?;
465464
let composefs_arg = cmdline
466-
.find_str("composefs")
465+
.find_str(COMPOSEFS_CMDLINE)
467466
.ok_or_else(|| anyhow::anyhow!("Failed to find composefs parameter in kernel cmdline"))?;
468467
let booted_image_verity = composefs_arg
469468
.value

0 commit comments

Comments
 (0)