Skip to content

Commit 62c338b

Browse files
authored
Merge pull request #80 from cgwalters/ensure-var
install: Handle missing `/var`
2 parents 7873787 + 57c529b commit 62c338b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

lib/src/install.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ fn require_systemd_pid1() -> Result<()> {
675675
Ok(())
676676
}
677677

678+
// Ensure the `/var` directory exists.
679+
fn ensure_var() -> Result<()> {
680+
std::fs::create_dir_all("/var")?;
681+
Ok(())
682+
}
683+
678684
/// We want to have proper /tmp and /var/tmp without requiring the caller to set them up
679685
/// in advance by manually specifying them via `podman run -v /tmp:/tmp` etc.
680686
/// Unfortunately, it's quite complex right now to "gracefully" dynamically reconfigure
@@ -693,19 +699,24 @@ pub(crate) fn propagate_tmp_mounts_to_host() -> Result<()> {
693699
if path.is_symlink() {
694700
continue;
695701
}
696-
std::os::unix::fs::symlink(&target, &tmp)
697-
.with_context(|| format!("Symlinking {target} to {tmp}"))?;
698-
let cwd = rustix::fs::cwd();
699-
rustix::fs::renameat_with(
700-
cwd,
701-
path.as_os_str(),
702-
cwd,
703-
&tmp,
704-
rustix::fs::RenameFlags::EXCHANGE,
705-
)
706-
.with_context(|| format!("Exchanging {path} <=> {tmp}"))?;
707-
std::fs::rename(&tmp, format!("{path}.old"))
708-
.with_context(|| format!("Renaming old {tmp}"))?;
702+
if path.try_exists()? {
703+
std::os::unix::fs::symlink(&target, &tmp)
704+
.with_context(|| format!("Symlinking {target} to {tmp}"))?;
705+
let cwd = rustix::fs::cwd();
706+
rustix::fs::renameat_with(
707+
cwd,
708+
path.as_os_str(),
709+
cwd,
710+
&tmp,
711+
rustix::fs::RenameFlags::EXCHANGE,
712+
)
713+
.with_context(|| format!("Exchanging {path} <=> {tmp}"))?;
714+
std::fs::rename(&tmp, format!("{path}.old"))
715+
.with_context(|| format!("Renaming old {tmp}"))?;
716+
} else {
717+
std::os::unix::fs::symlink(&target, path)
718+
.with_context(|| format!("Symlinking {target} to {path}"))?;
719+
};
709720
}
710721
Ok(())
711722
}
@@ -723,6 +734,7 @@ async fn prepare_install(
723734
let container_info = crate::containerenv::get_container_execution_info()?;
724735
let source = SourceInfo::from_container(&container_info)?;
725736

737+
ensure_var()?;
726738
propagate_tmp_mounts_to_host()?;
727739

728740
// Even though we require running in a container, the mounts we create should be specific

0 commit comments

Comments
 (0)