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