Skip to content

Commit

Permalink
Merge pull request kata-containers#10643 from justxuewei/fix-bind-vol
Browse files Browse the repository at this point in the history
runtime-rs & agent: Fix the issues with bind volumes
  • Loading branch information
lifupan authored Dec 12, 2024
2 parents 372346b + 3fb91dd commit 07fe732
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/agent/rustjail/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn init_rootfs(
// bind may be only specified in the oci spec options -> flags update r#type
let m = &{
let mut mbind = m.clone();
if mbind.typ().is_none() && flags & MsFlags::MS_BIND == MsFlags::MS_BIND {
if is_none_mount_type(mbind.typ()) && flags & MsFlags::MS_BIND == MsFlags::MS_BIND {
mbind.set_typ(Some("bind".to_string()));
}
mbind
Expand Down Expand Up @@ -397,6 +397,13 @@ fn mount_cgroups_v2(cfd_log: RawFd, m: &Mount, rootfs: &str, flags: MsFlags) ->
Ok(())
}

fn is_none_mount_type(typ: &Option<String>) -> bool {
match typ {
Some(t) => t == "none",
None => true,
}
}

fn mount_cgroups(
cfd_log: RawFd,
m: &Mount,
Expand Down
17 changes: 15 additions & 2 deletions src/libs/kata-sys-util/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use std::time::Instant;
use lazy_static::lazy_static;
use nix::mount::{mount, MntFlags, MsFlags};
use nix::{unistd, NixPath};
use oci_spec::runtime as oci;

use crate::fs::is_symlink;
use crate::sl;
Expand Down Expand Up @@ -799,8 +800,20 @@ pub fn get_mount_options(options: &Option<Vec<String>>) -> Vec<String> {
}
}

pub fn get_mount_type(typ: &Option<String>) -> String {
typ.clone().unwrap_or("bind".to_string())
pub fn get_mount_type(m: &oci::Mount) -> String {
m.typ()
.clone()
.map(|typ| {
if typ.as_str() == "none" {
if let Some(opts) = m.options() {
if opts.iter().any(|opt| opt == "bind" || opt == "rbind") {
return "bind".to_string();
}
}
}
typ
})
.unwrap_or("bind".to_string())
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl ShareFsMount for VirtiofsShareMount {
guest_path,
storages,
});
} else if get_mount_type(config.mount.typ()).as_str() == mount::KATA_EPHEMERAL_VOLUME_TYPE {
} else if get_mount_type(&config.mount).as_str() == mount::KATA_EPHEMERAL_VOLUME_TYPE {
// refer to the golang `handleEphemeralStorage` code at
// https://github.com/kata-containers/kata-containers/blob/9516286f6dd5cfd6b138810e5d7c9e01cf6fc043/src/runtime/virtcontainers/kata_agent.go#L1354

Expand Down
2 changes: 1 addition & 1 deletion src/runtime-rs/crates/resource/src/volume/direct_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub(crate) async fn handle_direct_volume(
}

pub(crate) fn is_direct_volume(m: &oci::Mount) -> Result<bool> {
let mnt_type = get_mount_type(m.typ());
let mnt_type = get_mount_type(m);
let mount_type = mnt_type.as_str();

// Filter the non-bind volume and non-direct-vol volume
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl SPDKVolume {
.context("generate host-guest shared path failed")?;
storage.mount_point = guest_path.clone();

if get_mount_type(m.typ()).as_str() != "bind" {
if get_mount_type(m).as_str() != "bind" {
storage.fs_type = mount_info.fs_type.clone();
} else {
storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl VfioVolume {
.context("generate host-guest shared path failed")?;
storage.mount_point = guest_path.clone();

if get_mount_type(m.typ()).as_str() != "bind" {
if get_mount_type(m).as_str() != "bind" {
storage.fs_type = mount_info.fs_type.clone();
} else {
storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string();
Expand Down
4 changes: 2 additions & 2 deletions src/runtime-rs/crates/resource/src/volume/share_fs_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ impl Volume for ShareFsVolume {
}

pub(crate) fn is_share_fs_volume(m: &oci::Mount) -> bool {
(get_mount_type(m.typ()).as_str() == "bind"
|| get_mount_type(m.typ()).as_str() == mount::KATA_EPHEMERAL_VOLUME_TYPE)
let mount_type = get_mount_type(m);
(mount_type == "bind" || mount_type == mount::KATA_EPHEMERAL_VOLUME_TYPE)
&& !is_host_device(&get_mount_path(&Some(m.destination().clone())))
&& !is_system_mount(&get_mount_path(m.source()))
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime-rs/crates/resource/src/volume/shm_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ impl Volume for ShmVolume {

pub(crate) fn is_shm_volume(m: &oci::Mount) -> bool {
get_mount_path(&Some(m.destination().clone())).as_str() == "/dev/shm"
&& get_mount_type(m.typ()).as_str() != KATA_EPHEMERAL_DEV_TYPE
&& get_mount_type(m).as_str() != KATA_EPHEMERAL_DEV_TYPE
}

0 comments on commit 07fe732

Please sign in to comment.