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