]>
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
= xrealloc(dev
->fss
, (dev
->nfss
+ 1)
64 * sizeof(struct libmnt_fs
*));
65 dev
->fss
[dev
->nfss
] = fs
;
70 struct libmnt_fs
**lsblk_device_get_filesystems(struct lsblk_device
*dev
, size_t *n
)
73 struct libmnt_iter
*itr
= NULL
;
77 assert(dev
->filename
);
82 lsblk_device_free_filesystems(dev
); /* reset */
85 mtab
= mnt_new_table();
89 mntcache
= mnt_new_cache();
91 mnt_table_set_parser_errcb(mtab
, table_parser_errcb
);
92 mnt_table_set_cache(mtab
, mntcache
);
95 mnt_table_parse_mtab(mtab
, NULL
);
98 snprintf(buf
, sizeof(buf
), "%s" _PATH_PROC_MOUNTINFO
, lsblk
->sysroot
);
99 mnt_table_parse_mtab(mtab
, buf
);
103 devno
= makedev(dev
->maj
, dev
->min
);
105 /* All mounpoint where is used devno or device name
107 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
108 while (mnt_table_next_fs(mtab
, itr
, &fs
) == 0) {
109 if (mnt_fs_get_devno(fs
) != devno
&&
110 !mnt_fs_streq_srcpath(fs
, dev
->filename
))
112 add_filesystem(dev
, fs
);
115 /* Try mnt_table_find_srcpath() which also canonicalizes patches, etc.
118 fs
= get_active_swap(dev
->filename
);
120 fs
= mnt_table_find_srcpath(mtab
, dev
->filename
, MNT_ITER_BACKWARD
);
125 add_filesystem(dev
, fs
);
135 /* Returns mounpoint where the device is mounted. If the device is used for
136 * more filesystems (subvolumes, ...) than returns the "best" one.
138 const char *lsblk_device_get_mountpoint(struct lsblk_device
*dev
)
140 struct libmnt_fs
*fs
= NULL
;
143 lsblk_device_get_filesystems(dev
, NULL
);
147 /* lsblk_device_get_filesystems() scans mountinfo/swaps in backward
148 * order. It means the first in fss[] is the last mounted FS. Let's
149 * keep it as default */
151 root
= mnt_fs_get_root(fs
);
153 if (root
&& strcmp(root
, "/") != 0) {
154 /* FS is subvolume (or subdirectory bind-mount). Try to get
155 * FS with "/" root */
158 for (i
= 1; i
< dev
->nfss
; i
++) {
159 root
= mnt_fs_get_root(dev
->fss
[i
]);
160 if (!root
|| strcmp(root
, "/") == 0) {
166 if (mnt_fs_is_swaparea(fs
))
168 return mnt_fs_get_target(fs
);
171 void lsblk_mnt_init(void)
176 void lsblk_mnt_deinit(void)
178 mnt_unref_table(mtab
);
179 mnt_unref_table(swaps
);
180 mnt_unref_cache(mntcache
);