mkfs.sh 7.5 KB

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