diff options
author | Jon Oster <jon@advancedtelematic.com> | 2017-05-31 14:19:24 +0200 |
---|---|---|
committer | Jon Oster <jon@advancedtelematic.com> | 2017-05-31 14:27:30 +0200 |
commit | f3041e0fb727c9b78684c85eaf64297cc9166e66 (patch) | |
tree | 8bcbe19316a1b2a7d8c90afaecebe3e3e61c41c0 | |
parent | 82d51fbf0afe8276d5c418a8cdcc0f1342b6a9ba (diff) | |
download | meta-updater-f3041e0fb727c9b78684c85eaf64297cc9166e66.tar.gz |
PRO-3202 Add a script to flash disk images
-rwxr-xr-x | meta-sota-raspberrypi/scripts/flash-image.sh | 127 |
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 | |||
3 | ask() { | ||
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 | |||
40 | if [[ $EUID -ne 0 ]]; then | ||
41 | echo "" | ||
42 | echo " This script must be run as root" 1>&2 | ||
43 | echo "" | ||
44 | exit 1 | ||
45 | fi | ||
46 | |||
47 | if [ -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 | ||
69 | fi | ||
70 | |||
71 | set -euo pipefail | ||
72 | |||
73 | DEVICE_TO_FLASH=$1 | ||
74 | IMAGE_TO_FLASH="${2-tmp/deploy/images/raspberrypi3/rpi-basic-image-raspberrypi3.rpi-sdimg-ota}" | ||
75 | DEVICE_IS_REMOVABLE=$(cat /sys/block/$DEVICE_TO_FLASH/removable) | ||
76 | |||
77 | if [[ $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 | ||
84 | fi | ||
85 | |||
86 | echo " " | ||
87 | echo " Writing image file: $IMAGE_TO_FLASH " | ||
88 | echo " to device : $DEVICE_TO_FLASH " | ||
89 | echo " " | ||
90 | if ask "Do you want to continue?" N; then | ||
91 | echo " " | ||
92 | else | ||
93 | exit 1 | ||
94 | fi | ||
95 | |||
96 | if [ ! -f "$IMAGE_TO_FLASH" ]; then | ||
97 | echo " " | ||
98 | echo " Error: $IMAGE_TO_FLASH doesn't exist" | ||
99 | echo "" | ||
100 | exit 1 | ||
101 | fi | ||
102 | |||
103 | echo "Unmounting all partitions on $DEVICE_TO_FLASH" | ||
104 | umount /dev/$DEVICE_TO_FLASH* || true | ||
105 | sleep 2 | ||
106 | |||
107 | echo "Writing image to $DEVICE_TO_FLASH..." | ||
108 | dd if=$IMAGE_TO_FLASH of=/dev/$DEVICE_TO_FLASH bs=32M && sync | ||
109 | sleep 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. | ||
114 | SECOND_PARTITION=$(fdisk -l /dev/$DEVICE_TO_FLASH | tail -n 1 | awk '{print $1}') | ||
115 | |||
116 | echo "Resizing rootfs partition to fill all of $DEVICE_TO_FLASH..." | ||
117 | parted -s /dev/$DEVICE_TO_FLASH resizepart 2 '100%' | ||
118 | sleep 2 | ||
119 | e2fsck -f $SECOND_PARTITION || true | ||
120 | sleep 2 | ||
121 | |||
122 | echo "Resizing filesystem on $SECOND_PARTITION to match partition size..." | ||
123 | resize2fs -p $SECOND_PARTITION | ||
124 | sleep 2 | ||
125 | |||
126 | echo "Done!" | ||
127 | |||