]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blkid: move cache to /run on new systems
authorKarel Zak <kzak@redhat.com>
Wed, 25 Jan 2012 11:56:39 +0000 (12:56 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 25 Jan 2012 12:20:37 +0000 (13:20 +0100)
 Why?

 * read-only root

 * /etc is pretty bad place for caches

 * all is usually cached by udev in /dev/disk/by-* and libblkid
   is able to use these symlinks

 * boot persistent cache is attractive for very small subset of
   Linux machines (and they already need extra udev tunning otherwise
   udev will probe all block devices during boot)

 * the default is possible to override in /etc/blkid.conf

The systems without /run directory will not be affected by this
change.

Signed-off-by: Karel Zak <kzak@redhat.com>
Documentation/blkid.txt
Documentation/howto-debug.txt
libblkid/libblkid.3
libblkid/src/blkidP.h
libblkid/src/cache.c
libblkid/src/read.c
libblkid/src/save.c
libmount/src/cache.c
misc-utils/blkid.8
misc-utils/blkid.c

index 4fa9be1f72d6897b79009fc94c24f65f6dba61e0..c58b04f6799e1acfd4504d520e3ba753229209ea 100644 (file)
@@ -58,7 +58,7 @@ type, or label, or uuid for a particular device:
        }
 
 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:
@@ -66,9 +66,11 @@ 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
index 28c6e2252629ebbcb2879e60c088e4e92e21bbef..99e2755d421a8d03b14d5b98abd7583f7306c15e 100644 (file)
@@ -58,8 +58,9 @@ The libblkid reads by default /etc/blkid.conf which can be overriden by 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
index a9291e2cc3934cc1468cb3812af6def9d1351c60..7f1c04522b6f78a689efdb458a152aa2ffd3d9cd 100644 (file)
@@ -26,10 +26,8 @@ specific block device names into configuration files.
 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.
@@ -58,40 +56,16 @@ in this situation.
 .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
index 00689e827a947364421c9d20e20a3f8ede167d2a..b6c2bc607fb4b14255d1b48135e9157e77cc3f6f 100644 (file)
@@ -286,9 +286,17 @@ extern char *blkid_strdup(const char *s);
 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
index c100cfa2036b0effc04a928763a8defc34910f68..2eab2d3292bc2342838319e90953f014f6826b49 100644 (file)
@@ -31,7 +31,7 @@ int blkid_debug_mask = 0;
  * @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
@@ -95,6 +95,16 @@ void blkid_init_debug(int mask)
 }
 #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)
 {
@@ -108,7 +118,7 @@ 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;
@@ -244,7 +254,7 @@ int main(int argc, char** argv)
 
        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) {
index b9fda3aaeedebd436cece2e3d4424a8f4b5e02dd..97fd4b4a2404c730739a58dcee2dbdeed0dfb82e 100644 (file)
@@ -489,7 +489,7 @@ int main(int argc, char**argv)
        }
        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);
 
index a71b068787b0c8106a898f697332433dc865fee3..7673ab42e74ba47e2345d4ee547850d513ec3916 100644 (file)
@@ -57,8 +57,8 @@ int blkid_flush_cache(blkid_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;
@@ -72,7 +72,20 @@ int blkid_flush_cache(blkid_cache cache)
                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) ||
@@ -157,6 +170,8 @@ int blkid_flush_cache(blkid_cache cache)
 
 errout:
        free(tmp);
+       if (filename != cache->bic_filename)
+               free(filename);
        return ret;
 }
 
index cf319ca25e88fb6ecb6bcbdb49e098aebbaad57b..1b3e94d00ee5b0dd03a18820b90fabbd2f4f7c22 100644 (file)
@@ -54,7 +54,7 @@ struct libmnt_cache {
         * 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.
         */
index 31805c6116dacf9fac0babe6773df069f06227b8..c8a31210a9dafe795ed53c143ebb83ae897d36c6 100644 (file)
@@ -76,10 +76,9 @@ same meaning as "KiB") or decimal (10^N) suffixes KB, MB, GB, PB and EB.
 .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
@@ -255,6 +254,33 @@ For usage or other errors, an exit code of 4 is returned.
 
 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
index 2976bca02683ebba50746716c4382a12f7e7d922..463fad3c0d37f8b07c0616bc6376c43f7612e5b7 100644 (file)
@@ -67,7 +67,8 @@ static void usage(int error)
                "       [-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"