Skip to content

Commit 461b2c8

Browse files
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 3d63b81 commit 461b2c8

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

crates/lib/src/composefs_consts.rs

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

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

crates/lib/src/install.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ use crate::boundimage::{BoundImage, ResolvedBoundImage};
7979
use crate::composefs_consts::{
8080
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, COMPOSEFS_INSECURE_CMDLINE,
8181
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,
82+
ORIGIN_KEY_BOOT_DIGEST, ORIGIN_KEY_BOOT_TYPE, SHARED_VAR_PATH, STAGED_BOOT_LOADER_ENTRIES,
83+
STATE_DIR_ABS, STATE_DIR_RELATIVE, USER_CFG, USER_CFG_STAGED,
8484
};
8585
use crate::containerenv::ContainerExecutionInfo;
8686
use crate::deploy::{
@@ -93,7 +93,7 @@ use crate::progress_jsonl::ProgressWriter;
9393
use crate::spec::ImageReference;
9494
use crate::store::Storage;
9595
use crate::task::Task;
96-
use crate::utils::sigpolicy_from_opt;
96+
use crate::utils::{path_relative_to, sigpolicy_from_opt};
9797
use bootc_mount::{inspect_filesystem, Filesystem};
9898

9999
/// The toplevel boot directory
@@ -2189,11 +2189,15 @@ pub(crate) fn write_composefs_state(
21892189
create_dir_all(state_path.join("etc/upper"))?;
21902190
create_dir_all(state_path.join("etc/work"))?;
21912191

2192-
let actual_var_path = root_path.join(format!("state/os/fedora/var"));
2192+
let actual_var_path = root_path.join(SHARED_VAR_PATH);
21932193
create_dir_all(&actual_var_path)?;
21942194

2195-
symlink(Path::new("../../os/fedora/var"), state_path.join("var"))
2196-
.context("Failed to create symlink for /var")?;
2195+
symlink(
2196+
path_relative_to(state_path.as_std_path(), actual_var_path.as_std_path())
2197+
.context("Getting var symlink path")?,
2198+
state_path.join("var"),
2199+
)
2200+
.context("Failed to create symlink for /var")?;
21972201

21982202
let ImageReference {
21992203
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;
@@ -194,6 +195,28 @@ pub(crate) fn digested_pullspec(image: &str, digest: &str) -> String {
194195
format!("{image}@{digest}")
195196
}
196197

198+
/// Computes a relative path from `from` to `to`.
199+
///
200+
/// Both `from` and `to` must be absolute paths.
201+
pub(crate) fn path_relative_to(from: &Path, to: &Path) -> Result<PathBuf> {
202+
if !from.is_absolute() || !to.is_absolute() {
203+
anyhow::bail!("Paths must be absolute");
204+
}
205+
206+
let from = from.components().collect::<Vec<_>>();
207+
let to = to.components().collect::<Vec<_>>();
208+
209+
let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
210+
211+
let up = std::iter::repeat(Component::ParentDir).take(from.len() - common);
212+
213+
let mut final_path = PathBuf::new();
214+
final_path.extend(up);
215+
final_path.extend(&to[common..]);
216+
217+
return Ok(final_path);
218+
}
219+
197220
#[cfg(test)]
198221
mod tests {
199222
use super::*;
@@ -231,4 +254,21 @@ mod tests {
231254
SignatureSource::ContainerPolicyAllowInsecure
232255
);
233256
}
257+
258+
#[test]
259+
fn test_relative_path() {
260+
let from = Path::new("/sysroot/state/deploy/image_id");
261+
let to = Path::new("/sysroot/state/os/default/var");
262+
263+
assert_eq!(
264+
path_relative_to(from, to).unwrap(),
265+
PathBuf::from("../../os/default/var")
266+
);
267+
assert_eq!(
268+
path_relative_to(&Path::new("state/deploy"), to)
269+
.unwrap_err()
270+
.to_string(),
271+
"Paths must be absolute"
272+
);
273+
}
234274
}

0 commit comments

Comments
 (0)