24
24
# setup Bash environment
25
25
set -euf -o pipefail
26
26
27
+ # setup sudo
28
+ SUDO=' '
29
+ if [[ $( id -u) -ne 0 ]]; then
30
+ SUDO=' sudo'
31
+ fi
27
32
28
33
# ##############################################################################
29
34
# constants
@@ -456,7 +461,7 @@ echo " using $TOOL_UDF"
456
461
457
462
echo " [+] Detecting logical block size..."
458
463
if [[ $TOOL_DRIVE_LISTING = " $TOOL_BLOCKDEV " ]]; then
459
- LOGICAL_BLOCK_SIZE=$( sudo blockdev --getss " /dev/$DEVICE " )
464
+ LOGICAL_BLOCK_SIZE=$( $SUDO blockdev --getss " /dev/$DEVICE " )
460
465
elif [[ $TOOL_DRIVE_LISTING = " $TOOL_DISKUTIL " ]]; then
461
466
LOGICAL_BLOCK_SIZE=$( diskutil info " $DEVICE " | grep -i ' Device Block Size' | awk -F ' :' ' {print $2}' | awk ' {print $1}' )
462
467
else
@@ -479,7 +484,7 @@ echo "[+] Validating detected logical block size..."
479
484
480
485
echo " [+] Detecting physical block size..."
481
486
if [[ $TOOL_DRIVE_INFO = " $TOOL_BLOCKDEV " ]]; then
482
- PHYSICAL_BLOCK_SIZE=$( sudo blockdev --getpbsz " /dev/$DEVICE " )
487
+ PHYSICAL_BLOCK_SIZE=$( $SUDO blockdev --getpbsz " /dev/$DEVICE " )
483
488
elif [[ $TOOL_DRIVE_INFO = " $TOOL_IOREG " ]]; then
484
489
# TODO - the 'Physical Block Size' item isn't always present. find a more reliable method on macOS.
485
490
# `true` is so that a failure here doesn't cause entire script to exit prematurely
@@ -554,7 +559,7 @@ echo "[*] Using file system block size of $FILE_SYSTEM_BLOCK_SIZE"
554
559
555
560
echo " [+] Detecting total size..."
556
561
if [[ $TOOL_DRIVE_LISTING = " $TOOL_BLOCKDEV " ]]; then
557
- TOTAL_SIZE=$( sudo blockdev --getsize64 " /dev/$DEVICE " )
562
+ TOTAL_SIZE=$( $SUDO blockdev --getsize64 " /dev/$DEVICE " )
558
563
elif [[ $TOOL_DRIVE_LISTING = " $TOOL_DISKUTIL " ]]; then
559
564
TOTAL_SIZE=$( diskutil info " $DEVICE " | grep -Ei ' (Total|Disk) Size' | awk -F ' :' ' {print $2}' | grep -Eoi ' \([0-9]+ B' | sed ' s/[^0-9]//g' )
560
565
else
593
598
594
599
echo " [+] Gathering drive information..."
595
600
if [[ $TOOL_DRIVE_SUMMARY = " $TOOL_BLKID " ]] && [[ $TOOL_DRIVE_LISTING = " $TOOL_BLOCKDEV " ]]; then
596
- sudo blkid -c /dev/null " /dev/$DEVICE " || true
601
+ $SUDO blkid -c /dev/null " /dev/$DEVICE " || true
597
602
cat " /sys/block/$PARENT_DEVICE /device/model" || true
598
- sudo blockdev --report | grep -E " (Device|$DEVICE )"
603
+ $SUDO blockdev --report | grep -E " (Device|$DEVICE )"
599
604
elif [[ $TOOL_DRIVE_LISTING = " $TOOL_DISKUTIL " ]]; then
600
605
diskutil list " $DEVICE "
601
606
else
621
626
echo " [+] Unmounting device..."
622
627
if [[ $TOOL_UNMOUNT = " $TOOL_UMOUNT " ]]; then
623
628
# `true` is so that a failure here doesn't cause entire script to exit prematurely
624
- sudo umount " /dev/$DEVICE " || true
629
+ $SUDO umount " /dev/$DEVICE " || true
625
630
elif [[ $TOOL_UNMOUNT = " $TOOL_DISKUTIL " ]]; then
626
631
# `true` is so that a failure here doesn't cause entire script to exit prematurely
627
- sudo diskutil unmountDisk " /dev/$DEVICE " || true
632
+ $SUDO diskutil unmountDisk " /dev/$DEVICE " || true
628
633
else
629
634
echo " [-] Internal error 5" >&2
630
635
exit 1
@@ -644,11 +649,11 @@ case $WIPE_METHOD in
644
649
;;
645
650
zero)
646
651
echo " [+] Overwriting device with zeros. This will likely take a LONG time..."
647
- sudo dd if=/dev/zero of=" /dev/$DEVICE " bs=" $LOGICAL_BLOCK_SIZE " || true
652
+ $SUDO dd if=/dev/zero of=" /dev/$DEVICE " bs=" $LOGICAL_BLOCK_SIZE " || true
648
653
;;
649
654
scrub)
650
655
echo " [+] Scrubbing device with random patterns. This will likely take a LONG time..."
651
- sudo scrub -f " /dev/$DEVICE "
656
+ $SUDO scrub -f " /dev/$DEVICE "
652
657
;;
653
658
* )
654
659
echo " [-] Internal error 6" >&2
663
668
664
669
echo " [+] Zeroing out first chunk of device..."
665
670
# 4096 was arbitrarily chosen to be "big enough" to delete first chunk of device
666
- sudo dd if=/dev/zero of=" /dev/$DEVICE " bs=" $LOGICAL_BLOCK_SIZE " count=4096
671
+ $SUDO dd if=/dev/zero of=" /dev/$DEVICE " bs=" $LOGICAL_BLOCK_SIZE " count=4096
667
672
668
673
669
674
# ##############################################################################
@@ -678,15 +683,15 @@ if [[ $TOOL_UDF = "$TOOL_MKUDFFS" ]]; then
678
683
# --lvid - logical volume identifier
679
684
# --vid - volume identifier
680
685
# --media-type - "hd" type covers both hard drives and USB drives
681
- (sudo mkudffs --utf8 --blocksize=" $FILE_SYSTEM_BLOCK_SIZE " --udfrev=0x0201 --lvid=" $LABEL " --vid=" $LABEL " --media-type=hd " /dev/$DEVICE " ) || (echo " [-] Format failed!" >&2 ; false)
686
+ ($SUDO mkudffs --utf8 --blocksize=" $FILE_SYSTEM_BLOCK_SIZE " --udfrev=0x0201 --lvid=" $LABEL " --vid=" $LABEL " --media-type=hd " /dev/$DEVICE " ) || (echo " [-] Format failed!" >&2 ; false)
682
687
elif [[ $TOOL_UDF = " $TOOL_NEWFS_UDF " ]]; then
683
688
# -b - the size of blocks in bytes. should be the same as the drive's physical block size.
684
689
# -m - "blk" type covers both hard drives and USB drives
685
690
# -t - "overwrite" access type
686
691
# -r - the udf revision to use. 2.01 is the latest revision available that supports writing in Linux.
687
692
# -v - volume identifier
688
693
# --enc - encode volume name in UTF8
689
- (sudo newfs_udf -b " $FILE_SYSTEM_BLOCK_SIZE " -m blk -t ow -r 2.01 -v " $LABEL " --enc utf8 " /dev/$DEVICE " ) || (echo " [-] Format failed!" >&2 ; false)
694
+ ($SUDO newfs_udf -b " $FILE_SYSTEM_BLOCK_SIZE " -m blk -t ow -r 2.01 -v " $LABEL " --enc utf8 " /dev/$DEVICE " ) || (echo " [-] Format failed!" >&2 ; false)
690
695
else
691
696
echo " [-] Internal error 7" >&2
692
697
exit 1
@@ -704,9 +709,9 @@ case $PARTITION_TYPE in
704
709
mbr)
705
710
echo " [+] Writing fake MBR..."
706
711
# first block has already been zero'd. start by writing the (only) partition entry at its correct offset.
707
- entire_disk_partition_entry " $TOTAL_SIZE " " $LOGICAL_BLOCK_SIZE " | xxd -r -p | sudo dd of=" /dev/$DEVICE " bs=1 seek=446 count=16
712
+ entire_disk_partition_entry " $TOTAL_SIZE " " $LOGICAL_BLOCK_SIZE " | xxd -r -p | $SUDO dd of=" /dev/$DEVICE " bs=1 seek=446 count=16
708
713
# Boot signature at the end of the block
709
- echo -n 55aa | xxd -r -p | sudo dd of=" /dev/$DEVICE " bs=1 seek=510 count=2
714
+ echo -n 55aa | xxd -r -p | $SUDO dd of=" /dev/$DEVICE " bs=1 seek=510 count=2
710
715
;;
711
716
* )
712
717
echo " [-] Internal error 8" >&2
721
726
722
727
# following call to blkid sometimes exits with failure, even though the device is formatted properly.
723
728
# `true` is so that a failure here doesn't cause entire script to exit prematurely
724
- SUMMARY=$( [[ $TOOL_DRIVE_SUMMARY = " $TOOL_BLKID " ]] && sudo blkid -c /dev/null " /dev/$DEVICE " 2> /dev/null) || true
729
+ SUMMARY=$( [[ $TOOL_DRIVE_SUMMARY = " $TOOL_BLKID " ]] && $SUDO blkid -c /dev/null " /dev/$DEVICE " 2> /dev/null) || true
725
730
echo " [+] Successfully formatted $SUMMARY "
726
731
727
- # TODO find a way to auto-mount (`sudo mount -a` doesn't work). in the meantime...
732
+ # TODO find a way to auto-mount (`$SUDO mount -a` doesn't work). in the meantime...
728
733
echo " Please disconnect/reconnect your drive now."
0 commit comments