Skip to content

Commit

Permalink
Merge pull request #158 from Burning1020/fix-storage
Browse files Browse the repository at this point in the history
vmm: support readonly option of volume
  • Loading branch information
abel-von authored Aug 14, 2024
2 parents e826a96 + a13d21c commit e89aa15
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
15 changes: 11 additions & 4 deletions vmm/sandbox/src/container/handler/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use containerd_sandbox::{
Sandbox,
};
use log::debug;
use vmm_common::{storage::ANNOTATION_KEY_STORAGE, DEV_SHM, STORAGE_FILE_PREFIX};
use vmm_common::{
storage::{Storage, ANNOTATION_KEY_STORAGE},
DEV_SHM, STORAGE_FILE_PREFIX,
};

use crate::{
container::handler::Handler, sandbox::KuasarSandbox, storage::mount::is_bind_shm,
Expand Down Expand Up @@ -55,14 +58,18 @@ where
let rootfs = &container.data.rootfs;

let mut handled_mounts = vec![];
let mut storages = vec![];
let mut storages: Vec<&Storage> = vec![];

for mut m in mounts {
if let Some(storage) = sandbox.storages.iter().find(|x| x.is_for_mount(&m)) {
debug!("found storage {:?} for mount {:?}", storage, m);
m.source.clone_from(&storage.mount_point);
m.options = vec!["bind".to_string()];
if storage.need_guest_handle {
m.options.push("bind".to_string());
if storage.need_guest_handle
&& !storages
.iter()
.any(|s| s.host_source == storage.host_source && s.r#type == storage.r#type)
{
storages.push(storage);
}
}
Expand Down
28 changes: 24 additions & 4 deletions vmm/sandbox/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where
};

let mut storage = Storage {
host_source: source.clone(),
host_source: m.source.clone(),
r#type: m.r#type.clone(),
id: id.to_string(),
device_id: Some(device_id.to_string()),
Expand Down Expand Up @@ -161,6 +161,13 @@ where
} else {
m.source.clone()
};

let options = if m.options.contains(&"ro".to_string()) {
vec!["ro".to_string()]
} else {
vec![]
};

let host_dest = format!("{}/{}", self.get_sandbox_shared_path(), &storage_id);
debug!("bind mount storage for mount {:?}, dest: {}", m, &host_dest);
let source_path = Path::new(&*source);
Expand Down Expand Up @@ -188,7 +195,7 @@ where
driver: "".to_string(),
driver_options: vec![],
fstype: "bind".to_string(),
options: vec![],
options,
mount_point: format!("{}/{}", KUASAR_STATE_DIR, &storage_id),
};

Expand All @@ -209,6 +216,13 @@ where
m
)));
}

let options = if m.options.contains(&"ro".to_string()) {
vec!["ro".to_string()]
} else {
vec![]
};

let host_dest = format!("{}/{}", self.get_sandbox_shared_path(), &storage_id);
debug!("overlay mount storage for {:?}, dest: {}", m, &host_dest);
tokio::fs::create_dir_all(&host_dest).await?;
Expand All @@ -226,7 +240,7 @@ where
driver: "".to_string(),
driver_options: vec![],
fstype: "bind".to_string(),
options: vec![],
options,
mount_point: format!("{}/{}", KUASAR_STATE_DIR, &storage_id),
};

Expand All @@ -242,6 +256,12 @@ where
m: &Mount,
mount_info: &MountInfo,
) -> Result<()> {
let options = if m.options.contains(&"ro".to_string()) {
vec!["ro".to_string()]
} else {
vec![]
};

let mut storage = Storage {
host_source: m.source.clone(),
r#type: m.r#type.clone(),
Expand All @@ -253,7 +273,7 @@ where
driver: DRIVEREPHEMERALTYPE.to_string(),
driver_options: vec![],
fstype: "tmpfs".to_string(),
options: vec![],
options,
mount_point: format!("{}{}", KUASAR_GUEST_SHARE_DIR, storage_id),
};
// only handle size option because other options may not supported in guest
Expand Down
2 changes: 1 addition & 1 deletion vmm/sandbox/src/storage/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn get_file_type<P: AsRef<Path>>(path: P) -> Result<FileType> {
let real_path = match tokio::fs::canonicalize(path).await {
Ok(rp) => rp,
Err(e) => {
return Err(e.into());
return Err(anyhow!("get file type {}", e).into());
}
};

Expand Down

0 comments on commit e89aa15

Please sign in to comment.