robocar-setup/bootstrap/build_sdcard.sh

89 lines
2.1 KiB
Bash
Raw Normal View History

2020-02-07 12:38:30 +00:00
#!/usr/bin/env bash
set -e
# Uncomment and fix variables before run script
SD_CARD_DEVICE="/dev/sda"
SD_CARD_BOOT_PARTITION=${SD_CARD_DEVICE}1
SD_CARD_ROOT_PARTITION=${SD_CARD_DEVICE}2
HOSTNAME_CAR="satanas"
WIFI_SECRET_FILE=".secret"
# For debug
ADD_TTL_SUPPORT_FOR_DEBUG=1
2022-01-02 16:26:47 +00:00
IMAGE_BASE=https://downloads.raspberrypi.org/raspios_lite_arm64_latest
2020-02-07 12:38:30 +00:00
########### End Configuration ############
mkdir -p build
if [ ! -f build/raspbian.zip ]
then
2022-01-02 16:26:47 +00:00
wget -O build/raspbian.zip ${IMAGE_BASE}
2020-02-07 12:38:30 +00:00
cd build
unzip raspbian.zip
cd ..
fi
raspbian_image=`ls build/*.img`
echo "Copy image ${raspbian_image} to sdcard"
sudo dd if=${raspbian_image} of=${SD_CARD_DEVICE} bs=4M conv=fsync status=progress
echo "Force disk sync"
sync
echo "Mount boot sdcard"
mkdir -p build/mnt
sudo mount ${SD_CARD_BOOT_PARTITION} ./build/mnt
echo "Force ssh to start at first boot"
sudo touch build/mnt/ssh
if [ "${ADD_TTL_SUPPORT_FOR_DEBUG}" == "1" ]
then
echo "# Disable bluetooth in order to enable serial TTL" | sudo tee -a build/mnt/config.txt > /dev/null
echo "dtoverlay=pi3-disable-bt" | sudo tee -a build/mnt/config.txt > /dev/null
fi
2022-01-02 16:26:47 +00:00
echo "Copy wifi configuration"
source ${WIFI_SECRET_FILE}
eval "echo \"$(cat wpa_supplicant.conf)\"" | sudo tee build/mnt/wpa_supplicant.conf > /dev/null
sudo chmod 600 build/mnt/wpa_supplicant.conf
2020-02-07 12:38:30 +00:00
echo "Umount boot partition"
sync
2020-02-07 12:38:30 +00:00
sudo umount build/mnt
echo "Mount root sdcard"
sudo mount ${SD_CARD_ROOT_PARTITION} ./build/mnt
echo "Set hostname"
echo ${HOSTNAME_CAR} | sudo tee build/mnt/etc/hostname > /dev/null
echo sudo sed -i "s/raspberrypi/$HOSTNAME_CAR/g" build/mnt/etc/hosts
echo "Disable default password for 'pi' use"
sudo sed -i -E "s/(pi:)[^:]*(:.*)/\1*\2/" build/mnt/etc/shadow*
echo "Copy authorized keys"
sudo mkdir -p build/mnt/home/pi/.ssh/
sudo cp ../ansible/roles/base/files/authorized_keys build/mnt/home/pi/.ssh/
sudo chmod 600 build/mnt/home/pi/.ssh/authorized_keys
sudo chown -R 1000:1000 build/mnt/home/pi
echo "Force disk sync"
sync
2020-02-07 12:38:30 +00:00
echo "Umount sdcard"
sleep 2
2020-02-07 12:38:30 +00:00
sudo umount ./build/mnt
echo "Bootstrap finished, plug sdcard on raspberrypi and connect with command:"
echo " ssh pi@${HOSTNAME_CAR}.local"