Skip to content

Commit e4f3f0c

Browse files
committed
Add USB flash script
1 parent 4ab18da commit e4f3f0c

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

security/erase_and_check_usb.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# -----
5+
# Initial checks
6+
# -----
7+
8+
if [ "${EUID}" -ne 0 ]; then
9+
echo "This script should be run as root."
10+
exit 1
11+
fi
12+
13+
if command -v f3probe &> /dev/null; then :; else
14+
echo "F3 seems not to be installed. Installing."
15+
apt update
16+
apt install f3
17+
fi
18+
19+
# -----
20+
# Environment variables
21+
# -----
22+
23+
DISK=$1
24+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
25+
LOG_DIR="$(dirname "${SCRIPT_DIR}")/logs"
26+
TIMESTAMP="$(date +%Y-%m-%d_%H-%M-%S)"
27+
LOG_PATH="${LOG_DIR}/usb_${TIMESTAMP}.txt"
28+
mkdir -p "${LOG_DIR}"
29+
30+
# -----
31+
# Disk validation
32+
# -----
33+
34+
if [[ ! -b "${DISK}" ]]; then
35+
echo "Error: ${DISK} is not a block device."
36+
exit 1
37+
fi
38+
if [[ "${DISK}" =~ [0-9]$ ]]; then
39+
echo "Error: Provide the whole disk (e.g. /dev/sda), not a partition (e.g. /dev/sda1)."
40+
exit 1
41+
fi
42+
43+
# Verify it's removable if possible (USB sticks usually have RM=1, but not always)
44+
RM="$(lsblk -dn -o RM "${DISK}" 2>/dev/null || echo "")"
45+
TRAN="$(lsblk -dn -o TRAN "${DISK}" 2>/dev/null || echo "")"
46+
MODEL="$(lsblk -dn -o MODEL "${DISK}" 2>/dev/null || echo "")"
47+
SIZE="$(lsblk -dn -o SIZE "${DISK}" 2>/dev/null || echo "")"
48+
49+
echo "Target: ${DISK} size=${SIZE} tran=${TRAN} rm=${RM} model=${MODEL:-unknown}" | tee "${LOG_PATH}"
50+
if [[ "${RM:-0}" != "1" ]]; then
51+
echo "WARNING: lsblk does not report this as removable (RM != 1)."
52+
echo "If this is your system disk, STOP NOW."
53+
fi
54+
55+
if mount | grep -qE "^${DISK}|^${DISK}[0-9]"; then
56+
echo "Error: Something on ${DISK} is mounted. Unmount it first."
57+
mount | grep -E "^${DISK}|^${DISK}[0-9]" || true
58+
exit 1
59+
fi
60+
61+
# -----
62+
# Run the tests
63+
# -----
64+
65+
# TRIM is unlikely to be supported by USB flash drives, but there's no harm in trying.
66+
blkdiscard -f "${DISK}" || true
67+
68+
# Documentation for the --reset-type argument:
69+
# https://github.com/AltraMayor/f3/issues/79
70+
f3probe --destructive --time-ops "${DISK}" | tee -a "${LOG_PATH}"
71+
72+
blkdiscard -f "${DISK}" 2>/dev/null || true
73+
74+
# type=c = FAT32
75+
# type=83 = Linux filesystem, e.g. ext4
76+
# 1 sector = 512 bytes -> 2048 sectors = 1 MB -> correctly aligned for modern drives
77+
sfdisk --wipe always --wipe-partitions always "${DISK}" <<'EOF'
78+
label: dos
79+
unit: sectors
80+
81+
1 : start=2048, type=c, bootable
82+
EOF
83+
84+
# Inform the kernel of the partition
85+
partprobe "${DISK}" || true
86+
87+
# Create a FAT32 filesystem
88+
mkfs.vfat -F 32 -n USB-TEST "${DISK}1"
89+
partprobe "${DISK}" || true
90+
91+
# Mount the drive
92+
MOUNT="$(mktemp -d /mnt/test-XXXXXX)"
93+
mount "${DISK}1" "${MOUNT}"
94+
95+
# Unmount the drive at script exit
96+
cleanup() {
97+
set +e
98+
sync
99+
umount "${MOUNT}" 2>/dev/null
100+
rmdir "${MOUNT}" 2>/dev/null
101+
}
102+
trap cleanup EXIT
103+
104+
f3write "${MOUNT}" | tee -a "${LOG_PATH}"
105+
f3read "${MOUNT}" | tee -a "${LOG_PATH}"
106+
107+
fstrim "${MOUNT}" 2>/dev/null || true
108+
109+
echo "USB drive erased and tested. You can now safely remove the drive."

0 commit comments

Comments
 (0)