Skip to content

Commit df27770

Browse files
committed
Support mounting rootfs from virtio-blk via initrd
Due to the 1 GiB kernel space limit on RV32-Linux, embedding large root filesystems in the kernel image can be impractical for certain testing scenarios. This commit introduces a script to generate a mountable ext4 rootfs image and modifies the init script to support switching the root filesystem to a virtio-blk device. During boot, the init script checks whether /dev/vda contains a valid rootfs. If so, it switches the root to the virtio-blk device. Otherwise, it falls back to the default initramfs.
1 parent f3e994b commit df27770

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

scripts/build-image.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function do_buildroot
2424
ASSERT git clone https://github.com/buildroot/buildroot -b 2024.11.1 --depth=1
2525
cp -f configs/buildroot.config buildroot/.config
2626
cp -f configs/busybox.config buildroot/busybox.config
27+
cp -f target/init buildroot/fs/cpio/init
2728
# Otherwise, the error below raises:
2829
# You seem to have the current working directory in your
2930
# LD_LIBRARY_PATH environment variable. This doesn't work.

scripts/rootfs_ext4.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/bash
2+
3+
ROOTFS_CPIO="rootfs_full.cpio"
4+
IMG="rootfs.ext4.img"
5+
IMG_SIZE=$((1024 * 1024 * 1024)) # 1GB
6+
IMG_SIZE_BLOCKS=$((${IMG_SIZE} / 4096)) # IMG_SIZE / 4k
7+
8+
DIR=rootfs
9+
10+
echo "[*] Remove old rootfs directory ..."
11+
rm -rf $DIR
12+
mkdir -p $DIR
13+
14+
echo "[*] Extract CPIO"
15+
pushd $DIR
16+
cpio -idmv < ../$ROOTFS_CPIO
17+
popd
18+
19+
echo "[*] Create empty image"
20+
dd if=/dev/zero of=${IMG} bs=4k count=${IMG_SIZE_BLOCKS}
21+
22+
echo "[*] Create ext4 rootfs image"
23+
fakeroot mkfs.ext4 -F ${IMG} -d $DIR
24+
25+
# Show image size
26+
du -h ${IMG}

target/init

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
3+
4+
ROOTFS_DEV=/dev/vda
5+
6+
mount -t devtmpfs devtmpfs /dev
7+
mount -t proc none /proc
8+
mount -t sysfs none /sys
9+
10+
# use the /dev/console device node from devtmpfs if possible to not
11+
# confuse glibc's ttyname_r().
12+
# This may fail (e.g., booted with console=), and errors from exec will
13+
# terminate the shell, so use a subshell for the test
14+
if (exec 0</dev/console) 2>/dev/null; then
15+
exec 0</dev/console
16+
exec 1>/dev/console
17+
exec 2>/dev/console
18+
fi
19+
20+
echo "[*] Attempting to mount $ROOTFS_DEV"
21+
mkdir -p /mnt
22+
23+
mount -t ext4 $ROOTFS_DEV /mnt
24+
if [ $? -ne 0 ]; then
25+
echo "[!] Failed to mount $ROOTFS_DEV. Using initramfs."
26+
exec /sbin/init "$@"
27+
fi
28+
29+
echo "[*] $ROOTFS_DEV mounted successfully. Checking for root filesystem..."
30+
if [ -x /mnt/sbin/init ]; then
31+
echo "[*] Valid root filesystem found. Switching root to $ROOTFS_DEV"
32+
#cd /mnt
33+
mount --move /sys /mnt/sys
34+
mount --move /proc /mnt/proc
35+
mount --move /dev /mnt/dev
36+
exec switch_root -c /dev/console /mnt /sbin/init "$@"
37+
else
38+
echo "[!] No valid root filesystem found on $ROOTFS_DEV. Using initramfs."
39+
umount /mnt
40+
exec /sbin/init "$@"
41+
fi

0 commit comments

Comments
 (0)