Skip to content

Commit 40b0804

Browse files
committed
Improve NVMe script
1 parent 013d1cf commit 40b0804

File tree

2 files changed

+105
-29
lines changed

2 files changed

+105
-29
lines changed

security/erase_and_check_nvme_ssd.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

security/erase_nvme_ssd.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
if [ "${EUID}" -ne 0 ]; then
5+
echo "This script should be run as root."
6+
return 1
7+
fi
8+
9+
DISK=$1
10+
if ! [ -e "${DISK}" ]; then
11+
echo "Please supply a valid disk as the first argument."
12+
return 1
13+
fi
14+
if [ $# -gt 2 ]; then
15+
echo "Too many arguments."
16+
return 1
17+
fi
18+
19+
echo "If you encounter any issues, check your UEFI/BIOS settings and disable the \"Block SID Authentication\" setting."
20+
21+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
22+
LOG_DIR="$(dirname "${SCRIPT_DIR}")/logs"
23+
TIMESTAMP="$(date +%Y-%m-%d_%H-%M-%S)"
24+
LOG_FILE="${LOG_DIR}/erase_nvme_ssd_${TIMESTAMP}.txt"
25+
mkdir -p "${LOG_DIR}"
26+
27+
apt update
28+
apt install nvme-cli smartmontools tar wget
29+
30+
nvme --version
31+
nvme list
32+
33+
# https://stackoverflow.com/a/1885534/
34+
read -p "Are you sure this is the right disk? " -n 1 -r
35+
echo # newline
36+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
37+
return 1
38+
fi
39+
40+
ID_CTRL=$(nvme id-ctrl "${DISK}" --human-readable)
41+
echo "${ID_CTRL}" &>> "${LOG_FILE}"
42+
43+
if [ $# -eq 1 ]; then
44+
nvme sanitize-log "${DISK}"
45+
SANITIZE=$(nvme sanitize "${DISK}" --sanact=start-block-erase)
46+
if ! (grep "Access Denied" <<< "${SANITIZE}") then
47+
nvme sanitize-log "${DISK}"
48+
echo "Sanitizing started."
49+
return 0
50+
fi
51+
52+
echo "Could not sanitize the drive. Suspending the computer to fix this. "
53+
echo "Start the computer again to continue."
54+
sleep 2
55+
# https://pcpartpicker.com/forums/topic/460000-an-ssd-that-cant-be-formatted-leads-to-solving-an-8-year-old-bug
56+
# https://superuser.com/a/1574593
57+
systemctl suspend
58+
read -p "Did the computer suspend? " -n 1 -r
59+
echo # newline
60+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
61+
return 1
62+
fi
63+
64+
# Attempt sanitize again
65+
SANITIZE=$(nvme sanitize "${DISK}" --sanact=start-block-erase)
66+
if ! (grep "Access Denied" <<< "${SANITIZE}"); then
67+
nvme sanitize-log "${DISK}"
68+
echo "Sanitizing started."
69+
return 0
70+
fi
71+
echo "Suspending did not unlock the disk. Unlocking the disk requires sedutil."
72+
else
73+
echo "PSID was provided. Using sedutil to erase the drive."
74+
fi
75+
76+
if (grep "SecureBoot enabled" <<< "$(mokutil --sb-state)"); then
77+
echo "Secure Boot seems to be enabled. You need to disable it to use sedutil."
78+
return 1
79+
fi
80+
81+
# https://github.com/Drive-Trust-Alliance/sedutil/wiki/Executable-Distributions
82+
SEDUTIL="${SCRIPT_DIR}/sedutil/Release_x86_64/sedutil-cli"
83+
if [ ! -f "${SEDUTIL}" ]; then
84+
wget "https://github.com/Drive-Trust-Alliance/exec/blob/master/sedutil_LINUX.tgz?raw=true" -O "${SCRIPT_DIR}/sedutil_LINUX.tgz"
85+
tar -xvzf "${SCRIPT_DIR}/sedutil_LINUX.tgz" "${SCRIPT_DIR}/sedutil"
86+
fi
87+
if [ "1" != "$(cat "/sys/module/libata/parameters/allow_tpm")" ]; then
88+
echo "libata.allow_tpm was not set. Setting it now."
89+
echo "1" > "/sys/module/libata/parameters/allow_tpm"
90+
fi
91+
92+
$SEDUTIL --scan
93+
$SEDUTIL --isValidSED "${DISK}"
94+
$SEDUTIL --printDefaultPassword "${DISK}"
95+
if [ $# -eq 1 ]; then
96+
echo "Please run this script again and provide the PSID of the drive as the second argument."
97+
return 0
98+
fi
99+
SEDUTIL_OUTPUT=$($SEDUTIL --yesIreallywanttoERASEALLmydatausingthePSID "${2}" "${DISK}")
100+
if (grep "revertTper completed successfully" <<< "${SEDUTIL_OUTPUT}"); then
101+
echo "Erasing the SSD using the PSID was completed successfully. (${SEDUTIL_OUTPUT})"
102+
else
103+
echo "${SEDUTIL_OUTPUT}"
104+
return 1
105+
fi

0 commit comments

Comments
 (0)