-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdd-rpi-image
More file actions
executable file
·79 lines (69 loc) · 1.69 KB
/
dd-rpi-image
File metadata and controls
executable file
·79 lines (69 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash -eu
function usage() {
cat <<EOF
Usage: $(basename "${0}") [-h] [-o HOST] IMAGE|URL DEVICE
Copy a Raspberry Pi image to a device.
Positional arguments:
IMAGE Image name. If IMAGE is not an existing file, assume it is
the name of a gzip-compressed image that can be downloaded
from a remote host, i.e., HOST/IMAGE.
URL Fully qualified URL of a gzip-compressed image.
DEVICE Target device.
Optional arguments:
-h, --help Show this help text and exit.
-o, --host HOST Hostname to download the image from. If not provided,
defaults to 'http://192.168.99.11/images'.
EOF
}
host="http://192.168.99.11/images"
img=
dev=
while [ "$#" -gt 0 ] ; do
case "${1}" in
-h|--help)
usage
exit
;;
-o|--host)
shift
host=${1}
;;
*) if [ -z "${img}" ] ; then
img=${1}
elif [ -z "${dev}" ] ; then
dev=${1}
else
echo "Invalid argument: ${1}" >&2
exit 2
fi
esac
shift
done
if [ -z "${img}" ] || [ -z "${dev}" ] ; then
usage
exit 2
fi
if ! [ -b "${dev}" ] ; then
echo "No such device file: ${dev}" >&2
exit 1
fi
readarray -t mnts < <(mount | grep -P "^${dev}[0-9]* " | awk '{ print $1 }')
for mnt in "${mnts[@]}" ; do
umount "${mnt}"
done
if [ -e "${img}" ] ; then
case "${img}" in
*.gz) cmd="zcat" ;;
*.xz) cmd="xzcat" ;;
*) cmd="cat" ;;
esac
echo "Copy ${img} to ${dev}"
"${cmd}" "${img}" | sudo dd of="${dev}" bs=4K conv=fsync status=progress
else
if [ "${img#http}" = "${img}" ] ; then
img=${host}/${img}
fi
echo "Download ${img} to ${dev}"
wget -nv -O - "${img}" | xzcat | \
sudo dd of="${dev}" bs=4K conv=fsync status=progress
fi