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