Skip to content

Commit 89835bc

Browse files
committed
Allow running as root user; Fixes #36
1 parent e34983c commit 89835bc

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

format-udf.sh

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
# setup Bash environment
2525
set -euf -o pipefail
2626

27+
# setup sudo
28+
SUDO=''
29+
if [[ $(id -u) -ne 0 ]]; then
30+
SUDO='sudo'
31+
fi
2732

2833
###############################################################################
2934
# constants
@@ -456,7 +461,7 @@ echo " using $TOOL_UDF"
456461

457462
echo "[+] Detecting logical block size..."
458463
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")
460465
elif [[ $TOOL_DRIVE_LISTING = "$TOOL_DISKUTIL" ]]; then
461466
LOGICAL_BLOCK_SIZE=$(diskutil info "$DEVICE" | grep -i 'Device Block Size' | awk -F ':' '{print $2}' | awk '{print $1}')
462467
else
@@ -479,7 +484,7 @@ echo "[+] Validating detected logical block size..."
479484

480485
echo "[+] Detecting physical block size..."
481486
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")
483488
elif [[ $TOOL_DRIVE_INFO = "$TOOL_IOREG" ]]; then
484489
# TODO - the 'Physical Block Size' item isn't always present. find a more reliable method on macOS.
485490
# `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"
554559

555560
echo "[+] Detecting total size..."
556561
if [[ $TOOL_DRIVE_LISTING = "$TOOL_BLOCKDEV" ]]; then
557-
TOTAL_SIZE=$(sudo blockdev --getsize64 "/dev/$DEVICE")
562+
TOTAL_SIZE=$($SUDO blockdev --getsize64 "/dev/$DEVICE")
558563
elif [[ $TOOL_DRIVE_LISTING = "$TOOL_DISKUTIL" ]]; then
559564
TOTAL_SIZE=$(diskutil info "$DEVICE" | grep -Ei '(Total|Disk) Size' | awk -F ':' '{print $2}' | grep -Eoi '\([0-9]+ B' | sed 's/[^0-9]//g')
560565
else
@@ -593,9 +598,9 @@ fi
593598

594599
echo "[+] Gathering drive information..."
595600
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
597602
cat "/sys/block/$PARENT_DEVICE/device/model" || true
598-
sudo blockdev --report | grep -E "(Device|$DEVICE)"
603+
$SUDO blockdev --report | grep -E "(Device|$DEVICE)"
599604
elif [[ $TOOL_DRIVE_LISTING = "$TOOL_DISKUTIL" ]]; then
600605
diskutil list "$DEVICE"
601606
else
@@ -621,10 +626,10 @@ fi
621626
echo "[+] Unmounting device..."
622627
if [[ $TOOL_UNMOUNT = "$TOOL_UMOUNT" ]]; then
623628
# `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
625630
elif [[ $TOOL_UNMOUNT = "$TOOL_DISKUTIL" ]]; then
626631
# `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
628633
else
629634
echo "[-] Internal error 5" >&2
630635
exit 1
@@ -644,11 +649,11 @@ case $WIPE_METHOD in
644649
;;
645650
zero)
646651
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
648653
;;
649654
scrub)
650655
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"
652657
;;
653658
*)
654659
echo "[-] Internal error 6" >&2
@@ -663,7 +668,7 @@ esac
663668

664669
echo "[+] Zeroing out first chunk of device..."
665670
# 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
667672

668673

669674
###############################################################################
@@ -678,15 +683,15 @@ if [[ $TOOL_UDF = "$TOOL_MKUDFFS" ]]; then
678683
# --lvid - logical volume identifier
679684
# --vid - volume identifier
680685
# --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)
682687
elif [[ $TOOL_UDF = "$TOOL_NEWFS_UDF" ]]; then
683688
# -b - the size of blocks in bytes. should be the same as the drive's physical block size.
684689
# -m - "blk" type covers both hard drives and USB drives
685690
# -t - "overwrite" access type
686691
# -r - the udf revision to use. 2.01 is the latest revision available that supports writing in Linux.
687692
# -v - volume identifier
688693
# --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)
690695
else
691696
echo "[-] Internal error 7" >&2
692697
exit 1
@@ -704,9 +709,9 @@ case $PARTITION_TYPE in
704709
mbr)
705710
echo "[+] Writing fake MBR..."
706711
# 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
708713
# 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
710715
;;
711716
*)
712717
echo "[-] Internal error 8" >&2
@@ -721,8 +726,8 @@ esac
721726

722727
# following call to blkid sometimes exits with failure, even though the device is formatted properly.
723728
# `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
725730
echo "[+] Successfully formatted $SUMMARY"
726731

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...
728733
echo "Please disconnect/reconnect your drive now."

0 commit comments

Comments
 (0)