mkinitrd

Unnamed repository; edit this file 'description' to name the repository.
git clone https://codeberg.org/emmett1/mkinitrd
Log | Files | Refs

init (2342B)


      1 #!/bin/busybox sh
      2 
      3 /bin/busybox --install -s /bin
      4 
      5 NEWROOT=/newroot
      6 init=/sbin/init
      7 root=
      8 rootdelay=
      9 rootfstype=auto
     10 ro="ro"
     11 rootflags=
     12 device=
     13 
     14 mkdir -p $NEWROOT
     15 mount -t proc     proc  /proc
     16 mount -t sysfs    sysfs /sys
     17 mount -t tmpfs    run   /run
     18 mount -t devtmpfs dev   /dev
     19 
     20 read -r cmdline < /proc/cmdline
     21 
     22 for param in $cmdline; do
     23 	case $param in
     24 		init=*      ) init=${param#init=}             ;;
     25 		root=*      ) root=${param#root=}             ;;
     26 		rootdelay=* ) rootdelay=${param#rootdelay=}   ;;
     27 		rootfstype=*) rootfstype=${param#rootfstype=} ;;
     28 		rootflags=* ) rootflags=${param#rootflags=}   ;;
     29 		ro          ) ro="ro"                         ;;
     30 		rw          ) ro="rw"                         ;;
     31 		shell       ) shell=1                         ;;
     32 	esac
     33 done
     34 
     35 # mdev
     36 [ -f /proc/sys/kernel/hotplug ] && \
     37 	echo $(which mdev) > /proc/sys/kernel/hotplug
     38 mdev -s
     39 
     40 # load modules using modalias
     41 find /sys -name 'modalias' -type f -exec cat '{}' + | sort -u | xargs modprobe -b -a 2>/dev/null
     42 
     43 # load modules specific modules
     44 for m in virtio virtio_ring virtio_pci virtio_blk virtio_scsi nvme \
     45 	ata_piix sd_mod sr_mod cdrom loop squashfs isofs overlay mbcache \
     46 	jbd jbd2 ext2 ext3 ext4 xfs btrfs f2fs reiserfs jfs fat vfat exfat ntfs3; do
     47 	modprobe "$m" 2>/dev/null
     48 done
     49 
     50 [ "$shell" ] && exec sh
     51 
     52 [ "$rootflags" ] && rootflags="$rootflags,"
     53 rootflags="$rootflags$ro"
     54 
     55 case "$root" in
     56 	/dev/*    ) device=$root ;;
     57 	UUID=*    ) eval $root; device="$(blkid | grep "$UUID"  | cut -d : -f1 | head -n1)" ;;
     58 	LABEL=*   ) eval $root; device="$(blkid | grep "$LABEL" | cut -d : -f1 | head -n1)" ;;
     59 	""        ) echo "!! no root device specified" ; exec sh;;
     60 esac
     61 
     62 if [ "$rootdelay" ]; then
     63 	while [ ! -b "$device" ] && [ $i -lt "$rootdelay" ]; do
     64 		echo ">> waiting for $device..."
     65 		sleep 1
     66 		i=$((i+1))
     67 	done
     68 fi
     69 
     70 while [ ! -b "$device" ] ; do
     71 	echo "!! device '$device' not found"
     72 	exec sh
     73 done
     74 
     75 if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" $NEWROOT; then
     76 	echo "!! failed to mount device '$device'"
     77 	exec sh
     78 fi
     79 
     80 [ "$shell" ] && exec sh
     81 
     82 mount --move /sys $NEWROOT/sys
     83 mount --move /proc $NEWROOT/proc
     84 mount --move /dev $NEWROOT/dev
     85 mount --move /run $NEWROOT/run
     86 
     87 # switch to newroot
     88 exec /bin/switch_root $NEWROOT $init $@
     89 
     90 echo ">> this is the end of initramfs"
     91 echo ">> nothing further, here's the shell"
     92 /bin/busybox sh