init (2911B)
1 #!/bin/busybox sh 2 # Credits to Wizzup for concept and help ❤️ 3 4 rescue_shell() { 5 echo "Something went wrong. Dropping to a shell." 6 exec /bin/sh 7 } 8 9 perform_update() { 10 echo " * Checking for updates to squashfs." 11 wget -O /tmp/update.txt http://pub.parazyd.cf/tmp/update.txt || { 12 echo " * Error: Downloading update.txt went wrong" 13 return 14 } 15 gpgv /tmp/update.txt || { 16 echo " * Error: Invalid GnuPG signature on update.txt" 17 return 18 } 19 newchecksum="$(grep 'filesystem.squashfs$' /tmp/update.txt)" 20 [ -n "$newchecksum" ] || { 21 echo " * Error: No valid checksum data found in update.txt" 22 return 23 } 24 25 echo " * Calculating old checksum..." 26 cd /mnt/mmc0p1 27 oldchecksum="$(sha256sum filesystem.squashfs)" 28 [ "$oldchecksum" = "$newchecksum" ] && { 29 echo " * No difference in checksums. We're up to date! :)" 30 return 31 } 32 33 echo " * Checksums differ. Downloading new update..." 34 mv filesystem.squashfs filesystem.squashfs.old 35 wget -O filesystem.squashfs http://pub.parazyd.cf/tmp/filesystem.squashfs || { 36 echo " * Download went wrong. Reverting to old squashfs." 37 echo " * Another update will be tried on next boot." 38 rm -f filesystem.squashfs 39 mv filesystem.squashfs.old filesystem.squashfs 40 return 41 } 42 43 echo " * Checking if what we downloaded has the same checksum as update.txt" 44 echo "$newchecksum" | sha256 -c || { 45 echo " * Error: Checksum mismatch. Reverting to old squashfs." 46 echo " * Another update will be tried on next boot." 47 rm -f filesystem.squashfs 48 mv filesystem.squashfs.old filesystem.squashfs 49 return 50 } 51 52 echo " * Everything looks alright. Proceeding with boot..." 53 } 54 55 /bin/busybox --install -s /bin 56 57 mount -t devtmpfs none /dev 58 mount -t proc none /proc 59 mount -t sysfs none /sys 60 61 echo " * enabling eth0" 62 ip link set eth0 up || rescue_shell 63 sleep 2 64 65 echo " * requesting dhcp" 66 udhcpc || rescue_shell 67 sleep 2 68 69 echo " * setting time from ntp" 70 ntpd -q -p pool.ntp.org || rescue_shell 71 sleep 2 72 73 echo " * mounting mmcblk0p1" 74 mkdir -p /mnt/mmc0p1 75 mount -t ext4 /dev/mmcblk0p1 /mnt/mmc0p1 76 77 perform_update 2>&1 | tee update.log 78 79 mkdir -p /mnt/ro 80 mkdir -p /mnt/rw 81 mkdir -p /mnt/overlay 82 83 echo " * mounting squashfs to /mnt/ro" 84 mount -t squashfs /mnt/mmc0p1/filesystem.squashfs /mnt/ro 85 86 storage=$(egrep -o 'storage[^ ]*' /proc/cmdline | sed 's/storage=//') 87 echo " * mounting $storage to /mnt/rw" 88 mount -t btrfs $storage /mnt/rw 89 90 mkdir -p /mnt/rw/upper 91 mkdir -p /mnt/rw/work 92 echo " * mounting overlayfs to /mnt/overlay" 93 mount -t overlay -o lowerdir=/mnt/ro,upperdir=/mnt/rw/upper,workdir=/mnt/rw/work overlay /mnt/overlay 94 95 [ -f update.log ] && { 96 mkdir -p /mnt/rw/upper/var/log 97 [ -f /mnt/rw/upper/var/log/update.log ] && { 98 mv /mnt/rw/upper/var/log/update.log /mnt/rw/upper/var/log/update.log.1 99 } 100 cp -f update.log /mnt/rw/upper/var/log/update.log 101 } 102 103 mount --move /dev /mnt/overlay/dev || rescue_shell 104 umount /proc /sys 105 echo " * switching root" 106 exec switch_root /mnt/overlay /sbin/init