forked from ntpsec/stratum-1-microserver-howto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddimage
executable file
·103 lines (94 loc) · 1.79 KB
/
ddimage
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
#
# Copy a good Raspbian image to an SD writer
# Alternatively, back up or copy a gzipped image
#
# Usage: ddimage [-b] [-c] [-i imgname ] [device-tag]
mode=copy
image=""
while getopts bci:n opt
do
case $opt in
b) mode=backup ;;
c) mode=copy ;; # default mode
i) image=$OPTARG ;;
esac
done
shift $(($OPTIND - 1))
target=/dev/${1-sdd}
echo "Checking $target"
if ls $target >/dev/null 2>&1
then
if [ -b $target ]
then
echo "$target exists as a storage device."
else
echo "$target is not a storage device"
exit 1
fi
else
echo "$target does not exist."
exit 1
fi
if df | grep $target >/dev/null
then
echo "$target is mounted and cannot be used."
exit 1
else
echo "$target is not mounted"
fi
if [ -z "$image" ]
then
case $mode in
copy)
set -- *.img
if [ $# != 1 ]
then
echo "Need a unique existing image file, specify with -i."
exit 1
else
image=`ls *img`
fi
;;
backup)
echo "Specify a unique image file with -i."
exit 1
;;
esac
fi
case $mode in
copy)
if [ -f $image ]
then
echo "Copying $image..."
if [[ $file =~ \.gz$ ]]
then
# Previously-made backup
gunzip $file | dcfldd bs=4M of=$target
else
# This is the arm for uncompressed Raspbian .img files
dcfldd statusinterval=16 sizeprobe=if bs=4M if=$image of=$target
fi
else
echo "No .img file, bailing out!"
exit 1
fi
;;
backup)
if [ -f ${image}.gz ]
then
echo "Refusing to overwrite ${image}.gz file, bailing out!"
exit 1
else
echo "Backing up $target to ${image}.gz..."
dcfldd statusinterval=16 sizeprobe=if bs=4M if=$target | gzip >${image}.gz
fi
;;
esac
if df | grep $target >/dev/null
then
echo "Unmounting..."
umount
fi
echo "Done"
exit 0