]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-blkid.c
tree-wide: drop redundant _cleanup_ macros (#8810)
[thirdparty/systemd.git] / src / udev / udev-builtin-blkid.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * probe disks for filesystems and partitions
4 *
5 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
6 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <blkid.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <getopt.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #include "sd-id128.h"
32
33 #include "alloc-util.h"
34 #include "blkid-util.h"
35 #include "efivars.h"
36 #include "fd-util.h"
37 #include "gpt.h"
38 #include "parse-util.h"
39 #include "string-util.h"
40 #include "udev.h"
41
42 static void print_property(struct udev_device *dev, bool test, const char *name, const char *value) {
43 char s[256];
44
45 s[0] = '\0';
46
47 if (streq(name, "TYPE")) {
48 udev_builtin_add_property(dev, test, "ID_FS_TYPE", value);
49
50 } else if (streq(name, "USAGE")) {
51 udev_builtin_add_property(dev, test, "ID_FS_USAGE", value);
52
53 } else if (streq(name, "VERSION")) {
54 udev_builtin_add_property(dev, test, "ID_FS_VERSION", value);
55
56 } else if (streq(name, "UUID")) {
57 blkid_safe_string(value, s, sizeof(s));
58 udev_builtin_add_property(dev, test, "ID_FS_UUID", s);
59 blkid_encode_string(value, s, sizeof(s));
60 udev_builtin_add_property(dev, test, "ID_FS_UUID_ENC", s);
61
62 } else if (streq(name, "UUID_SUB")) {
63 blkid_safe_string(value, s, sizeof(s));
64 udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB", s);
65 blkid_encode_string(value, s, sizeof(s));
66 udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB_ENC", s);
67
68 } else if (streq(name, "LABEL")) {
69 blkid_safe_string(value, s, sizeof(s));
70 udev_builtin_add_property(dev, test, "ID_FS_LABEL", s);
71 blkid_encode_string(value, s, sizeof(s));
72 udev_builtin_add_property(dev, test, "ID_FS_LABEL_ENC", s);
73
74 } else if (streq(name, "PTTYPE")) {
75 udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value);
76
77 } else if (streq(name, "PTUUID")) {
78 udev_builtin_add_property(dev, test, "ID_PART_TABLE_UUID", value);
79
80 } else if (streq(name, "PART_ENTRY_NAME")) {
81 blkid_encode_string(value, s, sizeof(s));
82 udev_builtin_add_property(dev, test, "ID_PART_ENTRY_NAME", s);
83
84 } else if (streq(name, "PART_ENTRY_TYPE")) {
85 blkid_encode_string(value, s, sizeof(s));
86 udev_builtin_add_property(dev, test, "ID_PART_ENTRY_TYPE", s);
87
88 } else if (startswith(name, "PART_ENTRY_")) {
89 strscpyl(s, sizeof(s), "ID_", name, NULL);
90 udev_builtin_add_property(dev, test, s, value);
91
92 } else if (streq(name, "SYSTEM_ID")) {
93 blkid_encode_string(value, s, sizeof(s));
94 udev_builtin_add_property(dev, test, "ID_FS_SYSTEM_ID", s);
95
96 } else if (streq(name, "PUBLISHER_ID")) {
97 blkid_encode_string(value, s, sizeof(s));
98 udev_builtin_add_property(dev, test, "ID_FS_PUBLISHER_ID", s);
99
100 } else if (streq(name, "APPLICATION_ID")) {
101 blkid_encode_string(value, s, sizeof(s));
102 udev_builtin_add_property(dev, test, "ID_FS_APPLICATION_ID", s);
103
104 } else if (streq(name, "BOOT_SYSTEM_ID")) {
105 blkid_encode_string(value, s, sizeof(s));
106 udev_builtin_add_property(dev, test, "ID_FS_BOOT_SYSTEM_ID", s);
107 }
108 }
109
110 static int find_gpt_root(struct udev_device *dev, blkid_probe pr, bool test) {
111
112 #if defined(GPT_ROOT_NATIVE) && ENABLE_EFI
113
114 _cleanup_free_ char *root_id = NULL;
115 bool found_esp = false;
116 blkid_partlist pl;
117 int i, nvals, r;
118
119 assert(pr);
120
121 /* Iterate through the partitions on this disk, and see if the
122 * EFI ESP we booted from is on it. If so, find the first root
123 * disk, and add a property indicating its partition UUID. */
124
125 errno = 0;
126 pl = blkid_probe_get_partitions(pr);
127 if (!pl)
128 return -errno ?: -ENOMEM;
129
130 nvals = blkid_partlist_numof_partitions(pl);
131 for (i = 0; i < nvals; i++) {
132 blkid_partition pp;
133 const char *stype, *sid;
134 sd_id128_t type;
135
136 pp = blkid_partlist_get_partition(pl, i);
137 if (!pp)
138 continue;
139
140 sid = blkid_partition_get_uuid(pp);
141 if (!sid)
142 continue;
143
144 stype = blkid_partition_get_type_string(pp);
145 if (!stype)
146 continue;
147
148 if (sd_id128_from_string(stype, &type) < 0)
149 continue;
150
151 if (sd_id128_equal(type, GPT_ESP)) {
152 sd_id128_t id, esp;
153
154 /* We found an ESP, let's see if it matches
155 * the ESP we booted from. */
156
157 if (sd_id128_from_string(sid, &id) < 0)
158 continue;
159
160 r = efi_loader_get_device_part_uuid(&esp);
161 if (r < 0)
162 return r;
163
164 if (sd_id128_equal(id, esp))
165 found_esp = true;
166
167 } else if (sd_id128_equal(type, GPT_ROOT_NATIVE)) {
168 unsigned long long flags;
169
170 flags = blkid_partition_get_flags(pp);
171 if (flags & GPT_FLAG_NO_AUTO)
172 continue;
173
174 /* We found a suitable root partition, let's
175 * remember the first one. */
176
177 if (!root_id) {
178 root_id = strdup(sid);
179 if (!root_id)
180 return -ENOMEM;
181 }
182 }
183 }
184
185 /* We found the ESP on this disk, and also found a root
186 * partition, nice! Let's export its UUID */
187 if (found_esp && root_id)
188 udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", root_id);
189 #endif
190
191 return 0;
192 }
193
194 static int probe_superblocks(blkid_probe pr) {
195 struct stat st;
196 int rc;
197
198 if (fstat(blkid_probe_get_fd(pr), &st))
199 return -errno;
200
201 blkid_probe_enable_partitions(pr, 1);
202
203 if (!S_ISCHR(st.st_mode) &&
204 blkid_probe_get_size(pr) <= 1024 * 1440 &&
205 blkid_probe_is_wholedisk(pr)) {
206 /*
207 * check if the small disk is partitioned, if yes then
208 * don't probe for filesystems.
209 */
210 blkid_probe_enable_superblocks(pr, 0);
211
212 rc = blkid_do_fullprobe(pr);
213 if (rc < 0)
214 return rc; /* -1 = error, 1 = nothing, 0 = success */
215
216 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
217 return 0; /* partition table detected */
218 }
219
220 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
221 blkid_probe_enable_superblocks(pr, 1);
222
223 return blkid_do_safeprobe(pr);
224 }
225
226 static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test) {
227 const char *root_partition;
228 int64_t offset = 0;
229 bool noraid = false;
230 _cleanup_close_ int fd = -1;
231 _cleanup_(blkid_free_probep) blkid_probe pr = NULL;
232 const char *data;
233 const char *name;
234 int nvals;
235 int i;
236 int err = 0;
237 bool is_gpt = false;
238
239 static const struct option options[] = {
240 { "offset", required_argument, NULL, 'o' },
241 { "noraid", no_argument, NULL, 'R' },
242 {}
243 };
244
245 for (;;) {
246 int option;
247
248 option = getopt_long(argc, argv, "o:R", options, NULL);
249 if (option == -1)
250 break;
251
252 switch (option) {
253 case 'o':
254 err = safe_atoi64(optarg, &offset);
255 if (err < 0)
256 goto out;
257 if (offset < 0) {
258 err = -ERANGE;
259 goto out;
260 }
261 break;
262 case 'R':
263 noraid = true;
264 break;
265 }
266 }
267
268 pr = blkid_new_probe();
269 if (!pr)
270 return EXIT_FAILURE;
271
272 blkid_probe_set_superblocks_flags(pr,
273 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
274 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
275 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
276
277 if (noraid)
278 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
279
280 fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC);
281 if (fd < 0) {
282 err = log_debug_errno(errno, "Failure opening block device %s: %m", udev_device_get_devnode(dev));
283 goto out;
284 }
285
286 err = blkid_probe_set_device(pr, fd, offset, 0);
287 if (err < 0)
288 goto out;
289
290 log_debug("probe %s %sraid offset=%"PRIi64,
291 udev_device_get_devnode(dev),
292 noraid ? "no" : "", offset);
293
294 err = probe_superblocks(pr);
295 if (err < 0)
296 goto out;
297
298 /* If we are a partition then our parent passed on the root
299 * partition UUID to us */
300 root_partition = udev_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID");
301
302 nvals = blkid_probe_numof_values(pr);
303 for (i = 0; i < nvals; i++) {
304 if (blkid_probe_get_value(pr, i, &name, &data, NULL))
305 continue;
306
307 print_property(dev, test, name, data);
308
309 /* Is this a disk with GPT partition table? */
310 if (streq(name, "PTTYPE") && streq(data, "gpt"))
311 is_gpt = true;
312
313 /* Is this a partition that matches the root partition
314 * property we inherited from our parent? */
315 if (root_partition && streq(name, "PART_ENTRY_UUID") && streq(data, root_partition))
316 udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT", "1");
317 }
318
319 if (is_gpt)
320 find_gpt_root(dev, pr, test);
321
322 out:
323 if (err < 0)
324 return EXIT_FAILURE;
325
326 return EXIT_SUCCESS;
327 }
328
329 const struct udev_builtin udev_builtin_blkid = {
330 .name = "blkid",
331 .cmd = builtin_blkid,
332 .help = "Filesystem and partition probing",
333 .run_once = true,
334 };