Skip to content

Commit

Permalink
feat(vmm): implemented automatic generation of rootfs with initramfs
Browse files Browse the repository at this point in the history
  • Loading branch information
MurielParaire committed Apr 25, 2024
1 parent 2338841 commit 5520619
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
46 changes: 35 additions & 11 deletions src/vmm/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::core::vmm::VMM;
use crate::VmmErrors;
use std::{
convert::From,
env::current_dir,
net::Ipv4Addr,
path::{Path, PathBuf},
process::{Command, Stdio},
Expand Down Expand Up @@ -42,9 +43,16 @@ impl VmmServiceTrait for VmmService {
const HOST_IP: Ipv4Addr = Ipv4Addr::new(172, 29, 0, 1);
const HOST_NETMASK: Ipv4Addr = Ipv4Addr::new(255, 255, 0, 0);

// get current directory
let mut curr_dir = current_dir()?;

// define kernel path
let mut kernel_entire_path = curr_dir.as_os_str().to_owned();
kernel_entire_path.push("/tools/kernel/linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin");

// Check if the kernel is on the system, else build it
if !Path::new("./tools/kernel/linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin")
.exists()
let kernel_exists = Path::new(&kernel_entire_path).try_exists().expect(&format!("Could not access folder {:?}", &kernel_entire_path));
if !kernel_exists
{
info!("Kernel not found, building kernel");
// Execute the script using sh and capture output and error streams
Expand All @@ -56,19 +64,35 @@ impl VmmServiceTrait for VmmService {
.expect("Failed to execute the kernel build script");

// Print output and error streams
error!("Script output: {}", String::from_utf8_lossy(&output.stdout));
info!("Script output: {}", String::from_utf8_lossy(&output.stdout));
error!("Script errors: {}", String::from_utf8_lossy(&output.stderr));
};
let kernel_path = Path::new(&kernel_entire_path);

// define initramfs file placement
let initramfs_entire_file_path = curr_dir.as_mut_os_string();
initramfs_entire_file_path.push("/tools/rootfs/initramfs.img");

// Check if the initramfs is on the system, else build it
let initramfs_exists = Path::new(&initramfs_entire_file_path).try_exists().expect(&format!("Could not access folder {:?}", &initramfs_entire_file_path));
if !initramfs_exists
{
info!("Initramfs not found, building initramfs");
// Execute the script using sh and capture output and error streams
let output = Command::new("sh")
.arg("./tools/rootfs/mkrootfs.sh")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.expect("Failed to execute the initramfs build script");

let kernel_path = &Path::new(
"./tools/kernel/linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin",
);
let mut initramfs_path: PathBuf = PathBuf::new();

// Todo - Check if the initramfs for the specified language is on the system, else build it
initramfs_path.push("./tools/rootfs/initramfs.img");
// Print output and error streams
info!("Script output: {}", String::from_utf8_lossy(&output.stdout));
error!("Script errors: {}", String::from_utf8_lossy(&output.stderr));
};
let initramfs_path = PathBuf::from(&initramfs_entire_file_path);

// // Create a new VMM
// Create a new VMM
let mut vmm = VMM::new(HOST_IP, HOST_NETMASK).map_err(VmmErrors::VmmNew)?;

// Configure the VMM parameters might need to be calculated rather than hardcoded
Expand Down
2 changes: 2 additions & 0 deletions tools/kernel/linux-config-x86_64
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,8 @@ CONFIG_VIRTIO_NET=y
# CONFIG_PPP is not set
CONFIG_SLIP=y
CONFIG_SLIP_COMPRESSED=y
CONFIG_SLIP_SMART=n
CONFIG_SLIP_MODE_SLIP6=n

#
# Host-side USB support is needed for USB Network Adapter support
Expand Down
4 changes: 2 additions & 2 deletions tools/kernel/mkkernel.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/bash

LINUX_REPO=linux-cloud-hypervisor
cd ./tools/kernel/

if [ ! -d $LINUX_REPO ]
then
git clone --depth 1 "https://github.com/cloud-hypervisor/linux.git" -b "ch-6.2" $LINUX_REPO
fi

pushd $LINUX_REPO
cd $LINUX_REPO
cp ../linux-config-x86_64 .config
KCFLAGS="-Wa,-mx86-used-note=no" make bzImage -j `nproc`
popd
47 changes: 10 additions & 37 deletions tools/rootfs/mkrootfs.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
#!/usr/bin/bash

if [ ! -d alpine-minirootfs ]
# test if its already in src folder => fs-gen available
# launch from src folder at the same level as tools
if [ -d src ]
then
curl -O https://dl-cdn.alpinelinux.org/alpine/v3.14/releases/x86_64/alpine-minirootfs-3.14.2-x86_64.tar.gz

mkdir alpine-minirootfs
tar xf alpine-minirootfs-3.14.2-x86_64.tar.gz -C alpine-minirootfs
cd src
fi

pushd alpine-minirootfs
cat > init <<EOF
#! /bin/sh
#
# /init executable file in the initramfs
#
mount -t devtmpfs dev /dev
mount -t proc proc /proc
mount -t sysfs sysfs /sys
ip link set up dev lo
slattach -L /dev/ttyS1&
while ! ifconfig sl0 &> /dev/null; do
sleep 1
done
ifconfig sl0 172.30.0.11 netmask 255.255.0.0 up
exec /sbin/getty -n -l /bin/sh 115200 /dev/console
poweroff -f
EOF

chmod +x init

find . -print0 |
cpio --null --create --verbose --owner root:root --format=newc |
xz -9 --format=lzma > ../initramfs.img

popd
if [ -d fs-gen ]
then
cargo run --bin fs-gen -- alpine:latest ./fs-gen/test -o ../tools/rootfs/initramfs.img
else
echo "Module fs-gen not found"
fi

0 comments on commit 5520619

Please sign in to comment.