buildInitramFs.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. set -x
  3. set -e
  4. #Build initramfs image
  5. # This file is part of PrawnOS (http://www.prawnos.com)
  6. # Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
  7. # PrawnOS is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2
  9. # as published by the Free Software Foundation.
  10. # PrawnOS is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
  16. outmnt=$(mktemp -d -p `pwd`)
  17. outdev=/dev/loop7
  18. if [ -z "$1" ]
  19. then
  20. echo "No base file system image filename supplied"
  21. exit 1
  22. fi
  23. BASE=$1
  24. ROOT_DIR=`pwd`
  25. build_resources=$ROOT_DIR/resources/BuildResources
  26. if [ ! -f $ROOT_DIR/$BASE ]
  27. then
  28. echo "No base filesystem, run 'make filesystem' first"
  29. exit 1
  30. fi
  31. #A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
  32. #Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
  33. cleanup() {
  34. set +e
  35. umount -l $outmnt > /dev/null 2>&1
  36. rmdir $outmnt > /dev/null 2>&1
  37. losetup -d $outdev > /dev/null 2>&1
  38. set +e
  39. umount -l $outmnt > /dev/null 2>&1
  40. rmdir $outmnt > /dev/null 2>&1
  41. losetup -d $outdev > /dev/null 2>&1
  42. }
  43. trap cleanup INT TERM EXIT
  44. [ ! -d build ] && mkdir build
  45. losetup -P $outdev $ROOT_DIR/$BASE
  46. #mount the root filesystem
  47. mount -o noatime ${outdev}p2 $outmnt
  48. #make a skeleton filesystem
  49. initramfs_src=$outmnt/InstallResources/initramfs_src
  50. rm -rf $initramfs_src*
  51. mkdir -p $initramfs_src
  52. mkdir $initramfs_src/bin
  53. mkdir $initramfs_src/dev
  54. mkdir $initramfs_src/etc
  55. mkdir $initramfs_src/newroot
  56. mkdir $initramfs_src/boot
  57. mkdir $initramfs_src/proc
  58. mkdir $initramfs_src/sys
  59. mkdir $initramfs_src/sbin
  60. mkdir $initramfs_src/run
  61. mkdir $initramfs_src/run/cryptsetup
  62. mkdir $initramfs_src/lib
  63. mkdir $initramfs_src/lib/arm-linux-gnueabihf
  64. mknod -m 622 $initramfs_src/dev/console c 5 1
  65. mknod -m 622 $initramfs_src/dev/tty c 4 0
  66. #install the few tools we need, and the supporting libs
  67. cp $outmnt/bin/busybox $outmnt/sbin/cryptsetup $initramfs_src/bin/
  68. cp $outmnt/lib/arm-linux-gnueabihf/libblkid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/
  69. cp $outmnt/lib/arm-linux-gnueabihf/libuuid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/
  70. cp $outmnt/lib/arm-linux-gnueabihf/libc.so.6 $initramfs_src/lib/arm-linux-gnueabihf/
  71. cp $outmnt/lib/ld-linux-armhf.so.3 $initramfs_src/lib/
  72. cp $outmnt/sbin/blkid $initramfs_src/bin/
  73. cp $outmnt/usr/lib/arm-linux-gnueabihf/libpopt.so.0 $initramfs_src/lib/arm-linux-gnueabihf/libpopt.so.0
  74. cp $outmnt/usr/lib/arm-linux-gnueabihf/libssl.so.1.1 $initramfs_src/lib/arm-linux-gnueabihf/libssl.so.1.1
  75. cp $outmnt/usr/lib/arm-linux-gnueabihf/libcrypto.so.1.1 $initramfs_src/lib/arm-linux-gnueabihf/libcrypto.so.1.1
  76. cp $outmnt/usr/lib/arm-linux-gnueabihf/libargon2.so.1 $initramfs_src/lib/arm-linux-gnueabihf/libargon2.so.1
  77. cp $outmnt/usr/lib/arm-linux-gnueabihf/libjson-c.so.3 $initramfs_src/lib/arm-linux-gnueabihf/libjson-c.so.3
  78. cp $outmnt/lib/arm-linux-gnueabihf/libm.so.6 $initramfs_src/lib/arm-linux-gnueabihf/libm.so.6
  79. cp $outmnt/lib/arm-linux-gnueabihf/libcryptsetup.so.12 $initramfs_src/lib/arm-linux-gnueabihf/libcryptsetup.so.12
  80. cp $outmnt/lib/arm-linux-gnueabihf/libuuid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/libuuid.so.1
  81. cp $outmnt/lib/arm-linux-gnueabihf/libblkid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/libblkid.so.1
  82. cp $outmnt/lib/arm-linux-gnueabihf/libc.so.6 $initramfs_src/lib/arm-linux-gnueabihf/libc.so.6
  83. cp $outmnt/lib/ld-linux-armhf.so.3 $initramfs_src/lib/ld-linux-armhf.so.3
  84. cp $outmnt/lib/arm-linux-gnueabihf/libdevmapper.so.1.02.1 $initramfs_src/lib/arm-linux-gnueabihf/libdevmapper.so.1.02.1
  85. cp $outmnt/lib/arm-linux-gnueabihf/librt.so.1 $initramfs_src/lib/arm-linux-gnueabihf/librt.so.1
  86. cp $outmnt/lib/arm-linux-gnueabihf/libdl.so.2 $initramfs_src/lib/arm-linux-gnueabihf/libdl.so.2
  87. cp $outmnt/lib/arm-linux-gnueabihf/libselinux.so.1 $initramfs_src/lib/arm-linux-gnueabihf/libselinux.so.1
  88. cp $outmnt/lib/arm-linux-gnueabihf/libudev.so.1 $initramfs_src/lib/arm-linux-gnueabihf/libudev.so.1
  89. cp $outmnt/lib/arm-linux-gnueabihf/libpthread.so.0 $initramfs_src/lib/arm-linux-gnueabihf/libpthread.so.0
  90. cp $outmnt/lib/arm-linux-gnueabihf/libpcre.so.3 $initramfs_src/lib/arm-linux-gnueabihf/libpcre.so.3
  91. cp $outmnt/lib/arm-linux-gnueabihf/libgcc_s.so.1 $initramfs_src/lib/arm-linux-gnueabihf/libgcc_s.so.1
  92. #add the init script
  93. cp $build_resources/initramfs-init $initramfs_src/init
  94. chmod +x $initramfs_src/init
  95. cp $initramfs_src/init $initramfs_src/sbin/init
  96. #compress and install
  97. rm -rf $outmnt/boot/PrawnOS-initramfs.cpio.gz
  98. cd $initramfs_src
  99. ln -s busybox bin/cat
  100. ln -s busybox bin/mount
  101. ln -s busybox bin/sh
  102. ln -s busybox bin/switch_root
  103. ln -s busybox bin/umount
  104. # store for kernel building
  105. find . -print0 | cpio --null --create --verbose --format=newc | gzip --best > $ROOT_DIR/build/PrawnOS-initramfs.cpio.gz