Skip to content

Commit 1b99733

Browse files
Johan-Liebert1cgwalters
authored andcommitted
composefs/state: Name state directory default
Instead of `/sysroot/state/os/fedora` use `/sysroot/state/os/default` as the default state directory. Signed-off-by: Johan-Liebert1 <[email protected]>
1 parent 009ee03 commit 1b99733

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

crates/lib/src/composefs_consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub(crate) const COMPOSEFS_STAGED_DEPLOYMENT_FNAME: &str = "staged-deployment";
1010
pub(crate) const STATE_DIR_ABS: &str = "/sysroot/state/deploy";
1111
/// Relative path to composefs-native state directory. Relative to /sysroot
1212
pub(crate) const STATE_DIR_RELATIVE: &str = "state/deploy";
13+
/// Relative path to the shared 'var' directory. Relative to /sysroot
14+
pub(crate) const SHARED_VAR_PATH: &str = "state/os/default/var";
1315

1416
/// Section in .origin file to store boot related metadata
1517
pub(crate) const ORIGIN_KEY_BOOT: &str = "boot";

crates/lib/src/install.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ use crate::boundimage::{BoundImage, ResolvedBoundImage};
7878
use crate::composefs_consts::{
7979
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, COMPOSEFS_STAGED_DEPLOYMENT_FNAME,
8080
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,
81+
SHARED_VAR_PATH, STAGED_BOOT_LOADER_ENTRIES, STATE_DIR_ABS, STATE_DIR_RELATIVE, USER_CFG,
82+
USER_CFG_STAGED,
8283
};
8384
use crate::containerenv::ContainerExecutionInfo;
8485
use crate::deploy::{
@@ -92,7 +93,7 @@ use crate::progress_jsonl::ProgressWriter;
9293
use crate::spec::ImageReference;
9394
use crate::store::Storage;
9495
use crate::task::Task;
95-
use crate::utils::sigpolicy_from_opt;
96+
use crate::utils::{path_relative_to, sigpolicy_from_opt};
9697
use bootc_mount::{inspect_filesystem, Filesystem};
9798

9899
/// The toplevel boot directory
@@ -2198,11 +2199,15 @@ pub(crate) fn write_composefs_state(
21982199
create_dir_all(state_path.join("etc/upper"))?;
21992200
create_dir_all(state_path.join("etc/work"))?;
22002201

2201-
let actual_var_path = root_path.join(format!("state/os/fedora/var"));
2202+
let actual_var_path = root_path.join(SHARED_VAR_PATH);
22022203
create_dir_all(&actual_var_path)?;
22032204

2204-
symlink(Path::new("../../os/fedora/var"), state_path.join("var"))
2205-
.context("Failed to create symlink for /var")?;
2205+
symlink(
2206+
path_relative_to(state_path.as_std_path(), actual_var_path.as_std_path())
2207+
.context("Getting var symlink path")?,
2208+
state_path.join("var"),
2209+
)
2210+
.context("Failed to create symlink for /var")?;
22062211

22072212
let ImageReference {
22082213
image: image_name,

crates/lib/src/utils.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::future::Future;
21
use std::io::Write;
32
use std::os::fd::BorrowedFd;
3+
use std::path::{Path, PathBuf};
44
use std::process::Command;
55
use std::time::Duration;
6+
use std::{future::Future, path::Component};
67

78
use anyhow::{Context, Result};
89
use bootc_utils::CommandRunExt;
@@ -186,6 +187,28 @@ pub(crate) fn digested_pullspec(image: &str, digest: &str) -> String {
186187
format!("{image}@{digest}")
187188
}
188189

190+
/// Computes a relative path from `from` to `to`.
191+
///
192+
/// Both `from` and `to` must be absolute paths.
193+
pub(crate) fn path_relative_to(from: &Path, to: &Path) -> Result<PathBuf> {
194+
if !from.is_absolute() || !to.is_absolute() {
195+
anyhow::bail!("Paths must be absolute");
196+
}
197+
198+
let from = from.components().collect::<Vec<_>>();
199+
let to = to.components().collect::<Vec<_>>();
200+
201+
let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
202+
203+
let up = std::iter::repeat(Component::ParentDir).take(from.len() - common);
204+
205+
let mut final_path = PathBuf::new();
206+
final_path.extend(up);
207+
final_path.extend(&to[common..]);
208+
209+
return Ok(final_path);
210+
}
211+
189212
#[cfg(test)]
190213
mod tests {
191214
use super::*;
@@ -223,4 +246,21 @@ mod tests {
223246
SignatureSource::ContainerPolicyAllowInsecure
224247
);
225248
}
249+
250+
#[test]
251+
fn test_relative_path() {
252+
let from = Path::new("/sysroot/state/deploy/image_id");
253+
let to = Path::new("/sysroot/state/os/default/var");
254+
255+
assert_eq!(
256+
path_relative_to(from, to).unwrap(),
257+
PathBuf::from("../../os/default/var")
258+
);
259+
assert_eq!(
260+
path_relative_to(&Path::new("state/deploy"), to)
261+
.unwrap_err()
262+
.to_string(),
263+
"Paths must be absolute"
264+
);
265+
}
226266
}

0 commit comments

Comments
 (0)