prepare-sd-card.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. # this script is not intended to be used without modifications!
  3. # ----------------------------------------------------------------------
  4. # Get latest Raspbian image and checksum file
  5. img_url_regex='http:\/\/downloads\.raspberrypi\.org\/raspbian_lite\/images\/raspbian_lite-[0-9-]{10}\/[0-9-]{10}-raspbian-buster-lite\.zip'
  6. img_url=`curl --silent https://downloads.raspberrypi.org/raspbian_lite_latest | \
  7. grep --extended-regexp --ignore-case --only-matching $img_url_regex`
  8. sha256_url=$img_url.sha256
  9. image=`basename $img_url`
  10. sha256=`basename $sha256_url`
  11. # Download latest image and checksum file if not present yet
  12. mkdir -p ~/images && cd ~/images
  13. if [[ -f "$image" && -f "$sha256" ]]; then
  14. echo "Latest Raspbian image found: $image"
  15. else
  16. echo "Downloading latest Raspbian..."
  17. curl --continue-at - --remote-name-all $img_url $sha256_url
  18. fi
  19. # Test checksum and exit if fails
  20. echo -n "Testing checksum... "
  21. set -e && sha256sum --check $sha256 || exit 1
  22. set +e
  23. # ----------------------------------------------------------------------
  24. # Select the disk
  25. PS3="Select disk to install Raspbian: "
  26. select disk in `lsblk --include 8 --nodeps --output NAME --noheadings`
  27. do
  28. echo "________________________________________________________________________"
  29. lsblk --nodeps /dev/$disk --output TRAN,VENDOR,MODEL,HOTPLUG
  30. echo "________________________________________________________________________"
  31. lsblk /dev/$disk --output NAME,TYPE,FSTYPE,SIZE,LABEL,MOUNTPOINT
  32. echo "________________________________________________________________________"
  33. read -p "ARE YOU SURE THAT YOU WANT TO OVERWRITE THIS DISK? (y/n) " -n 1 -r
  34. echo #newline
  35. if [[ $REPLY =~ ^[Yy]$ ]]; then
  36. echo "Proceeding with $sdcard"
  37. sdcard=/dev/$disk
  38. break
  39. fi
  40. done
  41. # ----------------------------------------------------------------------
  42. # Testing parameters / hostname
  43. name=$1
  44. if [ "X$name" == "X" ]; then
  45. echo "Kein Hostname für die Box angegeben!"
  46. echo "USAGE: $0 name"
  47. read -p "Hostname jetzt eingeben?: " name
  48. fi
  49. echo "Hostname: $name"
  50. echo "Image: $image"
  51. # ----------------------------------------------------------------------
  52. # Preparing the disk
  53. # Unmounting partitions
  54. for part in \
  55. `findmnt --output SOURCE,TARGET --raw | grep $sdcard | cut -d" " -f2`
  56. do sudo umount --verbose $part
  57. done
  58. # Write image to sdcard
  59. unzip -p $image *.img \
  60. | sudo dd of="$sdcard" bs=4K status=progress
  61. sync
  62. echo
  63. echo Rausnehmen, kurz warten, reinstecken und mount abwarten, dann Enter
  64. read -n 1 -p "Geht's weiter?" unused
  65. # Configure WiFi
  66. # https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md
  67. cp ~/wpa_supplicant.conf /media/$USER/boot/
  68. # Enable / Setup SSH
  69. # https://www.raspberrypi.org/documentation/remote-access/ssh/
  70. touch /media/$USER/boot/ssh
  71. mkdir -p /media/$USER/rootfs/home/pi/.ssh
  72. chmod 0700 /media/$USER/rootfs/home/pi/.ssh
  73. cp ~/.ssh/id_rsa.pub /media/$USER/rootfs/home/pi/.ssh/authorized_keys
  74. chmod 0600 /media/$USER/rootfs/home/pi/.ssh/authorized_keys
  75. chown -R 1000:1000 /media/$USER/rootfs/home/pi/.ssh
  76. # Setting hostname
  77. echo $name | sudo tee /media/$USER/rootfs/etc/hostname
  78. echo 127.0.1.2 $name | sudo tee -a /media/$USER/rootfs/etc/hosts
  79. # Set timezone
  80. echo "Europe/Berlin" \
  81. | sudo tee /media/$USER/rootfs/etc/timezone
  82. sudo ln -sf /usr/share/zoneinfo/Europe/Berlin \
  83. /media/dominik/rootfs/etc/localtime
  84. # Set locales
  85. sudo sed -i -e '/^# en_US\.UTF-8 UTF-8/s/^# //' \
  86. -e '/^# de_DE\.UTF-8 UTF-8/s/^# //' \
  87. /media/$USER/rootfs/etc/locale.gen
  88. #sudo locale-gen # need to run at first boot
  89. #idea: https://github.com/nmcclain/raspberian-firstboot
  90. #sudo update-locale LANG=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8 #equivalent
  91. echo -e "LANG=de_DE.UTF-8\nLC_MESSAGES=en_US.UTF-8" \
  92. | sudo tee /media/$USER/rootfs/etc/default/locale
  93. # Unmounting partitions
  94. sync
  95. for part in \
  96. `findmnt --output SOURCE,TARGET --raw | grep $sdcard | cut -d" " -f2`
  97. do sudo umount --verbose $part
  98. done