-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddx.sh
executable file
·110 lines (90 loc) · 2.18 KB
/
ddx.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
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
104
105
106
107
108
109
#!/bin/bash
set -aeu -o pipefail
# --------------------------------------------------------------
# Ce script écrit une image disque sur une carte SD (ou autre).
#
# Usage: ddx <device> <image>
#
# <device> Nom du fichier 'block-device' de la carte SD insérée
# (ex: '/dev/sdb')
# <image> Nom du fichier image à inscrire
# (ex: 'raspbian.bin')
#
# Note: Le script doit être lancé par 'sudo'
# --------------------------------------------------------------
SAVED_DIR=$PWD
# Il faut être SU
CHECK_SU=`id | grep "uid=0(root)" | wc -l || true`
if [ "$CHECK_SU" != "1" ] ; then
echo ""
echo "You MUST be SU. Aborting."
echo ""
exit -10
fi
# On attend deux arguments
if [ $# -ne 2 ] ; then
echo ""
echo "Usage: $0 <device> <image>"
echo ""
echo "Example:"
echo "$0 /dev/sdb image.bin"
echo ""
exit -1
fi
# $1 : Est-ce un nom de fichier?
if [ ! -b "$1" ] ; then
echo ""
echo "$1"
echo "Huh? This is NOT a (device) file! Aborting."
echo ""
exit -2
else
DEV=$1
fi
# $1 : Est-ce un block-device de disque?
DEV_IN_LSBLK_PD=`lsblk -pd | grep disk | grep $1 | wc -l || true`
if [ "$DEV_IN_LSBLK_PD" != "1" ] ; then
echo ""
echo "$1"
echo "Huh? This device is NOT a disk block-device! Aborting."
echo ""
exit -3
fi
# $1 : Est-il monté?
DEV_IN_MOUNTS=`mount | grep $1 | wc -l || true`
if [ "$DEV_IN_MOUNTS" != "0" ] ; then
echo ""
echo "$1"
echo "This device MUST NOT be mounted! Aborting."
echo ""
exit -4
else
OUTPUT_DEV=$1
echo "OK."
fi
# $2 : Est-ce un nom de fichier?
if [ ! -f "$2" ] ; then
echo ""
echo "$2"
echo "Huh? This is NOT a file! Aborting."
echo ""
exit -5
else
IN=$2
fi
INPUT_FILE=$IN
echo ""
echo "Going to WRITE '$INPUT_FILE' to '$OUTPUT_DEV'. Is it correct? (Y/N)"
read RESP
if [ "$RESP" == "Y" -o "$RESP" == "y" ] ; then
echo ""
echo "----------------------------------"
echo " WRITING "
echo "----------------------------------"
echo ""
(pv -n $INPUT_FILE | dd of=$OUTPUT_DEV bs=64M oflag=dsync) 2>&1 | dialog --gauge "Running dd ..." 10 70 0
echo "Sync'ing.)"
sync
fi
cd $SAVED_DIR
echo "FINISHED."