-
Notifications
You must be signed in to change notification settings - Fork 1
/
write2usb
executable file
·624 lines (564 loc) · 12.9 KB
/
write2usb
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/bin/bash
# Description: Write operational image to usb
# Destination: /usr/local/bin/write2usb
USB="/dev/sda"
ERROR="$(ls {/dev/sdb,/dev/sdc,/dev/sdd} 2> /dev/null)"
PI_EEPROM="pieeprom-2021-12-02"
WHT="\e[1;37m"
YLW="\e[1;33m"
FIN="\e[0m"
### RUN AS ROOT
run_as_root(){
if [ "$USER" != "root" ]; then
echo "Please run this as root or with sudo privileges."
exit 1
fi
}
internet_check(){
if [[ `command -v wget` ]]; then
[[ `wget -S --spider http://github.com 2>&1 | grep 'HTTP/1.1 200 OK'` ]];
else
if [[ `curl -I https://github.com 2>&1 | grep 'HTTP/2 200'` ]]; then
echo -e "${WHT}Installing missing depends${FIN}."
sudo apt update
sudo apt upgrade -y
sudo apt install -y wget;
else
echo -e "This script requires an internet connection."
exit;
fi
fi
if [[ `command -v aria2c` ]]; then
echo -e "${WHT}Installing missing depends${FIN}."
sudo apt update
sudo apt upgrade -y
sudo apt install -y aria2;
fi
}
correct(){
if [ -e "$USB" ]; then
echo ""
echo -e "${WHT}$USB found${FIN}."
else
check_node
fi
}
confirm(){
echo -e "${YLW}If more than one USB storage device is connected"
echo -e "then select no... or risk losing important data${FIN}."
echo -e ""
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) echo && echo -e "${WHT}Confirmed${FIN}!";;
n|N ) echo && echo -e "${YLW}You have chosen${FIN}... ${YLW}wisely${FIN}." && exit 0;;
* ) echo && echo -e "${YLW}Invalid choice${FIN}." && exit 0;;
esac
}
check_progressbar(){
DPB="sudo wget -cq https://raw.githubusercontent.com/pyavitz/bash_progress_bar/master/progress_bar.sh -P /etc/opt/"
MPB="sudo mv -f /etc/opt/progress_bar.sh /etc/opt/progressbar.txt"
if [ -e /etc/opt/progressbar.txt ]; then
:;
else
$DPB;
$MPB;
fi
}
### CHECK FOR NODE
check_node(){
echo ""
echo -e "You ${WHT}don't${FIN} have a ${WHT}USB storage device${FIN} connected."
while [ true ] ; do
read -t 3 -n 1
if [ $? = 0 ] ; then
exit ;
else
dialog --infobox "Please connect a usb storage device" 3 40
fi
done
}
error_node(){
echo ""
echo -e "You have one to many ${WHT}USB storage devices${FIN} connected."
while [ true ] ; do
read -t 3 -n 1
if [ $? = 0 ] ; then
exit ;
else
dialog --infobox "Please connect only ONE usb storage device" 3 40
fi
done
}
### EEPROM
update_eeprom(){
if [ -e /lib/firmware/raspberrypi/bootloader/stable/${PI_EEPROM}.bin ]; then
:;
else
if [[ `command -v deb-eeprom` ]]; then
deb-eeprom -u;
sleep 1s;
deb-eeprom -U;
sleep 1s;
else
echo "";
echo -e "${YLW}Something went wrong${FIN}?";
exit;
fi
fi
if [ -e /lib/firmware/raspberrypi/bootloader/stable/${PI_EEPROM}.bin ]; then
:;
else
echo "";
echo -e "${YLW}Something went wrong${FIN}?";
exit;
fi
sed -i 's/FIRMWARE_RELEASE_STATUS="default"/FIRMWARE_RELEASE_STATUS="stable"/g' /etc/default/rpi-eeprom-update
sed -i 's/FIRMWARE_RELEASE_STATUS="critical"/FIRMWARE_RELEASE_STATUS="stable"/g' /etc/default/rpi-eeprom-update
sed -i 's/FIRMWARE_RELEASE_STATUS="beta"/FIRMWARE_RELEASE_STATUS="stable"/g' /etc/default/rpi-eeprom-update
sudo rpi-eeprom-update -d -f /lib/firmware/raspberrypi/bootloader/stable/${PI_EEPROM}.bin
}
### EXT4 PARTITION
partition_ext4(){
partprobe ${USB}
sleep 4s
dd if=/dev/zero of=${USB} bs=512 count=1
truncate -s 2200MB ${USB}
fdisk ${USB} <<EOF
o
n
p
1
2048
524287
a
c
t
b
n
p
2
524288
p
w
EOF
partprobe ${USB}
echo 'y' | mkfs.vfat -n BOOT ${USB}1
echo 'y' | mkfs.ext4 -L ROOTFS ${USB}2
mkdir -p /mnt/p1 /mnt/p2
mount ${USB}1 /mnt/p1
mount ${USB}2 /mnt/p2
sync
umount /mnt/p2
mount -o defaults,noatime ${USB}2 /mnt/p2
bash growpart ${USB} 2 > /dev/null 2>&1
sleep 1s
resize2fs ${USB}2 > /dev/null 2>&1
sleep 1s
}
### F2FS PARTITION
partition_f2fs(){
partprobe ${USB}
sleep 4s
dd if=/dev/zero of=${USB} bs=512 count=1
truncate -s 2200MB ${USB}
fdisk ${USB} <<EOF
o
n
p
1
2048
524287
a
c
t
b
n
p
2
524288
p
w
EOF
partprobe ${USB}
echo 'y' | mkfs.vfat -n BOOT ${USB}1
echo 'y' | mkfs.f2fs -f -l ROOTFS ${USB}2
mkdir -p /mnt/p1 /mnt/p2
mount ${USB}1 /mnt/p1
mount ${USB}2 /mnt/p2
sync
umount /mnt/p2
bash resize.f2fs ${USB}2 > /dev/null 2>&1
mount -o rw,acl,active_logs=6,background_gc=on,user_xattr ${USB}2 /mnt/p2
sleep 1s
}
### BTRFS PARTITION
partition_btrfs(){
partprobe ${USB}
sleep 4s
dd if=/dev/zero of=${USB} bs=512 count=1
truncate -s 2200MB ${USB}
fdisk ${USB} <<EOF
o
n
p
1
2048
524287
a
c
t
b
n
p
2
524288
p
w
EOF
partprobe ${USB}
echo 'y' | mkfs.vfat -n BOOT ${USB}1
echo 'y' | mkfs.btrfs -f -L ROOTFS ${USB}2
mkdir -p /mnt/p1 /mnt/p2
mount ${USB}1 /mnt/p1
mount ${USB}2 /mnt/p2
btrfs subvolume create /mnt/p2/@
sync
umount /mnt/p2
mount -o compress=lzo,noatime,subvol=@ ${USB}2 /mnt/p2
bash growpart ${USB} 2 > /dev/null 2>&1
sleep 1s
btrfs filesystem resize max ${USB}2 > /dev/null 2>&1
sleep 1s
}
### RSYNC
transfer(){
cd /
cp -fdrP /boot/* /mnt/p1/
rsync -apvx --progress --stats --human-readable . /mnt/p2 > /dev/null
}
transfer_to_usb(){
source /etc/opt/progressbar.txt
enable_trapping
setup_scroll_area
for i in {0..100}
do
if [ $i = 0 ]; then
block_progress_bar $i
else
transfer
draw_progress_bar $i
fi
done
destroy_scroll_area
}
### FETCH UUID
sleep 1s
partition_uuid(){
echo 'BOOT_UUID="' > boot1
blkid -o export -- "${USB}1" | sed -ne 's/^UUID=//p' > boot2
echo '"' > boot3
paste -d '\0' boot1 boot2 boot3 > /etc/opt/boot-id.txt
rm -f boot1 boot2 boot3
echo 'ROOT_UUID="' > root1
blkid -o export -- "${USB}2" | sed -ne 's/^UUID=//p' > root2
echo '"' > root3
paste -d '\0' root1 root2 root3 > /etc/opt/root-id.txt
rm -f root1 root2 root3
echo 'ROOT_PARTUUID="' > root1
blkid -o export -- "${USB}2" | sed -ne 's/^PARTUUID=//p' > root2
echo '"' > root3
paste -d '\0' root1 root2 root3 > /etc/opt/root-pid.txt
rm -f root1 root2 root3
}
### CMDLINE
ext4_cmdline(){
source /etc/opt/root-pid.txt
rm -f /mnt/p1/cmdline.txt
tee /mnt/p1/cmdline.txt <<EOF
console=serial0,115200 console=tty1 root=PARTUUID=${ROOT_PARTUUID} rootfstype=ext4 fsck.repair=yes logo.nologo net.ifnames=0 firmware_class.path=/lib/firmware/updates/brcm rootwait
EOF
}
f2fs_cmdline(){
source /etc/opt/root-pid.txt
rm -f /mnt/p1/cmdline.txt
tee /mnt/p1/cmdline.txt <<EOF
console=serial0,115200 console=tty1 root=PARTUUID=${ROOT_PARTUUID} rootfstype=f2fs fsck.repair=yes logo.nologo net.ifnames=0 firmware_class.path=/lib/firmware/updates/brcm rootwait
EOF
}
btrfs_cmdline(){
source /etc/opt/root-pid.txt
rm -f /mnt/p1/cmdline.txt
tee /mnt/p1/cmdline.txt <<EOF
console=serial0,115200 console=tty1 root=PARTUUID=${ROOT_PARTUUID} rootfstype=btrfs rootflags=subvol=@ fsck.repair=yes logo.nologo net.ifnames=0 firmware_class.path=/lib/firmware/updates/brcm rootwait
EOF
}
### EXTLINUX
ext4_extlinux(){
source /etc/opt/root-pid.txt
rm -f /mnt/p1/extlinux/extlinux.conf
tee /mnt/p1/extlinux/extlinux.conf <<EOF
label default
kernel /Image
append console=tty1 console=ttyS0,115200 console=both 8250.nr_uarts=1 rw root=PARTUUID=${ROOT_PARTUUID} rootfstype=ext4 fsck.repair=yes logo.nologo net.ifnames=0 cma=320M firmware_class.path=/lib/firmware/updates/brcm rootwait
EOF
}
f2fs_extlinux(){
source /etc/opt/root-pid.txt
rm -f /mnt/p1/extlinux/extlinux.conf
tee /mnt/p1/extlinux/extlinux.conf <<EOF
label default
kernel /Image
append console=tty1 console=ttyS0,115200 console=both 8250.nr_uarts=1 rw root=PARTUUID=${ROOT_PARTUUID} rootfstype=f2fs fsck.repair=yes logo.nologo net.ifnames=0 cma=320M firmware_class.path=/lib/firmware/updates/brcm rootwait
EOF
}
btrfs_extlinux(){
source /etc/opt/root-pid.txt
rm -f /mnt/p1/extlinux/extlinux.conf
tee /mnt/p1/extlinux/extlinux.conf <<EOF
label default
kernel /Image
append console=tty1 console=ttyS0,115200 console=both 8250.nr_uarts=1 rw root=PARTUUID=${ROOT_PARTUUID} rootfstype=btrfs rootflags=subvol=@ fsck.repair=yes logo.nologo net.ifnames=0 cma=320M firmware_class.path=/lib/firmware/updates/brcm rootwait
EOF
}
### FSTAB
ext4_fstab(){
source /etc/opt/boot-id.txt
source /etc/opt/root-id.txt
rm -f /mnt/p2/etc/fstab
tee /mnt/p2/etc/fstab <<EOF
UUID=${BOOT_UUID} /boot vfat defaults 0 2
UUID=${ROOT_UUID} / ext4 defaults,noatime,nodiratime,commit=600,errors=remount-ro 0 1
tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
}
f2fs_fstab(){
source /etc/opt/boot-id.txt
source /etc/opt/root-id.txt
rm -f /mnt/p2/etc/fstab
tee /mnt/p2/etc/fstab <<EOF
UUID=${BOOT_UUID} /boot vfat defaults 0 2
UUID=${ROOT_UUID} / f2fs rw,acl,active_logs=6,background_gc=on,user_xattr 0 0
tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
}
btrfs_fstab(){
source /etc/opt/boot-id.txt
source /etc/opt/root-id.txt
rm -f /mnt/p2/etc/fstab
tee /mnt/p2/etc/fstab <<EOF
UUID=${BOOT_UUID} /boot vfat defaults 0 2
UUID=${ROOT_UUID} / btrfs defaults,compress=lzo,noatime,subvol=@ 0 1
tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF
}
### EXECUTION
run_ext4(){
run_as_root
internet_check
check_progressbar
if [ $ERROR ]; then
error_node;
else
correct
fi
echo
confirm
echo
echo -e "${WHT}Updating eeprom${FIN} ..."
update_eeprom
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Partitioning USB${FIN} ..."
partition_ext4 > /dev/null 2>&1
echo -e "${WHT}Done${FIN}."
echo
echo -e "${YLW}Please be patient as the boot and root filesystem${FIN}"
echo -e "${YLW}are transferred over to the USB drive${FIN}."
transfer_to_usb
clear
sleep 1s
echo
echo -e "${WHT}Fetching UUID${FIN} ..."
partition_uuid
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Creating new cmdline${FIN} ..."
if [[ `grep -w "bcm2711" "/etc/opt/soc.txt"` ]]; then
ext4_cmdline;
fi
if [[ `grep -w "bcm2711" "/etc/opt/board.txt"` ]]; then
ext4_extlinux;
fi
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Creating fstab${FIN} ..."
ext4_fstab
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Unmounting USB${FIN} ..."
rm -fdr /etc/opt/root-pid.txt /etc/opt/root-id.txt \
/etc/opt/boot-id.txt
rm -fdr /mnt/p2/mnt/*
umount /mnt/p2/boot > /dev/null 2>&1
umount /mnt/p2 > /dev/null 2>&1
umount /mnt/p1 > /dev/null 2>&1
rm -fdr /mnt/*
echo -e "${WHT}Done${FIN}."
echo
echo You may now power down and remove the sdcard.
}
run_f2fs(){
run_as_root
internet_check
if [[ `command -v mkfs.f2fs` ]]; then
:;
else
apt update;
apt install -y f2fs-tools;
fi
check_progressbar
if [ $ERROR ]; then
error_node;
else
correct
fi
echo
confirm
echo
echo -e "${WHT}Updating eeprom${FIN} ..."
update_eeprom
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Partitioning USB${FIN} ..."
partition_f2fs > /dev/null 2>&1
echo -e "${WHT}Done${FIN}."
echo
echo -e "${YLW}Please be patient as the boot and root filesystem${FIN}"
echo -e "${YLW}are transferred over to the USB drive${FIN}."
transfer_to_usb
clear
sleep 1s
echo
echo -e "${WHT}Fetching UUID${FIN} ..."
partition_uuid
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Creating new cmdline${FIN} ..."
if [[ `grep -w "bcm2711" "/etc/opt/soc.txt"` ]]; then
f2fs_cmdline;
fi
if [[ `grep -w "bcm2711" "/etc/opt/board.txt"` ]]; then
f2fs_extlinux;
fi
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Creating fstab${FIN} ..."
f2fs_fstab
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Unmounting USB${FIN} ..."
rm -fdr /etc/opt/root-pid.txt /etc/opt/root-id.txt \
/etc/opt/boot-id.txt
rm -fdr /mnt/p2/mnt/*
umount /mnt/p2/boot > /dev/null 2>&1
umount /mnt/p2 > /dev/null 2>&1
umount /mnt/p1 > /dev/null 2>&1
rm -fdr /mnt/*
echo -e "${WHT}Done${FIN}."
echo
echo You may now power down and remove the sdcard.
}
run_btrfs(){
run_as_root
internet_check
if [[ `command -v mkfs.btrfs` ]]; then
:;
else
apt update;
apt install -y btrfs-progs;
fi
check_progressbar
if [ $ERROR ]; then
error_node;
else
correct
fi
echo
confirm
echo
echo -e "${WHT}Updating eeprom${FIN} ..."
update_eeprom
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Partitioning USB${FIN} ..."
partition_btrfs > /dev/null 2>&1
echo -e "${WHT}Done${FIN}."
echo
echo -e "${YLW}Please be patient as the boot and root filesystem${FIN}"
echo -e "${YLW}are transferred over to the USB drive${FIN}."
transfer_to_usb
clear
sleep 1s
echo
echo -e "${WHT}Fetching UUID${FIN} ..."
partition_uuid
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Creating new cmdline${FIN} ..."
if [[ `grep -w "bcm2711" "/etc/opt/soc.txt"` ]]; then
btrfs_cmdline;
fi
if [[ `grep -w "bcm2711" "/etc/opt/board.txt"` ]]; then
btrfs_extlinux;
fi
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Creating fstab${FIN} ..."
btrfs_fstab
sleep 1s
echo -e "${WHT}Done${FIN}."
echo
echo -e "${WHT}Unmounting USB${FIN} ..."
rm -fdr /etc/opt/root-pid.txt /etc/opt/root-id.txt \
/etc/opt/boot-id.txt
rm -fdr /mnt/p2/mnt/*
umount /mnt/p2/boot > /dev/null 2>&1
umount /mnt/p2 > /dev/null 2>&1
umount /mnt/p1 > /dev/null 2>&1
rm -fdr /mnt/*
echo -e "${WHT}Done${FIN}."
echo
echo You may now power down and remove the sdcard.
}
echo -en "${WHT}"
options=("EXT4" "BTRFS" "F2FS" "Quit")
select opt in "${options[@]}"
do
case $opt in
"EXT4")
run_ext4
break
;;
"BTRFS")
run_btrfs
break
;;
"F2FS")
run_f2fs
break
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
echo -en "${FIN}"