mkfs.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/sh
  2. # Build and configure primary filesystem
  3. # Copyright Brian Conway <bconway@rcesoftware.com>, see LICENSE for details
  4. FS=resflash-${MACHINE}-${imgsizemb}MB-${COM0}${DATE}.fs
  5. echo "Creating filesystem: ${FS}"
  6. # Build the filesystem
  7. dd if=/dev/zero of=${FS} bs=1k count=0 seek=${fssizekb} status=none
  8. get_next_vnd
  9. fsvnd=${NEXTVND}
  10. vnconfig ${fsvnd} ${FS} > /dev/null
  11. # Newfs the filesystem
  12. newfs -O 2 ${fsvnd}c >> ${BUILDPATH}/01.mkfs.00.newfs.fs 2>&1
  13. # Mount and populate filesystem
  14. echo 'Populating filesystem and configuring fstab'
  15. fst=$(date +%s)
  16. mkdir -p ${BUILDPATH}/fs
  17. mount -o noatime,softdep /dev/${fsvnd}c ${BUILDPATH}/fs
  18. tar cf - -C ${basedir} .|tar xpf - -C ${BUILDPATH}/fs
  19. mkdir -p ${BUILDPATH}/fs/cfg ${BUILDPATH}/fs/mbr ${BUILDPATH}/fs/${DOSMNT}
  20. # Add resflash hooks
  21. mkdir -p ${BUILDPATH}/fs/resflash
  22. cp -p ${BINDIR}/host/* ${BUILDPATH}/fs/resflash/
  23. cp -p ${BINDIR}/resflash.sub ${BUILDPATH}/fs/resflash/
  24. cp -p ${BINDIR}/LICENSE ${BUILDPATH}/fs/resflash/
  25. chown -R root:wheel ${BUILDPATH}/fs/resflash
  26. echo "version: ${VERSION}" >> ${BUILDPATH}/fs/resflash/BUILD
  27. echo "build command: ${0} ${BUILDARGS}" >> ${BUILDPATH}/fs/resflash/BUILD
  28. echo "build date: $(date)" >> ${BUILDPATH}/fs/resflash/BUILD
  29. cp ${BINDIR}/etc/resflash.conf ${BUILDPATH}/fs/etc/
  30. chown root:wheel ${BUILDPATH}/fs/etc/resflash.conf
  31. cp -p ${BUILDPATH}/fs/etc/examples/rc.shutdown ${BUILDPATH}/fs/etc/
  32. echo '/resflash/resflash.save' >> ${BUILDPATH}/fs/etc/rc.shutdown
  33. echo '/resflash/resflash.relink' >> ${BUILDPATH}/fs/etc/rc.shutdown
  34. sed -i -e '/.*_mp=$(mount -t ffs/s/ffs/ffs,mfs/' \
  35. -e '/^rm.*fastboot$/a\
  36. /resflash/rc.resflash\
  37. # Re-read rc.conf and rc.conf.local from the new /etc\
  38. _rc_parse_conf\
  39. ' -e '/.*reorder_libs$/a\
  40. if mount -t mfs|grep -q /usr/share/relink; then umount /usr/share/relink; fi\
  41. ' ${BUILDPATH}/fs/etc/rc
  42. chown root:wheel ${BUILDPATH}/fs/bsd*
  43. chmod 600 ${BUILDPATH}/fs/bsd*
  44. # Populate /dev
  45. (cd ${BUILDPATH}/fs/dev && ./MAKEDEV all)
  46. # Configure fstab
  47. duid=$(disklabel ${imgvnd}|grep duid|cut -d ' ' -f 2)
  48. echo "${duid}.a /mbr ffs rw,noatime,nodev,noexec,noauto 1 2" >> \
  49. ${BUILDPATH}/fs/etc/fstab
  50. if [ ${swapsizemb} -gt 0 ]; then
  51. echo "${duid}.b none swap sw" >> ${BUILDPATH}/fs/etc/fstab
  52. fi
  53. echo "${duid}.d / ffs ro,noatime 1 1" >> ${BUILDPATH}/fs/etc/fstab
  54. echo "${duid}.f /cfg ffs rw,noatime,nodev,noexec,noauto 1 2" >> \
  55. ${BUILDPATH}/fs/etc/fstab
  56. echo "${duid}.i /${DOSMNT} msdos rw,noatime,nodev,noexec,noauto 0 0" >> \
  57. ${BUILDPATH}/fs/etc/fstab
  58. echo 'swap /tmp mfs rw,noatime,nodev,nosuid,-s32M 0 0' >> \
  59. ${BUILDPATH}/fs/etc/fstab
  60. # Install random.seed and host.random
  61. dd if=/dev/random of=${BUILDPATH}/fs/etc/random.seed bs=512 count=1 status=none
  62. chmod 600 ${BUILDPATH}/fs/etc/random.seed
  63. dd if=/dev/random of=${BUILDPATH}/fs/var/db/host.random bs=65536 count=1 \
  64. status=none
  65. chmod 600 ${BUILDPATH}/fs/var/db/host.random
  66. # Set com0 ttys on arches that default to ttyC*, if directed
  67. if [ -n "${com0sp+1}" ] && [ ${MACHINE} != 'arm64' ]; then
  68. sed -i -e "/^tty00/s/std\.9600/std\.${com0sp}/" \
  69. -e '/^tty00/s/unknown off/vt220 on secure/' \
  70. ${BUILDPATH}/fs/etc/ttys
  71. fi
  72. # Populate arm64 boot scaffold, if applicable
  73. if [ ${MACHINE} == 'arm64' ]; then
  74. mkdir -p ${BUILDPATH}/fs/usr/mdec/rpi/overlays ${BUILDPATH}/fs/usr/mdec/pine64
  75. cp /usr/local/share/raspberrypi-firmware/boot/bcm2710-rpi-2-b.dtb \
  76. ${BUILDPATH}/fs/usr/mdec/rpi/
  77. cp /usr/local/share/raspberrypi-firmware/boot/bcm2710-rpi-3-b.dtb \
  78. ${BUILDPATH}/fs/usr/mdec/rpi/
  79. cp /usr/local/share/raspberrypi-firmware/boot/bcm2710-rpi-3-b-plus.dtb \
  80. ${BUILDPATH}/fs/usr/mdec/rpi/
  81. cp /usr/local/share/raspberrypi-firmware/boot/bcm2710-rpi-cm3.dtb \
  82. ${BUILDPATH}/fs/usr/mdec/rpi/
  83. cp /usr/local/share/raspberrypi-firmware/boot/bcm2711-rpi-4-b.dtb \
  84. ${BUILDPATH}/fs/usr/mdec/rpi/
  85. cp /usr/local/share/raspberrypi-firmware/boot/bcm2711-rpi-400.dtb \
  86. ${BUILDPATH}/fs/usr/mdec/rpi/
  87. cp /usr/local/share/raspberrypi-firmware/boot/bcm2711-rpi-cm4.dtb \
  88. ${BUILDPATH}/fs/usr/mdec/rpi/
  89. cp /usr/local/share/raspberrypi-firmware/boot/bootcode.bin \
  90. ${BUILDPATH}/fs/usr/mdec/rpi/
  91. cp /usr/local/share/raspberrypi-firmware/boot/start.elf \
  92. ${BUILDPATH}/fs/usr/mdec/rpi/
  93. cp /usr/local/share/raspberrypi-firmware/boot/start4.elf \
  94. ${BUILDPATH}/fs/usr/mdec/rpi/
  95. cp /usr/local/share/raspberrypi-firmware/boot/fixup.dat \
  96. ${BUILDPATH}/fs/usr/mdec/rpi/
  97. cp /usr/local/share/raspberrypi-firmware/boot/fixup4.dat \
  98. ${BUILDPATH}/fs/usr/mdec/rpi/
  99. cp /usr/local/share/raspberrypi-firmware/boot/overlays/disable-bt.dtbo \
  100. ${BUILDPATH}/fs/usr/mdec/rpi/overlays/
  101. cp /usr/local/share/u-boot/rpi_arm64/u-boot.bin ${BUILDPATH}/fs/usr/mdec/rpi/
  102. cp /usr/local/share/u-boot/pine64_plus/u-boot-sunxi-with-spl.bin \
  103. ${BUILDPATH}/fs/usr/mdec/pine64/
  104. fi
  105. # Set root password in the form 'resflashYYYYMMDD'
  106. sed -i "/^root/s|root::|root:$(echo resflash$(echo ${DATE}|cut -d _ -f 1)|\
  107. encrypt -b 10):|" ${BUILDPATH}/fs/etc/master.passwd
  108. pwd_mkdb -p -d ${BUILDPATH}/fs/etc master.passwd
  109. # Work around clang hard links crossing mfs mounts
  110. rm ${BUILDPATH}/fs/usr/libexec/cpp
  111. ln -s /usr/bin/clang ${BUILDPATH}/fs/usr/libexec/cpp
  112. # Relocate libLLVM.so to save space in /usr/lib
  113. mkdir -p ${BUILDPATH}/fs/usr/libllvm
  114. mv ${BUILDPATH}/fs/usr/lib/libLLVM.so.*.? ${BUILDPATH}/fs/usr/libllvm/
  115. ln -s /usr/libllvm/$(ls -r ${BUILDPATH}/fs/usr/libllvm/|head -n 1) \
  116. ${BUILDPATH}/fs/usr/lib
  117. # Run fw_update in requested mode: all, bwfm, intel, or none
  118. cp -p /etc/resolv.conf ${BUILDPATH}/fs/etc/
  119. if [ ${CROSSARCH} -eq 0 ]; then
  120. st=$(date +%s)
  121. cp -p /var/run/dmesg.boot ${BUILDPATH}/fs/var/run/
  122. case ${fwupdate} in
  123. ALL) echo -n ' Running fw_update'
  124. DESTDIR=${BUILDPATH}/fs fw_update -av >> \
  125. ${BUILDPATH}/01.mkfs.01.fw_update 2>&1;;
  126. BWFM) echo -n ' Running fw_update: bwfm'
  127. DESTDIR=${BUILDPATH}/fs fw_update -v bwfm >> \
  128. ${BUILDPATH}/01.mkfs.01.fw_update 2>&1;;
  129. INTEL) echo -n ' Running fw_update: intel'
  130. DESTDIR=${BUILDPATH}/fs fw_update -v intel >> \
  131. ${BUILDPATH}/01.mkfs.01.fw_update 2>&1;;
  132. esac
  133. rm ${BUILDPATH}/fs/var/run/dmesg.boot
  134. if [ ${fwupdate} != 'NONE' ]; then
  135. echo " ($(($(date +%s) - st))s)"
  136. fi
  137. else
  138. echo '*** WARNING: fw_update not supported in cross-arch builds, skipping.' \
  139. '***'
  140. fi
  141. # Perform chroot activities (syspatch, packages)
  142. . ${BINDIR}/mkchroot.sh
  143. rm ${BUILDPATH}/fs/etc/resolv.conf
  144. # Unmount and free filesystem for copy
  145. df -h ${BUILDPATH}/fs >> ${BUILDPATH}/01.mkfs.02.df
  146. sync
  147. umount ${BUILDPATH}/fs
  148. vnconfig -u ${fsvnd}
  149. echo "Populating filesystem complete ($(($(date +%s) - fst))s)"
  150. # Write filesystem to image's d partition and calculate checksum
  151. echo -n 'Writing filesystem to disk image'
  152. st=$(date +%s)
  153. dd if=${FS} of=/dev/r${imgvnd}d bs=1m >> ${BUILDPATH}/01.mkfs.03.dd.fstoimg 2>&1
  154. echo " ($(($(date +%s) - st))s)"
  155. echo -n 'Calculating filesystem checksum'
  156. st=$(date +%s)
  157. cksum -a ${ALG} -h ${FS}.cksum ${FS}
  158. if [ -n "${XXHALG+1}" ]; then
  159. # Pass -q to xxhsum to prevent it from clearing the line
  160. xxhsum -H${XXHALG} -q ${FS} > ${FS}.xxhsum
  161. fi
  162. echo " ($(($(date +%s) - st))s)"