make-initrd.sh (1849B)
- #!/bin/sh
- # Copyright 2017 Haelwenn (lanodan) Monnier <contact+make-initrd@hacktivis.me>
- # SPDX-License-Identifier: ISC
- elves="busybox blkid lsblk cryptsetup mksh zfs zpool"
- WORKDIR="$(pwd)"
- die() {
- echo "[Error] die: ${*}"
- exit 1
- }
- [ -n "$1" ] || die "make-initrd takes kernel version as argument"
- kver="$1"
- modulesdir="/lib/modules/${kver}"
- [ -d "$modulesdir" ] || die "${modulesdir}: Directory not found"
- out_base="initramfs-${kver}"
- if test -e "$out_base"; then
- rm -fr "$out_base" || die "Failed: rm -fr $out_base"
- fi
- mkdir -p "$out_base" || die "Failed: mkdir $out_base"
- cd "$out_base" || die "Failed: cd $out_base"
- mkdir -p bin lib/modules dev proc sys newroot etc || die "Failed creating base directories"
- ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab"
- ln -s . usr
- ln -s lib lib64
- ln -s lib lib32
- cp -pr "${modulesdir}" lib/modules/ || die "Failed copying kernel modules"
- mknod -m 660 dev/console c 5 1
- mknod -m 666 dev/urandom c 1 9
- mknod -m 666 dev/random c 1 8
- #mknod -m 640 dev/mem c 1 1
- mknod -m 666 dev/null c 1 3
- mknod -m 666 dev/tty c 5 0
- mknod -m 666 dev/zero c 1 5
- mknod -m 620 dev/tty1 c 4 1
- for elf in $elves; do
- origin=$(command -v "$elf")
- cp "$origin" bin/ || die
- done
- if [ -f /root/initrd-keyfile ]; then
- cp /root/initrd-keyfile ./keyfile || die "copying keyfile"
- fi
- cp "${WORKDIR}/init" . || die "copying init"
- chmod 755 init || die "init chmod"
- ln bin/mksh bin/sh || die "default shell link"
- for lib in $(find bin -type f -exec lddtree -l {} + | grep -v bin/ | sort | uniq); do
- cp "$lib" lib/ || die
- done
- if find . -print0 | cpio --null -ov --format=newc | xz -9 --check=crc32 > "../${out_base}.cpio.xz"; then
- test -e "/boot/${out_base}.cpio.xz" && mv "/boot/${out_base}.cpio.xz" "/boot/${out_base}.cpio.xz.old"
- cp "../${out_base}.cpio.xz" /boot
- fi
- cd "${WORKDIR}"