Skip to content
Open
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 @@ -402,6 +402,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, _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: 46 additions & 11 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,10 +15,11 @@ use uhyve_interface::parameters::{
use uhyve_interface::{GuestPhysAddr, GuestVirtAddr, Hypercall};

use crate::arch::mm::paging;
use crate::env::is_uhyve;
use crate::env::{self, is_uhyve};
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 @@ -231,15 +233,48 @@ impl VfsNode for UhyveDirectory {
pub(crate) fn init() {
info!("Try to initialize uhyve filesystem");
if is_uhyve() {
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 = env::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.trim_end_matches('\0').split('\0') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already done in as_str in my previous suggestion.

Suggested change
for mount_point in mount_str.trim_end_matches('\0').split('\0') {
for mount_point in mount_str.split('\0') {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work, as the last element in mount_str also ends on \0, thus creating an empy string for mount_point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I mentioned the previous suggestion. as_str removes the trailing null.

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()))),
) {
debug!(
"Mounting of {mount_point} failed with {errno:?}. Creating missing parent folders"
);
Comment on lines +250 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the error and only attempt recovery on EBADF similar to create_dir_recursive?

let (parent, _file_name) = path.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?");
}
}
}
Loading