]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev-builtin-blkid.c
tabs are as useful as a hole in the head
[thirdparty/systemd.git] / src / udev-builtin-blkid.c
CommitLineData
81dadce5
KS
1/*
2 * probe disks for filesystems and partitions
3 *
4 * Copyright (C) 2011 Kay Sievers <kay.sievers@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>
c38a141b 28#include <getopt.h>
81dadce5
KS
29#include <sys/stat.h>
30#include <blkid/blkid.h>
31
32#include "udev.h"
33
c38a141b 34static void print_property(struct udev_device *dev, bool test, const char *name, const char *value)
81dadce5 35{
912541b0 36 char s[265];
81dadce5 37
912541b0 38 s[0] = '\0';
81dadce5 39
912541b0
KS
40 if (!strcmp(name, "TYPE")) {
41 udev_builtin_add_property(dev, test, "ID_FS_TYPE", value);
81dadce5 42
912541b0
KS
43 } else if (!strcmp(name, "USAGE")) {
44 udev_builtin_add_property(dev, test, "ID_FS_USAGE", value);
e5d8ce91 45
912541b0
KS
46 } else if (!strcmp(name, "VERSION")) {
47 udev_builtin_add_property(dev, test, "ID_FS_VERSION", value);
81dadce5 48
912541b0
KS
49 } else if (!strcmp(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);
81dadce5 54
912541b0
KS
55 } else if (!strcmp(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);
81dadce5 60
912541b0
KS
61 } else if (!strcmp(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);
81dadce5 66
912541b0
KS
67 } else if (!strcmp(name, "PTTYPE")) {
68 udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value);
81dadce5 69
912541b0
KS
70 } else if (!strcmp(name, "PART_ENTRY_NAME")) {
71 blkid_encode_string(value, s, sizeof(s));
72 udev_builtin_add_property(dev, test, "PART_ENTRY_NAME", s);
81dadce5 73
912541b0
KS
74 } else if (!strcmp(name, "PART_ENTRY_TYPE")) {
75 blkid_encode_string(value, s, sizeof(s));
76 udev_builtin_add_property(dev, test, "PART_ENTRY_TYPE", s);
81dadce5 77
912541b0
KS
78 } else if (!strncmp(name, "PART_ENTRY_", 11)) {
79 util_strscpyl(s, sizeof(s), "ID_", name, NULL);
80 udev_builtin_add_property(dev, test, name, value);
81 }
81dadce5
KS
82}
83
84static int probe_superblocks(blkid_probe pr)
85{
912541b0
KS
86 struct stat st;
87 int rc;
81dadce5 88
912541b0
KS
89 if (fstat(blkid_probe_get_fd(pr), &st))
90 return -1;
81dadce5 91
912541b0 92 blkid_probe_enable_partitions(pr, 1);
81dadce5 93
912541b0
KS
94 if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
95 blkid_probe_is_wholedisk(pr)) {
96 /*
97 * check if the small disk is partitioned, if yes then
98 * don't probe for filesystems.
99 */
100 blkid_probe_enable_superblocks(pr, 0);
81dadce5 101
912541b0
KS
102 rc = blkid_do_fullprobe(pr);
103 if (rc < 0)
104 return rc; /* -1 = error, 1 = nothing, 0 = succes */
81dadce5 105
912541b0
KS
106 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
107 return 0; /* partition table detected */
108 }
81dadce5 109
912541b0
KS
110 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
111 blkid_probe_enable_superblocks(pr, 1);
81dadce5 112
912541b0 113 return blkid_do_safeprobe(pr);
81dadce5
KS
114}
115
e216e514 116static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test)
81dadce5 117{
912541b0
KS
118 struct udev *udev = udev_device_get_udev(dev);
119 int64_t offset = 0;
120 bool noraid = false;
121 int fd = -1;
122 blkid_probe pr;
123 const char *data;
124 const char *name;
125 int nvals;
126 int i;
127 size_t len;
128 int err = 0;
129
130 static const struct option options[] = {
131 { "offset", optional_argument, NULL, 'o' },
132 { "noraid", no_argument, NULL, 'R' },
133 {}
134 };
135
136 for (;;) {
137 int option;
138
139 option = getopt_long(argc, argv, "oR", options, NULL);
140 if (option == -1)
141 break;
142
143 switch (option) {
144 case 'o':
145 offset = strtoull(optarg, NULL, 0);
146 break;
147 case 'R':
148 noraid = true;
149 break;
150 }
151 }
152
153 pr = blkid_new_probe();
154 if (!pr) {
155 err = -ENOMEM;
156 return EXIT_FAILURE;
157 }
158
159 blkid_probe_set_superblocks_flags(pr,
160 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
161 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
162 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
163
164 if (noraid)
165 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
166
167 fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC);
168 if (fd < 0) {
169 fprintf(stderr, "error: %s: %m\n", udev_device_get_devnode(dev));
170 goto out;
171 }
172
173 err = blkid_probe_set_device(pr, fd, offset, 0);
174 if (err < 0)
175 goto out;
176
177 info(udev, "probe %s %sraid offset=%llu\n",
178 udev_device_get_devnode(dev),
179 noraid ? "no" : "", (unsigned long long) offset);
180
181 err = probe_superblocks(pr);
182 if (err < 0)
183 goto out;
184
185 nvals = blkid_probe_numof_values(pr);
186 for (i = 0; i < nvals; i++) {
187 if (blkid_probe_get_value(pr, i, &name, &data, &len))
188 continue;
189 len = strnlen((char *) data, len);
190 print_property(dev, test, name, (char *) data);
191 }
192
193 blkid_free_probe(pr);
81dadce5 194out:
912541b0
KS
195 if (fd > 0)
196 close(fd);
197 if (err < 0)
198 return EXIT_FAILURE;
199 return EXIT_SUCCESS;
81dadce5
KS
200}
201
202const struct udev_builtin udev_builtin_blkid = {
912541b0
KS
203 .name = "blkid",
204 .cmd = builtin_blkid,
205 .help = "filesystem and partition probing",
206 .run_once = true,
81dadce5 207};