mkinitrd

Cool project repo :D
git clone git://git.emmett1.my/mkinitrd.git
Log | Files | Refs

commit 81966b4393623e7f701213b369df0d177696d236
parent 3f0d5ef3d03678a62ec3f0c4cff9e16dc3c35638
Author: emmett1 <emmett1.2miligrams@protonmail.com>
Date:   Sun,  7 Sep 2025 11:55:44 +0800

updated

Diffstat:
Minit | 53++++++++++++++++++++++++++---------------------------
Mmkinitrd | 20+++++++++++++++-----
2 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/init b/init @@ -2,11 +2,7 @@ /bin/busybox --install -s /bin -clear - NEWROOT=/newroot -INITRAMFS=/run/initramfs -MEDIA=$INITRAMFS/media init=/sbin/init root= rootdelay= @@ -14,8 +10,6 @@ rootfstype=auto ro="ro" rootflags= device= -resume= -noresume=false mkdir -p $NEWROOT mount -t proc proc /proc @@ -32,8 +26,6 @@ for param in $cmdline; do rootdelay=* ) rootdelay=${param#rootdelay=} ;; rootfstype=*) rootfstype=${param#rootfstype=} ;; rootflags=* ) rootflags=${param#rootflags=} ;; - resume=* ) resume=${param#resume=} ;; - noresume ) noresume=true ;; ro ) ro="ro" ;; rw ) ro="rw" ;; shell ) shell=1 ;; @@ -45,49 +37,56 @@ done echo $(which mdev) > /proc/sys/kernel/hotplug mdev -s -# load kernel modules +# load modules using modalias find /sys -name 'modalias' -type f -exec cat '{}' + | sort -u | xargs modprobe -b -a 2>/dev/null -for k in ext4 xfs btrfs f2fs jfs reiserfs vfat; do - modprobe $k 2>/dev/null +# load modules specific modules +for m in virtio virtio_ring virtio_pci virtio_blk virtio_scsi nvme \ + ata_piix sd_mod sr_mod cdrom loop squashfs isofs overlay mbcache \ + jbd jbd2 ext2 ext3 ext4 xfs btrfs f2fs reiserfs jfs fat vfat exfat ntfs3; do + modprobe "$m" 2>/dev/null done -[ "$shell" ] && sh +[ "$shell" ] && exec sh [ "$rootflags" ] && rootflags="$rootflags," rootflags="$rootflags$ro" case "$root" in /dev/* ) device=$root ;; - UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;; - PARTUUID=*) eval $root; device="/dev/disk/by-partuuid/$PARTUUID" ;; - LABEL=* ) eval $root; device="/dev/disk/by-label/$LABEL" ;; - "" ) echo "No root device specified." ; sh;; + UUID=* ) eval $root; device="$(blkid | grep "$UUID" | cut -d : -f1 | head -n1)" ;; + LABEL=* ) eval $root; device="$(blkid | grep "$LABEL" | cut -d : -f1 | head -n1)" ;; + "" ) echo "!! no root device specified" ; exec sh;; esac +if [ "$rootdelay" ]; then + while [ ! -b "$device" ] && [ $i -lt "$rootdelay" ]; do + echo ">> waiting for $device..." + sleep 1 + i=$((i+1)) + done +fi + while [ ! -b "$device" ] ; do - echo "device '$device' not found" - sh + echo "!! device '$device' not found" + exec sh done if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" $NEWROOT; then - echo "failed to mount device '$device'" - cat /proc/partitions - sh + echo "!! failed to mount device '$device'" + exec sh fi -[ "$shell" ] && sh +[ "$shell" ] && exec sh mount --move /sys $NEWROOT/sys mount --move /proc $NEWROOT/proc mount --move /dev $NEWROOT/dev mount --move /run $NEWROOT/run - # switch to newroot -clear -exec /bin/switch_root $NEWROOT /sbin/init $@ +exec /bin/switch_root $NEWROOT $init $@ -echo "This is the end of initramfs" -echo "Nothing further, here's the shell" +echo ">> this is the end of initramfs" +echo ">> nothing further, here's the shell" /bin/busybox sh diff --git a/mkinitrd b/mkinitrd @@ -1,17 +1,18 @@ #!/bin/sh +# busybox based initramfs script add_kmod() { while [ "$1" ]; do kmod=$1; shift - path=$(busybox modinfo -k $K -F filename $kmod 2>/dev/null) + path=$(modinfo -k $K -F filename $kmod 2>/dev/null) [ "$path" ] || continue [ -f $W/$path ] && continue add_file $path - for firmware in $(busybox modinfo -k $K -F firmware $kmod); do + for firmware in $(modinfo -k $K -F firmware $kmod); do [ -f /lib/firmware/$firmware.xz ] && add_file /lib/firmware/$firmware.xz [ -f /lib/firmware/$firmware ] && add_file /lib/firmware/$firmware done - for depends in $(busybox modinfo -k $K -F depends $kmod | tr ',' ' '); do + for depends in $(modinfo -k $K -F depends $kmod | tr ',' ' '); do [ "$depends" ] || continue add_kmod $depends done @@ -103,6 +104,14 @@ if [ ! "$(command -v busybox)" ]; then exit 1 fi +# create temporary bin path for busybox's applet +# we trying to use busybox utils as possible +mkdir -p /tmp/busybox-bin.$$ +for a in $(busybox --list); do + ln -s $(command -v busybox) /tmp/busybox-bin.$$/$a +done +export PATH=/tmp/busybox-bin.$$:$PATH + if [ ! -f "$I" ]; then echo "error: init file '$I' not found" exit 1 @@ -121,6 +130,7 @@ ln -s lib $W/lib64 if [ "$A" ]; then for i in /sys/module/*; do add_kmod ${i##*/} + echo ${i##*/} >> $W/modules done else for d in \ @@ -137,7 +147,7 @@ else fi add_file $M/modules.builtin add_file $M/modules.order -busybox depmod -b $W $K +depmod -b $W $K add_bin busybox add_file $I / 755 @@ -146,6 +156,6 @@ echo '$MODALIAS=.* 0:0 660 @modprobe "$MODALIAS' > $W/etc/mdev.conf ( cd $W ; find . | cpio -o -H newc --quiet | gzip -9 ) > "$O" du -sh "$O" -rm -rf $W +rm -rf $W /tmp/busybox-bin.$$ exit 0