download.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. # Get latest Raspbian image and checksum file
  3. img_url_regex='http:\/\/downloads\.raspberrypi\.org\/raspbian_lite\/images\/raspbian_lite-[0-9-]{10}\/[0-9-]{10}-raspbian-buster-lite\.zip'
  4. img_url=`curl --silent https://downloads.raspberrypi.org/raspbian_lite_latest | \
  5. grep --extended-regexp --ignore-case --only-matching $img_url_regex`
  6. sha256_url=$img_url.sha256
  7. image=`basename $img_url`
  8. sha256=`basename $sha256_url`
  9. # Download latest image and checksum file if not present yet
  10. mkdir -p ~/images && cd ~/images
  11. if [[ -f "$image" && -f "$sha256" ]]; then
  12. echo "Latest Raspbian image found: $image"
  13. else
  14. echo "Downloading latest Raspbian..."
  15. curl --continue-at - --remote-name-all $img_url $sha256_url
  16. fi
  17. # Test checksum and exit if fails
  18. echo -n "Testing checksum... "
  19. set -e && sha256sum --check $sha256 || exit 1
  20. set +e
  21. # Guess SDcard Reader
  22. sdcard=`find /dev/disk/by-id/ -iregex '.*Card.*0$'`
  23. sudo fdisk -l $sdcard
  24. read -p "Continue with this disk? " -n 1 -r
  25. echo #newline
  26. if [[ $REPLY =~ ^[Yy]$ ]]; then
  27. echo "Proceeding with $sdcard"
  28. else
  29. set -e && exit 1
  30. fi
  31. for part in `find /dev/disk/by-id/ -iregex '.*Card.*-part.*'`;
  32. do sudo umount --verbose $part && /bin/true;
  33. done
  34. unzip -p $image *.img \
  35. | sudo dd of="$sdcard" bs=4K status=progress
  36. # https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md
  37. # https://www.raspberrypi.org/documentation/remote-access/ssh/
  38. chown -R 1000:1000 /media/$USER/rootfs/home/pi/.ssh
  39. sync
  40. for part in `find /dev/disk/by-id/ -iregex '.*Card.*-part.*'`;
  41. do umount --verbose $part && /bin/true;
  42. done