forked from gotbletu/shownotes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mountjutsu
executable file
·1924 lines (1835 loc) · 78.5 KB
/
mountjutsu
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
### _ _ _ _
### __ _ ___ | |_| |__ | | ___| |_ _ _
### / _` |/ _ \| __| '_ \| |/ _ \ __| | | |
###| (_| | (_) | |_| |_) | | __/ |_| |_| |
### \__, |\___/ \__|_.__/|_|\___|\__|\__,_|
### |___/
### https://www.youtube.com/user/gotbletu
### https://twitter.com/gotbletu
### https://github.com/gotbletu
###
### Author : gotbletu
### Name : mountjutsu
### Version : 0.2
### Date : 20190626
### Description : menu interface to mount, unmount, eject, clone, exactcopy, format, LUKS encryption
### Depends On : bash sudo grep gawk coreutils udisks2 util-linux
### gptfdisk dosfstools ntfs-3g hfsprogs exfatprogs e2fsprogs
### cryptsetup clonezilla partclone partimage vlc cdw f3 smartmontools
### dvdbackup libdvdread libdvdcss cdrdao cdrtools(or cdrkit)
### Video Demo : https://www.youtube.com/watch?v=jipILuNW5Ks
### 2021-01-16 : change to exfatprogs, exFAT 32KB or 64KB cluster
# Next improvements: 1) delete MBR on format https://www.cyberciti.biz/faq/linux-clearing-out-master-boot-record-dd-command/
# clear out leftover label and fstype
# $ sudo dd if=/dev/zero of=/dev/sdX bs=446 count=1
# 2) blinking text warning
#-------- Bash Color Code {{{
#------------------------------------------------------
# DESC: color code for bash compatible shell
# LINK: https://wiki.archlinux.org/index.php?title=Bash/Prompt_customization&oldid=419076#List_of_colors_for_prompt_and_Bash
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# }}}
#-------- Mount Device (udisksctl) {{{
#------------------------------------------------------
mount-udisksctl() {
if [ $# -lt 1 ]; then
echo -e "mount device like most GUI file manager"
echo -e "\nUsage: $0 <device|partition>"
echo -e "Example: $0 sdx"
echo -e " $0 sdx1"
echo -e "Multiple:$0 sdx sdy sdz"
echo -e " $0 sdx*"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
udisksctl mount -b /dev/"$arg"
done
}
unmount-udisksctl() {
if [ $# -lt 1 ]; then
echo -e "mount device like most GUI file manager"
echo -e "\nUsage: $0 <device|partition>"
echo -e "Example: $0 sdx"
echo -e " $0 sdx1"
echo -e "Multiple:$0 sdx sdy sdz"
echo -e " $0 sdx*"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
udisksctl unmount --force -b /dev/"$arg"
done
}
# }}}
#-------- Mount LUKS {{{
#------------------------------------------------------
mount-udisksctl-luks() {
if [ $# -lt 1 ]
then
echo -e "mount LUKS encrypted device like most GUI file manager"
echo -e "\nUsage: $0 <luks-device|partition>"
echo -e "Example: $0 sdx"
echo -e " $0 sdx1"
echo -e "Multiple:$0 sdx sdy sdz"
echo -e " $0 sdx*"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
udisksctl unlock -b /dev/"$arg"
sleep 0.1
dm_mountpoint=$(lsblk -o "KNAME,NAME" | grep -A 1 "$arg" | tail -1 | awk '{print $1}')
udisksctl mount -b /dev/"$dm_mountpoint"
done
}
unmount-udisksctl-luks() {
if [ $# -lt 1 ]
then
echo -e "unmount LUKS encrypted device like most GUI file manager"
echo -e "\nUsage: $0 <luks-device|partition>"
echo -e "Example: $0 sdx"
echo -e " $0 sdx1"
echo -e "Multiple:$0 sdx sdy sdz"
echo -e " $0 sdx*"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
dm_mountpoint=$(lsblk -o "KNAME,NAME" | grep -A 1 "$arg" | tail -1 | awk '{print $1}')
udisksctl unmount --force -b /dev/"$dm_mountpoint"
udisksctl lock -b /dev/"$arg"
done
}
mount-luks() {
if [ $# -lt 1 ]; then
echo -e "mount LUKS disk encrypted device"
echo -e "\nUsage: $0 <luks-device|partition>"
echo -e "Example: $0 sdX"
echo -e " $0 sdX1"
echo -e "Multiple:$0 sdx sdy sdz"
echo -e " $0 sdx*"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
mappername="$arg"
sudo cryptsetup luksOpen /dev/"$arg" "$mappername"
mkdir -p /tmp/"$arg"
sudo mount /dev/mapper/"$mappername" /tmp/"$arg"
done
}
unmount-luks() {
if [ $# -lt 1 ]; then
echo -e "unmount LUKS disk encrypted device"
echo -e "\nUsage: $0 <luks-device|partition>"
echo -e "Example: $0 sdX"
echo -e " $0 sdX1"
echo -e "Multiple:$0 sdx sdy sdz"
echo -e " $0 sdx*"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
mappername="$arg"
sudo umount /tmp/"$arg"
sudo cryptsetup luksClose "$mappername"
rmdir "/tmp/$arg"
done
}
# }}}
#-------- Eject Device {{{
#------------------------------------------------------
eject-udisksctl() {
if [ $# -lt 1 ]
then
echo -e "mount using gvfs"
echo -e "\nUsage:\n$0 <disc_image>"
echo -e "\nExample:\n$0 disc_image.iso"
echo -e "$0 disc_image.iso disc_image2.bin disc_image3.mdf"
echo -e "$0 *.iso"
return 1
fi
myArray=( "$@" )
for arg in "${myArray[@]}"; do
udisksctl power-off -b /dev/"$arg"
done
}
# }}}
#-------- Format USB v4 (MBR 2TB Max) [last updated February 03, 2018] {{{
#------------------------------------------------------
# DEMO: https://www.youtube.com/watch?v=7txO1cdNJsQ
# DESC: format USB and create a single partition
format2usb-ext() {
if [ $# -lt 3 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <filesystem:ext2|ext3|ext4> <device_label> <device_name>"
echo -e "Example: $0 ext2 MY_USB sdx"
echo -e " $0 ext3 MY_USB sdx"
echo -e " $0 ext4 MY_USB sdx"
return 1
fi
FSTYPE="$1"
DEVICE_LABEL="$2"
DEVICE_NAME="$3"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$DEVICE_NAME|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
echo -e "y\n" | sudo mkfs."$FSTYPE" -L "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Yellow}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
echo -e "${Green}>>>Change EXT filesystem 5% reserved space to 0% (increase storage space) ${Color_Off}"
MOUNTED_TESTMOUNT=$(df | awk '/testmount/ {print $1}')
sudo tune2fs -m 0 "$MOUNTED_TESTMOUNT"
sudo tune2fs -l "$MOUNTED_TESTMOUNT" | grep --color=auto 'Reserved block count'
echo -e "${Red}>>>Unmounting and cleanup ${Color_Off}"
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-fat32-32kbcluster() {
if [ $# -lt 2 ]; then
echo -e "format device to work with wii & gamecube games using FAT32 with 32KB cluster"
echo -e "FAT32 label max is 11 character and is all uppercase"
echo -e "(512 bytes per sector * 64 sectors per cluster)/ 1024 Bytes = 32KB clusters (32768 Bytes)"
echo -e "more info: https://gist.github.com/joshenders/4376942"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# fat32 likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\nb\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# b W95 FAT32
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.fat -S 512 -s 64 -F 32 -n "$DEVICE_LABEL" -I /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-fat32-64kbcluster() {
if [ $# -lt 2 ]; then
echo -e "format device to work with few gaming consoles using FAT32 with 64KB cluster"
echo -e "FAT32 label max is 11 character and is all uppercase"
echo -e "(512 bytes per sector * 128 sectors per cluster) / 1024 Bytes = 64KB clusters (65536 Bytes)"
echo -e "more info: https://gist.github.com/joshenders/4376942"
echo -e "https://askubuntu.com/a/190033"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# fat32 likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\nb\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# b W95 FAT32
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.fat -S 512 -s 128 -F 32 -n "$DEVICE_LABEL" -I /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-exfat() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "exFAT label max is 15 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# exFat likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\n7\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# 7 HPFS/NTFS/exFAT
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.exfat -n "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-exfat-32kbcluster() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "exFAT label max is 15 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# exFat likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\n7\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# 7 HPFS/NTFS/exFAT
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.exfat -c 32K -n "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-exfat-64kbcluster() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "exFAT label max is 15 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# exFat likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\n7\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# 7 HPFS/NTFS/exFAT
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.exfat -c 64K -n "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-fat16() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "FAT16 label max is 11 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# fat32 likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\ne\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p primary
# 1 partition number 1
# default, first sector
# default, last sector
# t change a partition type (L to list all types)
# e W95 FAT16
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.fat -F 16 -n "$DEVICE_LABEL" -I /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-fat32() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "FAT32 label max is 11 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# fat32 likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\nb\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# b W95 FAT32
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.fat -F 32 -n "$DEVICE_LABEL" -I /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-ntfs() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
DEVICE_LABEL="$1"
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$DEVICE_NAME|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\n7\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p print the partition table
# 1 partition number 1
# default, start immediately after preceding partition
# default, extend partition to end of disk
# t change a partition type (L to list all types)
# 7 HPFS/NTFS/exFAT
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.ntfs -f -L "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2usb-hfsplus-nonjournal() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
DEVICE_LABEL="$1"
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$DEVICE_NAME|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "d\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\nd\n\no\nn\np\n1\n\n\nt\naf\nw" | sudo fdisk /dev/"$DEVICE_NAME"
# delete partiton x8 using d\n\n
# d delete a partition
# default, partition
# o create a new empty DOS partition table
# n add a new partition
# p primary
# 1 partition number 1
# default, first sector
# default, last sector
# t change a partition type (L to list all types)
# af HFS / HFS+
# w write table to disk and exit
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.hfsplus -v "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
# }}}
#-------- GPT Format USB (HDD Greater Than 2TB ) [last updated February 04, 2018] {{{
#------------------------------------------------------
# DEMO: https://www.youtube.com/watch?v=7txO1cdNJsQ
# DESC: format USB and create a single partition
# REFF: https://www.funtoo.org/Partitioning_using_gdisk
# https://matthew.komputerwiz.net/2015/12/13/formatting-universal-drive.html
#-------- gdisk Hex code GUID GPT {{{
#------------------------------------------------------
# o create a new empty GUID partition table (GPT)
# y yes, proceed
# n add a new partition
# 1 partition number 1
# default, first sector
# default, last sector
# 0700 Hex code or GUID (0700 Microsoft basic data)
# w write table to disk and exit
# y yes, proceed
# Hex code or GUID (L to show codes, Enter = 8300): L
# 0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
# 3000 ONIE boot 3001 ONIE config 3900 Plan 9
# 4100 PowerPC PReP boot 4200 Windows LDM data 4201 Windows LDM metadata
# 4202 Windows Storage Spac 7501 IBM GPFS 7f00 ChromeOS kernel
# 7f01 ChromeOS root 7f02 ChromeOS reserved 8200 Linux swap
# 8300 Linux filesystem 8301 Linux reserved 8302 Linux /home
# 8303 Linux x86 root (/) 8304 Linux x86-64 root (/ 8305 Linux ARM64 root (/)
# 8306 Linux /srv 8307 Linux ARM32 root (/) 8400 Intel Rapid Start
# 8e00 Linux LVM a000 Android bootloader a001 Android bootloader 2
# a002 Android boot a003 Android recovery a004 Android misc
# a005 Android metadata a006 Android system a007 Android cache
# a008 Android data a009 Android persistent a00a Android factory
# a00b Android fastboot/ter a00c Android OEM a500 FreeBSD disklabel
# a501 FreeBSD boot a502 FreeBSD swap a503 FreeBSD UFS
# a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID a580 Midnight BSD data
# a581 Midnight BSD boot a582 Midnight BSD swap a583 Midnight BSD UFS
# a584 Midnight BSD ZFS a585 Midnight BSD Vinum a600 OpenBSD disklabel
# a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS
# a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted
# a906 NetBSD RAID ab00 Recovery HD af00 Apple HFS/HFS+
# af01 Apple RAID af02 Apple RAID offline af03 Apple label
# af04 AppleTV recovery af05 Apple Core Storage af06 Apple SoftRAID Statu
# af07 Apple SoftRAID Scrat af08 Apple SoftRAID Volum af09 Apple SoftRAID Cache
# b300 QNX6 Power-Safe bc00 Acronis Secure Zone be00 Solaris boot
# bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap
# bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home
# bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2
# bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
# c001 HP-UX data c002 HP-UX service e100 ONIE boot
# e101 ONIE config ea00 Freedesktop $BOOT eb00 Haiku BFS
# ed00 Sony system partitio ed01 Lenovo system partit ef00 EFI System
# ef01 MBR partition scheme ef02 BIOS boot partition f800 Ceph OSD
# f801 Ceph dm-crypt OSD f802 Ceph journal f803 Ceph dm-crypt journa
# f804 Ceph disk in creatio f805 Ceph dm-crypt disk i fb00 VMWare VMFS
# fb01 VMWare reserved fc00 VMWare kcore crash p fd00 Linux RAID
# }}}
format2gpt-ext() {
if [ $# -lt 3 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <filesystem:ext2|ext3|ext4> <device_label> <device_name>"
echo -e "Example: $0 ext2 MY_USB sdx"
echo -e " $0 ext3 MY_USB sdx"
echo -e " $0 ext4 MY_USB sdx"
return 1
fi
FSTYPE="$1"
DEVICE_LABEL="$2"
DEVICE_NAME="$3"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$DEVICE_NAME|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "o\ny\nn\n1\n\n\n8300\nw\ny\n" | sudo gdisk /dev/"$DEVICE_NAME"
# o create a new empty GUID partition table (GPT) [Delete All Partition]
# y yes, proceed
# n add a new partition
# 1 partition number 1
# default, first sector
# default, last sector
# 8300 Hex code or GUID [8300 Linux filesystem]
# w write table to disk and exit
# y yes, proceed
echo -e "${Red}>>>Formatting the device ${Color_Off}"
echo -e "y\n" | sudo mkfs."$FSTYPE" -L "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Yellow}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
echo -e "${Green}>>>Change EXT filesystem 5% reserved space to 0% (increase storage space) ${Color_Off}"
MOUNTED_TESTMOUNT=$(df | awk '/testmount/ {print $1}')
sudo tune2fs -m 0 "$MOUNTED_TESTMOUNT"
sudo tune2fs -l "$MOUNTED_TESTMOUNT" | grep --color=auto 'Reserved block count'
echo -e "${Red}>>>Unmounting and cleanup ${Color_Off}"
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2gpt-exfat() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "exFAT label max is 15 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# exFat likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "o\ny\nn\n1\n\n\n0700\nw\ny\n" | sudo gdisk /dev/"$DEVICE_NAME"
# o create a new empty GUID partition table (GPT) [Delete All Partition]
# y yes, proceed
# n add a new partition
# 1 partition number 1
# default, first sector
# default, last sector
# 0700 Hex code or GUID [0700 Microsoft basic data]
# w write table to disk and exit
# y yes, proceed
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.exfat -n "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2gpt-fat32() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "FAT32 label max is 11 character and is all uppercase"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
# fat32 likes the labels to be in uppercase
DEVICE_LABEL=$(echo "$1" | tr '[:lower:]' '[:upper:]')
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$2|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "o\ny\nn\n1\n\n\n0700\nw\ny\n" | sudo gdisk /dev/"$DEVICE_NAME"
# o create a new empty GUID partition table (GPT) [Delete All Partition]
# y yes, proceed
# n add a new partition
# 1 partition number 1
# default, first sector
# default, last sector
# 0700 Hex code or GUID [0700 Microsoft basic data]
# w write table to disk and exit
# y yes, proceed
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.fat -F 32 -n "$DEVICE_LABEL" -I /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
format2gpt-ntfs() {
if [ $# -lt 2 ]; then
echo -e "format and create a partition that fills up the whole device"
echo -e "\nUsage: $0 <label> <device>"
echo -e "Example: $0 MY_USB sdx"
return 1
fi
DEVICE_LABEL="$1"
DEVICE_NAME="$2"
echo -e "${Yellow}>>>Checking if device is mounted ${Color_Off}"
MOUNT_STATUS=$(mount | grep /dev/"$DEVICE_NAME" | wc -l)
if [ "$MOUNT_STATUS" -ne 0 ]
then
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep "$DEVICE_NAME"
echo -e "${Red}>>>/dev/$DEVICE_NAME is mounted. You have to unmount the device and all of its partitions then try again ${Color_Off}"
return 1
fi
echo -e "${Yellow}>>>Please double check the device you are about to FORMAT ${Color_Off}"
lsblk -o "NAME,SIZE,FSTYPE,TYPE,LABEL,MOUNTPOINT,UUID" | grep --color -E "$DEVICE_NAME|$"
echo -ne "${Red}>>>WARNING: You are about to FORMAT a device at /dev/$DEVICE_NAME. Do you want to continue? [y/n] ${Color_Off}"
read REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "${Green}>>>You chose to continue ${Color_Off}"
else
return 1
fi
echo -e "${Red}>>>Delete any existing partition then create a new single partition ${Color_Off}"
echo -e "o\ny\nn\n1\n\n\n0700\nw\ny\n" | sudo gdisk /dev/"$DEVICE_NAME"
# o create a new empty GUID partition table (GPT) [Delete All Partition]
# y yes, proceed
# n add a new partition
# 1 partition number 1
# default, first sector
# default, last sector
# 0700 Hex code or GUID [0700 Microsoft basic data]
# w write table to disk and exit
# y yes, proceed
echo -e "${Red}>>>Formatting the device ${Color_Off}"
sudo mkfs.ntfs -f -L "$DEVICE_LABEL" /dev/"$DEVICE_NAME"1
echo -e "${Red}>>>Changing permission of the filesystem ${Color_Off}"
mkdir -p -v /tmp/testmount
sudo mount /dev/"$DEVICE_NAME"1 /tmp/testmount
sudo chmod -R 777 /tmp/testmount
sudo umount /tmp/testmount
rmdir -v /tmp/testmount
}
# }}}
#-------- LUKS Disk Encryption (Linux Unified Key Setup) v2 (last update October 23, 2016) {{{
#------------------------------------------------------
# DEMO:
# DESC: setup password protection and disk encryption on storage media
# LINK: https://gitlab.com/cryptsetup/cryptsetup
luks-setup-ext() {
if [ $# -lt 1 ]; then
echo -e "format device and apply LUKS disk encryption (Linux Unified Key Setup)"
echo -e "\nUsage: $0 <fstype:ext2/3/4> <device|partition>"
echo -e "Example: $0 ext2 sdx"
echo -e " $0 ext4 sdx1"
return 1