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