From ad7e5029cf1a5577a620c250782cac8bbea4af5e Mon Sep 17 00:00:00 2001 From: davidovski Date: Sat, 12 Mar 2022 22:33:23 +0000 Subject: added mkinitramfs --- Makefile | 17 ++++++-- src/initramfs-init.sh | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/mkinitramfs.sh | 77 +++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 src/initramfs-init.sh create mode 100755 src/mkinitramfs.sh diff --git a/Makefile b/Makefile index 6f25547..812a6a4 100755 --- a/Makefile +++ b/Makefile @@ -16,9 +16,6 @@ install-headers: src/*.h install-shtests: src/shtests.sh install -m755 src/shtests.sh ${DESTDIR}${PREFIX}/bin/shtests -install-chroot: src/xichroot.sh - install -m755 src/xichroot.sh ${DESTDIR}${PREFIX}/bin/xichroot - install-parseconf: src/parseconf.sh install -m755 src/parseconf.sh ${DESTDIR}${PREFIX}/bin/parseconf @@ -40,3 +37,17 @@ build-hbar: src/hbar.c install-colors clean: rm -r bin + + +# xichroot +# +install-chroot: src/xichroot.sh + install -m755 src/xichroot.sh ${DESTDIR}${PREFIX}/bin/xichroot + + +# initramfs +# +install-mkinitramfs: src/mkinitramfs.sh src/initramfs-init.sh + install -m755 src/mkinitramfs.sh ${DESTDIR}${PREFIX}/bin/mkinitramfs + install -m755 src/initramfs-init.sh ${DESTDIR}${PREFIX}/bin/initramfs-init + diff --git a/src/initramfs-init.sh b/src/initramfs-init.sh new file mode 100644 index 0000000..2466e0d --- /dev/null +++ b/src/initramfs-init.sh @@ -0,0 +1,108 @@ +#!/bin/sh + +PATH=/bin:/usr/bin:/sbin:/usr/sbin +export PATH + +problem() +{ + printf "Encountered a problem!\n\nDropping you to a shell.\n\n" + sh +} + +no_device() +{ + printf "The device %s, which is supposed to contain the\n" $1 + printf "root file system, does not exist.\n" + printf "Please fix this problem and exit this shell.\n\n" +} + +no_mount() +{ + printf "Could not mount device %s\n" $1 + printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n" + printf "Maybe the device is formatted with an unsupported file system?\n\n" + printf "Or maybe filesystem type autodetection went wrong, in which case\n" + printf "you should add the rootfstype=... parameter to the kernel command line.\n\n" + printf "Available partitions:\n" +} + +do_mount_root() +{ + mkdir /.root + [ -n "$rootflags" ] && rootflags="$rootflags," + rootflags="$rootflags$ro" + + case "$root" in + /dev/* ) device=$root ;; + UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;; + LABEL=*) eval $root; device="/dev/disk/by-label/$LABEL" ;; + "" ) echo "No root device specified." ; problem ;; + esac + + while [ ! -b "$device" ] ; do + no_device $device + problem + done + + if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" /.root ; then + no_mount $device + cat /proc/partitions + while true ; do sleep 10000 ; done + else + echo "Successfully mounted device $root" + fi +} + +init=/sbin/init +root= +rootdelay= +rootfstype=auto +ro="ro" +rootflags= +device= + +mount -n -t devtmpfs devtmpfs /dev +mount -n -t proc proc /proc +mount -n -t sysfs sysfs /sys +mount -n -t tmpfs tmpfs /run + +read -r cmdline < /proc/cmdline + +for param in $cmdline ; do + case $param in + init=* ) init=${param#init=} ;; + root=* ) root=${param#root=} ;; + rootdelay=* ) rootdelay=${param#rootdelay=} ;; + rootfstype=*) rootfstype=${param#rootfstype=} ;; + rootflags=* ) rootflags=${param#rootflags=} ;; + ro ) ro="ro" ;; + rw ) ro="rw" ;; + esac +done + +# udevd location depends on version +if [ -x /sbin/udevd ]; then + UDEVD=/sbin/udevd +elif [ -x /lib/udev/udevd ]; then + UDEVD=/lib/udev/udevd +elif [ -x /lib/systemd/systemd-udevd ]; then + UDEVD=/lib/systemd/systemd-udevd +else + echo "Cannot find udevd nor systemd-udevd" + problem +fi + +${UDEVD} --daemon --resolve-names=never +udevadm trigger +udevadm settle + +if [ -f /etc/mdadm.conf ] ; then mdadm -As ; fi +if [ -x /sbin/vgchange ] ; then /sbin/vgchange -a y > /dev/null ; fi +if [ -n "$rootdelay" ] ; then sleep "$rootdelay" ; fi + +do_mount_root + +killall -w ${UDEVD##*/} + +exec switch_root /.root "$init" "$@" + diff --git a/src/mkinitramfs.sh b/src/mkinitramfs.sh new file mode 100755 index 0000000..c3d00be --- /dev/null +++ b/src/mkinitramfs.sh @@ -0,0 +1,77 @@ +#!/bin/sh + +BUILD=/tmp/ramfs +OUT=/boot/initrd.img + + +# create base directory structure +printf "creating directories..." + +mkdir -p $BUILD/tmp +cd $BUILD +mkdir -p bin dev etc lib proc run sbin sys usr +mkdir -p lib/firmware lib/modules lib/udev +mkdir -p etc/modprobe.d etc/udev + +ln -s lib lib64 +ln -s ../bin usr/bin +ln -s ../sbin usr/sbin +printf "OK\n" + +# copy binaries +printf "populating /bin..." +for b in dash cat cp dd killall kmod ln ls mkdir \ + mknod mount rm sed sh sleep umount uname \ + basename readlink; do + cp -a /bin/$b bin/ +done + +ln -s kmod bin/insmod +ln -s kmod bin/lsmod +printf "OK\n" + +# create nodes for udev to mount +printf "creating nodes..." +mknod -m 600 dev/console c 5 1 +mknod -m 666 dev/null c 1 3 +printf "OK\n" + +# Copy udev configuration +printf "configuring udev..." +cp /etc/udev/udev.conf etc/udev/ +cp -r /etc/udev/rules.d etc/udev/ +cp -r /lib/udev/* lib/udev/ +printf "OK\n" + +# set module order +touch etc/modprobe.d/modprobe.conf + +# copy the init script +printf "installing init script..." +cp /sbin/initramfs-init sbin/init +printf "OK\n" + +# copy required libraries +printf "populating /lib..." +for l in libc acl attre blkid cap kmod lzma mount ncursesw readline z curses history terminfo zstd; do + cp /lib/lib$l.so lib/ +done +ln -s libc.so /ld-musl-$(uname -m).so.1 +printf "OK\n" + +# copy any firmware +printf "installing firmware..." +cp -rv /lib/firmware/* lib/firmware/ +printf "OK\n" + +# copy any modules +printf "installing modules..." +cp -rv /lib/modules/* lib/modules/ +printf "OK\n" + +# create the initramfs image +printf "creating initramfs image..." +find ./ | cpio -o -H newc | gzip -9 > $OUT +printf "OK\n" + +printf "Written initramfs image to $OUT\n" -- cgit v1.2.1