]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsblk-mnt.c
6bc634043fa3bfd0e991a3c2652ee55f2d6e3945
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
);
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 cxt
->mountpoint
= is_active_swap(cxt
->filename
) ? xstrdup("[SWAP]") : NULL
;
84 return cxt
->mountpoint
;
88 fsroot
= mnt_fs_get_root(fs
);
89 if (fsroot
&& strcmp(fsroot
, "/") != 0) {
90 /* hmm.. we found bind mount or btrfs subvolume, let's try to
91 * get real FS root mountpoint */
92 struct libmnt_fs
*rfs
;
93 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
95 mnt_table_set_iter(mtab
, itr
, fs
);
96 while (mnt_table_next_fs(mtab
, itr
, &rfs
) == 0) {
97 fsroot
= mnt_fs_get_root(rfs
);
98 if ((!fsroot
|| strcmp(fsroot
, "/") == 0)
99 && mnt_fs_match_source(rfs
, cxt
->filename
, mntcache
)) {
107 DBG(DEV
, ul_debugobj(cxt
, "mountpoint: %s", mnt_fs_get_target(fs
)));
108 cxt
->mountpoint
= xstrdup(mnt_fs_get_target(fs
));
110 return cxt
->mountpoint
;
113 void lsblk_mnt_init(void)
118 void lsblk_mnt_deinit(void)
120 mnt_unref_table(mtab
);
121 mnt_unref_table(swaps
);
122 mnt_unref_cache(mntcache
);