-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
95 lines (85 loc) · 3.7 KB
/
main.sh
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
#!/bin/bash
set -e
trap 'echo "An error occurred. Exiting."; exit 1' ERR
INPUT_DEVICE="/dev/urandom"
PASSES=1
BLOCK_SIZE="1M"
function usage {
echo "Usage: $0 [OPTIONS] /dev/sdX"
echo
echo "OPTIONS:"
echo " -p, --passes <n> Number of times to overwrite (default: 1)"
echo " -b, --bs <size> Block size for dd (default: 1M)"
echo " -z, --zero Use /dev/zero instead of /dev/urandom"
echo " -h, --help Display this help message"
echo
echo "Examples:"
echo " $0 -p 3 -b 4M /dev/sdX Overwrite 3 times with a 4M block size using /dev/urandom"
echo " $0 -z /dev/sdX Overwrite 1 time using /dev/zero"
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--passes)
PASSES="$2"
shift 2
;;
-b|--bs)
BLOCK_SIZE="$2"
shift 2
;;
-z|--zero)
INPUT_DEVICE="/dev/zero"
shift
;;
-h|--help)
usage
;;
*)
DISK="$1"
shift
;;
esac
done
if [ -z "${DISK:-}" ]; then
usage
fi
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (or with sudo)."
exit 1
fi
if [ ! -b "$DISK" ]; then
echo "Error: $DISK is not a valid block device."
exit 1
fi
if mount | grep -q "$DISK"; then
echo "Error: $DISK is mounted. Please unmount it first."
exit 1
fi
echo "================================================================================"
echo "▓█████▄ ██▀███ ██▓ ██▒ █▓▓█████ █ █░ ██▓ ██▓███ ▓█████ ██▀███ "
echo "▒██▀ ██▌▓██ ▒ ██▒▓██▒▓██░ █▒▓█ ▀ ▓█░ █ ░█░▓██▒▓██░ ██▒▓█ ▀ ▓██ ▒ ██▒"
echo "░██ █▌▓██ ░▄█ ▒▒██▒ ▓██ █▒░▒███ ▒█░ █ ░█ ▒██▒▓██░ ██▓▒▒███ ▓██ ░▄█ ▒"
echo "░▓█▄ ▌▒██▀▀█▄ ░██░ ▒██ █░░▒▓█ ▄ ░█░ █ ░█ ░██░▒██▄█▓▒ ▒▒▓█ ▄ ▒██▀▀█▄ "
echo "░▒████▓ ░██▓ ▒██▒░██░ ▒▀█░ ░▒████▒ ░░██▒██▓ ░██░▒██▒ ░ ░░▒████▒░██▓ ▒██▒"
echo " ▒▒▓ ▒ ░ ▒▓ ░▒▓░░▓ ░ ▐░ ░░ ▒░ ░ ░ ▓░▒ ▒ ░▓ ▒▓▒░ ░ ░░░ ▒░ ░░ ▒▓ ░▒▓░"
echo " ░ ▒ ▒ ░▒ ░ ▒░ ▒ ░ ░ ░░ ░ ░ ░ ▒ ░ ░ ▒ ░░▒ ░ ░ ░ ░ ░▒ ░ ▒░"
echo " ░ ░ ░ ░░ ░ ▒ ░ ░░ ░ ░ ░ ▒ ░░░ ░ ░░ ░ "
echo " ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ "
echo " ░ ░ "
echo "================================================================================"
echo "WARNING: This will overwrite $DISK with '$INPUT_DEVICE' $PASSES time(s)."
echo "Block size: $BLOCK_SIZE"
echo "ALL DATA ON THIS DEVICE WILL BE LOST. ARE YOU ABSOLUTELY SURE?"
echo "================================================================================"
read -p "Type YES to continue: " confirm
if [ "$confirm" != "YES" ]; then
echo "Aborted."
exit 1
fi
for (( pass=1; pass<=PASSES; pass++ )); do
echo "Starting pass $pass of $PASSES using $INPUT_DEVICE..."
dd if="$INPUT_DEVICE" of="$DISK" bs="$BLOCK_SIZE" status=progress conv=fdatasync
sync
done
echo "Done. $DISK has been overwritten $PASSES time(s) using $INPUT_DEVICE."