logo

make-initrd

Unnamed repository; edit this file 'description' to name the repository. git clone https://anongit.hacktivis.me/git/make-initrd.git/

make-initrd.sh (1849B)


  1. #!/bin/sh
  2. # Copyright 2017 Haelwenn (lanodan) Monnier <contact+make-initrd@hacktivis.me>
  3. # SPDX-License-Identifier: ISC
  4. elves="busybox blkid lsblk cryptsetup mksh zfs zpool"
  5. WORKDIR="$(pwd)"
  6. die() {
  7. echo "[Error] die: ${*}"
  8. exit 1
  9. }
  10. [ -n "$1" ] || die "make-initrd takes kernel version as argument"
  11. kver="$1"
  12. modulesdir="/lib/modules/${kver}"
  13. [ -d "$modulesdir" ] || die "${modulesdir}: Directory not found"
  14. out_base="initramfs-${kver}"
  15. if test -e "$out_base"; then
  16. rm -fr "$out_base" || die "Failed: rm -fr $out_base"
  17. fi
  18. mkdir -p "$out_base" || die "Failed: mkdir $out_base"
  19. cd "$out_base" || die "Failed: cd $out_base"
  20. mkdir -p bin lib/modules dev proc sys newroot etc || die "Failed creating base directories"
  21. ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab"
  22. ln -s . usr
  23. ln -s lib lib64
  24. ln -s lib lib32
  25. cp -pr "${modulesdir}" lib/modules/ || die "Failed copying kernel modules"
  26. mknod -m 660 dev/console c 5 1
  27. mknod -m 666 dev/urandom c 1 9
  28. mknod -m 666 dev/random c 1 8
  29. #mknod -m 640 dev/mem c 1 1
  30. mknod -m 666 dev/null c 1 3
  31. mknod -m 666 dev/tty c 5 0
  32. mknod -m 666 dev/zero c 1 5
  33. mknod -m 620 dev/tty1 c 4 1
  34. for elf in $elves; do
  35. origin=$(command -v "$elf")
  36. cp "$origin" bin/ || die
  37. done
  38. if [ -f /root/initrd-keyfile ]; then
  39. cp /root/initrd-keyfile ./keyfile || die "copying keyfile"
  40. fi
  41. cp "${WORKDIR}/init" . || die "copying init"
  42. chmod 755 init || die "init chmod"
  43. ln bin/mksh bin/sh || die "default shell link"
  44. for lib in $(find bin -type f -exec lddtree -l {} + | grep -v bin/ | sort | uniq); do
  45. cp "$lib" lib/ || die
  46. done
  47. if find . -print0 | cpio --null -ov --format=newc | xz -9 --check=crc32 > "../${out_base}.cpio.xz"; then
  48. test -e "/boot/${out_base}.cpio.xz" && mv "/boot/${out_base}.cpio.xz" "/boot/${out_base}.cpio.xz.old"
  49. cp "../${out_base}.cpio.xz" /boot
  50. fi
  51. cd "${WORKDIR}"