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