-
Notifications
You must be signed in to change notification settings - Fork 155
/
makeusbconf.sh
executable file
·75 lines (65 loc) · 1.9 KB
/
makeusbconf.sh
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
#!/bin/sh
# Usage:
#
# ./makeusbconf.sh [-d] [-i] [-s <size in Kb>] [-f <file> ] <output.img>
#
USAGE="Usage: $0 [-d] [-i] [-s <size in Kb>] [-f <file> ] <output.img>"
EVE="$(cd "$(dirname "$0")" && pwd)/../"
PATH="$EVE/build-tools/bin:$PATH"
MKFLASH_TAG="$(linuxkit pkg show-tag "$EVE/pkg/mkimage-raw-efi")"
cleanup() {
rm "$TMPDIR"/* 2>/dev/null
rmdir "$TMPDIR"/* 2>/dev/null
rmdir "$TMPDIR"
}
bail() {
echo "$*"
cleanup
exit 1
}
silent() {
if ! OUT=$("$@" 2>&1) ; then
bail "ERROR: $* $OUT"
fi
}
confirm_erase() {
echo ""
echo "This will erase $1 which contains:"
lsblk "$1"
echo ""
while /bin/true; do
printf 'Are you sure(Yes/No)? '
read -r resp
if [ "$resp" = "Yes" ]; then
break
elif [ "$resp" = "No" ]; then
exit 0
fi
done
echo "Proceeding to clear $1"
}
SIZE=204800
TMPDIR=$(mktemp -d)
while getopts dif:s: o
do case "$o" in
d) mkdir "$TMPDIR/dump" || bail "can't create $TMPDIR/dump";;
i) mkdir "$TMPDIR/identity" || bail "can't create $TMPDIR/identity";;
f) cp "$OPTARG" "$TMPDIR/usb.json" || bail "can't access $OPTARG" ;;
s) SIZE="$OPTARG";;
[?]) bail "$USAGE";;
esac
done
shift $((OPTIND-1))
[ $# != 1 ] && bail "$USAGE"
[ -z "$(ls -A "$TMPDIR")" ] && bail "ERROR: one of the -d -i or -f has to be given"
IMAGE="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
if [ -b "$IMAGE" ] ; then
[ "$(uname -s)" = Linux ] || bail "ERROR: writing directly to the device is only supported on Linux"
IMAGE_BIND_OPT="--device"
confirm_erase "$IMAGE"
else
[ -e "$IMAGE" ] || silent dd if=/dev/zero of="$IMAGE" seek="$SIZE" bs=1024 count=0
IMAGE_BIND_OPT="-v"
fi
(cd "$TMPDIR" || exit 1; tar cf - ./*) | docker run -i --rm "$IMAGE_BIND_OPT" "$IMAGE:/output.img" "${MKFLASH_TAG}" /output.img usb_conf
cleanup