]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/blkid/devname.c
2 * devname.c - get a dev by its device inode name
4 * Copyright (C) Andries Brouwer
5 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o
6 * Copyright (C) 2001 Andreas Dilger
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
23 #include <sys/types.h>
32 #include <sys/mkdev.h>
36 #include "blkid/blkid.h"
38 /* #define DEBUG_DEVNAME */
46 * Find a dev struct in the cache by device name, if available.
48 blkid_dev
*blkid_find_devname(blkid_cache
*cache
, const char *devname
)
50 blkid_dev
*dev
= NULL
;
53 if (!cache
|| !devname
)
56 list_for_each(p
, &cache
->bic_devs
) {
57 blkid_dev
*tmp
= list_entry(p
, blkid_dev
, bid_devs
);
59 if (strcmp(tmp
->bid_name
, devname
))
62 DBG(printf("found devname %s in cache\n", tmp
->bid_name
));
63 dev
= blkid_verify_devname(cache
, tmp
);
71 * Return a pointer to an dev struct, either from cache or by probing.
73 blkid_dev
*blkid_get_devname(blkid_cache
*cache
, const char *devname
)
77 if ((dev
= blkid_find_devname(cache
, devname
)))
80 dev
= blkid_devname_to_dev(devname
, 0);
81 return blkid_add_dev_to_cache(cache
, dev
);
85 * Probe a single block device to add to the device cache.
86 * If the size is not specified, it will be found in blkid_devname_to_dev().
88 static blkid_dev
*probe_one(blkid_cache
*cache
, const char *ptname
,
89 int major
, int minor
, unsigned long long size
)
91 dev_t devno
= makedev(major
, minor
);
96 /* See if we already have this device number in the cache. */
97 if ((dev
= blkid_find_devno(cache
, devno
)))
101 * Take a quick look at /dev/ptname for the device number. We check
102 * all of the likely device directories. If we don't find it, or if
103 * the stat information doesn't check out, use blkid_devno_to_devname()
104 * to find it via an exhaustive search for the device major/minor.
106 for (dir
= devdirs
; *dir
; dir
++) {
110 sprintf(device
, "%s/%s", *dir
, ptname
);
111 if ((dev
= blkid_find_devname(cache
, device
)) &&
112 dev
->bid_devno
== devno
)
115 if (stat(device
, &st
) == 0 && st
.st_rdev
== devno
) {
116 devname
= string_copy(device
);
121 devname
= blkid_devno_to_devname(devno
);
125 dev
= blkid_devname_to_dev(devname
, size
);
126 string_free(devname
);
128 return blkid_add_dev_to_cache(cache
, dev
);
131 #define PROC_PARTITIONS "/proc/partitions"
132 #define VG_DIR "/proc/lvm/VGs"
135 * This function initializes the UUID cache with devices from the LVM
136 * proc hierarchy. We currently depend on the names of the LVM
137 * hierarchy giving us the device structure in /dev. (XXX is this a
142 static int lvm_get_devno(const char *lvm_device
, int *major
, int *minor
,
152 DBG(printf("opening %s\n", lvm_device
));
153 if ((lvf
= fopen(lvm_device
, "r")) == NULL
) {
155 DBG(printf("%s: (%d) %s\n", lvm_device
, ret
, strerror(ret
)));
159 while (fgets(buf
, sizeof(buf
), lvf
)) {
160 if (sscanf(buf
, "size: %llu", size
) == 1) { /* sectors */
163 if (sscanf(buf
, "device: %d:%d", major
, minor
) == 2) {
173 static void lvm_probe_all(blkid_cache
**cache
)
176 struct dirent
*vg_iter
;
177 int vg_len
= strlen(VG_DIR
);
179 if ((vg_list
= opendir(VG_DIR
)) == NULL
)
182 DBG(printf("probing LVM devices under %s\n", VG_DIR
));
184 while ((vg_iter
= readdir(vg_list
)) != NULL
) {
188 struct dirent
*lv_iter
;
190 vg_name
= vg_iter
->d_name
;
191 if (!strcmp(vg_name
, ".") || !strcmp(vg_name
, ".."))
193 vdirname
= malloc(vg_len
+ strlen(vg_name
) + 8);
196 sprintf(vdirname
, "%s/%s/LVs", VG_DIR
, vg_name
);
198 lv_list
= opendir(vdirname
);
203 while ((lv_iter
= readdir(lv_list
)) != NULL
) {
204 char *lv_name
, *lvm_device
;
208 lv_name
= lv_iter
->d_name
;
209 if (!strcmp(lv_name
, ".") || !strcmp(lv_name
, ".."))
212 lvm_device
= malloc(vg_len
+ strlen(vg_name
) +
213 strlen(lv_name
) + 8);
218 sprintf(lvm_device
, "%s/%s/LVs/%s", VG_DIR
, vg_name
,
220 if (lvm_get_devno(lvm_device
, &major
, &minor
, &size
)) {
224 sprintf(lvm_device
, "%s/%s", vg_name
, lv_name
);
225 DBG(printf("LVM dev %s: devno 0x%02X%02X, size %Ld\n",
226 lvm_device
, major
, minor
, size
));
227 probe_one(*cache
, lvm_device
, major
, minor
, size
);
237 #define PROC_EVMS_VOLUMES "/proc/evms/volumes"
240 evms_probe_all(blkid_cache
**cache
)
243 int ma
, mi
, sz
, num
= 0;
247 procpt
= fopen(PROC_EVMS_VOLUMES
, "r");
250 while (fgets(line
, sizeof(line
), procpt
)) {
251 if (sscanf (line
, " %d %d %d %*s %*s %[^\n ]",
252 &ma
, &mi
, &sz
, device
) != 4)
255 DBG(printf("Checking partition %s (%d, %d)\n",
258 probe_one(*cache
, device
, ma
, mi
, sz
<< 10);
266 * Read the device data for all available block devices in the system.
268 int blkid_probe_all(blkid_cache
**cache
)
274 return -BLKID_ERR_PARAM
;
277 *cache
= blkid_new_cache();
280 return -BLKID_ERR_MEM
;
282 if ((*cache
)->bic_flags
& BLKID_BIC_FL_PROBED
&&
283 time(0) - (*cache
)->bic_time
< BLKID_PROBE_INTERVAL
)
286 if (evms_probe_all(cache
))
290 lvm_probe_all(cache
);
293 proc
= fopen(PROC_PARTITIONS
, "r");
295 return -BLKID_ERR_PROC
;
297 for (firstPass
= 1; firstPass
>= 0; firstPass
--) {
299 char ptname0
[128], ptname1
[128];
300 char *ptnames
[2] = { ptname0
, ptname1
};
301 int majors
[2], minors
[2];
302 unsigned long long sizes
[2];
303 int lens
[2] = { 0, 0 };
305 int which
= 0, last
= 0;
307 fseek(proc
, 0, SEEK_SET
);
309 while (fgets(line
, sizeof(line
), proc
)) {
313 if (sscanf(line
, " %d %d %lld %128[^\n ]",
314 &majors
[which
], &minors
[which
],
315 &sizes
[which
], ptnames
[which
]) != 4)
318 DBG(printf("read partition name %s\n", ptnames
[which
]));
320 /* look only at md devices on first pass */
321 handleOnFirst
= !strncmp(ptnames
[which
], "md", 2);
322 if (firstPass
!= handleOnFirst
)
325 /* Skip whole disk devs unless they have no partitions
326 * If we don't have a partition on this dev, also
327 * check previous dev to see if it didn't have a partn.
328 * heuristic: partition name ends in a digit.
330 * Skip extended partitions.
331 * heuristic: size is 1
333 * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs
336 lens
[which
] = strlen(ptnames
[which
]);
337 if (isdigit(ptnames
[which
][lens
[which
] - 1])) {
338 DBG(printf("partition dev %s, devno 0x%02X%02X\n",
339 ptnames
[which
], majors
[which
],
342 if (sizes
[which
] > 1)
343 probe_one(*cache
, ptnames
[which
],
344 majors
[which
], minors
[which
],
348 } else if (lens
[last
] &&
349 strncmp(ptnames
[last
], ptnames
[which
],
351 DBG(printf("whole dev %s, devno 0x%02X%02X\n",
352 ptnames
[last
], majors
[last
],
354 probe_one(*cache
, ptnames
[last
], majors
[last
],
355 minors
[last
], sizes
[last
] << 10);
360 /* Handle the last device if it wasn't partitioned */
362 probe_one(*cache
, ptnames
[which
], majors
[which
],
363 minors
[which
], sizes
[which
] << 10);
368 (*cache
)->bic_time
= time(0);
369 (*cache
)->bic_flags
|= BLKID_BIC_FL_PROBED
;
375 int main(int argc
, char **argv
)
377 blkid_cache
*cache
= NULL
;
380 fprintf(stderr
, "Usage: %s\n"
381 "Probe all devices and exit\n", argv
[0]);
384 if (blkid_probe_all(&cache
) < 0)
385 printf("%s: error probing devices\n", argv
[0]);
387 blkid_free_cache(cache
);