summaryrefslogtreecommitdiff
path: root/scripts/contrib/ddimage
blob: 2cba9b28f15f14cac7b59d589d29fc1abbb94a8b (plain)
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
80
81
82
83
84
85
86
87
#!/bin/sh

#BLACKLIST_DEVICES="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde"
BLACKLIST_DEVICES="/dev/sda"

# 1MB blocksize
BLOCKSIZE=1048576

function usage() {
	echo "Usage: $(basename $0) IMAGE DEVICE"
}

function image_details() {
	IMG=$1
	echo "Image details"
	echo "============="
	echo "    image: $(stat --printf '%N\n' $IMG)"
	echo "     size: $(stat -L --printf '%s bytes\n' $IMG)"
	echo " modified: $(stat -L --printf '%y\n' $IMG)"
	echo "     type: $(file -L -b $IMG)"
	echo ""
}

function device_details() {
	DEV=$1
	BLOCK_SIZE=512

	echo "Device details"
	echo "=============="
	echo "  device: $DEVICE"
	if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
		echo "  vendor: $(cat /sys/class/block/$DEV/device/vendor)"
	else
		echo "  vendor: UNKOWN"
	fi
	if [ -f "/sys/class/block/$DEV/device/model" ]; then
		echo "   model: $(cat /sys/class/block/$DEV/device/model)"
	else
		echo "   model: UNKNOWN"
	fi
	if [ -f "/sys/class/block/$DEV/size" ]; then
		echo "    size: $[$(cat /sys/class/block/$DEV/size)*BLOCK_SIZE] bytes"
	else
		echo "    size: UNKNOWN"
	fi
	echo ""
}

if [ $# -ne 2 ]; then
	usage
	exit 1
fi

IMAGE=$1
DEVICE=$2

if [ ! -e "$IMAGE" ]; then
	echo "ERROR: Image $IMAGE does not exist"
	usage
	exit 1
fi


if [ "${BLACKLIST_DEVICES/${DEVICE}/ERROR}" != "$BLACKLIST_DEVICES" ]; then
	echo "ERROR: Device $DEVICE is blacklisted"
	exit 1
fi

if [ ! -w "$DEVICE" ]; then
	echo "ERROR: Device $DEVICE does not exist or is not writable"
	usage
	exit 1
fi

image_details $IMAGE
device_details $(basename $DEVICE)

echo -n "Write $IMAGE to $DEVICE [y/N]? "
read RESPONSE
if [ "$RESPONSE" != "y" ]; then
	echo "Write aborted"
	exit 0
fi

echo "Writing image..."
dd if="$IMAGE" of="$DEVICE" bs="$BLOCKSIZE"
sync