Robustify writeimage.sh

This commit is contained in:
Bo Rydberg 2019-11-08 20:37:19 +01:00
parent b80d80a111
commit 230eda0b71

View File

@ -2,14 +2,16 @@
function usage() { function usage() {
echo "Usage: $0 [options...]" cat <<END_USAGE
echo "" Usage: $0 [options...]
echo "Available options:"
echo " <-i image_file> - indicates the path to the image file (e.g. -i /home/user/Download/file.img.gz)" Available options:
echo " <-d sdcard_dev> - indicates the path to the sdcard block device (e.g. -d /dev/mmcblk0)" <-i image_file> - indicates the path to the image file (e.g. -i /home/user/Download/file.img.gz)
echo " [-m modem:apn:user:pwd:pin] - configures the mobile network modem (e.g. -m ttyUSB0:internet)" <-d sdcard_dev> - indicates the path to the sdcard block device (e.g. -d /dev/mmcblk0)
echo " [-n ssid:psk] - sets the wireless network name and key (e.g. -n mynet:mykey1234)" [-m modem:apn:user:pwd:pin] - configures the mobile network modem (e.g. -m ttyUSB0:internet)
echo " [-s ip/cidr:gw:dns] - sets a static IP configuration instead of DHCP (e.g. -s 192.168.1.101/24:192.168.1.1:8.8.8.8)" [-n ssid:psk] - sets the wireless network name and key (e.g. -n mynet:mykey1234)
[-s ip/cidr:gw:dns] - sets a static IP configuration instead of DHCP (e.g. -s 192.168.1.101/24:192.168.1.1:8.8.8.8)
END_USAGE
exit 1 exit 1
} }
@ -64,14 +66,14 @@ function cleanup {
set +e set +e
# unmount sdcard # unmount sdcard
umount ${SDCARD_DEV}* >/dev/null 2>&1 umount "${SDCARD_DEV}"* >/dev/null 2>&1
} }
trap cleanup EXIT trap cleanup EXIT
BOOT=$(dirname $0)/.boot BOOT=$(dirname "$0")/.boot
if ! [ -f $DISK_IMG ]; then if ! [ -f "$DISK_IMG" ]; then
echo "could not find image file $DISK_IMG" echo "could not find image file $DISK_IMG"
exit 1 exit 1
fi fi
@ -85,7 +87,7 @@ if [[ $DISK_IMG == *.gz ]]; then
exit 1 exit 1
fi fi
msg "decompressing the .gz compressed image" msg "decompressing the .gz compressed image"
$gunzip -c $DISK_IMG > ${DISK_IMG%???} $gunzip -c "$DISK_IMG" > "${DISK_IMG%???}"
DISK_IMG=${DISK_IMG%???} DISK_IMG=${DISK_IMG%???}
elif [[ $DISK_IMG == *.xz ]]; then elif [[ $DISK_IMG == *.xz ]]; then
if [ -z "$unxz" ]; then if [ -z "$unxz" ]; then
@ -93,84 +95,90 @@ elif [[ $DISK_IMG == *.xz ]]; then
exit 1 exit 1
fi fi
msg "decompressing the .xz compressed image" msg "decompressing the .xz compressed image"
$unxz -T 0 -c $DISK_IMG > ${DISK_IMG%???} $unxz -T 0 -c "$DISK_IMG" > "${DISK_IMG%???}"
DISK_IMG=${DISK_IMG%???} DISK_IMG=${DISK_IMG%???}
fi fi
umount ${SDCARD_DEV}* 2>/dev/null || true umount "${SDCARD_DEV}"* 2>/dev/null || true
msg "writing disk image to sdcard" msg "writing disk image to sdcard"
dd if=$DISK_IMG of=$SDCARD_DEV bs=1048576 dd if="$DISK_IMG" of="$SDCARD_DEV" bs=1048576
sync sync
if which partprobe > /dev/null 2>&1; then if which partprobe > /dev/null 2>&1; then
msg "re-reading sdcard partition table" msg "re-reading sdcard partition table"
partprobe ${SDCARD_DEV} partprobe "${SDCARD_DEV}"
sleep 1 sleep 1
fi fi
msg "mounting sdcard" msg "mounting sdcard"
mkdir -p $BOOT mkdir -p "$BOOT"
if [ `uname` == "Darwin" ]; then if [ "$(uname)" == "Darwin" ]; then
BOOT_DEV=${SDCARD_DEV}s1 # e.g. /dev/disk4s1 BOOT_DEV=${SDCARD_DEV}s1 # e.g. /dev/disk4s1
diskutil unmountDisk ${SDCARD_DEV} diskutil unmountDisk "${SDCARD_DEV}"
mount -ft msdos $BOOT_DEV $BOOT mount -ft msdos "$BOOT_DEV" "$BOOT"
else # assuming Linux else # assuming Linux
BOOT_DEV=${SDCARD_DEV}p1 # e.g. /dev/mmcblk0p1 BOOT_DEV=${SDCARD_DEV}p1 # e.g. /dev/mmcblk0p1
if ! [ -e ${SDCARD_DEV}p1 ]; then if ! [ -e "${SDCARD_DEV}p1" ]; then
BOOT_DEV=${SDCARD_DEV}1 # e.g. /dev/sdc1 BOOT_DEV=${SDCARD_DEV}1 # e.g. /dev/sdc1
fi fi
mount $BOOT_DEV $BOOT mount "$BOOT_DEV" "$BOOT"
fi fi
# wifi # wifi
if [ -n "$SSID" ]; then if [ -n "$SSID" ]; then
msg "creating wireless configuration" msg "creating wireless configuration"
conf=$BOOT/wpa_supplicant.conf conf=$BOOT/wpa_supplicant.conf
echo "update_config=1" > $conf {
echo "ctrl_interface=/var/run/wpa_supplicant" >> $conf echo "update_config=1"
echo "network={" >> $conf echo "ctrl_interface=/var/run/wpa_supplicant"
echo " scan_ssid=1" >> $conf echo "network={"
echo " ssid=\"$SSID\"" >> $conf echo " scan_ssid=1"
echo " ssid=\"$SSID\""
if [ -n "$PSK" ]; then if [ -n "$PSK" ]; then
echo " psk=\"$PSK\"" >> $conf echo " psk=\"$PSK\""
fi fi
echo "}" >> $conf echo "}"
echo "" >> $conf echo ""
} > "$conf"
fi fi
# modem # modem
if [ -n "$MODEM" ]; then if [ -n "$MODEM" ]; then
msg "creating mobile network configuration" msg "creating mobile network configuration"
conf=$BOOT/ppp conf=$BOOT/ppp
mkdir -p $conf mkdir -p "$conf"
echo $MODEM > $conf/modem echo "$MODEM" > "$conf/modem"
echo "AT+CGDCONT=1,\"IP\",\"$APN\"" > $conf/apn echo "AT+CGDCONT=1,\"IP\",\"$APN\"" > "$conf/apn"
echo > $conf/extra echo > "$conf/extra"
echo > $conf/auth
echo > $conf/pin
if [ -n "$MUSER" ]; then if [ -n "$MUSER" ]; then
echo "user \"$MUSER\"" > $conf/auth echo "user \"$MUSER\""
echo "password \"$MPWD\"" >> $conf/auth echo "password \"$MPWD\""
fi else
echo
fi > "$conf/auth"
if [ -n "$PIN" ]; then if [ -n "$PIN" ]; then
echo "AT+CPIN=$PIN" > $conf/pin echo "AT+CPIN=$PIN"
fi else
echo
fi > "$conf/pin"
fi fi
# static ip # static ip
if [ -n "$IP" ] && [ -n "$GW" ] && [ -n "$DNS" ]; then if [ -n "$IP" ] && [ -n "$GW" ] && [ -n "$DNS" ]; then
msg "setting static IP configuration" msg "setting static IP configuration"
conf=$BOOT/static_ip.conf conf=$BOOT/static_ip.conf
echo "STATIC_IP=\"$IP\"" > $conf {
echo "STATIC_GW=\"$GW\"" >> $conf echo "STATIC_IP=\"$IP\""
echo "STATIC_DNS=\"$DNS\"" >> $conf echo "STATIC_GW=\"$GW\""
echo "STATIC_DNS=\"$DNS\""
} > "$conf"
fi fi
msg "unmounting sdcard" msg "unmounting sdcard"
sync sync
umount $BOOT umount "$BOOT"
rmdir $BOOT rmdir "$BOOT"
msg "you can now remove the sdcard" msg "you can now remove the sdcard"