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