summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Oster <jon@advancedtelematic.com>2017-05-31 14:19:24 +0200
committerJon Oster <jon@advancedtelematic.com>2017-05-31 14:27:30 +0200
commitf3041e0fb727c9b78684c85eaf64297cc9166e66 (patch)
tree8bcbe19316a1b2a7d8c90afaecebe3e3e61c41c0
parent82d51fbf0afe8276d5c418a8cdcc0f1342b6a9ba (diff)
downloadmeta-updater-f3041e0fb727c9b78684c85eaf64297cc9166e66.tar.gz
PRO-3202 Add a script to flash disk images
-rwxr-xr-xmeta-sota-raspberrypi/scripts/flash-image.sh127
1 files changed, 127 insertions, 0 deletions
diff --git a/meta-sota-raspberrypi/scripts/flash-image.sh b/meta-sota-raspberrypi/scripts/flash-image.sh
new file mode 100755
index 0000000..74707bc
--- /dev/null
+++ b/meta-sota-raspberrypi/scripts/flash-image.sh
@@ -0,0 +1,127 @@
1#!/bin/bash
2
3ask() {
4 # http://djm.me/ask
5 local prompt default REPLY
6
7 while true; do
8
9 if [ "${2:-}" = "Y" ]; then
10 prompt="Y/n"
11 default=Y
12 elif [ "${2:-}" = "N" ]; then
13 prompt="y/N"
14 default=N
15 else
16 prompt="y/n"
17 default=
18 fi
19
20 # Ask the question (not using "read -p" as it uses stderr not stdout)
21 echo -n "$1 [$prompt] "
22
23 # Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
24 read REPLY </dev/tty
25
26 # Default?
27 if [ -z "$REPLY" ]; then
28 REPLY=$default
29 fi
30
31 # Check if the reply is valid
32 case "$REPLY" in
33 Y*|y*) return 0 ;;
34 N*|n*) return 1 ;;
35 esac
36
37 done
38}
39
40if [[ $EUID -ne 0 ]]; then
41 echo ""
42 echo " This script must be run as root" 1>&2
43 echo ""
44 exit 1
45fi
46
47if [ -z "$1" ]; then
48 echo ""
49 echo " Flash a built image with an ATS Garage device config file baked in."
50 echo ""
51 echo " Usage: ./flash-configured-image.sh device [imagefile] "
52 echo ""
53 echo ""
54 echo " device : The device name to flash. Must be a removable device."
55 echo " Example: sdb"
56 echo ""
57 echo " imagefile : An image file generated by bitbake (optional)."
58 echo " Default: ./tmp/deploy/images/raspberrypi3/rpi-basic-image-raspberrypi3.rpi-sdimg-ota"
59 echo ""
60 echo " The following utilities are prerequisites:"
61 echo ""
62 echo " dd"
63 echo " parted"
64 echo " e2fsck"
65 echo " fdisk"
66 echo " resize2fs"
67 echo ""
68 exit 1
69fi
70
71set -euo pipefail
72
73DEVICE_TO_FLASH=$1
74IMAGE_TO_FLASH="${2-tmp/deploy/images/raspberrypi3/rpi-basic-image-raspberrypi3.rpi-sdimg-ota}"
75DEVICE_IS_REMOVABLE=$(cat /sys/block/$DEVICE_TO_FLASH/removable)
76
77if [[ $DEVICE_IS_REMOVABLE != "1" ]]; then
78 echo ""
79 echo " For safety, this script will only flash removable block devices."
80 echo ""
81 echo " This check is implemented by reading /sys/block/$DEVICE_TO_FLASH/removable."
82 echo ""
83 exit 1
84fi
85
86echo " "
87echo " Writing image file: $IMAGE_TO_FLASH "
88echo " to device : $DEVICE_TO_FLASH "
89echo " "
90if ask "Do you want to continue?" N; then
91 echo " "
92else
93 exit 1
94fi
95
96if [ ! -f "$IMAGE_TO_FLASH" ]; then
97 echo " "
98 echo " Error: $IMAGE_TO_FLASH doesn't exist"
99 echo ""
100 exit 1
101fi
102
103echo "Unmounting all partitions on $DEVICE_TO_FLASH"
104umount /dev/$DEVICE_TO_FLASH* || true
105sleep 2
106
107echo "Writing image to $DEVICE_TO_FLASH..."
108dd if=$IMAGE_TO_FLASH of=/dev/$DEVICE_TO_FLASH bs=32M && sync
109sleep 2
110
111# It turns out there are card readers that give their partitions funny names, like
112# "/dev/mmcblk0" will be the device, but the partitions are called "/dev/mmcblk0p1"
113# for example. Better to just get the name of the partition after we flash it.
114SECOND_PARTITION=$(fdisk -l /dev/$DEVICE_TO_FLASH | tail -n 1 | awk '{print $1}')
115
116echo "Resizing rootfs partition to fill all of $DEVICE_TO_FLASH..."
117parted -s /dev/$DEVICE_TO_FLASH resizepart 2 '100%'
118sleep 2
119e2fsck -f $SECOND_PARTITION || true
120sleep 2
121
122echo "Resizing filesystem on $SECOND_PARTITION to match partition size..."
123resize2fs -p $SECOND_PARTITION
124sleep 2
125
126echo "Done!"
127