chiark / gitweb /
23a24da907fa7ce910e48921324bbe6683e55ac5
[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[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 probe_superblocks(blkid_probe pr)
104 {
105         struct stat st;
106         int rc;
107
108         if (fstat(blkid_probe_get_fd(pr), &st))
109                 return -1;
110
111         blkid_probe_enable_partitions(pr, 1);
112
113         if (!S_ISCHR(st.st_mode) &&
114             blkid_probe_get_size(pr) <= 1024 * 1440 &&
115             blkid_probe_is_wholedisk(pr)) {
116                 /*
117                  * check if the small disk is partitioned, if yes then
118                  * don't probe for filesystems.
119                  */
120                 blkid_probe_enable_superblocks(pr, 0);
121
122                 rc = blkid_do_fullprobe(pr);
123                 if (rc < 0)
124                         return rc;        /* -1 = error, 1 = nothing, 0 = success */
125
126                 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
127                         return 0;        /* partition table detected */
128         }
129
130         blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
131         blkid_probe_enable_superblocks(pr, 1);
132
133         return blkid_do_safeprobe(pr);
134 }
135
136 static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test)
137 {
138         int64_t offset = 0;
139         bool noraid = false;
140         _cleanup_close_ int fd = -1;
141         blkid_probe pr;
142         const char *data;
143         const char *name;
144         int nvals;
145         int i;
146         size_t len;
147         int err = 0;
148
149         static const struct option options[] = {
150                 { "offset", optional_argument, NULL, 'o' },
151                 { "noraid", no_argument, NULL, 'R' },
152                 {}
153         };
154
155         for (;;) {
156                 int option;
157
158                 option = getopt_long(argc, argv, "oR", options, NULL);
159                 if (option == -1)
160                         break;
161
162                 switch (option) {
163                 case 'o':
164                         offset = strtoull(optarg, NULL, 0);
165                         break;
166                 case 'R':
167                         noraid = true;
168                         break;
169                 }
170         }
171
172         pr = blkid_new_probe();
173         if (!pr)
174                 return EXIT_FAILURE;
175
176         blkid_probe_set_superblocks_flags(pr,
177                 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
178                 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
179                 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
180
181         if (noraid)
182                 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
183
184         fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC);
185         if (fd < 0) {
186                 fprintf(stderr, "error: %s: %m\n", udev_device_get_devnode(dev));
187                 goto out;
188         }
189
190         err = blkid_probe_set_device(pr, fd, offset, 0);
191         if (err < 0)
192                 goto out;
193
194         log_debug("probe %s %sraid offset=%llu",
195                   udev_device_get_devnode(dev),
196                   noraid ? "no" : "", (unsigned long long) offset);
197
198         err = probe_superblocks(pr);
199         if (err < 0)
200                 goto out;
201
202         nvals = blkid_probe_numof_values(pr);
203         for (i = 0; i < nvals; i++) {
204                 if (blkid_probe_get_value(pr, i, &name, &data, &len))
205                         continue;
206                 len = strnlen((char *) data, len);
207                 print_property(dev, test, name, (char *) data);
208         }
209
210         blkid_free_probe(pr);
211 out:
212         if (err < 0)
213                 return EXIT_FAILURE;
214
215         return EXIT_SUCCESS;
216 }
217
218 const struct udev_builtin udev_builtin_blkid = {
219         .name = "blkid",
220         .cmd = builtin_blkid,
221         .help = "filesystem and partition probing",
222         .run_once = true,
223 };