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