-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstick
More file actions
executable file
·205 lines (179 loc) · 5.36 KB
/
Copy pathinstick
File metadata and controls
executable file
·205 lines (179 loc) · 5.36 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# Preparing a USB stick with a Debian installer and all the needed stuff
#
# Version 0.7.2 - OLOC - 19/05/2014 - Minor modifications
# Version 0.7.1 - kiyop - 06/05/2014 - convert mkfs into "dd if=/dev/zero bs=512 count=1 of=..."
# Version 0.7 - OLOC - 26/04/2014 - Local Repository
# Version 0.6 - OLOC - 04/04/2014 - _wget simplification
# Version 0.5 - OLOC - 04/04/2014 - add retry option
# Version 0.4 - OLOC - 27/03/2014
###################
DevUsb=sdb
Arch=i386
Src=iso-cd
CDRepo=http://cdimage.debian.org/debian-cd/current/${Arch}/${Src}
Name=$(basename $0)
LogDir=/var/log
LogFile=${LogDir}/${Name}.$(date +%Y%m%d.%H%M%S).log
LclRpt=/usr/share/${Name}/${Src}
###################
# Functions
_echo() {
echo "$(date +%Y%m%d-%H%M%S) - $1" | tee -a ${LogFile}
}
_end() {
RC=$1
if [ ${RC} -ne 0 ] ; then
_echo "[ERROR] The process is stopped [Return Code=${RC}]. See the log: ${LogFile}"
exit ${RC}
fi
}
_log() {
echo "$(date +%Y%m%d-%H%M%S) * $1" >> ${LogFile}
}
_wget() {
Url=$1/$2
cd ${LclRpt}
wget -c ${Url}
_end $?
# Check of the quality of the recieved iso file
if [ ! -r ${LclRpt}/$2 ] ; then
_echo "- Cannot read the retrieved file $2."
_end 42
fi
}
###################
# Start annoucement
_log "${Name} - start."
_log "Log is here: ${LogFile}"
_log "Remote Reppository is here: http://cdimage.debian.org/debian-cd/current/${Arch}/${Src}"
_log "Local Repository is here: ${LclRpt}"
###################
# Argument management
while getopts "cr" Option
do
case ${Option} in
r|R) typeset -i Retry=1 ;;
c|C) typeset -i Clear=1 ;;
esac
done
shift $(($OPTIND - 1))
###################
# Menu - Plug
while true
do
clear
echo "Please plug your USB stick and answer:"
echo -e "\t1. The stick is ready."
echo -e "\t2. I want to exit."
echo -n "Enter your choice: "
read PlugAnswer
case "${PlugAnswer}" in
1) break ;;
2) exit;;
*) ;;
esac
done
###################
# Checks of the USB device
SerialNumber=$(dmesg | grep 'SerialNumber' | tail -1 | awk '{print $NF}')
DevUsb1=$(ls -l /dev/disk/by-id | grep ${SerialNumber} | awk -F'../../' '{print $2}')
DevUsb2=$(dmesg | grep -i 'removable disk' | tail -1 | cut -c16- | awk '{print $3}' | tr -d '[]')
_log "Serial Number = ${SerialNumber}"
_log "DevUsb by SN in by-id = ${DevUsb1}"
_log "DevUsb by removable disk = ${DevUsb2}"
if [ -z ${DevUsb1} ] ; then
_echo "[ERROR] The usb device seems to be unknown."
_end 42
fi
if [[ "${DevUsb1}" == "${DevUsb2}" ]] ; then
DevUsb=${DevUsb1}
else
_echo "[WARNING] Confusing usb devices: ${DevUsb1} and ${DevUsb2}"
_echo "Default usb device is: ${DevUsb}"
fi
_echo "The detected USB stick is : [${DevUsb}]"
echo -n "Press enter to use it, Q to quit or name it: "
read StickAnswer
if [[ $(echo ${StickAnswer} | tr [:lower:] [:upper:]) == 'Q' ]] ; then
_log "The user quits."
exit
fi
if [ ! -z ${StickAnswer} ] ; then
DevUsb=${StickAnswer}
fi
_echo "The USB stick to use is : ${DevUsb}"
###################
# Distribution choice
echo -e "\r"
while true
do
echo "Distributions: "
echo -e "\t1. The default one"
echo -e "\t2. kde"
echo -e "\t3. lxde"
echo -e "\t4. netinst"
echo -e "\t5. xfce"
echo -e "\tq. I change my mind and I want to quit\r"
echo -n "Enter your choice: "
read PlugAnswer
case "${PlugAnswer}" in
q*|Q*) _log "The user quits" ; exit;;
1) IsoMsk=${Arch}-CD-1.iso ; break ;;
2) IsoMsk=${Arch}-kde-CD-1.iso ; break ;;
3) IsoMsk=${Arch}-lxde-CD-1.iso ; break ;;
4) IsoMsk=${Arch}-netinst.iso ; break ;;
5) IsoMsk=${Arch}-xfce-CD-1.iso ; break ;;
*) ;;
esac
done
_log "Distribution choice: ${IsoMsk}"
###################
# Preparation of the Local Repository
if [ ${Clear} ] ; then
_echo "Clearing the local repository ${LclRpt}..."
_log "rm -Rf ${LclRpt} 2>/dev/null"
rm -Rf ${LclRpt} 2>/dev/null
fi
mkdir -p ${LclRpt} 2>/dev/null
CurDir=$(pwd)
cd ${LclRpt}
###################
# Retrieve
_echo "Retrieve of the iso file in progress..."
_wget ${CDRepo} MD5SUMS
Search=$(grep ${IsoMsk} MD5SUMS)
IsoMd5s=$(echo ${Search} | awk '{print $1}')
IsoFile=$(echo ${Search} | awk '{print $2}')
_log "$(ls -l ${LclRpt}/${IsoFile})"
if [ ! ${Retry} ] ; then
_log "rm -f ${LclRpt}/${IsoFile} 2>/dev/null"
rm -f ${LclRpt}/${IsoFile} 2>/dev/null
fi
_wget ${CDRepo} ${IsoFile}
_log "The md5sum is: ${IsoMd5s}"
_echo "Cheking md5 sum in progress..."
if [ $(md5sum ${IsoFile} | awk '{print $1}') != ${IsoMd5s} ] ; then
_echo "[ERROR] The md5 sum is not check."
_end 42
fi
_echo "Retrieved iso file is: ${IsoFile}"
###################
_echo "The preparation of ${DevUsb} is in progress..."
# kiyop - _log "mkfs /dev/${DevUsb}"
# kiyop - mkfs /dev/${DevUsb}
_log "dd if=/dev/zero bs=512 count=1 of=/dev/${DevUsb}"
dd if=/dev/zero bs=512 count=1 of=/dev/${DevUsb}
_log "dd if=${LclRpt}/${IsoFile} of=/dev/${DevUsb}"
dd if=${LclRpt}/${IsoFile} of=/dev/${DevUsb} ; sync
_echo "The preparation of ${DevUsb} is done."
###################
# Clean up
_echo "Clean-up in progress..."
cd ${CurDir}
rm -f ${LclRpt}/MD5SUMS 2>/dev/null
_log "Clean-up done."
###################
# End annoucement
_echo "Stick preparation done. All details in ${LogFile} ."
_log "${Name} - end."