From 5db57cfc469b45ada0162a64d627ce25a3d2431f Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 1 Jul 2014 21:40:04 +0100 Subject: swapon: share get_swap_prober() with swaplabel to print uuid and label The swapon(8) listing was almost complete, apart from label and uuid. This change moves the code from swaplabel(8) to shared scope to be used for printouts in other swap commands, such as swapon. Adding this feature to lsblk(8) was a consideration, but lsblk is not interested of swapfiles, so the swapon seems like a better option to add this information. Signed-off-by: Sami Kerola --- sys-utils/swapon-common.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'sys-utils/swapon-common.c') diff --git a/sys-utils/swapon-common.c b/sys-utils/swapon-common.c index 6dd7bacb1..203d5c4d0 100644 --- a/sys-utils/swapon-common.c +++ b/sys-utils/swapon-common.c @@ -1,6 +1,8 @@ +#include #include "c.h" #include "nls.h" +#include "swapheader.h" #include "swapon-common.h" #include "xalloc.h" @@ -104,3 +106,45 @@ size_t numof_uuids(void) return ulct; } +blkid_probe get_swap_prober(const char *devname) +{ + blkid_probe pr; + int rc; + const char *version = NULL; + char *swap_filter[] = { "swap", NULL }; + + pr = blkid_new_probe_from_filename(devname); + if (!pr) { + warn(_("%s: unable to probe device"), devname); + return NULL; + } + + blkid_probe_enable_superblocks(pr, TRUE); + blkid_probe_set_superblocks_flags(pr, + BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | + BLKID_SUBLKS_VERSION); + + blkid_probe_filter_superblocks_type(pr, BLKID_FLTR_ONLYIN, swap_filter); + + rc = blkid_do_safeprobe(pr); + if (rc == -1) + warn(_("%s: unable to probe device"), devname); + else if (rc == -2) + warnx(_("%s: ambivalent probing result, use wipefs(8)"), devname); + else if (rc == 1) + warnx(_("%s: not a valid swap partition"), devname); + + if (rc == 0) { + /* Only the SWAPSPACE2 is supported. */ + if (blkid_probe_lookup_value(pr, "VERSION", &version, NULL) == 0 + && version + && strcmp(version, stringify_value(SWAP_VERSION))) + warnx(_("%s: unsupported swap version '%s'"), + devname, version); + else + return pr; + } + + blkid_free_probe(pr); + return NULL; +} -- cgit v1.2.3-55-g7522