]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-loop-block.c
tree-wide: hook up image dissection policy logic everywhere
[thirdparty/systemd.git] / src / test / test-loop-block.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
f93ba375
LP
2
3#include <fcntl.h>
4#include <linux/loop.h>
5#include <pthread.h>
19df770f 6#include <sys/file.h>
59a4c0d7
LP
7#include <sys/ioctl.h>
8#include <sys/mount.h>
f93ba375
LP
9
10#include "alloc-util.h"
baf0ab69 11#include "capability-util.h"
f93ba375
LP
12#include "dissect-image.h"
13#include "fd-util.h"
14#include "fileio.h"
15#include "fs-util.h"
16#include "gpt.h"
d4dd3689 17#include "main-func.h"
f93ba375
LP
18#include "missing_loop.h"
19#include "mkfs-util.h"
20#include "mount-util.h"
21#include "namespace-util.h"
d4dd3689 22#include "parse-util.h"
baf0ab69 23#include "path-util.h"
f93ba375
LP
24#include "string-util.h"
25#include "strv.h"
26#include "tests.h"
27#include "tmpfile-util.h"
28#include "user-util.h"
29#include "virt.h"
30
d4dd3689
LP
31static unsigned arg_n_threads = 5;
32static unsigned arg_n_iterations = 3;
33static usec_t arg_timeout = 0;
f93ba375 34
e745ddb1 35#if HAVE_BLKID
f93ba375
LP
36static usec_t end = 0;
37
baf0ab69
YW
38static void verify_dissected_image(DissectedImage *dissected) {
39 assert_se(dissected->partitions[PARTITION_ESP].found);
40 assert_se(dissected->partitions[PARTITION_ESP].node);
41 assert_se(dissected->partitions[PARTITION_XBOOTLDR].found);
42 assert_se(dissected->partitions[PARTITION_XBOOTLDR].node);
43 assert_se(dissected->partitions[PARTITION_ROOT].found);
44 assert_se(dissected->partitions[PARTITION_ROOT].node);
45 assert_se(dissected->partitions[PARTITION_HOME].found);
46 assert_se(dissected->partitions[PARTITION_HOME].node);
47}
48
02c15120
LP
49static void verify_dissected_image_harder(DissectedImage *dissected) {
50 verify_dissected_image(dissected);
51
52 assert_se(streq(dissected->partitions[PARTITION_ESP].fstype, "vfat"));
53 assert_se(streq(dissected->partitions[PARTITION_XBOOTLDR].fstype, "vfat"));
54 assert_se(streq(dissected->partitions[PARTITION_ROOT].fstype, "ext4"));
55 assert_se(streq(dissected->partitions[PARTITION_HOME].fstype, "ext4"));
56}
57
f93ba375
LP
58static void* thread_func(void *ptr) {
59 int fd = PTR_TO_FD(ptr);
60 int r;
61
d4dd3689 62 for (unsigned i = 0; i < arg_n_iterations; i++) {
f93ba375
LP
63 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
64 _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
65 _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
66
67 if (now(CLOCK_MONOTONIC) >= end) {
68 log_notice("Time's up, exiting thread's loop");
69 break;
70 }
71
72 log_notice("> Thread iteration #%u.", i);
73
74 assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
75
fd83c98e 76 r = loop_device_make(fd, O_RDONLY, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_SH, &loop);
f93ba375
LP
77 if (r < 0)
78 log_error_errno(r, "Failed to allocate loopback device: %m");
79 assert_se(r >= 0);
cc5bae6c 80 assert_se(loop->dev);
e77cab82 81 assert_se(loop->backing_file);
f93ba375
LP
82
83 log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted);
84
84be0c71 85 r = dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected);
f93ba375
LP
86 if (r < 0)
87 log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node);
88 assert_se(r >= 0);
89
90 log_info("Dissected loop device %s", loop->node);
91
92 for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
93 if (!dissected->partitions[d].found)
94 continue;
95
96 log_notice("Found node %s fstype %s designator %s",
97 dissected->partitions[d].node,
98 dissected->partitions[d].fstype,
99 partition_designator_to_string(d));
100 }
101
baf0ab69 102 verify_dissected_image(dissected);
f93ba375 103
21b61b1d 104 r = dissected_image_mount(dissected, mounted, UID_INVALID, UID_INVALID, DISSECT_IMAGE_READ_ONLY);
f93ba375
LP
105 log_notice_errno(r, "Mounted %s → %s: %m", loop->node, mounted);
106 assert_se(r >= 0);
107
41bc4849
LP
108 /* Now the block device is mounted, we don't need no manual lock anymore, the devices are now
109 * pinned by the mounts. */
110 assert_se(loop_device_flock(loop, LOCK_UN) >= 0);
111
f93ba375
LP
112 log_notice("Unmounting %s", mounted);
113 mounted = umount_and_rmdir_and_free(mounted);
114
115 log_notice("Unmounted.");
116
117 dissected = dissected_image_unref(dissected);
118
119 log_notice("Detaching loop device %s", loop->node);
120 loop = loop_device_unref(loop);
121 log_notice("Detached loop device.");
122 }
123
124 log_notice("Leaving thread");
125
126 return NULL;
127}
e745ddb1 128#endif
f93ba375
LP
129
130static bool have_root_gpt_type(void) {
92e72028 131#ifdef SD_GPT_ROOT_NATIVE
f93ba375
LP
132 return true;
133#else
134 return false;
135#endif
136}
137
d4dd3689 138static int run(int argc, char *argv[]) {
baf0ab69
YW
139#if HAVE_BLKID
140 _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
141 _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
142 pthread_t threads[arg_n_threads];
143 sd_id128_t id;
144#endif
f93ba375
LP
145 _cleanup_free_ char *p = NULL, *cmd = NULL;
146 _cleanup_(pclosep) FILE *sfdisk = NULL;
147 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
254d1313 148 _cleanup_close_ int fd = -EBADF;
f93ba375
LP
149 int r;
150
151 test_setup_logging(LOG_DEBUG);
152 log_show_tid(true);
153 log_show_time(true);
d4dd3689
LP
154 log_show_color(true);
155
156 if (argc >= 2) {
157 r = safe_atou(argv[1], &arg_n_threads);
158 if (r < 0)
159 return log_error_errno(r, "Failed to parse first argument (number of threads): %s", argv[1]);
160 if (arg_n_threads <= 0)
161 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Number of threads must be at least 1, refusing.");
162 }
163
164 if (argc >= 3) {
165 r = safe_atou(argv[2], &arg_n_iterations);
166 if (r < 0)
167 return log_error_errno(r, "Failed to parse second argument (number of iterations): %s", argv[2]);
168 if (arg_n_iterations <= 0)
169 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Number of iterations must be at least 1, refusing.");
170 }
171
172 if (argc >= 4) {
173 r = parse_sec(argv[3], &arg_timeout);
174 if (r < 0)
175 return log_error_errno(r, "Failed to parse third argument (timeout): %s", argv[3]);
176 }
177
178 if (argc >= 5)
179 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments (expected 3 at max).");
180
baf0ab69
YW
181 if (!have_root_gpt_type())
182 return log_tests_skipped("No root partition GPT defined for this architecture");
f93ba375 183
baf0ab69
YW
184 r = find_executable("sfdisk", NULL);
185 if (r < 0)
186 return log_tests_skipped_errno(r, "Could not find sfdisk command");
f93ba375
LP
187
188 assert_se(tempfn_random_child("/var/tmp", "sfdisk", &p) >= 0);
189 fd = open(p, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOFOLLOW, 0666);
190 assert_se(fd >= 0);
191 assert_se(ftruncate(fd, 256*1024*1024) >= 0);
192
193 assert_se(cmd = strjoin("sfdisk ", p));
194 assert_se(sfdisk = popen(cmd, "we"));
195
196 /* A reasonably complex partition table that fits on a 64K disk */
197 fputs("label: gpt\n"
198 "size=32M, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B\n"
199 "size=32M, type=BC13C2FF-59E6-4262-A352-B275FD6F7172\n"
200 "size=32M, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
201 "size=32M, type=", sfdisk);
202
92e72028
ZJS
203#ifdef SD_GPT_ROOT_NATIVE
204 fprintf(sfdisk, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_GPT_ROOT_NATIVE));
f93ba375 205#else
92e72028 206 fprintf(sfdisk, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_GPT_ROOT_X86_64));
f93ba375
LP
207#endif
208
209 fputs("\n"
210 "size=32M, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915\n", sfdisk);
211
212 assert_se(pclose(sfdisk) == 0);
213 sfdisk = NULL;
214
baf0ab69 215#if HAVE_BLKID
84be0c71 216 assert_se(dissect_image_file(p, NULL, NULL, NULL, 0, &dissected) >= 0);
baf0ab69
YW
217 verify_dissected_image(dissected);
218 dissected = dissected_image_unref(dissected);
219#endif
220
221 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) {
222 log_tests_skipped("not running privileged");
223 return 0;
224 }
225
226 if (detect_container() > 0) {
227 log_tests_skipped("Test not supported in a container, requires udev/uevent notifications");
228 return 0;
229 }
230
fd83c98e 231 assert_se(loop_device_make(fd, O_RDWR, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_EX, &loop) >= 0);
e745ddb1
LP
232
233#if HAVE_BLKID
84be0c71 234 assert_se(dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
baf0ab69 235 verify_dissected_image(dissected);
f93ba375 236
baf0ab69
YW
237 FOREACH_STRING(fs, "vfat", "ext4") {
238 r = mkfs_exists(fs);
239 assert_se(r >= 0);
240 if (!r) {
241 log_tests_skipped("mkfs.{vfat|ext4} not installed");
242 return 0;
243 }
244 }
245 assert_se(r >= 0);
f93ba375
LP
246
247 assert_se(sd_id128_randomize(&id) >= 0);
e1878ef7 248 assert_se(make_filesystem(dissected->partitions[PARTITION_ESP].node, "vfat", "EFI", NULL, id, true, 0, NULL) >= 0);
f93ba375
LP
249
250 assert_se(sd_id128_randomize(&id) >= 0);
e1878ef7 251 assert_se(make_filesystem(dissected->partitions[PARTITION_XBOOTLDR].node, "vfat", "xbootldr", NULL, id, true, 0, NULL) >= 0);
f93ba375
LP
252
253 assert_se(sd_id128_randomize(&id) >= 0);
e1878ef7 254 assert_se(make_filesystem(dissected->partitions[PARTITION_ROOT].node, "ext4", "root", NULL, id, true, 0, NULL) >= 0);
f93ba375
LP
255
256 assert_se(sd_id128_randomize(&id) >= 0);
e1878ef7 257 assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", NULL, id, true, 0, NULL) >= 0);
f93ba375
LP
258
259 dissected = dissected_image_unref(dissected);
9f2d9a4a 260
59a4c0d7
LP
261 /* We created the file systems now via the per-partition block devices. But the dissection code might
262 * probe them via the whole block device. These block devices have separate buffer caches though,
263 * hence what was written via the partition device might not appear on the whole block device
264 * yet. Let's hence explicitly flush the whole block device, so that the read-back definitely
265 * works. */
266 assert_se(ioctl(loop->fd, BLKFLSBUF, 0) >= 0);
267
9f2d9a4a
LP
268 /* Try to read once, without pinning or adding partitions, i.e. by only accessing the whole block
269 * device. */
84be0c71 270 assert_se(dissect_loop_device(loop, NULL, NULL, NULL, 0, &dissected) >= 0);
9f2d9a4a
LP
271 verify_dissected_image_harder(dissected);
272 dissected = dissected_image_unref(dissected);
273
274 /* Now go via the loopback device after all, but this time add/pin, because now we want to mount it. */
84be0c71 275 assert_se(dissect_loop_device(loop, NULL, NULL, NULL, DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES, &dissected) >= 0);
02c15120 276 verify_dissected_image_harder(dissected);
f93ba375
LP
277
278 assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
279
41bc4849
LP
280 /* We are particularly correct here, and now downgrade LOCK → LOCK_SH. That's because we are done
281 * with formatting the file systems, so we don't need the exclusive lock anymore. From now on a
282 * shared one is fine. This way udev can now probe the device if it wants, but still won't call
283 * BLKRRPART on it, and that's good, because that would destroy our partition table while we are at
284 * it. */
285 assert_se(loop_device_flock(loop, LOCK_SH) >= 0);
286
baf0ab69
YW
287 /* This is a test for the loopback block device setup code and it's use by the image dissection
288 * logic: since the kernel APIs are hard use and prone to races, let's test this in a heavy duty
289 * test: we open a bunch of threads and repeatedly allocate and deallocate loopback block devices in
290 * them in parallel, with an image file with a number of partitions. */
291 assert_se(detach_mount_namespace() >= 0);
292
f93ba375 293 /* This first (writable) mount will initialize the mount point dirs, so that the subsequent read-only ones can work */
21b61b1d 294 assert_se(dissected_image_mount(dissected, mounted, UID_INVALID, UID_INVALID, 0) >= 0);
f93ba375 295
41bc4849
LP
296 /* Now we mounted everything, the partitions are pinned. Now it's fine to release the lock
297 * fully. This means udev could now issue BLKRRPART again, but that's OK given this will fail because
298 * we now mounted the device. */
299 assert_se(loop_device_flock(loop, LOCK_UN) >= 0);
300
f93ba375
LP
301 assert_se(umount_recursive(mounted, 0) >= 0);
302 loop = loop_device_unref(loop);
303
304 log_notice("Threads are being started now");
305
d4dd3689
LP
306 /* zero timeout means pick default: let's make sure we run for 10s on slow systems at max */
307 if (arg_timeout == 0)
308 arg_timeout = slow_tests_enabled() ? 5 * USEC_PER_SEC : 1 * USEC_PER_SEC;
f93ba375 309
d4dd3689 310 end = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
da9ae8ea 311
d4dd3689
LP
312 if (arg_n_threads > 1)
313 for (unsigned i = 0; i < arg_n_threads; i++)
da9ae8ea 314 assert_se(pthread_create(threads + i, NULL, thread_func, FD_TO_PTR(fd)) == 0);
f93ba375
LP
315
316 log_notice("All threads started now.");
317
d4dd3689 318 if (arg_n_threads == 1)
da9ae8ea
LP
319 assert_se(thread_func(FD_TO_PTR(fd)) == NULL);
320 else
d4dd3689 321 for (unsigned i = 0; i < arg_n_threads; i++) {
da9ae8ea 322 log_notice("Joining thread #%u.", i);
f93ba375 323
da9ae8ea
LP
324 void *k;
325 assert_se(pthread_join(threads[i], &k) == 0);
baf0ab69 326 assert_se(!k);
f93ba375 327
da9ae8ea
LP
328 log_notice("Joined thread #%u.", i);
329 }
f93ba375
LP
330
331 log_notice("Threads are all terminated now.");
e745ddb1
LP
332#else
333 log_notice("Cutting test short, since we do not have libblkid.");
334#endif
f93ba375
LP
335 return 0;
336}
d4dd3689
LP
337
338DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);