]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-loop-block.c
Merge pull request #18863 from keszybz/cmdline-escaping
[thirdparty/systemd.git] / src / test / test-loop-block.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/loop.h>
5 #include <pthread.h>
6
7 #include "alloc-util.h"
8 #include "dissect-image.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "gpt.h"
13 #include "missing_loop.h"
14 #include "mkfs-util.h"
15 #include "mount-util.h"
16 #include "namespace-util.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "tests.h"
20 #include "tmpfile-util.h"
21 #include "user-util.h"
22 #include "virt.h"
23
24 #define N_THREADS 5
25 #define N_ITERATIONS 3
26
27 static usec_t end = 0;
28
29 static void* thread_func(void *ptr) {
30 int fd = PTR_TO_FD(ptr);
31 int r;
32
33 for (unsigned i = 0; i < N_ITERATIONS; i++) {
34 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
35 _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
36 _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
37
38 if (now(CLOCK_MONOTONIC) >= end) {
39 log_notice("Time's up, exiting thread's loop");
40 break;
41 }
42
43 log_notice("> Thread iteration #%u.", i);
44
45 assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
46
47 r = loop_device_make(fd, O_RDONLY, 0, UINT64_MAX, LO_FLAGS_PARTSCAN, &loop);
48 if (r < 0)
49 log_error_errno(r, "Failed to allocate loopback device: %m");
50 assert_se(r >= 0);
51
52 log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted);
53
54 r = dissect_image(loop->fd, NULL, NULL, loop->uevent_seqnum_not_before, loop->timestamp_not_before, DISSECT_IMAGE_READ_ONLY, &dissected);
55 if (r < 0)
56 log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node);
57 assert_se(r >= 0);
58
59 log_info("Dissected loop device %s", loop->node);
60
61 for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
62 if (!dissected->partitions[d].found)
63 continue;
64
65 log_notice("Found node %s fstype %s designator %s",
66 dissected->partitions[d].node,
67 dissected->partitions[d].fstype,
68 partition_designator_to_string(d));
69 }
70
71 assert_se(dissected->partitions[PARTITION_ESP].found);
72 assert_se(dissected->partitions[PARTITION_ESP].node);
73 assert_se(dissected->partitions[PARTITION_XBOOTLDR].found);
74 assert_se(dissected->partitions[PARTITION_XBOOTLDR].node);
75 assert_se(dissected->partitions[PARTITION_ROOT].found);
76 assert_se(dissected->partitions[PARTITION_ROOT].node);
77 assert_se(dissected->partitions[PARTITION_HOME].found);
78 assert_se(dissected->partitions[PARTITION_HOME].node);
79
80 r = dissected_image_mount(dissected, mounted, UID_INVALID, DISSECT_IMAGE_READ_ONLY);
81 log_notice_errno(r, "Mounted %s → %s: %m", loop->node, mounted);
82 assert_se(r >= 0);
83
84 log_notice("Unmounting %s", mounted);
85 mounted = umount_and_rmdir_and_free(mounted);
86
87 log_notice("Unmounted.");
88
89 dissected = dissected_image_unref(dissected);
90
91 log_notice("Detaching loop device %s", loop->node);
92 loop = loop_device_unref(loop);
93 log_notice("Detached loop device.");
94 }
95
96 log_notice("Leaving thread");
97
98 return NULL;
99 }
100
101 static bool have_root_gpt_type(void) {
102 #ifdef GPT_ROOT_NATIVE
103 return true;
104 #else
105 return false;
106 #endif
107 }
108
109 int main(int argc, char *argv[]) {
110 _cleanup_free_ char *p = NULL, *cmd = NULL;
111 _cleanup_(pclosep) FILE *sfdisk = NULL;
112 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
113 _cleanup_close_ int fd = -1;
114 _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
115 _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
116 pthread_t threads[N_THREADS];
117 const char *fs;
118 sd_id128_t id;
119 int r;
120
121 test_setup_logging(LOG_DEBUG);
122 log_show_tid(true);
123 log_show_time(true);
124
125 if (!have_root_gpt_type()) {
126 log_tests_skipped("No root partition GPT defined for this architecture, exiting.");
127 return EXIT_TEST_SKIP;
128 }
129
130 if (detect_container() > 0) {
131 log_tests_skipped("Test not supported in a container, requires udev/uevent notifications.");
132 return EXIT_TEST_SKIP;
133 }
134
135 if (strstr_ptr(ci_environment(), "autopkgtest") || strstr_ptr(ci_environment(), "github-actions")) {
136 // FIXME: we should reenable this one day
137 log_tests_skipped("Skipping test on Ubuntu autopkgtest CI/GH Actions, test too slow and installed udev too flakey.");
138 return EXIT_TEST_SKIP;
139 }
140
141 /* This is a test for the loopback block device setup code and it's use by the image dissection
142 * logic: since the kernel APIs are hard use and prone to races, let's test this in a heavy duty
143 * test: we open a bunch of threads and repeatedly allocate and deallocate loopback block devices in
144 * them in parallel, with an image file with a number of partitions. */
145
146 r = detach_mount_namespace();
147 if (ERRNO_IS_PRIVILEGE(r)) {
148 log_tests_skipped("Lacking privileges");
149 return EXIT_TEST_SKIP;
150 }
151
152 FOREACH_STRING(fs, "vfat", "ext4") {
153 r = mkfs_exists(fs);
154 assert_se(r >= 0);
155 if (!r) {
156 log_tests_skipped("mkfs.{vfat|ext4} not installed");
157 return EXIT_TEST_SKIP;
158 }
159 }
160
161 assert_se(r >= 0);
162
163 assert_se(tempfn_random_child("/var/tmp", "sfdisk", &p) >= 0);
164 fd = open(p, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOFOLLOW, 0666);
165 assert_se(fd >= 0);
166 assert_se(ftruncate(fd, 256*1024*1024) >= 0);
167
168 assert_se(cmd = strjoin("sfdisk ", p));
169 assert_se(sfdisk = popen(cmd, "we"));
170
171 /* A reasonably complex partition table that fits on a 64K disk */
172 fputs("label: gpt\n"
173 "size=32M, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B\n"
174 "size=32M, type=BC13C2FF-59E6-4262-A352-B275FD6F7172\n"
175 "size=32M, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
176 "size=32M, type=", sfdisk);
177
178 #ifdef GPT_ROOT_NATIVE
179 fprintf(sfdisk, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(GPT_ROOT_NATIVE));
180 #else
181 fprintf(sfdisk, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(GPT_ROOT_X86_64));
182 #endif
183
184 fputs("\n"
185 "size=32M, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915\n", sfdisk);
186
187 assert_se(pclose(sfdisk) == 0);
188 sfdisk = NULL;
189
190 assert_se(loop_device_make(fd, O_RDWR, 0, UINT64_MAX, LO_FLAGS_PARTSCAN, &loop) >= 0);
191 assert_se(dissect_image(loop->fd, NULL, NULL, loop->uevent_seqnum_not_before, loop->timestamp_not_before, 0, &dissected) >= 0);
192
193 assert_se(dissected->partitions[PARTITION_ESP].found);
194 assert_se(dissected->partitions[PARTITION_ESP].node);
195 assert_se(dissected->partitions[PARTITION_XBOOTLDR].found);
196 assert_se(dissected->partitions[PARTITION_XBOOTLDR].node);
197 assert_se(dissected->partitions[PARTITION_ROOT].found);
198 assert_se(dissected->partitions[PARTITION_ROOT].node);
199 assert_se(dissected->partitions[PARTITION_HOME].found);
200 assert_se(dissected->partitions[PARTITION_HOME].node);
201
202 assert_se(sd_id128_randomize(&id) >= 0);
203 assert_se(make_filesystem(dissected->partitions[PARTITION_ESP].node, "vfat", "EFI", id, true) >= 0);
204
205 assert_se(sd_id128_randomize(&id) >= 0);
206 assert_se(make_filesystem(dissected->partitions[PARTITION_XBOOTLDR].node, "vfat", "xbootldr", id, true) >= 0);
207
208 assert_se(sd_id128_randomize(&id) >= 0);
209 assert_se(make_filesystem(dissected->partitions[PARTITION_ROOT].node, "ext4", "root", id, true) >= 0);
210
211 assert_se(sd_id128_randomize(&id) >= 0);
212 assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", id, true) >= 0);
213
214 dissected = dissected_image_unref(dissected);
215 assert_se(dissect_image(loop->fd, NULL, NULL, loop->uevent_seqnum_not_before, loop->timestamp_not_before, 0, &dissected) >= 0);
216
217 assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
218
219 /* This first (writable) mount will initialize the mount point dirs, so that the subsequent read-only ones can work */
220 assert_se(dissected_image_mount(dissected, mounted, UID_INVALID, 0) >= 0);
221
222 assert_se(umount_recursive(mounted, 0) >= 0);
223 loop = loop_device_unref(loop);
224
225 log_notice("Threads are being started now");
226
227 /* Let's make sure we run for 10s on slow systems at max */
228 end = usec_add(now(CLOCK_MONOTONIC),
229 slow_tests_enabled() ? 5 * USEC_PER_SEC :
230 1 * USEC_PER_SEC);
231
232 for (unsigned i = 0; i < N_THREADS; i++)
233 assert_se(pthread_create(threads + i, NULL, thread_func, FD_TO_PTR(fd)) == 0);
234
235 log_notice("All threads started now.");
236
237 for (unsigned i = 0; i < N_THREADS; i++) {
238 log_notice("Joining thread #%u.", i);
239
240 void *k;
241 assert_se(pthread_join(threads[i], &k) == 0);
242 assert_se(k == NULL);
243
244 log_notice("Joined thread #%u.", i);
245 }
246
247 log_notice("Threads are all terminated now.");
248
249 return 0;
250 }