#!/usr/bin/env bash # Check Rustbelt's ext2 writes with the host's own tools (e2fsprogs). # # 1. e2fsck the pristine images build.rs made (sanity check of the build). # 2. Copy them to target/fsck/ and boot Rustbelt on the *writable* copies # (no snapshot=on), so whatever it writes really lands in the files: # - the shell self-test (cp B: -> C:, mkdir, echo > file) # - the tests/ext2.rs kernel (create/write/truncate/unlink/rmdir on # all three images: 1 KiB blocks, 4 KiB blocks, revision 0) # 3. `e2fsck -fn` every copy: -f = check even if marked clean, -n = open # read-only and answer "no" to every question (report, don't fix). # Any complaint means the driver wrote something inconsistent. # 4. Show the root directory with debugfs, to see the new files. # # Usage: ./scripts/ext2-check.sh (after `cargo build` and # `cargo test --test ext2`, which build the images and the kernels) set -euo pipefail cd "$(dirname "$0")/.." export PATH="$HOME/.cargo/bin:$PATH:/usr/sbin:/sbin" for tool in e2fsck debugfs qemu-system-x86_64; do command -v $tool >/dev/null || { echo "need $tool (apt install e2fsprogs qemu-system-x86)"; exit 1; } done fsck() { # fsck echo "== e2fsck -fn $1 ($2)" e2fsck -fn "$1" || { echo "!! e2fsck found problems in $1"; status=1; } } status=0 for img in target/ext2.img target/ext2-4k.img target/ext2-rev0.img; do fsck $img "pristine, as built" done mkdir -p target/fsck cp target/ext2.img target/fsck/selftest.img for img in ext2 ext2-4k ext2-rev0; do cp target/$img.img target/fsck/tests-$img.img; done echo "== boot the shell self-test on target/fsck/selftest.img" EXT2_IMG=target/fsck/selftest.img ./scripts/run-headless.sh >/dev/null && echo "self-test OK" echo "== run the tests/ext2.rs kernel on target/fsck/tests-*.img" KERNEL=$(ls -t target/x86_64-rust_kernel/debug/build/rustbelt/*/out/bootimage-ext2-*.bin 2>/dev/null | head -1 || true) [ -n "$KERNEL" ] || { echo "no test kernel: run 'cargo test --test ext2' first"; exit 1; } set +e timeout 120 qemu-system-x86_64 -smp 4 -drive format=raw,file="$KERNEL" -no-reboot \ -drive file=target/disk.img,format=raw,if=virtio,snapshot=on \ -drive file=target/fsck/tests-ext2.img,format=raw,if=virtio \ -drive file=target/exfat.img,format=raw,if=virtio,snapshot=on \ -drive file=target/btrfs.img,format=raw,if=virtio,snapshot=on \ -drive file=target/fsck/tests-ext2-4k.img,format=raw,if=virtio \ -drive file=target/fsck/tests-ext2-rev0.img,format=raw,if=virtio \ -drive file=target/exfat-512.img,format=raw,if=virtio,snapshot=on \ -device isa-debug-exit,iobase=0xf4,iosize=0x04 \ -serial stdio -display none > target/fsck/tests.log code=$? set -e grep -c "\[ok\]" target/fsck/tests.log | sed 's/$/ ext2 tests ok/' [ $code = 33 ] || { echo "!! test kernel failed (exit $code), see target/fsck/tests.log"; exit 1; } for img in target/fsck/selftest.img target/fsck/tests-ext2*.img; do fsck "$img" "after Rustbelt wrote to it" done echo "== debugfs: ls -l / on the self-test copy" debugfs -R "ls -l /" target/fsck/selftest.img 2>/dev/null echo "== debugfs: cat /made-here/note.txt" debugfs -R "cat /made-here/note.txt" target/fsck/selftest.img 2>/dev/null [ $status = 0 ] && echo "== all images are clean" || echo "== e2fsck found problems" exit $status