Debian Codegraph @

test/test-mic

raw
1 #!/bin/bash
2 # NEED_ROOT
3
4 set -e
5
6 [ "$(id -u)" = "0" ] ||
7 { echo "sorry, must be root"; exit 1; }
8
9 PT_TYPE="${PT_TYPE:-gpt}" # dos or gpt
10
11 cleanup() {
12 [ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
13 }
14 rq() {
15 local out="${TEMP_D}/out"
16 "$@" > "$out" 2>&1 || { error "FAILED [$?]:" "$@"; cat "$out"; return 1; }
17 }
18 fail() { echo "$@" 1>&2; exit 1; }
19 error() { echo "$@" 1>&2; }
20 msg() { error "$@"; }
21
22 do_pt_test() {
23 local img="$1" pt="$2" data="$3" info="$4" premsg="$5" out=""
24 msg "${premsg}testing partition $pt in $img"
25 mount-image-callback --cd "--part=$pt" "$img" -- \
26 sh -c "echo '$data' > data.txt" || {
27 error "${premsg}failed writing to partition $pt in $img";
28 return 1;
29 }
30 local ret="" expected=$(printf "%s\n%s\n" "$info" "$data")
31 out=$(mount-image-callback --read-only --cd "--part=$pt" "$img" -- \
32 sh -c '
33 r=0; for f in "$@"; do
34 cat $f || { echo "$f: cat failed rc=$?"; r=99; }; done;
35 exit $r' sh-extract info.txt data.txt)
36 ret=$?
37 if [ $ret -ne 0 -a $ret -ne 99 ]; then
38 error "${premsg}failed mounting part $pt for verification";
39 error "out=${out}"
40 return 1;
41 fi
42 [ "$expected" = "$out" ] || {
43 error "${premsg}"
44 error "== expected on $pt =="
45 error "$expected"
46 error "== found on $pt =="
47 error "$out"
48 return 1
49 }
50 }
51
52
53 TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
54 trap cleanup EXIT
55
56 pt1="${TEMP_D}/pt1.img"
57 pt2="${TEMP_D}/pt2.img"
58 prept="${TEMP_D}/header.img"
59 postpt="${TEMP_D}/foot.img"
60 img_mbr="${TEMP_D}/disk-mbr.img"
61 img_gpt="${TEMP_D}/disk-gpt.img"
62 MB=$((1024*1024))
63 SSIZE=512
64 pt1_size=$((100*$MB))
65 pt2_size=$((200*$MB))
66 prept_size=$MB
67 postpt_size=$MB
68
69 pt1_d="${TEMP_D}/pt1"
70 pt2_d="${TEMP_D}/pt2"
71 mkdir -p "$pt1_d" "$pt2_d"
72 echo "partition 1" > "$pt1_d/info.txt"
73 echo "file 1" > "$pt1_d/file1.txt"
74 mkdir "$pt1_d/dev" "$pt1_d/sys" "$pt1_d/proc" "$pt1_d/mnt"
75 echo "partition 2" > "$pt2_d/info.txt"
76
77 ## Stage 1
78 ## Create 2 un-partitioned images, put a filesystem on them.
79 ## And then mount them write a file, and then mount and
80 ## read the file to verify its there.
81 truncate -s "$pt1_size" "$pt1"
82 truncate -s "$pt2_size" "$pt2"
83 rq mkfs.ext4 -F "${pt1}"
84 rq mkfs.ext4 -F "${pt2}"
85
86 ## Stage 1.5: Verify
87 msg "testing partition 1 image"
88 mount-image-callback "$pt1" -- cp -r "$pt1_d/"* _MOUNTPOINT_ ||
89 fail "copying file to pt1 mount failed"
90
91 out=$(mount-image-callback --read-only "$pt1" --cd -- cat info.txt) &&
92 [ "$out" = "partition 1" ] ||
93 fail "failed verification of pt1 contents"
94
95 out=$(mount-image-callback --read-only --cd "$pt1" -- cat file1.txt)
96 [ "$out" = "file 1" ] ||
97 fail "found unexpected contents in file1.txt on pt1: $out"
98
99 msg "testing partition 2 image"
100 mount-image-callback "$pt2" -- cp -r "$pt2_d/"* _MOUNTPOINT_ ||
101 fail "copying file to pt2 mount failed"
102
103 out=$(mount-image-callback --read-only "$pt2" --cd -- cat info.txt) &&
104 [ "$out" = "partition 2" ] ||
105 fail "failed verification of pt2 contents"
106
107 ## Stage 1.6: Mount with overlay
108 copy_out="${TEMP_D}/copy-out"
109 mkdir -p "$TEMP_D/copy-out"
110 mount-image-callback --overlay --cd "$pt1" -- \
111 sh -ec 't="$1";
112 echo hi > new-file.txt; echo xxx > file1.txt;
113 cp -r * "$t"' -- "$copy_out"
114 read found < "$copy_out/file1.txt"
115 [ "$found" = "xxx" ] ||
116 fail "unexpected contents in file1.txt from overlay: $found."
117
118 [ -f "$copy_out/new-file.txt" ] ||
119 fail "new file created during overlay does not exist in copy out"
120 read found <"$copy_out/new-file.txt"
121 [ "$found" = "hi" ] ||
122 fail "unexpected contents in new-file.txt from overlay: $found."
123
124 ## verify the overlay mount changes did not change partition
125 out=$(mount-image-callback --cd "$pt1" -- cat file1.txt)
126 [ "$out" = "file 1" ] ||
127 fail "found unexpected contents in pt1:file1.txt after overlay: $out"
128
129 ## Stage 1.7: Test --system-mounts
130 msg "testing --system-mounts on pt1"
131 mount-image-callback --read-only --system-mounts --cd -- "$pt1" sh -c '
132 fails=0
133 logfail() { fails=$(($fails+1)); echo "$@" 1>&2; }
134 [ -e proc/1 ] || logfail "proc/1 did not exist: proc not mounted"
135 [ -d sys/class ] || logfail "sys/class did not exist: sys not mounted"
136 [ -e dev/null ] || logfail "dev/null did not exist: dev not mounted"' ||
137 fail "testing system-mounts failed."
138
139 ## Stage 1.8: Test unmounts of unexpected mounted dirs.
140 msg "testing unexpected mount get unmounted."
141 mount-image-callback --read-only --cd -- "$pt1" sh -ec '
142 fail() { echo "$@" 1>&2; }
143 mount -t tmpfs none -o size=10240 mnt/ || fail "failed tmpfs mount"
144 echo "hi mom" > mnt/file-on-tmpfs ||
145 fail "failed write to file-on-tmpfs"
146 mkdir mnt/mnt2 || fail "failed mkdir mnt/mnt2"
147 mount -t tmpfs none -o size=4096 mnt/mnt2 || fail "failed 2nd tmpfs mount"
148 echo "hi again" > mnt/mnt2/second-file-on-tmpfs' ||
149 fail "testing unexpected mounts failed."
150
151 # verify the file is not there.
152 mount-image-callback --read-only --cd -- "$pt1" sh -c '
153 fail() { echo "$@" 1>&2; exit 1; }
154 [ ! -e mnt/file-on-tmpfs ] || fail mnt/file-on-tmpfs existed
155 [ ! -e mnt/mnt2 ] || fail mnt/mnt2 existed
156 [ ! -e mnt/mnt2/second-file-on-tmpfs ] || fail second file existed
157 exit 0' ||
158 fail "file mnt/file-on-tmpfs existed."
159
160 ## Stage 2
161 ## Create a full disk image with those 2 partition images inside
162 ## and a partition table that points to them. Do one for MBR and GPT.
163 truncate -s "$prept_size" "$prept"
164 truncate -s "$postpt_size" "$postpt"
165
166 msg "writing hunks to disk image ${img_mbr}"
167 for hunk in "$prept" "$pt1" "$pt2" "$postpt"; do
168 rq dd bs=1M conv=notrunc oflag=append "if=$hunk" "of=${img_mbr}" ||
169 fail "failed adding $hunk to disk image"
170 done
171 cp "${img_mbr}" "$img_gpt"
172
173 #
174 sfdisk_in="$TEMP_D/ptable_in"
175 curstart=0
176 (
177 echo unit: sectors
178 for pair in "-:${prept_size}" "1:${pt1_size}" "2:${pt2_size}" "-:${postpt_size}"; do
179 op=${pair%%:*}
180 size="${pair#*:}"
181 if [ "$op" != "-" ]; then
182 echo "${curstart} $((size/$SSIZE)) L"
183 fi
184 curstart=$(($curstart+($size/$SSIZE)))
185 done
186 ) > "$sfdisk_in"
187
188 msg "partitioning MBR disk image ${img_mbr}"
189 (echo "label: dos"; cat "$sfdisk_in"; ) | rq sfdisk "${img_mbr}"
190
191 msg "partitioning GPT disk image ${img_gpt}"
192 (echo "label: gpt"; cat "$sfdisk_in"; ) | rq sfdisk "${img_gpt}"
193
194 ## Stage 2.5
195 ## Verify we can mount each partition with '--part=N'
196 ## write data, then remount and read expected data.
197 for toks in "MBR:${img_mbr}" "GPT:${img_gpt}"; do
198 img=${toks#*:}
199 prefix=${toks%%:*}
200 do_pt_test "$img" 1 "foo1" "partition 1" "$prefix: " ||
201 fail "${prefix}: failed testing partition 1 on $img"
202
203 do_pt_test "$img" 2 "foo2" "partition 2" "$prefix: " ||
204 fail "${prefix}: failed testing partition 2 on $img"
205 done
206
207 error "Finished tests."
208
209 # vi: ts=4 noexpandtab