Skip to content

Commit d8f1d5c

Browse files
committed
test: add script for verifying reads/writes
1 parent 7aa1357 commit d8f1d5c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tools/test-usb-msc-write.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
DISK=${1#/dev/}
6+
7+
if [ -z "$DISK" ]; then
8+
echo "Usage: $0 /dev/sdX"
9+
exit 1
10+
fi
11+
12+
if [ ! -b "/dev/$DISK" ]; then
13+
echo "Error: /dev/$DISK is not a block device."
14+
exit 1
15+
fi
16+
17+
# Create a temporary file to hold the random data
18+
TEMPFILE=$(mktemp /tmp/test-usb-msc.XXXXXX)
19+
20+
# Delete the temporary file on exit or error
21+
trap 'rm -f "$TEMPFILE"' EXIT
22+
23+
# Generate random data to fill the disk
24+
dd if=/dev/urandom of=$TEMPFILE bs=$(cat /sys/block/$DISK/queue/hw_sector_size) count=$(cat /sys/block/$DISK/size)
25+
26+
CHECKSUM=$(md5sum $TEMPFILE | awk '{print $1}')
27+
28+
# Do a full-disk erase (unmap)
29+
echo "unmap" >/sys/block/$DISK/device/scsi_disk/*/provisioning_mode
30+
blkdiscard /dev/$DISK
31+
32+
# Write the random data to the disk
33+
dd if=$TEMPFILE of=/dev/$DISK bs=$(cat /sys/block/$DISK/queue/hw_sector_size)
34+
35+
# Flush the disk cache to read back the data directly from disk
36+
sync
37+
echo 3 >/proc/sys/vm/drop_caches
38+
39+
# Read the data back from the disk and verify it
40+
DISK_CHECKSUM=$(md5sum /dev/$DISK | awk '{print $1}')
41+
if [ "$CHECKSUM" != "$DISK_CHECKSUM" ]; then
42+
echo "Error: Checksum mismatch"
43+
echo "Original checksum: $CHECKSUM"
44+
echo "Read checksum: $DISK_CHECKSUM"
45+
exit 1
46+
else
47+
echo "Success: Checksum matches"
48+
fi

0 commit comments

Comments
 (0)