Created
October 26, 2022 03:06
-
-
Save RobertCNelson/ac2d390663d7bbb9b09aff89efafb4a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -e | |
| if ! id | grep -q root; then | |
| echo "must be run as root:" | |
| exit | |
| fi | |
| version_message="1.20221010.1, rsync add -H option for hardlinks, final image running out of space..." | |
| #mke2fs -c | |
| #Check the device for bad blocks before creating the file system. | |
| #If this option is specified twice, then a slower read-write test is | |
| #used instead of a fast read-only test. | |
| mkfs_options="" | |
| #mkfs_options="-c" | |
| #mkfs_options="-cc" | |
| cylon_leds() { | |
| #ls /sys/class/leds/beaglebone\:green\:usr* | |
| #cat /sys/class/leds/beaglebone\:green\:usr*/trigger | |
| local leds_base=/sys/class/leds/beaglebone\:green\:usr | |
| if [ -e ${leds_base}0/trigger ] ; then | |
| echo none > ${leds_base}0/trigger || true | |
| echo none > ${leds_base}1/trigger || true | |
| echo none > ${leds_base}2/trigger || true | |
| echo none > ${leds_base}3/trigger || true | |
| STATE=1 | |
| while : ; do | |
| case $STATE in | |
| 1) | |
| echo 1 > ${leds_base}0/brightness || true | |
| echo 0 > ${leds_base}1/brightness || true | |
| STATE=2 | |
| ;; | |
| 2) | |
| echo 1 > ${leds_base}1/brightness || true | |
| echo 0 > ${leds_base}0/brightness || true | |
| STATE=3 | |
| ;; | |
| 3) | |
| echo 1 > ${leds_base}2/brightness || true | |
| echo 0 > ${leds_base}1/brightness || true | |
| STATE=4 | |
| ;; | |
| 4) | |
| echo 1 > ${leds_base}3/brightness || true | |
| echo 0 > ${leds_base}2/brightness || true | |
| STATE=5 | |
| ;; | |
| 5) | |
| echo 1 > ${leds_base}2/brightness || true | |
| echo 0 > ${leds_base}3/brightness || true | |
| STATE=6 | |
| ;; | |
| 6) | |
| echo 1 > ${leds_base}1/brightness || true | |
| echo 0 > ${leds_base}2/brightness || true | |
| STATE=1 | |
| ;; | |
| *) | |
| echo 1 > ${leds_base}0/brightness || true | |
| echo 0 > ${leds_base}1/brightness || true | |
| STATE=2 | |
| ;; | |
| esac | |
| sleep 0.1 | |
| done | |
| fi | |
| } | |
| reset_leds() { | |
| if [ "x${has_usr_leds}" = "xenable" ] ; then | |
| if [ -e /proc/$CYLON_PID ]; then | |
| kill $CYLON_PID > /dev/null 2>&1 | |
| fi | |
| leds_pattern0=${leds_pattern0:-"heartbeat"} | |
| leds_pattern1=${leds_pattern1:-"mmc0"} | |
| leds_pattern2=${leds_pattern2:-"activity"} | |
| leds_pattern3=${leds_pattern3:-"mmc1"} | |
| leds_base=/sys/class/leds/beaglebone\:green\:usr | |
| if [ -e ${leds_base}0/trigger ] ; then | |
| echo ${leds_pattern0} > ${leds_base}0/trigger || true | |
| echo ${leds_pattern1} > ${leds_base}1/trigger || true | |
| echo ${leds_pattern2} > ${leds_base}2/trigger || true | |
| echo ${leds_pattern3} > ${leds_base}3/trigger || true | |
| fi | |
| fi | |
| } | |
| flush_cache () { | |
| sync | |
| if [ "x${destination}" != "x" ] ; then | |
| message="INFO: flush_cache: [blockdev --flushbufs ${destination}]" ; broadcast | |
| blockdev --flushbufs ${destination} || true | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| fi | |
| } | |
| broadcast () { | |
| if [ "x${message}" != "x" ] ; then | |
| echo "${message}" | |
| if [ "x${debug_over_display}" != "x" ] ; then | |
| echo "${message}" > /dev/${debug_over_display} || true | |
| fi | |
| fi | |
| } | |
| broadcast_over_display () { | |
| if [ "x${message}" != "x" ] ; then | |
| if [ "x${debug_over_display}" != "x" ] ; then | |
| echo "${message}" > /dev/${debug_over_display} || true | |
| fi | |
| fi | |
| } | |
| example_failure () { | |
| message="Example: /etc/default/beagle-flasher" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="source=/dev/mmcblk0" ; broadcast | |
| message="destination=/dev/mmcblk1" ; broadcast | |
| message="rfs_partition=dual" ; broadcast | |
| message="rfs_boot_startmb=1" ; broadcast | |
| message="rfs_boot_size_mb=128" ; broadcast | |
| message="bootloader=/opt/u-boot/bb-u-boot-beagleboneai64/install-emmc.sh" ; broadcast | |
| message="flash_back=bbai64-emmc-to-microsd" ; broadcast | |
| message="extlinux=/opt/u-boot/bb-u-boot-beagleboneai64/emmc-extlinux.conf" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| exit 2 | |
| } | |
| write_failure () { | |
| message="ERROR: writing to [destination=${destination}] failed..." ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| if [ "x${destination}" != "x" ] ; then | |
| umount ${destination}p1 > /dev/null 2>&1 || true | |
| fi | |
| } | |
| check_running_system () { | |
| message="INFO: Giving system time to stablize..." ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="5" ; broadcast | |
| sleep 1 | |
| message="4" ; broadcast | |
| sleep 1 | |
| message="3" ; broadcast | |
| sleep 1 | |
| message="2" ; broadcast | |
| sleep 1 | |
| message="1" ; broadcast | |
| sleep 1 | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="INFO: Preparing /tmp" ; broadcast | |
| message="INFO: [mount -t tmpfs tmpfs /tmp]" ; broadcast | |
| mount -t tmpfs tmpfs /tmp | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="INFO: /proc/cmdline" ; broadcast | |
| message="INFO: [cat /proc/cmdline]" ; broadcast | |
| cat /proc/cmdline | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| has_usr_leds="enable" | |
| if [ "x${has_usr_leds}" = "xenable" ] ; then | |
| cylon_leds & CYLON_PID=$! | |
| fi | |
| if [ "x${source}" != "x" ] && [ "x${destination}" != "x" ] ; then | |
| message="INFO: [lsblk -i]" ; broadcast | |
| message="`lsblk -i || true`" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="INFO: copying [${source}] -> [${destination}]" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| else | |
| message="INFO: [lsblk -i]" ; broadcast | |
| message="`lsblk -i || true`" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="ERROR: Setup: [source] and [destination] in /etc/default/beagle-flasher" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| example_failure | |
| fi | |
| if [ ! -b "${source}" ] ; then | |
| message="ERROR: [source=${source}] does not exist" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| example_failure | |
| else | |
| message="INFO: [source=${source}] is a valid block device" ; broadcast | |
| fi | |
| if [ ! -b "${destination}" ] ; then | |
| message="ERROR: [destination=${destination}] does not exist" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| example_failure | |
| else | |
| message="INFO: [destination=${destination}] is a valid block device" ; broadcast | |
| fi | |
| flush_cache | |
| } | |
| format_boot () { | |
| message="/sbin/mkfs.vfat -F 16 ${destination}p1 -n ${boot_label}" ; broadcast | |
| LC_ALL=C /sbin/mkfs.vfat -F 16 ${destination}p1 -n ${boot_label} | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| } | |
| format_root () { | |
| message="/sbin/mkfs.ext4 ${ext4_options} ${destination}p2 -L ${rootfs_label}" ; broadcast | |
| LC_ALL=C /sbin/mkfs.ext4 ${ext4_options} ${destination}p2 -L ${rootfs_label} | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| } | |
| format_single_root () { | |
| message="/sbin/mkfs.ext4 ${mkfs_options} ${destination}p1 -L ${single_root_label}" ; broadcast | |
| LC_ALL=C /sbin/mkfs.ext4 ${mkfs_options} ${destination}p1 -L ${single_root_label} | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| } | |
| copy_boot () { | |
| message="Copying: ${source}p1 -> ${destination}p1" ; broadcast | |
| mount ${source}p1 /boot/firmware || true | |
| mkdir -p /tmp/boot/ || true | |
| mount ${destination}p1 /tmp/boot/ -o sync | |
| message="rsync: (-aHAXx) /boot/firmware/ -> /tmp/boot/" ; broadcast | |
| rsync -aHAXx /boot/firmware/ /tmp/boot/ || write_failure | |
| flush_cache | |
| if [ "x${extlinux}" != "x" ] ; then | |
| if [ -f ${extlinux} ] ; then | |
| message="INFO: [/boot/extlinux/extlinux.conf]" ; broadcast | |
| mkdir -p /tmp/boot/extlinux/ || true | |
| cp -v ${extlinux} /tmp/boot/extlinux/extlinux.conf | |
| message="INFO: [cat /tmp/boot/extlinux/extlinux.conf]" ; broadcast | |
| message="`cat /tmp/boot/extlinux/extlinux.conf`" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| fi | |
| fi | |
| flush_cache | |
| umount /tmp/boot/ || umount -l /tmp/boot/ || write_failure | |
| flush_cache | |
| umount /boot/firmware || umount -l /boot/firmware || true | |
| } | |
| copy_rootfs () { | |
| message="INFO: Copying: ${source}p${media_rootfs} -> ${destination}p${media_rootfs}" ; broadcast | |
| mkdir -p /tmp/rootfs/ || true | |
| message="INFO: [mount ${destination}p${media_rootfs} /tmp/rootfs/ -o async,noatime]" ; broadcast | |
| mount ${destination}p${media_rootfs} /tmp/rootfs/ -o async,noatime | |
| message="INFO: /usr/bin/rsync: (-aHAXx) [/ -> /tmp/rootfs/]" ; broadcast | |
| /usr/bin/rsync -aHAXx /* /tmp/rootfs/ --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/lib/modules/*,/uEnv.txt} || write_failure | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| message="INFO: Copying: Kernel modules" ; broadcast | |
| mkdir -p /tmp/rootfs/lib/modules/$(uname -r)/ || true | |
| message="INFO: /usr/bin/rsync: (-aHAXx) /lib/modules/$(uname -r)/ -> /tmp/rootfs/lib/modules/$(uname -r)/" ; broadcast | |
| /usr/bin/rsync -aHAXx /lib/modules/$(uname -r)/* /tmp/rootfs/lib/modules/$(uname -r)/ || write_failure | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| message="INFO: Generating: /etc/fstab" ; broadcast | |
| echo "# /etc/fstab: static file system information." > /tmp/rootfs/etc/fstab | |
| echo "#" >> /tmp/rootfs/etc/fstab | |
| echo "${destination}p${media_rootfs} / ext4 noatime,errors=remount-ro 0 1" >> /tmp/rootfs/etc/fstab | |
| if [ "x${media_rootfs}" = "x2" ] ; then | |
| echo "${destination}p1 /boot/firmware vfat defaults 0 0" >> /tmp/rootfs/etc/fstab | |
| fi | |
| echo "debugfs /sys/kernel/debug debugfs mode=755,uid=root,gid=gpio,defaults 0 0" >> /tmp/rootfs/etc/fstab | |
| message="INFO: [cat /tmp/rootfs/etc/fstab]" ; broadcast | |
| message="`cat /tmp/rootfs/etc/fstab`" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| if [ "x${flash_back}" != "x" ] ; then | |
| if [ -f /etc/beagle-flasher/${flash_back} ] ; then | |
| message="INFO: [cp -v /etc/beagle-flasher/${flash_back} /tmp/rootfs/etc/default/beagle-flasher]" ; broadcast | |
| cp -v /etc/beagle-flasher/${flash_back} /tmp/rootfs/etc/default/beagle-flasher | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| fi | |
| fi | |
| if [ -f /tmp/rootfs/etc/default/generic-sys-mods ] ; then | |
| . /tmp/rootfs/etc/default/generic-sys-mods | |
| echo "#This file is sourced by /usb/bin/bb-growpart" > /tmp/rootfs/etc/default/generic-sys-mods | |
| echo "ROOT_DRIVE=${destination}" >> /tmp/rootfs/etc/default/generic-sys-mods | |
| echo "ROOT_PARTITION=${ROOT_PARTITION}" >> /tmp/rootfs/etc/default/generic-sys-mods | |
| echo "ARCH_SOC_MODULES=${ARCH_SOC_MODULES}" >> /tmp/rootfs/etc/default/generic-sys-mods | |
| message="INFO: [cat /tmp/rootfs/etc/default/generic-sys-mods]" ; broadcast | |
| message="`cat /tmp/rootfs/etc/default/generic-sys-mods`" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| fi | |
| if [ -f /tmp/rootfs/etc/systemd/system/multi-user.target.wants/beagle-flasher-init-shutdown.service ] ; then | |
| rm -rf /tmp/rootfs/etc/systemd/system/multi-user.target.wants/beagle-flasher-init-shutdown.service || true | |
| fi | |
| if [ -f /tmp/rootfs/etc/systemd/system/multi-user.target.wants/grow_partition.service ] ; then | |
| rm -rf /tmp/rootfs/etc/systemd/system/multi-user.target.wants/grow_partition.service || true | |
| fi | |
| message="Copying: ${source}p${media_rootfs} -> ${destination}p${media_rootfs} complete" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| message="INFO: [umount /tmp/rootfs/ || umount -l /tmp/rootfs/ || write_failure]" ; broadcast | |
| umount /tmp/rootfs/ || umount -l /tmp/rootfs/ || write_failure | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="INFO: [umount /tmp || true]" ; broadcast | |
| umount /tmp || true | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| message="INFO: Force writeback of eMMC buffers by Syncing: ${destination}" ; broadcast | |
| message="INFO: [dd if=${destination} of=/dev/null count=100000 status=progress]" ; broadcast | |
| dd if=${destination} of=/dev/null count=100000 status=progress | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| flush_cache | |
| if [ "x${has_usr_leds}" = "xenable" ] ; then | |
| reset_leds | |
| fi | |
| unset are_we_flasher | |
| are_we_flasher=$(grep init-beagle-flasher /proc/cmdline || true) | |
| if [ ! "x${are_we_flasher}" = "x" ] ; then | |
| message="INFO: [We are init]" ; broadcast | |
| #When run as init | |
| exec /sbin/init | |
| exit #We should not hit that | |
| fi | |
| } | |
| partition_drive () { | |
| message="INFO: Erasing: [${destination}]" ; broadcast | |
| flush_cache | |
| message="INFO: [dd if=/dev/zero of=${destination} bs=1M count=148 status=progress]" ; broadcast | |
| dd if=/dev/zero of=${destination} bs=1M count=148 status=progress | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| sync | |
| message="INFO: [dd if=${destination} of=/dev/null bs=1M count=148 status=progress]" ; broadcast | |
| dd if=${destination} of=/dev/null bs=1M count=148 status=progress | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| sync | |
| flush_cache | |
| message="INFO: Erasing: [${destination}] complete" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| if [ "x${bootloader}" != "x" ] ; then | |
| if [ -f ${bootloader} ] ; then | |
| message="INFO: [/bin/bash ${bootloader}]" ; broadcast | |
| /bin/bash ${bootloader} | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| fi | |
| fi | |
| if [ "x${rfs_partition}" = "xsingle" ] ; then | |
| rfs_rootfs_startmb=${rfs_rootfs_startmb:-"4"} | |
| if [ "x${rfs_rootfs_type}" = "xext4" ] ; then | |
| sfdisk_fstype="L" | |
| fi | |
| single_root_label=${single_root_label:-"rootfs"} | |
| sfdisk_options="--force" | |
| message="INFO: Partitioning: ${destination}" ; broadcast | |
| message="INFO: /sbin/sfdisk: [$(LC_ALL=C /sbin/sfdisk --version)]" ; broadcast | |
| message="INFO: /sbin/sfdisk: [/sbin/sfdisk ${sfdisk_options} ${destination}]" ; broadcast | |
| message="INFO: /sbin/sfdisk: [${rfs_rootfs_startmb}M,,${sfdisk_fstype},*]" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| LC_ALL=C /sbin/sfdisk ${sfdisk_options} "${destination}" <<-__EOF__ | |
| ${rfs_rootfs_startmb}M,,${sfdisk_fstype},* | |
| __EOF__ | |
| flush_cache | |
| message="INFO: Formatting: ${destination}" ; broadcast | |
| format_single_root | |
| message="INFO: Formatting: ${destination} complete" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| media_rootfs="1" | |
| copy_rootfs | |
| elif [ "x${rfs_partition}" = "xdual" ] ; then | |
| rfs_boot_startmb=${rfs_boot_startmb:-"1"} | |
| rfs_boot_size_mb=${rfs_boot_size_mb:-"128"} | |
| rfs_sfdisk_fstype=${rfs_sfdisk_fstype:-"0xc"} | |
| sfdisk_rootfs_startmb=$(($rfs_boot_startmb + $rfs_boot_size_mb)) | |
| boot_label=${boot_label:-"FIRMWARE"} | |
| rootfs_label=${rootfs_label:-"rootfs"} | |
| sfdisk_options="--force" | |
| message="INFO: Partitioning: ${destination}" ; broadcast | |
| message="INFO: /sbin/sfdisk: [$(LC_ALL=C /sbin/sfdisk --version)]" ; broadcast | |
| message="INFO: /sbin/sfdisk: [/sbin/sfdisk ${sfdisk_options} ${destination}]" ; broadcast | |
| message="INFO: /sbin/sfdisk: [${rfs_boot_startmb}M,${rfs_boot_size_mb}M,${rfs_sfdisk_fstype},*]" ; broadcast | |
| message="INFO: /sbin/sfdisk: [${sfdisk_rootfs_startmb}M,,,-]" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| LC_ALL=C /sbin/sfdisk ${sfdisk_options} "${destination}" <<-__EOF__ | |
| ${rfs_boot_startmb}M,${rfs_boot_size_mb}M,${rfs_sfdisk_fstype},* | |
| ${sfdisk_rootfs_startmb}M,,,- | |
| __EOF__ | |
| flush_cache | |
| message="INFO: Formatting: ${destination}" ; broadcast | |
| format_boot | |
| format_root | |
| message="INFO: Formatting: ${destination} complete" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| copy_boot | |
| media_rootfs="2" | |
| copy_rootfs | |
| else | |
| example_failure | |
| fi | |
| } | |
| ###FIXME | |
| if [ -f /etc/default/beagle-flasher ] ; then | |
| clear | |
| . /etc/default/beagle-flasher | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="Version: [${version_message}]" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="cat /etc/default/beagle-flasher:" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="`cat /etc/default/beagle-flasher`" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| else | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| message="Version: [${version_message}]" ; broadcast | |
| message="--------------------------------------------------------------------------------" ; broadcast | |
| example_failure | |
| fi | |
| check_running_system | |
| partition_drive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment