}
If a program needs to call multiple blkid functions, then passing in a
-cache value of NULL is not recommended, since the /etc/blkid.tab file
+cache value of NULL is not recommended, since the blkid.tab file
will be repeatedly parsed over and over again, with memory allocated
and deallocated. To initialize the blkid cache, blkid_get_cache()
function is used:
if (blkid_get_cache(&cache, NULL) < 0)
goto errout;
-The second parameter of blkid_get_cache (if non-zero) is the alternate
-filename of the blkid cache file (where the default is
-/etc/blkid.tab). Normally, programs should just pass in NULL.
+The second parameter of blkid_get_cache (if non-zero) is the alternate filename
+of the blkid cache file (see blkid man page for more information about the
+default cach file location).
+
+Normally, programs should just pass in NULL.
If you have called blkid_get_cache(), you should call blkid_put_cache()
when you are done using the blkid library functions. This will save the
environment variable BLKID_CONF. See manual libblkid/libblkid.3 for details
about the configuration file.
-Block device information is normally kept in a cache file /etc/blkid.tab that
-can be overridden by the environment variable BLKID_FILE.
+Block device information is normally kept in a cache file (see blkid man page
+for more information about the cache file location) that can be overridden by
+the environment variable BLKID_FILE.
To libmount uses three paths, which can be override by using environment
variables. Notice that these environment variables are ignored for non-root
The low-level part of the library also allows to extract infomation about
partitions and block device topology.
.P
-The high-level part of the library keeps information about block devices
-in a cache file
-.I /etc/blkid.tab
-and is verified to still be valid before being returned to the user
+The high-level part of the library keeps information about block devices in a
+cache file and is verified to still be valid before being returned to the user
(if the user has read permission on the raw block device, otherwise not).
The cache file also allows unprivileged users (normally anyone other
than root, or those not in the "disk" group) to locate devices by label/id.
.SH CONFIGURATION FILE
The standard location of the
.I /etc/blkid.conf
-config file can be overridden by the environment variable BLKID_CONF.
-The following options control the libblkid library:
-.TP
-.I SEND_UEVENT=<yes|not>
-Sends uevent when
-.I /dev/disk/by-{label,uuid}/
-symlink does not match with LABEL or UUID on the device. Default is "yes".
-.TP
-.I CACHE_FILE=<path>
-Overrides the standard location of the cache file. This setting can be
-overridden by the environment variable BLKID_FILE. Default is
-.I /etc/blkid.tab.
-.TP
-.I EVALUATE=<methods>
-Defines LABEL and UUID evaluation method(s). Currently, the libblkid library
-supports "udev" and "scan" methods. More than one methods may be specified in
-a comma separated list. Default is "udev,scan". The "udev" method uses udev
-.I /dev/disk/by-*
-symlinks and the "scan" method scans all block devices from the
-.I /proc/partitions
-file.
+config file can be overridden by the environment variable BLKID_CONF. For more
+details about the config file see
+.BR blkid (8)
+man page.
.SH AUTHOR
.B libblkid
was written by Andreas Dilger for the ext2 filesystem utilties, with input
from Ted Ts'o. The library was subsequently heavily modified by Ted Ts'o.
The low-level probing code was rewritten by Karel Zak.
-.SH FILES
-.TP 18
-.I /etc/blkid.tab
-caches data extracted from each recognized block device
-.TP
-.I /etc/blkid.conf
-configuration file
.SH AVAILABILITY
.B libblkid
is part of the util-linux package since version 2.15 and is available from
extern char *blkid_strndup(const char *s, const int length);
extern char *blkid_strconcat(const char *a, const char *b, const char *c);
-#define BLKID_CACHE_FILE "/etc/blkid.tab"
+/* config file */
#define BLKID_CONFIG_FILE "/etc/blkid.conf"
+/* cache file on systemds with /run */
+#define BLKID_RUNTIME_TOPDIR "/run"
+#define BLKID_RUNTIME_DIR BLKID_RUNTIME_TOPDIR "/blkid"
+#define BLKID_CACHE_FILE BLKID_RUNTIME_DIR "/blkid.tab"
+
+/* old systems */
+#define BLKID_CACHE_FILE_OLD "/etc/blkid.tab"
+
#define BLKID_ERR_IO 5
#define BLKID_ERR_PROC 9
#define BLKID_ERR_MEM 12
* @title: Cache
* @short_description: basic routines to work with libblkid cache
*
- * Block device information is normally kept in a cache file /etc/blkid.tab and is
+ * Block device information is normally kept in a cache file blkid.tab and is
* verified to still be valid before being returned to the user (if the user has
* read permission on the raw block device, otherwise not). The cache file also
* allows unprivileged users (normally anyone other than root, or those not in the
}
#endif
+static const char *get_default_cache_filename(void)
+{
+ struct stat st;
+
+ if (stat(BLKID_RUNTIME_TOPDIR, &st) == 0 && S_ISDIR(st.st_mode))
+ return BLKID_CACHE_FILE; /* cache in /run */
+
+ return BLKID_CACHE_FILE_OLD; /* cache in /etc */
+}
+
/* returns allocated path to cache */
char *blkid_get_cache_filename(struct blkid_config *conf)
{
else {
struct blkid_config *c = blkid_read_config(NULL);
if (!c)
- filename = blkid_strdup(BLKID_CACHE_FILE);
+ filename = blkid_strdup(get_default_cache_filename());
else {
filename = c->cachefile; /* already allocated */
c->cachefile = NULL;
if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
fprintf(stderr, "error %d parsing cache file %s\n", ret,
- argv[1] ? argv[1] : BLKID_CACHE_FILE);
+ argv[1] ? argv[1] : blkid_get_cache_filename(NULL));
exit(1);
}
if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
}
if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
fprintf(stderr, "error %d reading cache file %s\n", ret,
- argv[1] ? argv[1] : BLKID_CACHE_FILE);
+ argv[1] ? argv[1] : blkid_get_cache_filename(NULL));
blkid_put_cache(cache);
{
struct list_head *p;
char *tmp = NULL;
- const char *opened = NULL;
- const char *filename;
+ char *opened = NULL;
+ char *filename;
FILE *file = NULL;
int fd, ret = 0;
struct stat st;
return 0;
}
- filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
+ filename = cache->bic_filename ? cache->bic_filename :
+ blkid_get_cache_filename(NULL);
+
+ if (filename && strncmp(filename,
+ BLKID_RUNTIME_DIR "/", sizeof(BLKID_RUNTIME_DIR)) == 0) {
+
+ /* default destination, create the directory if necessary */
+ if (stat(BLKID_RUNTIME_DIR, &st) && errno == ENOENT) {
+
+ mkdir(BLKID_RUNTIME_DIR, S_IWUSR|
+ S_IRUSR|S_IRGRP|S_IROTH|
+ S_IXUSR|S_IXGRP|S_IXOTH);
+ }
+ }
/* If we can't write to the cache file, then don't even try */
if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
errout:
free(tmp);
+ if (filename != cache->bic_filename)
+ free(filename);
return ret;
}
* 1/ all tags are evaluated by udev /dev/disk/by-* symlinks,
* then the blkid_cache is NULL.
*
- * 2/ all tags are read from /etc/blkid.tab and verified by /dev
+ * 2/ all tags are read from blkid.tab and verified by /dev
* scanning, then the blkid_cache is not NULL and then it's
* better to reuse the blkid_cache.
*/
.BI \-c " cachefile"
Read from
.I cachefile
-instead of reading from the default cache file
-.IR /etc/blkid.tab .
-If you want to start with a clean cache (i.e. don't report devices previously
-scanned but not necessarily available at this time), specify
+instead of reading from the default cache file (see CONFIGURATION FILE section
+for more details). If you want to start with a clean cache (i.e. don't report
+devices previously scanned but not necessarily available at this time), specify
.IR /dev/null .
.TP
.B \-d
If the ambivalent low-level probing result was detected, an exit code of 8 is
returned.
+.SH CONFIGURATION FILE
+The standard location of the
+.I /etc/blkid.conf
+config file can be overridden by the environment variable BLKID_CONF.
+The following options control the libblkid library:
+.TP
+.I SEND_UEVENT=<yes|not>
+Sends uevent when
+.I /dev/disk/by-{label,uuid}/
+symlink does not match with LABEL or UUID on the device. Default is "yes".
+.TP
+.I CACHE_FILE=<path>
+Overrides the standard location of the cache file. This setting can be
+overridden by the environment variable BLKID_FILE. Default is
+.I /run/blkid/blkid.tab
+or
+.I /etc/blkid.tab
+on systems without /run direcotry
+.TP
+.I EVALUATE=<methods>
+Defines LABEL and UUID evaluation method(s). Currently, the libblkid library
+supports "udev" and "scan" methods. More than one methods may be specified in
+a comma separated list. Default is "udev,scan". The "udev" method uses udev
+.I /dev/disk/by-*
+symlinks and the "scan" method scans all block devices from the
+.I /proc/partitions
+file.
.SH AUTHOR
.B blkid
was written by Andreas Dilger for libblkid and improved by Theodore Ts'o
" [-o <format>] <dev> ...\n\n"
" %1$s -i [-s <tag>] [-o <format>] <dev> ...\n\n"
"Options:\n"
- " -c <file> cache file (default: /etc/blkid.tab, /dev/null = none)\n"
+ " -c <file> read from <file> instead of reading from the default\n"
+ " cache file (-c /dev/null means no cache)\n"
" -d don't encode non-printing characters\n"
" -h print this usage message and exit\n"
" -g garbage collect the blkid cache\n"