Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle the rootfs path with bundle #153

Merged
merged 4 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions oci_spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use caps::Capability;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fs::File;
use std::fs;
use std::path::{Path, PathBuf};

mod linux;
Expand Down Expand Up @@ -65,15 +65,39 @@ impl Default for Spec {
impl Spec {
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref();
let file =
File::open(path).with_context(|| format!("load spec: failed to open {:?}", path))?;
let file = fs::File::open(path)
.with_context(|| format!("load spec: failed to open {:?}", path))?;
let spec: Spec = serde_json::from_reader(&file)?;
Ok(spec)
}

pub fn canonicalize_rootfs(&mut self) -> Result<()> {
self.root.path = std::fs::canonicalize(&self.root.path)
.with_context(|| format!("failed to canonicalize {:?}", self.root.path))?;
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let file = fs::File::create(path)
.with_context(|| format!("save spec: failed to create/open {:?}", path))?;
serde_json::to_writer(&file, self)
.with_context(|| format!("failed to save spec to {:?}", path))?;

Ok(())
}

pub fn canonicalize_rootfs<P: AsRef<Path>>(&mut self, bundle: P) -> Result<()> {
let canonical_root_path = if self.root.path.is_absolute() {
fs::canonicalize(&self.root.path)
.with_context(|| format!("failed to canonicalize {:?}", self.root.path))?
} else {
let canonical_bundle_path = fs::canonicalize(&bundle).context(format!(
"failed to canonicalize bundle: {:?}",
bundle.as_ref()
))?;

fs::canonicalize(&canonical_bundle_path.join(&self.root.path)).context(format!(
"failed to canonicalize rootfs: {:?}",
&self.root.path
))?
};
self.root.path = canonical_root_path;

Ok(())
}
}
17 changes: 6 additions & 11 deletions src/container/init_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use nix::unistd;
use oci_spec::Spec;
use rootless::detect_rootless;
Expand Down Expand Up @@ -102,16 +102,11 @@ impl InitContainerBuilder {
fn load_and_safeguard_spec(&self, container_dir: &Path) -> Result<Spec> {
let source_spec_path = self.bundle.join("config.json");
let target_spec_path = container_dir.join("config.json");
fs::copy(&source_spec_path, &target_spec_path).with_context(|| {
format!(
"failed to copy {:?} to {:?}",
source_spec_path, target_spec_path
)
})?;

let mut spec = oci_spec::Spec::load(&target_spec_path)?;
unistd::chdir(&self.bundle)?;
spec.canonicalize_rootfs()?;

let mut spec = oci_spec::Spec::load(&source_spec_path)?;
spec.canonicalize_rootfs(&self.bundle)?;
spec.save(target_spec_path)?;

Ok(spec)
}

Expand Down