logo

make-initrd

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

make-initrd.sh (1697B)


  1. #!/bin/sh
  2. # Copyright 2017-2019 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
  3. # Distributed under the terms of the ISC license
  4. out_base=initramfs-all
  5. elves="lvm busybox blkid lsblk cryptsetup mksh zfs zpool"
  6. WORKDIR="$(pwd)"
  7. die() {
  8. echo "[Error] die: ${*}"
  9. exit 1
  10. }
  11. if test -e "$out_base"; then
  12. rm -fr "$out_base" || die "Failed: rm -fr $out_base"
  13. fi
  14. mkdir -p "$out_base" || die "Failed: mkdir $out_base"
  15. cd "$out_base" || die "Failed: cd $out_base"
  16. mkdir -p usr/bin bin lib dev proc sys newroot etc || die "Failed creating base directories"
  17. ln -s /proc/mounts etc/mtab || die "Failed symlink for /etc/mtab"
  18. if test -d "/lib/modules"
  19. then
  20. cp -pr "/lib/modules" lib || die "Failed copying kernel modules"
  21. else
  22. echo "[Warning] “/lib/modules” doesn’t exist"
  23. fi
  24. mknod -m 600 dev/console c 5 1
  25. mknod -m 666 dev/urandom c 1 9
  26. mknod -m 666 dev/random c 1 8
  27. mknod -m 640 dev/mem c 1 1
  28. mknod -m 666 dev/null c 1 3
  29. mknod -m 666 dev/tty c 5 0
  30. mknod -m 666 dev/zero c 1 5
  31. mknod -m 640 dev/tty1 c 4 1
  32. for elf in $elves; do
  33. origin=$(command -v "$elf")
  34. cp "$origin" bin/
  35. done
  36. cp "${WORKDIR}/init" . && chmod 755 init || exit
  37. ln bin/mksh bin/sh
  38. bin/busybox --install usr/bin
  39. for bin in $(find bin); do
  40. if ldd "$bin" >/dev/null
  41. then
  42. for lib in $(ldd "$bin" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p')
  43. do
  44. filename=$(basename "$lib")
  45. test -e "/lib/${filename}" || cp "$lib" lib
  46. done
  47. fi
  48. done
  49. if find . -print0 | cpio --null -ov --format=newc | xz -9 --check=crc32 > "../${out_base}.cpio.xz"; then
  50. test -e "/boot/${out_base}.cpio.xz" && mv "/boot/${out_base}.cpio.xz" "/boot/${out_base}.cpio.xz.old"
  51. cp "../${out_base}.cpio.xz" /boot
  52. fi
  53. cd "${WORKDIR}"