From 0bdd2f97fcbcf773c8174dca2be60d1360f2c5df Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 18 Feb 2009 00:37:28 +0100 Subject: [PATCH] blkid: add cmdline interface for blkid_probe_filter_usage() Signed-off-by: Karel Zak --- libs/blkid/TODO | 11 +--------- libs/blkid/bin/blkid.8 | 18 ++++++++++++++++- libs/blkid/bin/blkid.c | 46 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/libs/blkid/TODO b/libs/blkid/TODO index e09ec7948..bb4e425b4 100644 --- a/libs/blkid/TODO +++ b/libs/blkid/TODO @@ -13,19 +13,10 @@ - test blkid within udev rules - - add command line interface for blkid_probe_filter_usage(): - - # blkid -p -o udev --filter-usage filesystems - - # blkid -p -o udev --filter-usage noraid - - and blkid_probe_filter_types(): + - add command line interface for blkid_probe_filter_types(): # blkid -p -o udev --filter-type nofat - note that "--filter-usage" is necessary for compatibility with vol_id where - we in udev rules use things like "vol_id --skip-raid" - - (?) we need to ignore cache and config files when the files are writable for non-root users and the library is linked with suid programs diff --git a/libs/blkid/bin/blkid.8 b/libs/blkid/bin/blkid.8 index 14aad9e99..ff8e8e570 100644 --- a/libs/blkid/bin/blkid.8 +++ b/libs/blkid/bin/blkid.8 @@ -40,6 +40,8 @@ blkid \- command\-line utility to locate/print block device attributes .IR size ] .RB [ \-o .IR format ] +.RB [ \-u +.IR list ] .I device [\fIdevice\fR ...] @@ -93,6 +95,20 @@ udev symlinks (depends on setting in /etc/blkid.conf). Avoid to use the symlinks directly. It is not reliable to use the symlinks without verification. The \fB-L\fR option is portable and works on systems with and without udev. .TP +.B \-u " list " +Restrict probing functions to defined (comma separated) list of "usage" types. +Supported usage types are: filesystem, raid, crypto and other. The list can be +prefixed with "no" to specify the usage types which should be ignored. For example: +.sp + blkid -p -u filesystem,other /dev/sda1 +.sp +probes for all filesystems and others (e.g. swap) formats, and +.sp + blkid -p -u noraid /dev/sda1 +.sp +probes for all supported formats exclude RAIDs. This option is useful with +\fB-p\fR only. +.TP .B \-U " uuid " Look up one device that uses the uuid. For more details see the \fB-L\fR option. .TP @@ -138,7 +154,7 @@ In order to just refresh the cache without showing any tokens, use with no other options. .TP .BI \-S " bytes" -Ooverwrite device/file size (only useful with \fB-p\fR). +Overwrite device/file size (only useful with \fB-p\fR). .TP .BI \-t " NAME" = "value" Search for block devices with tokens named diff --git a/libs/blkid/bin/blkid.c b/libs/blkid/bin/blkid.c index f0852e45e..8126d685d 100644 --- a/libs/blkid/bin/blkid.c +++ b/libs/blkid/bin/blkid.c @@ -76,7 +76,9 @@ static void usage(int error) "Low-level probing options:\n" " -p switch to low-level mode (bypass cache)\n" " -S overwrite device size\n" - " -O probe at the given offset\n\n", + " -O probe at the given offset\n" + " -u filter by \"usage\" (e.g. -u filesystem,raid)\n" + "\n", progname); exit(error); @@ -361,6 +363,38 @@ error: return -1; } +/* converts comma separated list to BLKID_USAGE_* mask */ +static int list_to_usage(const char *list, int *flag) +{ + int mask = 0; + const char *word, *p = list; + + if (p && strncmp(p, "no", 2) == 0) { + *flag = BLKID_FLTR_NOTIN; + p += 2; + } + + for (word = p; p && *p; p++) { + if (*p == ',' || *(p + 1) == '\0') { + if (!strncmp(word, "filesystem", 10)) + mask |= BLKID_USAGE_FILESYSTEM; + else if (!strncmp(word, "raid", 4)) + mask |= BLKID_USAGE_RAID; + else if (!strncmp(word, "crypto", 6)) + mask |= BLKID_USAGE_CRYPTO; + else if (!strncmp(word, "other", 5)) + mask |= BLKID_USAGE_OTHER; + else { + fprintf(stderr, "unknown usage keyword '%*s'\n", + (int) (p - word), word); + exit(1); + } + word = p + 1; + } + } + return mask; +} + int main(int argc, char **argv) { blkid_cache cache = NULL; @@ -369,6 +403,8 @@ int main(int argc, char **argv) char *search_type = NULL, *search_value = NULL; char *read = NULL; char *write = NULL; + int fltr_usage = 0; + int fltr_flag = BLKID_FLTR_ONLYIN; unsigned int numdev = 0, numtag = 0; int version = 0; int err = 4; @@ -378,7 +414,7 @@ int main(int argc, char **argv) int c; blkid_loff_t offset = 0, size = 0; - while ((c = getopt (argc, argv, "c:f:ghlL:o:O:ps:S:t:U:w:v")) != EOF) + while ((c = getopt (argc, argv, "c:f:ghlL:o:O:ps:S:t:u:U:w:v")) != EOF) switch (c) { case 'c': if (optarg && !*optarg) @@ -393,6 +429,9 @@ int main(int argc, char **argv) search_value = strdup(optarg); search_type = strdup("LABEL"); break; + case 'u': + fltr_usage = list_to_usage(optarg, &fltr_flag); + break; case 'U': eval++; search_value = strdup(optarg); @@ -509,6 +548,9 @@ int main(int argc, char **argv) BLKID_PROBREQ_LABEL | BLKID_PROBREQ_UUID | BLKID_PROBREQ_TYPE | BLKID_PROBREQ_SECTYPE | BLKID_PROBREQ_USAGE | BLKID_PROBREQ_VERSION); + if (fltr_usage && + blkid_probe_filter_usage(pr, fltr_flag, fltr_usage)) + goto exit; for (i = 0; i < numdev; i++) err += lowprobe_device(pr, devices[i],