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 | |
Nico Huber | cc960f2 | 2024-01-29 01:13:45 +0100 | [diff] [blame^] | 10 | test_ext2() { |
Nico Huber | b18c2cc | 2024-01-28 13:56:28 +0100 | [diff] [blame] | 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 | |
Nico Huber | cc960f2 | 2024-01-29 01:13:45 +0100 | [diff] [blame^] | 73 | test_img ${img} |
| 74 | } |
| 75 | |
| 76 | test_isofs() { |
| 77 | img=${tmp}/test.iso |
| 78 | template=${tmp}/ext2.img |
| 79 | |
| 80 | printf "\n===> Prepping \`${img}'\n" |
| 81 | |
| 82 | sudo mount -o loop ${template} ${mnt} -o ro |
| 83 | |
| 84 | mkisofs -o ${img} ${mnt} |
| 85 | |
| 86 | sudo umount ${mnt} |
| 87 | |
| 88 | test_img ${img} |
| 89 | } |
| 90 | |
| 91 | test_img() { |
| 92 | img=$1 |
| 93 | |
Nico Huber | b18c2cc | 2024-01-28 13:56:28 +0100 | [diff] [blame] | 94 | sudo mount -o loop ${img} ${mnt} -o ro |
| 95 | cd ${mnt} |
| 96 | |
| 97 | printf "\n ==> Running simple test for \`test' files\n" |
| 98 | ${build}/fstest -d ${img} |
| 99 | |
| 100 | printf "\n ==> Comparing all file contents" |
| 101 | find . \( -name lost+found -prune \) -o ! -type d -printf . \ |
| 102 | \( -exec sh -c "${build}/fscat -d ${img} {} | cmp - {}" \; \ |
| 103 | -o \( -exec false {} + -quit \) \) |
| 104 | printf " done\n" |
| 105 | |
| 106 | printf "\n ==> Comparing all file contents (at once)... " |
| 107 | sum=$(find . \( -name lost+found -prune \) -o ! -type d \ |
| 108 | -exec ${build}/fscat -d ${img} {} + | cksum) |
| 109 | sum2=$(find . \( -name lost+found -prune \) -o ! -type d -exec cat {} + | cksum) |
| 110 | if [ "${sum}" = "${sum2}" ]; then |
| 111 | printf "done\n" |
| 112 | else |
| 113 | printf "FAILED!\n" |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | cd - >/dev/null |
| 118 | sudo umount ${mnt} |
| 119 | } |
| 120 | |
Nico Huber | cc960f2 | 2024-01-29 01:13:45 +0100 | [diff] [blame^] | 121 | test_ext2 ext2 |
| 122 | test_ext2 ext4 |
| 123 | test_isofs |