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