Refine the name about initramfs

The bootloader loads the initramfs, then the kernel unpacks it to rootfs.
This commit is contained in:
LI Qing 2023-08-01 10:52:41 +08:00 committed by Tate, Hongliang Tian
parent 55267f0d81
commit 7de44a0e0e
7 changed files with 39 additions and 25 deletions

View File

@ -7,6 +7,6 @@ set timeout=1
menuentry 'jinux' { menuentry 'jinux' {
multiboot2 /boot/jinux #KERNEL_COMMAND_LINE# multiboot2 /boot/jinux #KERNEL_COMMAND_LINE#
module2 --nounzip /boot/ramdisk.cpio.gz module2 --nounzip /boot/initramfs.cpio.gz
boot boot
} }

View File

@ -156,8 +156,8 @@ fn create_bootdev_image(path: PathBuf, kcmdline: &str) -> String {
kcmdline, kcmdline,
); );
fs::copy( fs::copy(
"regression/build/ramdisk.cpio.gz", "regression/build/initramfs.cpio.gz",
"target/iso_root/boot/ramdisk.cpio.gz", "target/iso_root/boot/initramfs.cpio.gz",
) )
.unwrap(); .unwrap();

View File

@ -2,7 +2,7 @@ MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
CUR_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH))) CUR_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
BUILD_DIR := $(CUR_DIR)/build BUILD_DIR := $(CUR_DIR)/build
INITRAMFS := $(BUILD_DIR)/initramfs INITRAMFS := $(BUILD_DIR)/initramfs
RAMDISK := $(BUILD_DIR)/ramdisk.cpio.gz INITRAMFS_IMAGE := $(BUILD_DIR)/initramfs.cpio.gz
SHELL := /bin/bash SHELL := /bin/bash
INITRAMFS_EMPTY_DIRS := \ INITRAMFS_EMPTY_DIRS := \
$(INITRAMFS)/etc \ $(INITRAMFS)/etc \
@ -59,14 +59,14 @@ $(INITRAMFS)/opt/syscall_test:
# If the BUILD_SYSCALL_TEST variable is set, we should depend on the # If the BUILD_SYSCALL_TEST variable is set, we should depend on the
# sub make output to do incremental building. # sub make output to do incremental building.
ifeq ($(BUILD_SYSCALL_TEST), 1) ifeq ($(BUILD_SYSCALL_TEST), 1)
$(RAMDISK): $(INITRAMFS_ALL_DIRS) $(INITRAMFS)/opt/syscall_test $(INITRAMFS_IMAGE): $(INITRAMFS_ALL_DIRS) $(INITRAMFS)/opt/syscall_test
else else
$(RAMDISK): $(INITRAMFS_ALL_DIRS) $(INITRAMFS_IMAGE): $(INITRAMFS_ALL_DIRS)
endif endif
@echo "Generating the ramdisk image..." @echo "Generating the initramfs image..."
@(cd $(INITRAMFS); find . | cpio -o -H newc | gzip) > $@ @(cd $(INITRAMFS); find . | cpio -o -H newc | gzip) > $@
build: $(RAMDISK) build: $(INITRAMFS_IMAGE)
clean: clean:
@rm -rf $(BUILD_DIR) @rm -rf $(BUILD_DIR)

View File

