chiark / gitweb /
udev: handle network controllers in nonstandard domains
[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 "udev.h"
33
34 static void print_property(struct udev_device *dev, bool test, const char *name, const char *value)
35 {
36         char s[265];
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, "PART_ENTRY_NAME")) {
71                 blkid_encode_string(value, s, sizeof(s));
72                 udev_builtin_add_property(dev, test, "ID_PART_ENTRY_NAME", s);
73
74         } else if (streq(name, "PART_ENTRY_TYPE")) {
75                 blkid_encode_string(value, s, sizeof(s));
76                 udev_builtin_add_property(dev, test, "ID_PART_ENTRY_TYPE", s);
77
78         } else if (startswith(name, "PART_ENTRY_")) {
79                 strscpyl(s, sizeof(s), "ID_", name, NULL);
80                 udev_builtin_add_property(dev, test, s, value);
81
82         } else if (streq(name, "SYSTEM_ID")) {
83                 blkid_encode_string(value, s, sizeof(s));
84                 udev_builtin_add_property(dev, test, "ID_FS_SYSTEM_ID", s);
85
86         } else if (streq(name, "PUBLISHER_ID")) {
87                 blkid_encode_string(value, s, sizeof(s));
88                 udev_builtin_add_property(dev, test, "ID_FS_PUBLISHER_ID", s);
89
90         } else if (streq(name, "APPLICATION_ID")) {
91                 blkid_encode_string(value, s, sizeof(s));
92                 udev_builtin_add_property(dev, test, "ID_FS_APPLICATION_ID", s);
93
94         } else if (streq(name, "BOOT_SYSTEM_ID")) {
95                 blkid_encode_string(value, s, sizeof(s));
96                 udev_builtin_add_property(dev, test, "ID_FS_BOOT_SYSTEM_ID", s);
97         }
98 }
99
100 static int probe_superblocks(blkid_probe pr)
101 {
102         struct stat st;
103         int rc;
104
105         if (fstat(blkid_probe_get_fd(pr), &st))
106                 return -1;
107
108         blkid_probe_enable_partitions(pr, 1);
109
110         if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
111             blkid_probe_is_wholedisk(pr)) {
112                 /*
113                  * check if the small disk is partitioned, if yes then
114                  * don't probe for filesystems.
115                  */
116                 blkid_probe_enable_superblocks(pr, 0);
117
118                 rc = blkid_do_fullprobe(pr);
119                 if (rc < 0)
120                         return rc;        /* -1 = error, 1 = nothing, 0 = succes */
121
122                 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
123                         return 0;        /* partition table detected */
124         }
125
126         blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
127         blkid_probe_enable_superblocks(pr, 1);
128
129         return blkid_do_safeprobe(pr);
130 }
131
132 static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test)
133 {
134         int64_t offset = 0;
135         bool noraid = false;
136         int fd = -1;
137         blkid_probe pr;
138         const char *data;
139         const char *name;
140         int nvals;
141         int i;
142         size_t len;
143         int err = 0;
144
145         static const struct option options[] = {
146                 { "offset", optional_argument, NULL, 'o' },
147                 { "noraid", no_argument, NULL, 'R' },
148                 {}
149         };
150
151         for (;;) {
152                 int option;
153
154                 option = getopt_long(argc, argv, "oR", options, NULL);
155                 if (option == -1)
156                         break;
157
158                 switch (option) {
159                 case 'o':
160                         offset = strtoull(optarg, NULL, 0);
161                         break;
162                 case 'R':
163                         noraid = true;
164                         break;
165                 }
166         }
167
168         pr = blkid_new_probe();
169         if (!pr)
170                 return EXIT_FAILURE;
171
172         blkid_probe_set_superblocks_flags(pr,
173                 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
174                 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
175                 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
176
177         if (noraid)
178                 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
179
180         fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC);
181         if (fd < 0) {
182                 fprintf(stderr, "error: %s: %m\n", udev_device_get_devnode(dev));
183                 goto out;
184         }
185
186         err = blkid_probe_set_device(pr, fd, offset, 0);
187         if (err < 0)
188                 goto out;
189
190         log_debug("probe %s %sraid offset=%llu\n",
191                   udev_device_get_devnode(dev),
192                   noraid ? "no" : "", (unsigned long long) offset);
193
194         err = probe_superblocks(pr);
195         if (err < 0)
196                 goto out;
197
198         nvals = blkid_probe_numof_values(pr);
199         for (i = 0; i < nvals; i++) {
200                 if (blkid_probe_get_value(pr, i, &name, &data, &len))
201                         continue;
202                 len = strnlen((char *) data, len);
203                 print_property(dev, test, name, (char *) data);
204         }
205
206         blkid_free_probe(pr);
207 out:
208         if (fd > 0)
209                 close(fd);
210         if (err < 0)
211                 return EXIT_FAILURE;
212         return EXIT_SUCCESS;
213 }
214
215 const struct udev_builtin udev_builtin_blkid = {
216         .name = "blkid",
217         .cmd = builtin_blkid,
218         .help = "filesystem and partition probing",
219         .run_once = true,
220 };