logo

make-initrd

Unnamed repository; edit this file 'description' to name the repository.

make-initrd.bash (1889B)


      1 #!/bin/bash
      2 # Copyright 2017-2018 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
      3 # Distributed under the terms of the ISC license
      4 
      5 kv=${1:-$(uname -r)}
      6 arc=$(getconf LONG_BIT)
      7 out_base=initramfs-${kv}
      8 elves="lvm busybox blkid lsblk cryptsetup mksh zfs zpool"
      9 WORKDIR="$(pwd)"
     10 
     11 function docp() {
     12 	local link=${1} prefix
     13 	[[ -n ${link} ]] || return
     14 	cp -a {,.}${link}
     15 
     16 	[[ -h ${link} ]] &&
     17 	while true; do
     18 		prefix=${link%/*}
     19 			link=$(readlink ${link})
     20 			[[ ${link%/*} == ${link} ]] && link=${prefix}/${link}
     21 			cp -a {,.}${link} || die
     22 			[[ -h ${link} ]] || break
     23 	done
     24 
     25 	return 0
     26 }
     27 
     28 test -e $out_base && rm -fr $out_base
     29 mkdir -p $out_base && pushd $out_base
     30 mkdir -p {usr/,}{bin,lib$arc} dev proc sys newroot etc
     31 ln -s lib{$arc,}
     32 ln -s /proc/mounts etc/mtab
     33 pushd usr && ln -s lib{$arc,} && popd
     34 
     35 mkdir -p lib/modules ; cp -pr /lib/modules/$kv lib/modules
     36 
     37 mknod -m 600 dev/console c 5 1
     38 mknod -m 666 dev/urandom c 1 9
     39 mknod -m 666 dev/random  c 1 8
     40 mknod -m 640 dev/mem     c 1 1
     41 mknod -m 666 dev/null    c 1 3
     42 mknod -m 666 dev/tty     c 5 0
     43 mknod -m 666 dev/zero    c 1 5
     44 mknod -m 640 dev/tty1    c 4 1
     45 
     46 [[ $(echo "$kv" | cut -d'.' -f1 ) -eq 3 ]] &&
     47 	[[ $(echo "$kv" | cut -d'.' -f2) -ge 1 ]] &&
     48 	mknod -m 600 /dev/loop-control c 10 237
     49 
     50 for i in $elves; do
     51 	cp $(which $i) bin
     52 done
     53 
     54 cp "${WORKDIR}/init" . && chmod 755 init || exit
     55 ln bin/mksh bin/sh
     56 bin/busybox --install usr/bin
     57 
     58 for bin in $(find bin); do
     59 	ldd $bin >/dev/null || continue
     60 	for lib in $(ldd $bin | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' \
     61 		-e 's,.*(/lib.*/ld.*.so.*) .*,\1,p')
     62 	do
     63 		mkdir -p ${lib%/*} && docp {,.}$lib
     64 	done
     65 done
     66 
     67 if find . -print0 | cpio --null -ov --format=newc | xz -9 --check=crc32 > ../${out_base}.cpio.xz; then
     68 	test -e /boot/${out_base}.cpio.xz && mv /boot/${out_base}.cpio.xz /boot/${out_base}.cpio.xz.old
     69 	cp ../${out_base}.cpio.xz /boot
     70 fi
     71 
     72 popd
     73 
     74 unset -v arc kv out_base elves