Coder Social home page Coder Social logo

Add initramfs support about asterinas HOT 5 CLOSED

asterinas avatar asterinas commented on July 23, 2024
Add initramfs support

from asterinas.

Comments (5)

tatetian avatar tatetian commented on July 23, 2024

A few crates or tools that are probably useful for initramfs.

mkinitramfs command

cpio-rs crate

miniz_oxide crate

  • DEFLATE/zlib encoder/decoder
  • https://crates.io/crates/miniz_oxide
  • Pros
    • Supports no_std
    • Features no unsafe
  • Cons
    • Don't know if this crate supports Lempel-Ziv coding (LZ77), which is used by gzip.
    • The libflate crate is based on miniz_oxide and documented to support LZ77. But libflate crate is not no_std.

from asterinas.

liqinggd avatar liqinggd commented on July 23, 2024

The Linux kernel will create a tmpfs file system, extract the contents of the archive on it, and then launch the init script located in the root of the tmpfs file system. This script then mounts the real root file system.

Here is a simple initramfs example:

#!/bin/sh

cat > hello.c << EOF
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  printf("Hello world!\n");
  sleep(999999999);
}
EOF
gcc -static hello.c -o init
echo init | cpio -o -H newc | gzip > test.cpio.gz
# Testing external initramfs using the initrd loading mechanism.
qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zero

The mkintramfs script:

#!/bin/sh

# Copyright 2006 Rob Landley <[email protected]> and TimeSys Corporation.
# Licensed under GPL version 2

if [ $# -ne 2 ]
then
echo "usage: mkinitramfs directory imagename.cpio.gz"
exit 1
fi

if [ -d "$1" ]
then
echo "creating $2 from $1"
(cd "$1"; find . | cpio -o -H newc | gzip) > "$2"
else
echo "First argument must be a directory"
exit 1
fi

As we can see, the format of cpio file is in newc format." newc is short for New portable format and CRC format.
Each file has a 110-byte header, a variable length NULL-terminated filename, and variable length file data.
A header for the filename "TRAILER!!!" indicates the end of the archive.
image

from asterinas.

liqinggd avatar liqinggd commented on July 23, 2024
  1. Implement a no_std crate to decode the newc format cpio archive.
  2. Use the decoded information from cpio to prepare fs in ramfs.
  3. investigate how to load the initramfs-image file from the -initrd parameter.
  4. implement a no_std crate to unzip the gzip(.gz) file.

from asterinas.

liqinggd avatar liqinggd commented on July 23, 2024
  1. investigate how to load the initramfs image file from the -initrd parameter.

Usually, it is the bootloader's capability to load the initramfs image into memory, then pass the address and length to the kernel.

The Rust bootloader implemented the ramdisk support in January(rust-osdev/bootloader#308), it can load the ramdisk image. So we have to upgrade the bootloader to use this feature.

from asterinas.

tatetian avatar tatetian commented on July 23, 2024

Done

from asterinas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.