-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
img-mode
executable file
·423 lines (357 loc) · 14.8 KB
/
img-mode
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/bin/bash
DIRECTORY="$(dirname "$0")"
#ensure prerequesites are installed
"${DIRECTORY}/installgui" "yad" &>/dev/null || exit 0
#kill all subprocesses on exit
trap 'kill $(jobs -p)' EXIT
trap 'killall img-mode' EXIT
#ERRORBOX
errorbox(){
#$1 is message
#$2 is type: fatal or left blank
if [ -z "$2" ];then
yad --title='Pi Power Tools - Error' --window-icon="${DIRECTORY}/icons/logo-64.png" \
--text="Oh no!\n\n<b>$1</b>\nTry again?" \
--center --width=300 --text-align=center \
--button="OK!${DIRECTORY}/icons/forward.png:0" 2>/dev/null
elif [ "$2" = 'fatal' ];then
yad --title='Pi Power Tools - Error' --window-icon="${DIRECTORY}/icons/logo-64.png" \
--text="Fatal error:\n<b>$1</b>\nClosing in 30 seconds." \
--center --width=300 --text-align=center \
--timeout=30 --timeout-indicator=bottom \
--button="Close!${DIRECTORY}/icons/exit.png:0" 2>/dev/null
exit 1
else
errorbox 'errorbox error' fatal
fi
}
function imgfromsystem {
echo "This feature creates an .img file from your Pi"\'"s internal SD card.
You can save the img anywhere, but please make sure there is enough free space to save it!
Rsync copies individual files to the image instead of using dd. This approach is much faster, saves space, and lets you do incremental backups." | yad \
--title='Please read this' --window-icon="${DIRECTORY}/icons/logo-64.png" --text-info \
--center --wrap --width=500 --height=200 --fontname=12 \
--button=Cancel!"${DIRECTORY}/icons/exit.png":1 --button="Continue"!"${DIRECTORY}/icons/forward.png":0
#only do anything if user clicks Continue
if [ $? == 0 ];then
savedimg="$( yad --file --save --center --text="Tip: You can select a pre-existing img file and rsync will only copy new/changed files." --filename="$HOME/mynewimage.img" --title='Where to save this image?' --window-icon="${DIRECTORY}/icons/logo-64.png")"
if [ ! -z "$savedimg" ];then
(
echo "Checking for large files on filesystem..."
lfiles="$(find /home -mount -type f -size +1G 2>/dev/null)"
echo "Done"
if [ ! -z "$lfiles" ];then
#indent each file
PREIFS="$IFS"
IFS=$'\n'
for file in $lfiles
do
displaylfiles="${displaylfiles}\n ${file}"
done
IFS="$PREIFS"
echo -e "These files are larger than 1GB:\n${displaylfiles}" | yad --text-info \
--title='Exclude large files?' --window-icon="${DIRECTORY}/icons/logo-64.png" --text-info \
--center --wrap --width=500 --height=200 --fontname=12 \
--button='Include these files in the img':1 --button="Skip these files":0
if [ "$?" == '0' ];then
#'large files not included' enabled
rsyncargs="${rsyncargs} --max-size=1g"
fi
fi
#if img exists, expand it to 1TB
if [ -f "$savedimg" ];then
echo "Expanding image to a 1 TB sparse file. It won"\'"t actually take up 1TB, don"\'"t worry."
sudo "${DIRECTORY}/functions/image-shrink" "$savedimg" $((1024*1024)) || errorbox "Failed to expand image!" fatal
fi
echo "Starting backup now."
sudo "${DIRECTORY}/functions/milliways-image-backup" "$savedimg" "$rsyncargs" || errorbox "Failed to backup image!" fatal
echo "Backup finished. Shrinking image now."
sudo "${DIRECTORY}/functions/image-shrink" "$savedimg" 0 || errorbox "Failed to shrink image!" fatal
echo "All done. Closing in 10 seconds."
sleep 10
pkill yad
) 2>&1 | yad --text-info --tail \
--center --width=900 --height=500 --window-icon="${DIRECTORY}/icons/logo-64.png" --title='Creating image from this SD card' \
--button=Cancel!"${DIRECTORY}/icons/exit.png":"pkill -P $$" --back=black --fore='#0040c0'
fi
fi
img=''
}
#DOWNLOAD
function download {
downloadicons="${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/rpi-logo.png\n${DIRECTORY}/icons/twister.png"
downloadlist="$(cat ${DIRECTORY}/data/mirrors | grep -v ^\# | awk -F ' ' '{print $1" "$2" "$2" "$3}')"
downloadlist="$(paste -d '\n' <(echo -e "$downloadicons") <(echo -e "$downloadlist"))"
downloadlist="$(echo "$downloadlist" | tr ' ' '\n')"
repeat=1
while [ $repeat = 1 ];do
output="$(echo -e "$downloadlist" | yad --list --separator='\n' --center --height=400 --width=700 \
--title='Download new IMG' --window-icon="${DIRECTORY}/icons/logo-64.png" \
--text="Select IMG to download." --column=:IMG --column=Name --column='Size:SZ' --column='bytes:HD' --column=URL \
--button='Back'!"${DIRECTORY}/icons/back.png"!:1 \
--button='Download'!"${DIRECTORY}/icons/download.png":0 \
2>/dev/null)"
button=$?
if [ ! $button -eq 0 ];then
img=''
return
fi
echo "Output: ${output}EOO"
if [ -z "$output" ];then
errorbox "You need to select a download site."
else
dlname="$(echo "$output" | sed -n 2p)"
dlsize="$(echo "$output" | sed -n 4p)"
dlurl="$(echo "$output" | sed -n 5p)"
#getting the filesize over the internet doesn't work for Raspbian Full for some reason.
#dlsize="$(wget -qO- "$dlurl" | dd bs=1M count=1 | busybox unzip -lq - | sed -n 3p | awk '{print $1}')"
dlfilename="$(wget -qO- "$dlurl" | dd bs=1M count=1 2>/dev/null | busybox unzip -lq - 2>/dev/null | sed -n 3p | awk '{print $4}')"
echo "Download URL: $dlurl"
echo "Download size: $dlsize"
echo "Download name: $dlname"
echo "Download filename: $dlfilename"
if [ -z "$dlsize" ] || [ -z "$dlfilename" ];then
errorbox "Could not connect to server. Likely no Internet connection." fatal
fi
repeat=0
fi
done
#past this point, a download url is selected.
startseconds=$(cat /proc/uptime | awk '{print $1}' | awk '{print int($1+0.5)}')
(echo "0"
#Download-img is a temporary folder the disk image is downloaded to.
#The script has to get the full filename.
#Temporarily storing in this folder prevents issues if there is already a img in the home directory.
echo "# Removing residual folder ${HOME}/download-img"
rm -rf "${HOME}/download-img"
mkdir "${HOME}/download-img"
cd "${HOME}/download-img"
curbytes=1
while true;do
curbytes="$(ls -al * 2>/dev/null | awk '{print $5}')"
#echo $curbytes
progress="$((curbytes/(dlsize/100)))"
if [ $progress -gt 98 ];then
progress=98
fi
echo $progress
echo "# Downloading ${dlname}: $(numfmt --to=iec --suffix=B --padding=7 $curbytes)"
sleep 0.1
done &
childpid=$!
trap "kill $childpid" EXIT
#add first 10MB to cache. This speeds the download process.
wget -qO- "$dlurl" | "${DIRECTORY}/functions/buffer" | busybox unzip - -p | dd ibs=10M obs=10M | dd bs=10M count=1 of=/dev/null 2>/dev/null
trap "killall wget" EXIT
wget -qO- "$dlurl" | "${DIRECTORY}/functions/buffer" | busybox unzip - -p | "${DIRECTORY}/functions/buffer" | dd of="${HOME}/download-img/ppt-dl-${dlfilename}" bs=10M
kill "$childpid"
sleep 5
imagelocation="$(find "$(pwd)" -type f)"
echo "Image location: $imagelocation"
filename="$(echo "$imagelocation" | awk -F '/' 'NF>1{print $NF}')"
cd "$HOME"
echo "# Moving to $HOME"
mv "$imagelocation" "$HOME"
echo "# deleting temporary folder"
rm -r "${HOME}/download-img"
echo "${HOME}/${filename}" >> "${DIRECTORY}/data/imglist"
img="${HOME}/${filename}"
echo "# Done."
echo 100
) |
yad --progress --title="Downloading $dlname" --auto-close --auto-kill --center \
--window-icon="${DIRECTORY}/icons/logo-64.png" \
--button='Cancel'!"${DIRECTORY}/icons/exit.png":1 \
2>/dev/null || echo 'canceled'
echo "closed"
endseconds=$(cat /proc/uptime | awk '{print $1}' | awk '{print int($1+0.5)}')
(echo "# Download finished. Elapsed time: $((endseconds-startseconds)) seconds."
) | yad --progress --title="Downloading $dlname" --center \
--window-icon="${DIRECTORY}/icons/logo-64.png" \
--button='OK'!"${DIRECTORY}/icons/forward.png":0 \
2>/dev/null
}
#SELECT IMG
function selectimg {
while [ -z "$img" ] || [ ! -f "$img" ];do
#parse imglist
imglist="$(${DIRECTORY}/functions/imglist-parser || errorbox 'Failed to parse imglist!')"
img=''
img="$(yad --form --separator='\n' \
--title='Pi Power Tools - IMG Mode' --center --window-icon="${DIRECTORY}/icons/logo-64.png" \
--field="Select an img to continue.":LBL '' \
--field=":CBE" "$imglist" \
--button='Home'!"${DIRECTORY}/icons/home.png"!'Back to home':4 \
--button='SD to IMG'!"${DIRECTORY}/icons/sd-to-img.png"!"Creates an .img file from your Pi"\'"s internal SD card.:6" \
--button='Download New'!"${DIRECTORY}/icons/download.png":2 \
--button=Next!"${DIRECTORY}/icons/forward.png":0 \
2>/dev/null)"
button=$?
#if neither next nor download nor usb mode buttons pressed
if [ ! $button -eq 2 ]&&[ ! $button -eq 0 ]&&[ ! $button -eq 4 ]&&[ ! $button -eq 6 ];then
#exit the program
exit 0
fi
if [ $button -eq 4 ];then
img=''
"${DIRECTORY}/home" &
exit 0
fi
#if clicked next
if [ $button -eq 0 ];then
if [ -z $img ];then
errorbox 'No image specified.'
else
#check if file:// appended to filename
if [[ "$img" == *"file://"* ]];then
img="${img:8}"
echo -e "removing file:// from filename"
#take off last letter
img="${img::-1}"
else
#remove blank line break
img="$(echo -e "$img" | sed -n 2p)"
fi
fi
fi
#if clicked download
if [ $button -eq 2 ];then
img=''
download
fi
#if clicked IMG from this SD
if [ $button -eq 6 ];then
img=''
imgfromsystem
fi
done
echo "$img" >> "${DIRECTORY}/data/imglist"
}
if [ ! -z $1 ];then
img="$1"
if [ -f "$img" ] && [[ "$img" == *".img" ]];then
#file exists
#clear variables
set ''
else
echo "variable: $1"
errorbox "The specified IMG file does not exist." fatal
fi
fi
while true;do
if [ -z "$img" ];then
selectimg
fi
#if yad exits uncleanly it leaves behind a problematic memory file
#this deletes it
ipcrm -M 12345 &>/dev/null
echo "Image file selected: $img"
output="$(yad --form --columns=4 --separator='\n' \
--title='Pi Power Tools - IMG Mode' --center --window-icon="${DIRECTORY}/icons/logo-64.png" \
--text="Editing <u><b>$img</b></u>
IMG-specific tools:" \
--field='Reset PT'!"${DIRECTORY}/icons/backup.png"!'Messed up the partitions? This tries to fix it.
Reverts the partition table to the default one from Raspbian Buster.':FBTN "${DIRECTORY}/functions/terminal-run \"
if ${DIRECTORY}/functions/restore-pt $img ; then
echo -en '\n\e[32mCommand succeeded.\e[39m\nClosing in 10 seconds.'
sleep 10
else
echo -en '\n\e[31mCommand failed!\e[39m\nPress Enter to exit.'
read enter
fi
\" \"Resetting partition table of $img\" " \
--field="Advmount"!"${DIRECTORY}/icons/usb-mount.png"!'Control exactly how and where to mount each partition':FBTN "${DIRECTORY}/functions/advmount $img" \
--field="Shrink"!"${DIRECTORY}/icons/shrink.png"!'Removes all free space. IMG is made as small as possible.':FBTN "${DIRECTORY}/functions/terminal-run \"
if sudo ${DIRECTORY}/functions/image-shrink $img 0; then
#run zerofree if enabled
if [ $(cat "${DIRECTORY}/data/home.conf" | sed -n 7p) = TRUE ];then
echo -e '\e[32mCommand succeeded.\e[39m Now running zerofree:'
${DIRECTORY}/functions/zerofree_runner $img
else
echo -en '\n\e[32mCommand succeeded.\e[39m\nClosing in 10 seconds.'
fi
sleep 10
else
echo -en '\n\e[31mCommand failed!\e[39m\nPress Enter to exit.'
read enter
fi
\" \"Shrinking $img\"" \
--field="$(cat "${DIRECTORY}/data/home.conf" | sed -n 8p)GB Free"!"${DIRECTORY}/icons/expand.png"!'Ran out of space? Add some free space!':FBTN "${DIRECTORY}/functions/terminal-run \"
if sudo ${DIRECTORY}/functions/image-shrink $img $(($(cat "${DIRECTORY}/data/home.conf" | sed -n 8p)*1024)) ; then
echo -en '\n\e[32mCommand succeeded.\e[39m\nClosing in 10 seconds.'
sleep 10
else
echo -en '\n\e[31mCommand failed!\e[39m\nPress Enter to exit.'
read enter
fi
\" \"Adding $(cat "${DIRECTORY}/data/home.conf" | sed -n 8p)GB free space to $img\"" \
\
--button=Back!"${DIRECTORY}/icons/back.png"!:3 \
--button=Flash!"${DIRECTORY}/icons/burn.png"!'Copy everything from this img to a device.:2' \
--button=Boot!"${DIRECTORY}/icons/power.png"!'Boots the selected like a virtual machine.:4' \
--button=View!"${DIRECTORY}/icons/search.png"!'Browse the contents like you would a real storage device.:6' \
--button=Edit!"${DIRECTORY}/icons/gparted.png"!'This edits the partitions using gparted.:8' \
--buttons-layout=end \
2>/dev/null)"
button=$? #get exit code to determine which button was pressed
echo "${output}EOO"
#echo "$button"
if [ $button -eq 252 ];then #if window manager x was pressed
exit 0
fi
mntpnt="$(cat ${DIRECTORY}/data/mountpoint)"
case $button in
1)
errorbox "YAD exited with an error." fatal
;;
3)
echo "Back"
img=''
;;
4)
echo "Boot"
"${DIRECTORY}/installgui" "systemd-container xserver-xephyr expect" &>/dev/null || exit 0
"${DIRECTORY}/functions/terminal-run" "sudo ${DIRECTORY}/vdesktop/vdesktop $img $(cat "${DIRECTORY}/data/vdesktop.conf");sleep 10" "Boot Console"
sleep 10
#wait until vdesktop is closed before reopening UI
while ps -C vdesktop >/dev/null;do sleep 1; done
;;
6)
echo "View"
LOOP="$(sudo losetup -fP --show $img)" #create loopback device
sudo mkdir "$mntpnt" 2>/dev/null
sudo mount -o rw "${LOOP}p2" "$mntpnt"
sudo mount -o rw "${LOOP}p1" "${mntpnt}/boot"
pcmanfm -n "$mntpnt" #view the files
yad --title='Viewing IMG' --window-icon="${DIRECTORY}/icons/logo-64.png" \
--text="Close this window when you are finished viewing the files." \
--center --text-align=center \
--button="Close!${DIRECTORY}/icons/exit.png:1" 2>/dev/null
sudo losetup -d "$LOOP" &>/dev/null #delete loopback device
sudo umount -fl "${mntpnt}/boot" &>/dev/null
sudo umount -fl "$mntpnt" &>/dev/null
;;
8)
"${DIRECTORY}/installgui" "gparted" &>/dev/null || exit 0
echo image
LOOP="$(sudo losetup -fP --show $img)" #create loopback device
echo $LOOP
if [ -z $LOOP ];then
echo "Could not create loopback device."
exit 1
fi
sudo gparted "$LOOP"
sudo losetup -d "$LOOP" &>/dev/null #delete loopback device
;;
2)
echo "Flash"
"${DIRECTORY}/flash" "$img"
;;
*)
echo "unknown"
errorbox "Unknown button pressed: $button" fatal
;;
esac
echo ""
done
exit