@ -3,16 +3,11 @@ use alloc::str;
use super::file_table::FileDescripter; use super::file_table::FileDescripter;
use super::inode_handle::InodeHandle; use super::inode_handle::InodeHandle;
use super::ramfs::RamFS; use super::rootfs::root_mount;
use super::utils::{ use super::utils::{
AccessMode, CreationFlags, Dentry, InodeMode, InodeType, MountNode, StatusFlags, PATH_MAX, AccessMode, CreationFlags, Dentry, InodeMode, InodeType, StatusFlags, PATH_MAX, SYMLINKS_MAX,
SYMLINKS_MAX,
}; };
lazy_static! {
static ref ROOT_MOUNT: Arc<MountNode> = MountNode::new_root(RamFS::new(true)).unwrap();
}
#[derive(Debug)] #[derive(Debug)]
pub struct FsResolver { pub struct FsResolver {
root: Arc<Dentry>, root: Arc<Dentry>,
@ -31,8 +26,8 @@ impl Clone for FsResolver {
impl FsResolver { impl FsResolver {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
root: ROOT_MOUNT.root_dentry().clone(), root: root_mount().root_dentry().clone(),
cwd: ROOT_MOUNT.root_dentry().clone(), cwd: root_mount().root_dentry().clone(),
} }
} }

View File

@ -3,9 +3,9 @@ pub mod epoll;
pub mod file_handle; pub mod file_handle;
pub mod file_table; pub mod file_table;
pub mod fs_resolver; pub mod fs_resolver;
pub mod initramfs;
pub mod inode_handle; pub mod inode_handle;
pub mod pipe; pub mod pipe;
pub mod procfs; pub mod procfs;
pub mod ramfs; pub mod ramfs;
pub mod rootfs;
pub mod utils; pub mod utils;

View File

@ -3,18 +3,21 @@ use crate::prelude::*;
use super::fs_resolver::{FsPath, FsResolver}; use super::fs_resolver::{FsPath, FsResolver};
use super::procfs::ProcFS; use super::procfs::ProcFS;
use super::ramfs::RamFS; use super::ramfs::RamFS;
use super::utils::{InodeMode, InodeType}; use super::utils::{InodeMode, InodeType, MountNode};
use cpio_decoder::{CpioDecoder, FileType}; use cpio_decoder::{CpioDecoder, FileType};
use lending_iterator::LendingIterator; use lending_iterator::LendingIterator;
use libflate::gzip::Decoder as GZipDecoder; use libflate::gzip::Decoder as GZipDecoder;
use spin::Once;
/// Unpack and prepare the fs from the ramdisk CPIO buffer. /// Unpack and prepare the rootfs from the initramfs CPIO buffer.
pub fn init(gzip_ramdisk_buf: &[u8]) -> Result<()> { pub fn init(initramfs_buf: &[u8]) -> Result<()> {
println!("[kernel] unzipping ramdisk.cpio.gz ..."); init_root_mount()?;
println!("[kernel] unpacking the initramfs.cpio.gz to rootfs ...");
let fs = FsResolver::new(); let fs = FsResolver::new();
let mut decoder = CpioDecoder::new( let mut decoder = CpioDecoder::new(
GZipDecoder::new(gzip_ramdisk_buf) GZipDecoder::new(initramfs_buf)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?, .map_err(|_| Error::with_message(Errno::EINVAL, "invalid gzip buffer"))?,
); );
@ -74,7 +77,23 @@ pub fn init(gzip_ramdisk_buf: &[u8]) -> Result<()> {
// Mount DevFS // Mount DevFS
let dev_dentry = fs.lookup(&FsPath::try_from("/dev")?)?; let dev_dentry = fs.lookup(&FsPath::try_from("/dev")?)?;
dev_dentry.mount(RamFS::new(false))?; dev_dentry.mount(RamFS::new(false))?;
println!("[kernel] initramfs is ready"); println!("[kernel] rootfs is ready");
Ok(()) Ok(())
} }
static ROOT_MOUNT: Once<Arc<MountNode>> = Once::new();
fn init_root_mount() -> Result<()> {
ROOT_MOUNT.try_call_once(|| -> Result<Arc<MountNode>> {
let rootfs = RamFS::new(true);
let root_mount = MountNode::new_root(rootfs)?;
Ok(root_mount)
})?;
Ok(())
}
pub fn root_mount() -> &'static Arc<MountNode> {
ROOT_MOUNT.get().unwrap()
}

View File

@ -52,7 +52,7 @@ pub fn init() {
driver::init(); driver::init();
net::init(); net::init();
process::fifo_scheduler::init(); process::fifo_scheduler::init();
fs::initramfs::init(boot::initramfs()).unwrap(); fs::rootfs::init(boot::initramfs()).unwrap();
device::init().unwrap(); device::init().unwrap();
} }