Debian Codegraph @

debian/tests/unmkinitramfs

raw
1 #!/bin/sh
2 set -eu
3
4 if command -v 3cpio >/dev/null; then
5 cpio_create="3cpio --create"
6 else
7 cpio_create="cpio -H newc -o --quiet"
8 fi
9
10 make_one_archive() {
11 local type="$1"
12 local i="$2"
13 local dir_name
14 local j
15
16 case "$type" in
17 early)
18 dir_name="kernel/dir$i"
19 ;;
20 main)
21 dir_name="dir$i"
22 ;;
23 *)
24 echo >&2 "E: Bad archive type $type"
25 exit 1
26 ;;
27 esac
28
29 mkdir -p "$AUTOPKGTEST_TMP/input/$type$i/$dir_name"
30 for j in $(seq 0 9); do
31 size=$((j * 987 + i * 654 + 321))
32 dd if=/dev/urandom of="$AUTOPKGTEST_TMP/input/$type$i/$dir_name/file$j" \
33 bs="$size" count=1 status=none
34 done
35 (
36 cd "$AUTOPKGTEST_TMP/input/$type$i"
37 find . | $cpio_create > ../"$type$i.cpio"
38 )
39 }
40
41 # Create input files and archives
42 mkdir "$AUTOPKGTEST_TMP/input"
43 for in_type in early main; do
44 for i in 0 1 2; do
45 make_one_archive "$in_type" "$i"
46 done
47 done
48
49 # Dummy compressor; wrapper for cat that ignores its argument (-c)
50 # shellcheck disable=SC2329
51 no_compressor() {
52 # shellcheck disable=SC2317
53 cat
54 }
55
56 rc=0
57
58 # Test up to 2 early and 2 main cpio archives, and all supported
59 # compressors (including none) for the last main archive
60 for n_early in 0 1 2; do
61 for n_main in 0 1 2; do
62 # There must be at least 1 archive
63 if [ $((n_early + n_main)) -eq 0 ]; then
64 continue
65 fi
66
67 for compressor in no_compressor gzip bzip2 lzma xz lzop 'lz4 -l' zstd; do
68 # If last archive is early, it can't be compressed
69 if [ $n_main -eq 0 ] && [ "$compressor" != no_compressor ]; then
70 continue
71 fi
72
73 echo "I: Testing $n_early early + $n_main main archive(s) with $compressor"
74
75 # Construct initramfs image
76 : > "$AUTOPKGTEST_TMP/initrd.img"
77 for i in $(seq 0 $((n_early - 1))); do
78 cat "$AUTOPKGTEST_TMP/input/early$i.cpio" \
79 >> "$AUTOPKGTEST_TMP/initrd.img"
80 done
81 for i in $(seq 0 $((n_main - 2))); do
82 cat "$AUTOPKGTEST_TMP/input/main$i.cpio" \
83 >> "$AUTOPKGTEST_TMP/initrd.img"
84 done
85 if [ $n_main -ge 1 ]; then
86 $compressor -c < "$AUTOPKGTEST_TMP/input/main$((n_main - 1)).cpio" \
87 >> "$AUTOPKGTEST_TMP/initrd.img"
88 fi
89
90 # Unpack it
91 rm -rf "$AUTOPKGTEST_TMP/output"
92 unmkinitramfs "$AUTOPKGTEST_TMP/initrd.img" \
93 "$AUTOPKGTEST_TMP/output" \
94 || {
95 echo >&2 'E: unmkinitramfs failed'
96 rc=1
97 continue
98 }
99
100 # Construct what we expect output to look like
101 rm -rf "$AUTOPKGTEST_TMP/reference"
102 mkdir "$AUTOPKGTEST_TMP/reference"
103 for i in $(seq 0 $((n_early - 1))); do
104 mkdir -p "$AUTOPKGTEST_TMP/reference/kernel"
105 ln -s ../../input/"early$i/kernel/dir$i" \
106 "$AUTOPKGTEST_TMP/reference/kernel/"
107 done
108 for i in $(seq 0 $((n_main - 1))); do
109 ln -s ../input/"main$i/dir$i" \
110 "$AUTOPKGTEST_TMP/reference/"
111 done
112
113 # Compare reference and output
114 diff -r "$AUTOPKGTEST_TMP/reference" \
115 "$AUTOPKGTEST_TMP/output" \
116 || {
117 echo >&2 'E: Output files do not match input'
118 rc=1
119 }
120 done
121 done
122 done
123
124 exit $rc