nydus_rootfs/prefetch_files: add prefetch_files for RAFS

A sandbox annotation used to specify prefetch_files.list
path the container image being used, and runtime will pass
it to Hypervisor to search for corresponding prefetch file:
format looks like:
"io.katacontainers.config.hypervisor.prefetch_files.list"
      = /path/to/<uid>/xyz.com/fedora:36/prefetch_file.list

Fixes: #6582

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn
2023-04-06 19:59:00 +08:00
parent 1c6d7cb0f7
commit f3595e48b0
3 changed files with 97 additions and 2 deletions

View File

@@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: Apache-2.0
//
use std::{fs, sync::Arc};
use std::{fs, path::Path, sync::Arc};
use super::{Rootfs, TYPE_OVERLAY_FS};
use crate::{
@@ -28,6 +28,8 @@ const NYDUS_ROOTFS_V6: &str = "v6";
const SNAPSHOT_DIR: &str = "snapshotdir";
const KATA_OVERLAY_DEV_TYPE: &str = "overlayfs";
// nydus prefetch file list name
const NYDUS_PREFETCH_FILE_LIST: &str = "prefetch_file.list";
pub(crate) struct NydusRootfs {
guest_path: String,
@@ -42,6 +44,9 @@ impl NydusRootfs {
cid: &str,
rootfs: &Mount,
) -> Result<Self> {
let prefetch_list_path =
get_nydus_prefetch_files(h.hypervisor_config().await.prefetch_list_path).await;
let share_fs_mount = share_fs.get_share_fs_mount();
let extra_options =
NydusExtraOptions::new(rootfs).context("failed to parse nydus extra options")?;
@@ -59,7 +64,7 @@ impl NydusRootfs {
rafs_meta.to_string(),
rafs_mnt,
extra_options.config.clone(),
None,
prefetch_list_path,
)
.await
.context("failed to do rafs mount")?;
@@ -151,3 +156,67 @@ impl Rootfs for NydusRootfs {
Ok(())
}
}
// Check prefetch files list path, and if invalid, discard it directly.
// As the result of caller `rafs_mount`, it returns `Option<String>`.
async fn get_nydus_prefetch_files(nydus_prefetch_path: String) -> Option<String> {
// nydus_prefetch_path is an annotation and pod with it will indicate
// that prefetch_files will be included.
if nydus_prefetch_path.is_empty() {
info!(sl!(), "nydus prefetch files path not set, just skip it.");
return None;
}
// Ensure the string ends with "/prefetch_files.list"
if !nydus_prefetch_path.ends_with(format!("/{}", NYDUS_PREFETCH_FILE_LIST).as_str()) {
info!(
sl!(),
"nydus prefetch file path no {:?} file exist.", NYDUS_PREFETCH_FILE_LIST
);
return None;
}
// ensure the prefetch_list_path is a regular file.
let prefetch_list_path = Path::new(nydus_prefetch_path.as_str());
if !prefetch_list_path.is_file() {
info!(
sl!(),
"nydus prefetch list file {:?} not a regular file", &prefetch_list_path
);
return None;
}
return Some(prefetch_list_path.display().to_string());
}
#[cfg(test)]
mod tests {
use super::*;
use std::{fs::File, path::PathBuf};
use tempfile::tempdir;
#[tokio::test]
async fn test_get_nydus_prefetch_files() {
let temp_dir = tempdir().unwrap();
let prefetch_list_path01 = temp_dir.path().join("nydus_prefetch_files");
// /tmp_dir/nydus_prefetch_files/
std::fs::create_dir_all(prefetch_list_path01.clone()).unwrap();
// /tmp_dir/nydus_prefetch_files/prefetch_file.list
let prefetch_list_path02 = prefetch_list_path01
.as_path()
.join(NYDUS_PREFETCH_FILE_LIST);
let file = File::create(prefetch_list_path02.clone());
assert!(file.is_ok());
let prefetch_file =
get_nydus_prefetch_files(prefetch_list_path02.as_path().display().to_string()).await;
assert!(prefetch_file.is_some());
assert_eq!(PathBuf::from(prefetch_file.unwrap()), prefetch_list_path02);
drop(file);
temp_dir.close().unwrap_or_default();
}
}