fix: 修正 libos 找 rootfs 的方式

This commit is contained in:
YdrMaster 2022-05-11 11:13:05 +08:00
parent e70eecb5ba
commit fa155fe95b
4 changed files with 47 additions and 41 deletions

View File

@ -41,11 +41,10 @@ doc:
clean:
cargo clean
find zCore -maxdepth 1 -name "*.img" -delete
rm -rf rootfs
rm -rf riscv_rootfs
find zCore -maxdepth 1 -name "*.img" -delete
rt-test:
cd rootfs && git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/clrkwllms/rt-tests --depth 1
cd rootfs/rt-tests && make
cd rootfs/x86_64 && git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/clrkwllms/rt-tests --depth 1
cd rootfs/x86_64/rt-tests && make
echo x86 gcc build rt-test,now need manual modificy.

View File

@ -13,7 +13,7 @@ async fn main() {
}
let envs = vec!["PATH=/usr/sbin:/usr/bin:/sbin:/bin".into()];
let rootfs_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../rootfs");
let rootfs_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("../rootfs/x86_64");
let hostfs = rcore_fs_hostfs::HostFS::new(rootfs_path);
let proc = zcore_loader::linux::run(args[1..].to_vec(), envs, hostfs);

View File

@ -1,13 +1,15 @@
use rcore_fs_hostfs::HostFS;
use std::fs;
const LIBOS_ROOTFS: &str = "../rootfs/x86_64";
/// test with cmd line
async fn test(cmdline: &str) -> i64 {
kernel_hal::init();
let args: Vec<String> = cmdline.split(' ').map(|s| s.into()).collect();
let envs = vec!["PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/x86_64-alpine-linux-musl/bin".into()]; // TODO
let hostfs = HostFS::new("../rootfs");
let hostfs = HostFS::new(LIBOS_ROOTFS);
let proc = zcore_loader::linux::run(args, envs, hostfs);
proc.wait_for_exit().await
}
@ -45,24 +47,26 @@ async fn test_dir() {
#[async_std::test]
async fn test_create_remove_file() {
let test_file = format!("{LIBOS_ROOTFS}/testfile");
test("/bin/busybox rm testfile").await; // can't remove
fs::read("../rootfs/testfile").unwrap_err();
fs::read(&test_file).unwrap_err();
test("/bin/busybox touch testfile").await;
fs::read("../rootfs/testfile").unwrap();
fs::read(&test_file).unwrap();
test("/bin/busybox touch testfile").await;
fs::read("../rootfs/testfile").unwrap();
fs::read(&test_file).unwrap();
test("/bin/busybox rm testfile").await;
fs::read("../rootfs/testfile").unwrap_err();
fs::read(&test_file).unwrap_err();
}
#[async_std::test]
async fn test_create_remove_dir() {
let test = format!("{LIBOS_ROOTFS}/test");
test("/bin/busybox rmdir test").await; // can't remove
fs::read_dir("../rootfs/test").unwrap_err();
fs::read_dir(&test).unwrap_err();
test("/bin/busybox mkdir test").await;
fs::read_dir("../rootfs/test").unwrap();
fs::read_dir(&test).unwrap();
test("/bin/busybox rmdir test").await;
fs::read_dir("../rootfs/test").unwrap_err();
fs::read_dir(&test).unwrap_err();
}
#[async_std::test]
@ -73,22 +77,24 @@ async fn test_readfile() {
#[async_std::test]
async fn test_cp_mv() {
let hostname = format!("{LIBOS_ROOTFS}/etc/hostname.bak");
test("/bin/busybox cp /etc/hostnama /etc/hostname.bak").await; // can't move
fs::read("../rootfs/etc/hostname.bak").unwrap_err();
fs::read(&hostname).unwrap_err();
test("/bin/busybox cp /etc/hostname /etc/hostname.bak").await;
fs::read("../rootfs/etc/hostname.bak").unwrap();
fs::read(&hostname).unwrap();
test("/bin/busybox mv /etc/hostname.bak /etc/hostname.mv").await;
fs::read("../rootfs/etc/hostname.bak").unwrap_err();
fs::read(&hostname).unwrap_err();
}
#[async_std::test]
async fn test_link() {
let hostname = format!("{LIBOS_ROOTFS}/etc/hostname.ln");
test("/bin/busybox ln /etc/hostnama /etc/hostname.ln").await; // can't ln
fs::read("../rootfs/etc/hostname.ln").unwrap_err();
fs::read(&hostname).unwrap_err();
test("/bin/busybox ln /etc/hostname /etc/hostname.ln").await;
fs::read("../rootfs/etc/hostname.ln").unwrap();
fs::read(&hostname).unwrap();
test("/bin/busybox unlink /etc/hostname.ln").await;
fs::read("../rootfs/etc/hostname.ln").unwrap_err();
fs::read(&hostname).unwrap_err();
}
#[async_std::test]
@ -109,7 +115,7 @@ async fn test_sleep() {
#[async_std::test]
async fn test_truncate() {
assert_eq!(test("/bin/busybox truncate -s 12 testtruncate").await, 0);
fs::read("../rootfs/testtruncate").unwrap();
fs::read(format!("{LIBOS_ROOTFS}/testtruncate")).unwrap();
}
#[async_std::test]

View File

@ -1,22 +1,3 @@
#![allow(dead_code)]
fn init_ram_disk() -> Option<&'static mut [u8]> {
if cfg!(feature = "link-user-img") {
extern "C" {
fn _user_img_start();
fn _user_img_end();
}
Some(unsafe {
core::slice::from_raw_parts_mut(
_user_img_start as *mut u8,
_user_img_end as usize - _user_img_start as usize,
)
})
} else {
kernel_hal::boot::init_ram_disk()
}
}
cfg_if! {
if #[cfg(feature = "linux")] {
use alloc::sync::Arc;
@ -24,12 +5,14 @@ cfg_if! {
#[cfg(feature = "libos")]
pub fn rootfs() -> Arc<dyn FileSystem> {
let base = if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
let mut rootfs = if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
std::path::Path::new(&dir).join("..")
} else {
std::env::current_dir().unwrap()
};
rcore_fs_hostfs::HostFS::new(base.join("rootfs"))
rootfs.push("rootfs");
rootfs.push(std::env::consts::ARCH);
rcore_fs_hostfs::HostFS::new(rootfs)
}
#[cfg(not(feature = "libos"))]
@ -61,6 +44,24 @@ cfg_if! {
}
}
#[cfg(not(feature = "libos"))]
fn init_ram_disk() -> Option<&'static mut [u8]> {
if cfg!(feature = "link-user-img") {
extern "C" {
fn _user_img_start();
fn _user_img_end();
}
Some(unsafe {
core::slice::from_raw_parts_mut(
_user_img_start as *mut u8,
_user_img_end as usize - _user_img_start as usize,
)
})
} else {
kernel_hal::boot::init_ram_disk()
}
}
// Hard link rootfs img
#[cfg(not(feature = "libos"))]
#[cfg(feature = "link-user-img")]