Nico Huber | b18c2cc | 2024-01-28 13:56:28 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | set -e |
| 3 | |
| 4 | mnt=/mnt/mount |
| 5 | tmp=$(mktemp -d) |
| 6 | trap "sh -c 'sudo umount ${mnt} 2>/dev/null; rm -rf ${tmp}'" EXIT INT |
| 7 | |
| 8 | build=${PWD}/build |
| 9 | |
| 10 | test() { |
| 11 | type=$1 |
| 12 | img=${tmp}/${type}.img |
| 13 | |
| 14 | printf "\n===> Prepping \`${img}'\n" |
| 15 | dd if=/dev/zero bs=1M count=64 >${img} 2>/dev/null |
| 16 | mkfs.${type} -E root_owner ${img} >/dev/null 2>&1 |
| 17 | |
| 18 | sudo mount -o loop ${img} ${mnt} -o rw |
| 19 | mkdir -p ${mnt}/dir/subdir |
| 20 | |
| 21 | echo " ==> Adding \`test' files and links" |
| 22 | echo 'Moin!' >${mnt}/test |
| 23 | echo 'Moin! from `dir'\' >${mnt}/dir/test |
| 24 | echo 'Moin! from `subdir'\' >${mnt}/dir/subdir/test |
| 25 | ln -s test ${mnt}/l5 |
| 26 | ln -s ../l5 ${mnt}/dir/l4 |
| 27 | ln -s ../l4 ${mnt}/dir/subdir/l3 |
| 28 | ln -s ../dir/subdir/l3 ${mnt}/dir/l2 |
| 29 | ln -s ../dir/subdir/../l2 ${mnt}/dir/l1 |
| 30 | ln -s l1 ${mnt}/dir/ltest |
| 31 | ln -s ../../dir/../dir/../dir/../dir/subdir/../../dir/subdir/../subdir/../subdir/../../dir/test ${mnt}/dir/subdir/ltest |
| 32 | |
| 33 | echo " ==> Adding random small files" |
| 34 | i=0; step=123; files=12345 |
| 35 | for j in $(seq $((files/step))); do |
| 36 | dd if=/dev/urandom bs=123 count=1 2>/dev/null | \ |
| 37 | tee $(printf "${mnt}/dir/random%05d " $(seq $((i+1)) $((i+step)))) >/dev/null |
| 38 | i=$((i+step)) |
| 39 | done |
| 40 | |
| 41 | echo " => Removing some files to produce gaps" |
| 42 | i=0 |
| 43 | for j in $(seq $((files/step))); do |
| 44 | rm $(printf "${mnt}/dir/random%05d " $(seq $((i+2)) 2 $((i+step)))) |
| 45 | i=$((i+step)) |
| 46 | done |
| 47 | |
| 48 | echo " ==> Adding a big file (should be fragmented)" |
| 49 | dd if=/dev/urandom bs=1M count=30 >${mnt}/random 2>/dev/null |
| 50 | |
| 51 | echo " ==> Removing some more small files" |
| 52 | i=0 |
| 53 | for j in $(seq $((files/step))); do |
| 54 | rm $(printf "${mnt}/dir/random%05d " $(seq $((i+3)) 4 $((i+step)))) |
| 55 | rm $(printf "${mnt}/dir/random%05d " $(seq $((i+5)) 8 $((i+step)))) |
| 56 | rm $(printf "${mnt}/dir/random%05d " $(seq $((i+9)) 16 $((i+step)))) |
| 57 | i=$((i+step)) |
| 58 | done |
| 59 | |
| 60 | echo " ==> Adding a big, fragmented, sparse file" |
| 61 | for i in $(seq 1 2 8765); do |
| 62 | dd if=/dev/urandom bs=4K count=1 conv=notrunc \ |
| 63 | seek=${i} of=${mnt}/dir/subdir/sparse 2>/dev/null |
| 64 | done |
| 65 | if [ "${type}" = "ext4" ]; then |
| 66 | echo " => Increasing sparse file >1GiB" |
| 67 | dd if=/dev/urandom bs=4K count=1 conv=notrunc \ |
| 68 | seek=345678 of=${mnt}/dir/subdir/sparse 2>/dev/null |
| 69 | fi |
| 70 | |
| 71 | sudo umount ${mnt} |
| 72 | |
| 73 | sudo mount -o loop ${img} ${mnt} -o ro |
| 74 | cd ${mnt} |
| 75 | |
| 76 | printf "\n ==> Running simple test for \`test' files\n" |
| 77 | ${build}/fstest -d ${img} |
| 78 | |
| 79 | printf "\n ==> Comparing all file contents" |
| 80 | find . \( -name lost+found -prune \) -o ! -type d -printf . \ |
| 81 | \( -exec sh -c "${build}/fscat -d ${img} {} | cmp - {}" \; \ |
| 82 | -o \( -exec false {} + -quit \) \) |
| 83 | printf " done\n" |
| 84 | |
| 85 | printf "\n ==> Comparing all file contents (at once)... " |
| 86 | sum=$(find . \( -name lost+found -prune \) -o ! -type d \ |
| 87 | -exec ${build}/fscat -d ${img} {} + | cksum) |
| 88 | sum2=$(find . \( -name lost+found -prune \) -o ! -type d -exec cat {} + | cksum) |
| 89 | if [ "${sum}" = "${sum2}" ]; then |
| 90 | printf "done\n" |
| 91 | else |
| 92 | printf "FAILED!\n" |
| 93 | exit 1 |
| 94 | fi |
| 95 | |
| 96 | cd - >/dev/null |
| 97 | sudo umount ${mnt} |
| 98 | } |
| 99 | |
| 100 | test ext2 |
| 101 | test ext4 |