Debian Codegraph @

src/test/test-reread-partition-table.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/loop.h>
4 #include <sys/file.h>
5
6 #include "blockdev-util.h"
7 #include "capability-util.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "loop-util.h"
11 #include "memfd-util.h"
12 #include "path-util.h"
13 #include "process-util.h"
14 #include "reread-partition-table.h"
15 #include "tests.h"
16 #include "tmpfile-util.h"
17 #include "virt.h"
18
19 static void sfdisk(const char *sfdisk_path, LoopDevice *loop, const char *definition) {
20 int r;
21
22 assert(sfdisk_path);
23 assert(loop);
24 assert(definition);
25
26 _cleanup_close_ int memfd = memfd_new_and_seal("sfdisk", definition, SIZE_MAX);
27 ASSERT_OK(memfd);
28
29 r = pidref_safe_fork_full(
30 "(sfdisk)",
31 (int[]) { memfd, STDOUT_FILENO, STDERR_FILENO },
32 /* except_fds= */ NULL,
33 /* n_except_fds= */ 0,
34 FORK_CLOSE_ALL_FDS|FORK_RESET_SIGNALS|FORK_REARRANGE_STDIO|FORK_LOG|FORK_WAIT,
35 /* ret= */ NULL);
36 if (r == 0) {
37 /* child */
38 execl(sfdisk_path, "fdisk", "--no-tell-kernel", "--no-reread", loop->node, NULL);
39 _exit(EXIT_FAILURE);
40 }
41
42 ASSERT_OK(r);
43 }
44
45 TEST(rereadpt) {
46 int r;
47
48 if (detect_container() > 0)
49 return (void) log_tests_skipped("test not available in container");
50 if (have_effective_cap(CAP_SYS_ADMIN) <= 0)
51 return (void) log_tests_skipped("test requires privileges");
52 if (running_in_chroot() != 0)
53 return (void) log_tests_skipped("test not available in chroot()");
54
55 _cleanup_free_ char *sfdisk_path = NULL;
56 r = find_executable("sfdisk", &sfdisk_path);
57 if (r == -ENOENT)
58 return (void) log_tests_skipped("sfdisk not found");
59 ASSERT_OK(r);
60
61 _cleanup_close_ int fd = open_tmpfile_unlinkable("/var/tmp", O_RDWR);
62 ASSERT_FD(fd);
63
64 ASSERT_OK_ERRNO(ftruncate(fd, 100 * 1024 * 1024));
65
66 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
67 r = loop_device_make(
68 fd,
69 O_RDWR,
70 /* offset= */ 0,
71 /* size= */ UINT64_MAX,
72 /* sector_size= */ 512U,
73 LO_FLAGS_PARTSCAN,
74 LOCK_EX, &loop);
75 if (ERRNO_IS_NEG_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))
76 return (void) log_tests_skipped("loopback block devices not available");
77 if (r < 0)
78 return (void) log_tests_skipped_errno(r, "Failed to create loop device");
79
80 _cleanup_free_ char *p = NULL;
81 ASSERT_OK(partition_node_of(loop->node, 1, &p));
82 ASSERT_ERROR_ERRNO(access(p, F_OK), ENOENT);
83
84 /* No change */
85 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
86 ASSERT_ERROR_ERRNO(access(p, F_OK), ENOENT);
87
88 /* Create */
89 log_notice("CREATING 20M");
90 sfdisk(sfdisk_path,
91 loop,
92 "label: gpt\n"
93 "start=, size=20M, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7\n");
94
95 ASSERT_ERROR_ERRNO(access(p, F_OK), ENOENT);
96 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
97
98 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
99
100 _cleanup_close_ int pfd = -EBADF;
101 ASSERT_OK_ERRNO(pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
102 uint64_t size;
103 ASSERT_OK(blockdev_get_device_size(pfd, &size));
104 ASSERT_EQ(size, 20U*1024U*1024U);
105 pfd = safe_close(pfd);
106
107 /* No change */
108 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
109 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
110
111 /* No change, but synthesize change anyway */
112 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ REREADPT_FORCE_UEVENT));
113 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
114
115 /* Resize */
116 log_notice("RESIZING TO 30M");
117 sfdisk(sfdisk_path,
118 loop,
119 "label: gpt\n"
120 "start=, size=30M, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7\n");
121
122 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
123 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
124 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
125
126 ASSERT_OK_ERRNO(pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
127 ASSERT_OK(blockdev_get_device_size(pfd, &size));
128 ASSERT_EQ(size, 30U*1024U*1024U);
129 pfd = safe_close(pfd);
130
131 /* No change */
132 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
133 ASSERT_OK_ERRNO(pfd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
134
135 /* Move */
136 log_notice("MOVING BY 50M");
137 sfdisk(sfdisk_path,
138 loop,
139 "label: gpt\n"
140 "start=50M, size=15M, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7\n");
141
142 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
143 ASSERT_ERROR(reread_partition_table_fd(loop->fd, /* flags= */ 0), EBUSY);
144 pfd = safe_close(pfd);
145
146 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
147 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
148
149 pfd = ASSERT_OK_ERRNO(open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
150 ASSERT_OK(blockdev_get_device_size(pfd, &size));
151 ASSERT_EQ(size, 15U*1024U*1024U);
152 pfd = safe_close(pfd);
153
154 /* No change */
155 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
156 pfd = ASSERT_OK_ERRNO(open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY));
157
158 /* Remove */
159 log_notice("REMOVING");
160 sfdisk(sfdisk_path,
161 loop,
162 "label: gpt\n");
163
164 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
165 ASSERT_ERROR(reread_partition_table_fd(loop->fd, /* flags= */ 0), EBUSY);
166 pfd = safe_close(pfd);
167
168 ASSERT_OK_ZERO_ERRNO(access(p, F_OK));
169 ASSERT_OK(reread_partition_table_fd(loop->fd, /* flags= */ 0));
170 ASSERT_ERROR_ERRNO(access(p, F_OK), ENOENT);
171 }
172
173 DEFINE_TEST_MAIN(LOG_DEBUG);