Merge pull request #10643 from justxuewei/fix-bind-vol

runtime-rs & agent: Fix the issues with bind volumes
This commit is contained in:
Fupan Li 2024-12-12 11:34:52 +08:00 committed by GitHub
commit 07fe7325c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 30 additions and 10 deletions

View File

@ -233,7 +233,7 @@ pub fn init_rootfs(
// bind may be only specified in the oci spec options -> flags update r#type // bind may be only specified in the oci spec options -> flags update r#type
let m = &{ let m = &{
let mut mbind = m.clone(); 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.set_typ(Some("bind".to_string()));
} }
mbind mbind
@ -397,6 +397,13 @@ fn mount_cgroups_v2(cfd_log: RawFd, m: &Mount, rootfs: &str, flags: MsFlags) ->
Ok(()) Ok(())
} }
fn is_none_mount_type(typ: &Option<String>) -> bool {
match typ {
Some(t) => t == "none",
None => true,
}
}
fn mount_cgroups( fn mount_cgroups(
cfd_log: RawFd, cfd_log: RawFd,
m: &Mount, m: &Mount,

View File

@ -53,6 +53,7 @@ use std::time::Instant;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use nix::mount::{mount, MntFlags, MsFlags}; use nix::mount::{mount, MntFlags, MsFlags};
use nix::{unistd, NixPath}; use nix::{unistd, NixPath};
use oci_spec::runtime as oci;
use crate::fs::is_symlink; use crate::fs::is_symlink;
use crate::sl; use crate::sl;
@ -799,8 +800,20 @@ pub fn get_mount_options(options: &Option<Vec<String>>) -> Vec<String> {
} }
} }
pub fn get_mount_type(typ: &Option<String>) -> String { pub fn get_mount_type(m: &oci::Mount) -> String {
typ.clone().unwrap_or("bind".to_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)] #[cfg(test)]

View File

@ -120,7 +120,7 @@ impl ShareFsMount for VirtiofsShareMount {
guest_path, guest_path,
storages, 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 // refer to the golang `handleEphemeralStorage` code at
// https://github.com/kata-containers/kata-containers/blob/9516286f6dd5cfd6b138810e5d7c9e01cf6fc043/src/runtime/virtcontainers/kata_agent.go#L1354 // https://github.com/kata-containers/kata-containers/blob/9516286f6dd5cfd6b138810e5d7c9e01cf6fc043/src/runtime/virtcontainers/kata_agent.go#L1354

View File

@ -98,7 +98,7 @@ pub(crate) async fn handle_direct_volume(
} }
pub(crate) fn is_direct_volume(m: &oci::Mount) -> Result<bool> { 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(); let mount_type = mnt_type.as_str();
// Filter the non-bind volume and non-direct-vol volume // Filter the non-bind volume and non-direct-vol volume

View File

@ -125,7 +125,7 @@ impl SPDKVolume {
.context("generate host-guest shared path failed")?; .context("generate host-guest shared path failed")?;
storage.mount_point = guest_path.clone(); 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(); storage.fs_type = mount_info.fs_type.clone();
} else { } else {
storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string(); storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string();

View File

@ -80,7 +80,7 @@ impl VfioVolume {
.context("generate host-guest shared path failed")?; .context("generate host-guest shared path failed")?;
storage.mount_point = guest_path.clone(); 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(); storage.fs_type = mount_info.fs_type.clone();
} else { } else {
storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string(); storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string();

View File

@ -308,8 +308,8 @@ impl Volume for ShareFsVolume {
} }
pub(crate) fn is_share_fs_volume(m: &oci::Mount) -> bool { pub(crate) fn is_share_fs_volume(m: &oci::Mount) -> bool {
(get_mount_type(m.typ()).as_str() == "bind" let mount_type = get_mount_type(m);
|| get_mount_type(m.typ()).as_str() == mount::KATA_EPHEMERAL_VOLUME_TYPE) (mount_type == "bind" || mount_type == mount::KATA_EPHEMERAL_VOLUME_TYPE)
&& !is_host_device(&get_mount_path(&Some(m.destination().clone()))) && !is_host_device(&get_mount_path(&Some(m.destination().clone())))
&& !is_system_mount(&get_mount_path(m.source())) && !is_system_mount(&get_mount_path(m.source()))
} }

View File

@ -115,5 +115,5 @@ impl Volume for ShmVolume {
pub(crate) fn is_shm_volume(m: &oci::Mount) -> bool { pub(crate) fn is_shm_volume(m: &oci::Mount) -> bool {
get_mount_path(&Some(m.destination().clone())).as_str() == "/dev/shm" 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
} }