]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsblk-mnt.c
8 static struct libmnt_table
*mtab
, *swaps
;
9 static struct libmnt_cache
*mntcache
;
11 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
12 const char *filename
, int line
)
15 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
19 static struct libmnt_fs
*get_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
);
45 void lsblk_device_free_filesystems(struct lsblk_device
*dev
)
58 static void add_filesystem(struct lsblk_device
*dev
, struct libmnt_fs
*fs
)
63 dev
->fss
= xreallocarray(dev
->fss
, dev
->nfss
+ 1, sizeof(struct libmnt_fs
*));
64 dev
->fss
[dev
->nfss
] = fs
;
69 struct libmnt_fs
**lsblk_device_get_filesystems(struct lsblk_device
*dev
, size_t *n
)
72 struct libmnt_iter
*itr
= NULL
;
76 assert(dev
->filename
);
81 lsblk_device_free_filesystems(dev
); /* reset */
84 mtab
= mnt_new_table();
88 mntcache
= mnt_new_cache();
90 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
91 mnt_table_set_cache(mtab
, mntcache
);
94 mnt_table_parse_mtab(mtab
, NULL
);
97 snprintf(buf
, sizeof(buf
), "%s" _PATH_PROC_MOUNTINFO
, lsblk
->sysroot
);
98 mnt_table_parse_mtab(mtab
, buf
);
102 devno
= makedev(dev
->maj
, dev
->min
);
104 /* All mounpoint where is used devno or device name
106 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
107 while (mnt_table_next_fs(mtab
, itr
, &fs
) == 0) {
108 if (mnt_fs_get_devno(fs
) != devno
&&
109 !mnt_fs_streq_srcpath(fs
, dev
->filename
))
111 add_filesystem(dev
, fs
);
114 /* Try mnt_table_find_srcpath() which also canonicalizes patches, etc.
117 fs
= get_active_swap(dev
->filename
);
119 fs
= mnt_table_find_srcpath(mtab
, dev
->filename
, MNT_ITER_BACKWARD
);
124 add_filesystem(dev
, fs
);
134 /* Returns mounpoint where the device is mounted. If the device is used for
135 * more filesystems (subvolumes, ...) than returns the "best" one.
137 const char *lsblk_device_get_mountpoint(struct lsblk_device
*dev
)
139 struct libmnt_fs
*fs
= NULL
;
142 lsblk_device_get_filesystems(dev
, NULL
);
146 /* lsblk_device_get_filesystems() scans mountinfo/swaps in backward
147 * order. It means the first in fss[] is the last mounted FS. Let's
148 * keep it as default */
150 root
= mnt_fs_get_root(fs
);
152 if (root
&& strcmp(root
, "/") != 0) {
153 /* FS is subvolume (or subdirectory bind-mount). Try to get
154 * FS with "/" root */
157 for (i
= 1; i
< dev
->nfss
; i
++) {
158 root
= mnt_fs_get_root(dev
->fss
[i
]);
159 if (!root
|| strcmp(root
, "/") == 0) {
165 if (mnt_fs_is_swaparea(fs
))
167 return mnt_fs_get_target(fs
);
170 void lsblk_mnt_init(void)
175 void lsblk_mnt_deinit(void)
177 mnt_unref_table(mtab
);
178 mnt_unref_table(swaps
);
179 mnt_unref_cache(mntcache
);