chiark / gitweb /
bin/check-blkdev-size: Add script for checking suspicious block devices.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 22 Mar 2020 19:16:51 +0000 (19:16 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 22 Mar 2020 19:16:51 +0000 (19:16 +0000)
I've had this lying around for a while.

Makefile
bin/check-blkdev-size [new file with mode: 0755]

index 3b1d78851dec8d5f30b30fad5727282f264f8cf0..b32be2369979a967d3ef40cb515e9a1ddc5ce666 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -225,6 +225,7 @@ SCRIPTLINKS         += guest-console
 SCRIPTLINKS            += hyperspec
 SCRIPTLINKS            += datasyms
 SCRIPTLINKS            += check-debsyms
+SCRIPTLINKS            += check-blkdev-size
 
 ## Random odds and ends.
 DOTLINKS               += .infokey .sqliterc
diff --git a/bin/check-blkdev-size b/bin/check-blkdev-size
new file mode 100755 (executable)
index 0000000..0f68a53
--- /dev/null
@@ -0,0 +1,35 @@
+#! /bin/bash
+
+set -e -o pipefail
+
+case $# in 1) ;; *) echo >&2 "usage: $0 DEV"; exit 127 ;; esac
+dev=$1
+
+typeset -i sz; sz=$(blockdev --getsize64 "$dev")
+k=$(gorp -fhex 256)
+
+## Figure out a useful block size.
+typeset -i bsz=1 nb=$sz
+for i in 2 3 5 7; do
+  while :; do
+    case $((nb%i)) in
+      0) bsz=$((bsz*i)) nb=$((nb/i)) ;;
+      *) break ;;
+    esac
+    if (( bsz >= 65536 )); then break 2; fi
+  done
+done
+echo >&2 "$0: using $nb blocks of size $bsz"
+
+## Write the initial stream.
+echo >&2 "$0: writing pseudorandom pattern..."
+rspit salsa20/8 -H$k -z$sz -pT | \
+  dd status=none iflag=fullblock oflag=direct bs=$bsz count=$nb of="$dev"
+echo >&2 "$0: writing pseudorandom pattern... done"
+
+## Read the stream back, and check against the reference.
+echo >&2 "$0: checking pseudorandom pattern..."
+cmp <(rspit salsa20/8 -H$k -z$sz -pT) \
+    <(dd status=none iflag=direct bs=$bsz count=$nb if="$dev")
+echo >&2 "$0: checking pseudorandom pattern... done"
+echo >&2 "$0: all ok"