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