]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsblk-mnt.c
10 static struct libmnt_table
*mtab
, *swaps
;
11 static struct libmnt_cache
*mntcache
;
13 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
14 const char *filename
, int line
)
17 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
21 static int is_active_swap(const char *filename
)
24 swaps
= mnt_new_table();
28 mntcache
= mnt_new_cache();
30 mnt_table_set_parser_errcb(swaps
, table_parser_errcb
);
31 mnt_table_set_cache(swaps
, mntcache
);
34 mnt_table_parse_swaps(swaps
, NULL
);
37 snprintf(buf
, sizeof(buf
), "%s" _PATH_PROC_SWAPS
, lsblk
->sysroot
);
38 mnt_table_parse_swaps(swaps
, buf
);
42 return mnt_table_find_srcpath(swaps
, filename
, MNT_ITER_BACKWARD
) != NULL
;
45 char *lsblk_device_get_mountpoint(struct blkdev_cxt
*cxt
)
51 assert(cxt
->filename
);
53 if (cxt
->is_mounted
|| cxt
->is_swap
)
54 return cxt
->mountpoint
;
57 mtab
= mnt_new_table();
61 mntcache
= mnt_new_cache();
63 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
64 mnt_table_set_cache(mtab
, mntcache
);
67 mnt_table_parse_mtab(mtab
, NULL
);
70 snprintf(buf
, sizeof(buf
), "%s" _PATH_PROC_MOUNTINFO
, lsblk
->sysroot
);
71 mnt_table_parse_mtab(mtab
, buf
);
75 /* Note that maj:min in /proc/self/mountinfo does not have to match with
76 * devno as returned by stat(), so we have to try devname too
78 fs
= mnt_table_find_devno(mtab
, makedev(cxt
->maj
, cxt
->min
), MNT_ITER_BACKWARD
);
80 fs
= mnt_table_find_srcpath(mtab
, cxt
->filename
, MNT_ITER_BACKWARD
);
82 if (is_active_swap(cxt
->filename
)) {
83 cxt
->mountpoint
= xstrdup("[SWAP]");
86 cxt
->mountpoint
= NULL
;
88 return cxt
->mountpoint
;
92 fsroot
= mnt_fs_get_root(fs
);
93 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
94 /* hmm.. we found bind mount or btrfs subvolume, let's try to
95 * get real FS root mountpoint */
96 struct libmnt_fs
*rfs
;
97 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
99 mnt_table_set_iter(mtab
, itr
, fs
);
100 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
101 fsroot
= mnt_fs_get_root(rfs
);
102 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
103 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
111 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
112 cxt
->mountpoint
= xstrdup(mnt_fs_get_target(fs
));
114 return cxt
->mountpoint
;
117 void lsblk_mnt_init(void)
122 void lsblk_mnt_deinit(void)
124 mnt_unref_table(mtab
);
125 mnt_unref_table(swaps
);
126 mnt_unref_cache(mntcache
);