summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2012-04-03 13:43:50 +0200
committerKarel Zak2012-04-03 13:43:50 +0200
commit9554f7abd889cf65918182666ab40e05f73da791 (patch)
treee32d77c4a7a6f6965954b83f665badae3be2033d
parentlibmount: add support to parse /proc/swaps (diff)
downloadkernel-qcow2-util-linux-9554f7abd889cf65918182666ab40e05f73da791.tar.gz
kernel-qcow2-util-linux-9554f7abd889cf65918182666ab40e05f73da791.tar.xz
kernel-qcow2-util-linux-9554f7abd889cf65918182666ab40e05f73da791.zip
lsblk: use libmount to get mountpoints/swaps
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--configure.ac1
-rw-r--r--misc-utils/Makefile.am5
-rw-r--r--misc-utils/lsblk.c75
3 files changed, 66 insertions, 15 deletions
diff --git a/configure.ac b/configure.ac
index 2140bc596..00d15b7f8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -895,6 +895,7 @@ AM_CONDITIONAL(BUILD_SWAPON, test "x$build_swapon" = xyes)
UL_BUILD_INIT([lsblk], [check])
UL_REQUIRES_LINUX([lsblk])
UL_REQUIRES_BUILD([lsblk], [libblkid])
+UL_REQUIRES_BUILD([lsblk], [libmount])
AM_CONDITIONAL(BUILD_LSBLK, test "x$build_lsblk" = xyes)
diff --git a/misc-utils/Makefile.am b/misc-utils/Makefile.am
index acf2c9d1a..6e11582fc 100644
--- a/misc-utils/Makefile.am
+++ b/misc-utils/Makefile.am
@@ -55,13 +55,12 @@ lsblk_SOURCES = \
lsblk.c \
$(top_srcdir)/lib/at.c \
$(top_srcdir)/lib/canonicalize.c \
- $(top_srcdir)/lib/ismounted.c \
$(top_srcdir)/lib/mbsalign.c \
$(top_srcdir)/lib/strutils.c \
$(top_srcdir)/lib/sysfs.c \
$(top_srcdir)/lib/tt.c
-lsblk_LDADD = $(ul_libblkid_la)
-lsblk_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
+lsblk_LDADD = $(ul_libblkid_la) $(ul_libmount_la)
+lsblk_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir) -I$(ul_libmount_incdir)
if HAVE_UDEV
lsblk_LDADD += -ludev
endif
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 4ff0102db..ad773c598 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -40,6 +40,7 @@
#include <ctype.h>
#include <blkid.h>
+#include <libmount.h>
#ifdef HAVE_LIBUDEV
#include <libudev.h>
@@ -51,7 +52,6 @@
#include "pathnames.h"
#include "blkdev.h"
#include "canonicalize.h"
-#include "ismounted.h"
#include "nls.h"
#include "tt.h"
#include "xalloc.h"
@@ -148,6 +148,9 @@ int ncolumns; /* number of enabled columns */
int excludes[256];
size_t nexcludes;
+static struct libmnt_table *mtab, *swaps;
+static struct libmnt_cache *mntcache;
+
struct blkdev_cxt {
struct blkdev_cxt *parent;
@@ -270,25 +273,67 @@ static char *get_device_path(struct blkdev_cxt *cxt)
return xstrdup(path);
}
+static int is_active_swap(const char *filename)
+{
+ if (!swaps) {
+ swaps = mnt_new_table();
+ if (!swaps)
+ return 0;
+ if (!mntcache)
+ mntcache = mnt_new_cache();
+
+ mnt_table_set_cache(swaps, mntcache);
+ mnt_table_parse_swaps(swaps, NULL);
+ }
+
+ return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != 0;
+}
+
static char *get_device_mountpoint(struct blkdev_cxt *cxt)
{
- int fl = 0;
- char mnt[PATH_MAX];
+ struct libmnt_fs *fs;
+ const char *fsroot;
assert(cxt);
assert(cxt->filename);
- *mnt = '\0';
+ if (!mtab) {
+ mtab = mnt_new_table();
+ if (!mtab)
+ return NULL;
+ if (!mntcache)
+ mntcache = mnt_new_cache();
- /*
- * TODO: use libmount and parse /proc/mountinfo only once
- */
- if (check_mount_point(cxt->filename, &fl, mnt, sizeof(mnt)) == 0 &&
- (fl & MF_MOUNTED)) {
- if (fl & MF_SWAP)
- strcpy(mnt, "[SWAP]");
+ mnt_table_set_cache(mtab, mntcache);
+ mnt_table_parse_mtab(mtab, NULL);
}
- return strlen(mnt) ? xstrdup(mnt) : NULL;
+
+ /* try /etc/mtab or /proc/self/mountinfo */
+ fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD);
+ if (!fs)
+ return is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL;
+
+ /* found */
+ fsroot = mnt_fs_get_root(fs);
+ if (fsroot && strcmp(fsroot, "/") != 0) {
+ /* hmm.. we found bind mount or btrfs subvolume, let's try to
+ * get real FS root mountpoint */
+ struct libmnt_fs *rfs;
+ struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
+
+ mnt_table_set_iter(mtab, itr, fs);
+ while (mnt_table_next_fs(mtab, itr, &rfs) == 0) {
+ fsroot = mnt_fs_get_root(rfs);
+ if ((!fsroot || strcmp(fsroot, "/") == 0)
+ && mnt_fs_match_source(rfs, cxt->filename, mntcache)) {
+ fs = rfs;
+ break;
+ }
+ }
+ mnt_free_iter(itr);
+ }
+
+ return xstrdup(mnt_fs_get_target(fs));
}
#ifndef HAVE_LIBUDEV
@@ -1249,6 +1294,9 @@ int main(int argc, char *argv[])
errx_mutually_exclusive("--{all,exclude}");
else if (!nexcludes)
excludes[nexcludes++] = 1; /* default: ignore RAM disks */
+
+ mnt_init_debug(0);
+
/*
* initialize output columns
*/
@@ -1277,5 +1325,8 @@ int main(int argc, char *argv[])
leave:
tt_free_table(lsblk->tt);
+ mnt_free_table(mtab);
+ mnt_free_table(swaps);
+ mnt_free_cache(mntcache);
return status;
}