Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ pub fn create_dir(path: &str, mode: AccessPermission) -> io::Result<()> {
})
}

/// Creates a directory and creates all missing parent directories as well.
fn create_dir_recursive(path: &str, mode: AccessPermission) -> io::Result<()> {
trace!("create_dir_recursive: {path}");
create_dir(path, mode).or_else(|errno| {
if errno != Errno::Badf {
return Err(errno);
}
let (parent_path, _file_name) = path.rsplit_once('/').unwrap();
create_dir_recursive(parent_path, mode)?;
create_dir(path, mode)
})
}

/// Returns an vector with all the entries within a directory.
pub fn readdir(name: &str) -> io::Result<Vec<DirectoryEntry>> {
debug!("Read directory {name}");
Expand Down
57 changes: 47 additions & 10 deletions src/fs/uhyve.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::ffi::CString;
use alloc::string::{String, ToString};
Expand All @@ -14,9 +15,11 @@ use uhyve_interface::parameters::{
use uhyve_interface::{GuestPhysAddr, GuestVirtAddr, Hypercall};

use crate::arch::mm::paging;
use crate::env::fdt;
use crate::errno::Errno;
use crate::fs::{
self, AccessPermission, FileAttr, NodeKind, ObjectInterface, OpenOption, SeekWhence, VfsNode,
create_dir_recursive,
};
use crate::io;
use crate::syscalls::interfaces::uhyve::uhyve_hypercall;
Expand Down Expand Up @@ -229,14 +232,48 @@ impl VfsNode for UhyveDirectory {

pub(crate) fn init() {
info!("Try to initialize uhyve filesystem");
let mount_point = hermit_var_or!("UHYVE_MOUNT", "/root").to_string();
info!("Mounting uhyve filesystem at {mount_point}");
fs::FILESYSTEM
.get()
.unwrap()
.mount(
&mount_point,
Box::new(UhyveDirectory::new(Some(mount_point.clone()))),
)
.expect("Mount failed. Duplicate mount_point?");
let mount_str = fdt().and_then(|fdt| {
fdt.find_node("/uhyve,mounts")
.and_then(|node| node.property("mounts"))
.and_then(|property| property.as_str())
});
if let Some(mount_str) = mount_str {
assert_ne!(mount_str.len(), 0, "Invalid /uhyve,mounts node in FDT");
for mount_point in mount_str.split('\0') {
info!("Mounting uhyve filesystem at {mount_point}");

if let Err(errno) = fs::FILESYSTEM.get().unwrap().mount(
mount_point,
Box::new(UhyveDirectory::new(Some(mount_point.to_owned()))),
) {
assert_eq!(errno, Errno::Badf);
debug!(
"Mounting of {mount_point} failed with {errno:?}. Creating missing parent folders"
);
let (parent_path, _file_name) = mount_point.rsplit_once('/').unwrap();
create_dir_recursive(parent_path, AccessPermission::S_IRWXU).unwrap();

fs::FILESYSTEM
.get()
.unwrap()
.mount(
mount_point,
Box::new(UhyveDirectory::new(Some(mount_point.to_owned()))),
)
.unwrap();
}
}
} else {
// No FDT -> Uhyve legacy mounting (to /root)
let mount_point = hermit_var_or!("UHYVE_MOUNT", "/root").to_string();
info!("Mounting uhyve filesystem at {mount_point}");
fs::FILESYSTEM
.get()
.unwrap()
.mount(
&mount_point,
Box::new(UhyveDirectory::new(Some(mount_point.clone()))),
)
.expect("Mount failed. Duplicate mount_point?");
}
}