4 * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #ifdef HAVE_SYS_IOCTL_H
29 # include <sys/ioctl.h>
33 #include <sys/statvfs.h>
34 #include <sys/types.h>
39 #include <libsmartcols.h>
41 #include "pathnames.h"
43 #include "closestream.h"
89 const char *name
; /* header */
90 double whint
; /* width hint (N < 1 is in percent of termwidth) */
91 int flags
; /* libsmartcols flags */
92 const char *help
; /* column description */
93 const char *match
; /* pattern for match_func() */
94 void *match_data
; /* match specific data */
97 /* columns descriptions (don't use const, this is writable) */
98 static struct colinfo infos
[] = {
99 [COL_SOURCE
] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES
, N_("source device") },
100 [COL_TARGET
] = { "TARGET", 0.30, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("mountpoint") },
101 [COL_FSTYPE
] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC
, N_("filesystem type") },
102 [COL_OPTIONS
] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("all mount options") },
103 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC
, N_("VFS specific mount options") },
104 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("FS specific mount options") },
105 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
106 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
107 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
108 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
109 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
110 [COL_ACTION
] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH
, N_("action detected by --poll") },
111 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("old mount options saved by --poll") },
112 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
113 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size") },
114 [COL_AVAIL
] = { "AVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available") },
115 [COL_USED
] = { "USED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used") },
116 [COL_USEPERC
] = { "USE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
117 [COL_FSROOT
] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES
, N_("filesystem root") },
118 [COL_TID
] = { "TID", 4, SCOLS_FL_RIGHT
, N_("task ID") },
119 [COL_ID
] = { "ID", 2, SCOLS_FL_RIGHT
, N_("mount ID") },
120 [COL_OPT_FIELDS
] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC
, N_("optional mount fields") },
121 [COL_PROPAGATION
] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
122 [COL_FREQ
] = { "FREQ", 1, SCOLS_FL_RIGHT
, N_("dump(8) period in days [fstab only]") },
123 [COL_PASSNO
] = { "PASSNO", 1, SCOLS_FL_RIGHT
, N_("pass number on parallel fsck(8) [fstab only]") }
126 /* columns[] array specifies all currently wanted output column. The columns
127 * are defined by infos[] array and you can specify (on command line) each
128 * column twice. That's enough, dynamically allocated array of the columns is
129 * unnecessary overkill and over-engineering in this case */
130 static int columns
[ARRAY_SIZE(infos
) * 2];
131 static size_t ncolumns
;
133 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
136 errx(EXIT_FAILURE
, _("too many columns specified, "
137 "the limit is %zu columns"),
142 #define add_column(ary, n, id) \
143 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
145 /* poll actions (parsed --poll=<list> */
146 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
147 static int actions
[FINDMNT_NACTIONS
];
150 /* global (accessed from findmnt-verify.c too) */
153 struct libmnt_cache
*cache
;
157 static struct udev
*udev
;
160 static int match_func(struct libmnt_fs
*fs
, void *data
__attribute__ ((__unused__
)));
163 static int get_column_id(int num
)
166 assert((size_t) num
< ncolumns
);
167 assert((size_t) columns
[num
] < ARRAY_SIZE(infos
));
171 static struct colinfo
*get_column_info(int num
)
173 return &infos
[ get_column_id(num
) ];
176 static const char *column_id_to_name(int id
)
178 assert((size_t) id
< ARRAY_SIZE(infos
));
179 return infos
[id
].name
;
182 static const char *get_column_name(int num
)
184 return get_column_info(num
)->name
;
187 static float get_column_whint(int num
)
189 return get_column_info(num
)->whint
;
192 static int get_column_flags(int num
)
194 return get_column_info(num
)->flags
;
197 static const char *get_match(int id
)
199 assert((size_t) id
< ARRAY_SIZE(infos
));
200 return infos
[id
].match
;
203 static void *get_match_data(int id
)
205 assert((size_t) id
< ARRAY_SIZE(infos
));
206 return infos
[id
].match_data
;
209 static void set_match(int id
, const char *match
)
211 assert((size_t) id
< ARRAY_SIZE(infos
));
212 infos
[id
].match
= match
;
215 static void set_match_data(int id
, void *data
)
217 assert((size_t) id
< ARRAY_SIZE(infos
));
218 infos
[id
].match_data
= data
;
222 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
225 static void set_source_match(const char *data
)
229 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
230 dev_t
*devno
= xmalloc(sizeof(dev_t
));
232 *devno
= makedev(maj
, min
);
233 set_match(COL_MAJMIN
, data
);
234 set_match_data(COL_MAJMIN
, (void *) devno
);
235 flags
|= FL_NOSWAPMATCH
;
237 set_match(COL_SOURCE
, data
);
241 * Extra functionality for --target <path>. The function mnt_table_find_mountpoint()
242 * also checks parents (path elements in reverse order) to get mountpoint.
244 * @tb has to be from kernel (so no fstab or so)!
246 static void enable_extra_target_match(struct libmnt_table
*tb
)
249 const char *tgt
= NULL
, *mnt
= NULL
;
250 struct libmnt_fs
*fs
;
253 * Check if match pattern is mountpoint, if not use the
256 if (flags
& FL_NOCACHE
)
257 tgt
= get_match(COL_TARGET
);
259 tgt
= cn
= mnt_resolve_path(get_match(COL_TARGET
), cache
);
264 fs
= mnt_table_find_mountpoint(tb
, tgt
, MNT_ITER_BACKWARD
);
266 mnt
= mnt_fs_get_target(fs
);
267 if (mnt
&& strcmp(mnt
, tgt
) != 0)
268 set_match(COL_TARGET
, xstrdup(mnt
)); /* replace the current setting */
275 static int is_tabdiff_column(int id
)
277 assert((size_t) id
< ARRAY_SIZE(infos
));
282 case COL_OLD_OPTIONS
:
291 * "findmnt" without any filter
293 int is_listall_mode(void)
295 if ((flags
& FL_DF
) && !(flags
& FL_ALL
))
298 return (!get_match(COL_SOURCE
) &&
299 !get_match(COL_TARGET
) &&
300 !get_match(COL_FSTYPE
) &&
301 !get_match(COL_OPTIONS
) &&
302 !get_match(COL_MAJMIN
));
306 * Returns 1 if the @act is in the --poll=<list>
308 static int has_poll_action(int act
)
313 return 1; /* all actions enabled */
314 for (i
= 0; i
< nactions
; i
++)
315 if (actions
[i
] == act
)
320 static int poll_action_name_to_id(const char *name
, size_t namesz
)
324 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
325 id
= MNT_TABDIFF_MOVE
;
326 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
327 id
= MNT_TABDIFF_MOUNT
;
328 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
329 id
= MNT_TABDIFF_UMOUNT
;
330 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
331 id
= MNT_TABDIFF_REMOUNT
;
333 warnx(_("unknown action: %s"), name
);
339 * findmnt --first-only <devname|TAG=|mountpoint>
341 * ... it works like "mount <devname|TAG=|mountpoint>"
343 static int is_mount_compatible_mode(void)
345 if (!get_match(COL_SOURCE
))
346 return 0; /* <devname|TAG=|mountpoint> is required */
347 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
348 return 0; /* cannot be restricted by -t or -O */
349 if (!(flags
& FL_FIRSTONLY
))
350 return 0; /* we have to return the first entry only */
355 static void disable_columns_truncate(void)
359 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
360 infos
[i
].flags
&= ~SCOLS_FL_TRUNC
;
364 * converts @name to column ID
366 static int column_name_to_id(const char *name
, size_t namesz
)
370 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
371 const char *cn
= column_id_to_name(i
);
373 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
376 warnx(_("unknown column: %s"), name
);
382 static char *get_tag_from_udev(const char *devname
, int col
)
384 struct udev_device
*dev
;
385 const char *data
= NULL
;
386 char *res
= NULL
, *path
;
393 /* libudev don't like /dev/mapper/ symlinks */
394 path
= realpath(devname
, NULL
);
398 if (strncmp(devname
, "/dev/", 5) == 0)
401 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", devname
);
409 data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC");
412 data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC");
415 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID");
418 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME");
426 unhexmangle_string(res
);
429 udev_device_unref(dev
);
432 #endif /* HAVE_LIBUDEV */
434 /* Returns LABEL or UUID */
435 static char *get_tag(struct libmnt_fs
*fs
, const char *tagname
, int col
437 __attribute__((__unused__
))
444 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
447 const char *dev
= mnt_fs_get_source(fs
);
449 if (dev
&& !(flags
& FL_NOCACHE
))
450 dev
= mnt_resolve_spec(dev
, cache
);
453 res
= get_tag_from_udev(dev
, col
);
456 res
= mnt_cache_find_tag_value(cache
, dev
, tagname
);
458 /* don't return pointer to cache */
466 static char *get_vfs_attr(struct libmnt_fs
*fs
, int sizetype
)
469 uint64_t vfs_attr
= 0;
472 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
477 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
480 vfs_attr
= buf
.f_frsize
* buf
.f_bavail
;
483 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
486 if (buf
.f_blocks
== 0)
489 xasprintf(&sizestr
, "%.0f%%",
490 (double)(buf
.f_blocks
- buf
.f_bfree
) /
496 sizestr
= xstrdup("0");
497 else if (flags
& FL_BYTES
)
498 xasprintf(&sizestr
, "%ju", vfs_attr
);
500 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
505 /* reads FS data from libmount
507 static char *get_data(struct libmnt_fs
*fs
, int num
)
510 int col_id
= get_column_id(num
);
515 const char *root
= mnt_fs_get_root(fs
);
516 const char *spec
= mnt_fs_get_srcpath(fs
);
519 if (spec
&& (flags
& FL_CANONICALIZE
))
520 spec
= cn
= mnt_resolve_path(spec
, cache
);
522 spec
= mnt_fs_get_source(fs
);
524 if (spec
&& (flags
& FL_EVALUATE
))
525 spec
= cn
= mnt_resolve_spec(spec
, cache
);
527 if (root
&& spec
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/"))
528 xasprintf(&str
, "%s[%s]", spec
, root
);
536 str
= xstrdup(mnt_fs_get_target(fs
));
539 str
= xstrdup(mnt_fs_get_fstype(fs
));
542 str
= xstrdup(mnt_fs_get_options(fs
));
544 case COL_VFS_OPTIONS
:
545 str
= xstrdup(mnt_fs_get_vfs_options(fs
));
548 str
= xstrdup(mnt_fs_get_fs_options(fs
));
551 str
= xstrdup(mnt_fs_get_optional_fields(fs
));
554 str
= get_tag(fs
, "UUID", col_id
);
557 str
= get_tag(fs
, "PARTUUID", col_id
);
560 str
= get_tag(fs
, "LABEL", col_id
);
563 str
= get_tag(fs
, "PARTLABEL", col_id
);
568 dev_t devno
= mnt_fs_get_devno(fs
);
572 if ((flags
& FL_RAW
) || (flags
& FL_EXPORT
) || (flags
& FL_JSON
))
573 xasprintf(&str
, "%u:%u", major(devno
), minor(devno
));
575 xasprintf(&str
, "%3u:%-3u", major(devno
), minor(devno
));
582 str
= get_vfs_attr(fs
, col_id
);
585 str
= xstrdup(mnt_fs_get_root(fs
));
588 if (mnt_fs_get_tid(fs
))
589 xasprintf(&str
, "%d", mnt_fs_get_tid(fs
));
592 if (mnt_fs_get_id(fs
))
593 xasprintf(&str
, "%d", mnt_fs_get_id(fs
));
595 case COL_PROPAGATION
:
596 if (mnt_fs_is_kernel(fs
)) {
597 unsigned long fl
= 0;
600 if (mnt_fs_get_propagation(fs
, &fl
) != 0)
603 n
= xstrdup((fl
& MS_SHARED
) ? "shared" : "private");
606 xasprintf(&str
, "%s,slave", n
);
610 if (fl
& MS_UNBINDABLE
) {
611 xasprintf(&str
, "%s,unbindable", n
);
619 if (!mnt_fs_is_kernel(fs
))
620 xasprintf(&str
, "%d", mnt_fs_get_freq(fs
));
623 if (!mnt_fs_is_kernel(fs
))
624 xasprintf(&str
, "%d", mnt_fs_get_passno(fs
));
632 static char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
633 struct libmnt_fs
*new_fs
,
639 switch (get_column_id(num
)) {
642 case MNT_TABDIFF_MOUNT
:
645 case MNT_TABDIFF_UMOUNT
:
648 case MNT_TABDIFF_REMOUNT
:
651 case MNT_TABDIFF_MOVE
:
660 case COL_OLD_OPTIONS
:
661 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
662 change
== MNT_TABDIFF_UMOUNT
))
663 str
= xstrdup(mnt_fs_get_options(old_fs
));
666 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
667 change
== MNT_TABDIFF_UMOUNT
))
668 str
= xstrdup(mnt_fs_get_target(old_fs
));
672 str
= get_data(new_fs
, num
);
674 str
= get_data(old_fs
, num
);
680 /* adds one line to the output @tab */
681 static struct libscols_line
*add_line(struct libscols_table
*table
, struct libmnt_fs
*fs
,
682 struct libscols_line
*parent
)
685 struct libscols_line
*line
= scols_table_new_line(table
, parent
);
688 err(EXIT_FAILURE
, _("failed to allocate output line"));
690 for (i
= 0; i
< ncolumns
; i
++) {
691 if (scols_line_refer_data(line
, i
, get_data(fs
, i
)))
692 err(EXIT_FAILURE
, _("failed to add output data"));
695 scols_line_set_userdata(line
, fs
);
699 static struct libscols_line
*add_tabdiff_line(struct libscols_table
*table
, struct libmnt_fs
*new_fs
,
700 struct libmnt_fs
*old_fs
, int change
)
703 struct libscols_line
*line
= scols_table_new_line(table
, NULL
);
706 err(EXIT_FAILURE
, _("failed to allocate output line"));
708 for (i
= 0; i
< ncolumns
; i
++) {
709 if (scols_line_refer_data(line
, i
,
710 get_tabdiff_data(old_fs
, new_fs
, change
, i
)))
711 err(EXIT_FAILURE
, _("failed to add output data"));
717 static int has_line(struct libscols_table
*table
, struct libmnt_fs
*fs
)
719 struct libscols_line
*ln
;
720 struct libscols_iter
*itr
;
723 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
727 while (scols_table_next_line(table
, itr
, &ln
) == 0) {
728 if ((struct libmnt_fs
*) scols_line_get_userdata(ln
) == fs
) {
734 scols_free_iter(itr
);
738 /* reads filesystems from @tb (libmount) and fillin @table (output table) */
739 static int create_treenode(struct libscols_table
*table
, struct libmnt_table
*tb
,
740 struct libmnt_fs
*fs
, struct libscols_line
*parent_line
)
742 struct libmnt_fs
*chld
= NULL
;
743 struct libmnt_iter
*itr
= NULL
;
744 struct libscols_line
*line
;
748 /* first call, get root FS */
749 if (mnt_table_get_root_fs(tb
, &fs
))
753 } else if ((flags
& FL_SUBMOUNTS
) && has_line(table
, fs
))
756 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
760 if ((flags
& FL_SUBMOUNTS
) || match_func(fs
, NULL
)) {
761 line
= add_line(table
, fs
, parent_line
);
768 * add all children to the output table
770 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
771 if (create_treenode(table
, tb
, chld
, line
))
781 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
782 const char *filename
, int line
)
784 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
789 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
791 files
= xrealloc(files
, sizeof(char *) * (*nfiles
+ 1));
792 files
[(*nfiles
)++] = filename
;
796 static char **append_pid_tabfile(char **files
, int *nfiles
, pid_t pid
)
800 xasprintf(&path
, "/proc/%d/mountinfo", (int) pid
);
801 return append_tabfile(files
, nfiles
, path
);
804 /* calls libmount fstab/mtab/mountinfo parser */
805 static struct libmnt_table
*parse_tabfiles(char **files
,
809 struct libmnt_table
*tb
;
812 tb
= mnt_new_table();
814 warn(_("failed to initialize libmount table"));
817 mnt_table_set_parser_errcb(tb
, parser_errcb
);
820 /* NULL means that libmount will use default paths */
821 const char *path
= nfiles
? *files
++ : NULL
;
825 rc
= mnt_table_parse_fstab(tb
, path
);
828 rc
= mnt_table_parse_mtab(tb
, path
);
832 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
833 _PATH_PROC_MOUNTINFO
:
836 rc
= mnt_table_parse_file(tb
, path
);
841 warn(_("can't read %s"), path
);
844 } while (--nfiles
> 0);
850 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
851 * necessary if @tb in main() was read from a non-kernel source.
853 static void cache_set_targets(struct libmnt_cache
*tmp
)
855 struct libmnt_table
*tb
;
858 tb
= mnt_new_table();
862 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
863 _PATH_PROC_MOUNTINFO
:
866 if (mnt_table_parse_file(tb
, path
) == 0)
867 mnt_cache_set_targets(tmp
, tb
);
872 /* checks if @tb contains parent->child relations */
873 static int tab_is_tree(struct libmnt_table
*tb
)
875 struct libmnt_fs
*fs
= NULL
;
876 struct libmnt_iter
*itr
;
879 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
883 rc
= (mnt_table_next_fs(tb
, itr
, &fs
) == 0 &&
884 mnt_fs_is_kernel(fs
) &&
885 mnt_fs_get_root(fs
));
891 /* checks if all fs in @tb are from kernel */
892 static int tab_is_kernel(struct libmnt_table
*tb
)
894 struct libmnt_fs
*fs
= NULL
;
895 struct libmnt_iter
*itr
;
897 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
901 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
902 if (!mnt_fs_is_kernel(fs
)) {
912 /* filter function for libmount (mnt_table_find_next_fs()) */
913 static int match_func(struct libmnt_fs
*fs
,
914 void *data
__attribute__ ((__unused__
)))
916 int rc
= flags
& FL_INVERT
? 1 : 0;
920 m
= get_match(COL_FSTYPE
);
921 if (m
&& !mnt_fs_match_fstype(fs
, m
))
924 m
= get_match(COL_OPTIONS
);
925 if (m
&& !mnt_fs_match_options(fs
, m
))
928 md
= get_match_data(COL_MAJMIN
);
929 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
932 m
= get_match(COL_TARGET
);
933 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
936 m
= get_match(COL_SOURCE
);
937 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
940 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
941 const char *type
= mnt_fs_get_fstype(fs
);
943 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
946 if (mnt_fs_is_pseudofs(fs
))
950 if ((flags
& FL_REAL
) && mnt_fs_is_pseudofs(fs
))
953 if ((flags
& FL_PSEUDO
) && !mnt_fs_is_pseudofs(fs
))
959 /* iterate over filesystems in @tb */
960 struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
961 struct libmnt_iter
*itr
)
963 struct libmnt_fs
*fs
= NULL
;
965 if (is_listall_mode()) {
969 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
972 } else if (is_mount_compatible_mode()) {
974 * Look up for FS in the same way how mount(8) searches in fstab
978 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
979 mnt_iter_get_direction(itr
));
981 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
982 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
983 mnt_iter_get_direction(itr
));
986 * Look up for all matching entries
988 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
989 * findmnt [-l] <spec> [-O <options>] [-t <types>]
992 if (mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
) != 0)
996 !(flags
& FL_NOSWAPMATCH
) &&
997 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
999 /* swap 'spec' and target. */
1000 set_match(COL_TARGET
, get_match(COL_SOURCE
));
1001 set_match(COL_SOURCE
, NULL
);
1002 mnt_reset_iter(itr
, -1);
1012 * Filter out unwanted lines for --list output or top level lines for
1013 * --submounts tree output.
1015 static int add_matching_lines(struct libmnt_table
*tb
,
1016 struct libscols_table
*table
, int direction
)
1018 struct libmnt_iter
*itr
;
1019 struct libmnt_fs
*fs
;
1020 int nlines
= 0, rc
= -1;
1022 itr
= mnt_new_iter(direction
);
1024 warn(_("failed to initialize libmount iterator"));
1028 while((fs
= get_next_fs(tb
, itr
))) {
1029 if ((flags
& FL_TREE
) || (flags
& FL_SUBMOUNTS
))
1030 rc
= create_treenode(table
, tb
, fs
, NULL
);
1032 rc
= !add_line(table
, fs
, NULL
);
1036 if (flags
& FL_FIRSTONLY
)
1038 flags
|= FL_NOSWAPMATCH
;
1048 static int poll_match(struct libmnt_fs
*fs
)
1050 int rc
= match_func(fs
, NULL
);
1052 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
1053 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
1055 * findmnt --poll /foo
1056 * The '/foo' maybe source as well as target.
1058 const char *str
= get_match(COL_SOURCE
);
1060 set_match(COL_TARGET
, str
); /* swap */
1061 set_match(COL_SOURCE
, NULL
);
1063 rc
= match_func(fs
, NULL
);
1065 set_match(COL_TARGET
, NULL
); /* restore */
1066 set_match(COL_SOURCE
, str
);
1072 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
1073 int timeout
, struct libscols_table
*table
, int direction
)
1077 struct libmnt_iter
*itr
= NULL
;
1078 struct libmnt_table
*tb_new
;
1079 struct libmnt_tabdiff
*diff
= NULL
;
1080 struct pollfd fds
[1];
1082 tb_new
= mnt_new_table();
1084 warn(_("failed to initialize libmount table"));
1088 itr
= mnt_new_iter(direction
);
1090 warn(_("failed to initialize libmount iterator"));
1094 diff
= mnt_new_tabdiff();
1096 warn(_("failed to initialize libmount tabdiff"));
1100 /* cache is unnecessary to detect changes */
1101 mnt_table_set_cache(tb
, NULL
);
1102 mnt_table_set_cache(tb_new
, NULL
);
1104 f
= fopen(tabfile
, "r");
1106 warn(_("cannot open %s"), tabfile
);
1110 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
1112 fds
[0].fd
= fileno(f
);
1113 fds
[0].events
= POLLPRI
;
1116 struct libmnt_table
*tmp
;
1117 struct libmnt_fs
*old
, *new;
1120 count
= poll(fds
, 1, timeout
);
1122 break; /* timeout */
1124 warn(_("poll() failed"));
1129 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
1131 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
1136 mnt_reset_iter(itr
, direction
);
1137 while(mnt_tabdiff_next_change(
1138 diff
, itr
, &old
, &new, &change
) == 0) {
1140 if (!has_poll_action(change
))
1142 if (!poll_match(new ? new : old
))
1145 rc
= !add_tabdiff_line(table
, new, old
, change
);
1148 if (flags
& FL_FIRSTONLY
)
1153 rc
= scols_table_print_range(table
, NULL
, NULL
);
1155 fputc('\n', scols_table_get_stream(table
));
1166 /* remove already printed lines to reduce memory usage */
1167 scols_table_remove_lines(table
);
1168 mnt_reset_table(tb_new
);
1170 if (count
&& (flags
& FL_FIRSTONLY
))
1176 mnt_unref_table(tb_new
);
1177 mnt_free_tabdiff(diff
);
1184 static int uniq_fs_target_cmp(
1185 struct libmnt_table
*tb
__attribute__((__unused__
)),
1186 struct libmnt_fs
*a
,
1187 struct libmnt_fs
*b
)
1189 return !mnt_fs_match_target(a
, mnt_fs_get_target(b
), cache
);
1192 static void __attribute__((__noreturn__
)) usage(void)
1197 fputs(USAGE_HEADER
, out
);
1200 " %1$s [options] <device> | <mountpoint>\n"
1201 " %1$s [options] <device> <mountpoint>\n"
1202 " %1$s [options] [--source <device>] [--target <path> | --mountpoint <dir>]\n"),
1203 program_invocation_short_name
);
1205 fputs(USAGE_SEPARATOR
, out
);
1206 fputs(_("Find a (mounted) filesystem.\n"), out
);
1208 fputs(USAGE_OPTIONS
, out
);
1209 fputs(_(" -s, --fstab search in static table of filesystems\n"), out
);
1210 fputs(_(" -m, --mtab search in table of mounted filesystems\n"
1211 " (includes user space mount options)\n"), out
);
1212 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1213 " filesystems (default)\n"), out
);
1215 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out
);
1216 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out
);
1219 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out
);
1220 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out
);
1221 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
1222 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out
);
1223 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out
);
1224 fputs(_(" -D, --df imitate the output of df(1)\n"), out
);
1225 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out
);
1226 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1227 " to device names\n"), out
);
1228 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out
);
1229 fputs(_(" -f, --first-only print the first found filesystem only\n"), out
);
1230 fputs(_(" -i, --invert invert the sense of matching\n"), out
);
1231 fputs(_(" -J, --json use JSON output format\n"), out
);
1232 fputs(_(" -l, --list use list format output\n"), out
);
1233 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out
);
1234 fputs(_(" -n, --noheadings don't print column headings\n"), out
);
1235 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out
);
1236 fputs(_(" -o, --output <list> the output columns to be shown\n"), out
);
1237 fputs(_(" --output-all output all available columns\n"), out
);
1238 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1239 fputs(_(" --pseudo print only pseudo-filesystems\n"), out
);
1240 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out
);
1241 fputs(_(" -r, --raw use raw output format\n"), out
);
1242 fputs(_(" --real print only real filesystems\n"), out
);
1243 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1244 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out
);
1245 fputs(_(" -T, --target <path> the path to the filesystem to use\n"), out
);
1246 fputs(_(" --tree enable tree format output is possible\n"), out
);
1247 fputs(_(" -M, --mountpoint <dir> the mountpoint directory\n"), out
);
1248 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out
);
1249 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out
);
1250 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
1251 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out
);
1254 fputs(_(" -x, --verify verify mount table content (default is fstab)\n"), out
);
1255 fputs(_(" --verbose print more details\n"), out
);
1257 fputs(USAGE_SEPARATOR
, out
);
1258 printf(USAGE_HELP_OPTIONS(24));
1260 fputs(USAGE_COLUMNS
, out
);
1261 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1262 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1264 printf(USAGE_MAN_TAIL("findmnt(8)"));
1269 int main(int argc
, char *argv
[])
1271 struct libmnt_table
*tb
= NULL
;
1272 char **tabfiles
= NULL
;
1273 int direction
= MNT_ITER_FORWARD
;
1275 int c
, rc
= -1, timeout
= -1;
1276 int ntabfiles
= 0, tabtype
= 0;
1277 char *outarg
= NULL
;
1279 int force_tree
= 0, istree
= 0;
1281 struct libscols_table
*table
= NULL
;
1284 FINDMNT_OPT_VERBOSE
= CHAR_MAX
+ 1,
1286 FINDMNT_OPT_OUTPUT_ALL
,
1291 static const struct option longopts
[] = {
1292 { "all", no_argument
, NULL
, 'A' },
1293 { "ascii", no_argument
, NULL
, 'a' },
1294 { "bytes", no_argument
, NULL
, 'b' },
1295 { "canonicalize", no_argument
, NULL
, 'c' },
1296 { "direction", required_argument
, NULL
, 'd' },
1297 { "df", no_argument
, NULL
, 'D' },
1298 { "evaluate", no_argument
, NULL
, 'e' },
1299 { "first-only", no_argument
, NULL
, 'f' },
1300 { "fstab", no_argument
, NULL
, 's' },
1301 { "help", no_argument
, NULL
, 'h' },
1302 { "invert", no_argument
, NULL
, 'i' },
1303 { "json", no_argument
, NULL
, 'J' },
1304 { "kernel", no_argument
, NULL
, 'k' },
1305 { "list", no_argument
, NULL
, 'l' },
1306 { "mountpoint", required_argument
, NULL
, 'M' },
1307 { "mtab", no_argument
, NULL
, 'm' },
1308 { "noheadings", no_argument
, NULL
, 'n' },
1309 { "notruncate", no_argument
, NULL
, 'u' },
1310 { "options", required_argument
, NULL
, 'O' },
1311 { "output", required_argument
, NULL
, 'o' },
1312 { "output-all", no_argument
, NULL
, FINDMNT_OPT_OUTPUT_ALL
},
1313 { "poll", optional_argument
, NULL
, 'p' },
1314 { "pairs", no_argument
, NULL
, 'P' },
1315 { "raw", no_argument
, NULL
, 'r' },
1316 { "types", required_argument
, NULL
, 't' },
1317 { "nocanonicalize", no_argument
, NULL
, 'C' },
1318 { "nofsroot", no_argument
, NULL
, 'v' },
1319 { "submounts", no_argument
, NULL
, 'R' },
1320 { "source", required_argument
, NULL
, 'S' },
1321 { "tab-file", required_argument
, NULL
, 'F' },
1322 { "task", required_argument
, NULL
, 'N' },
1323 { "target", required_argument
, NULL
, 'T' },
1324 { "timeout", required_argument
, NULL
, 'w' },
1325 { "uniq", no_argument
, NULL
, 'U' },
1326 { "verify", no_argument
, NULL
, 'x' },
1327 { "version", no_argument
, NULL
, 'V' },
1328 { "verbose", no_argument
, NULL
, FINDMNT_OPT_VERBOSE
},
1329 { "tree", no_argument
, NULL
, FINDMNT_OPT_TREE
},
1330 { "real", no_argument
, NULL
, FINDMNT_OPT_REAL
},
1331 { "pseudo", no_argument
, NULL
, FINDMNT_OPT_PSEUDO
},
1332 { NULL
, 0, NULL
, 0 }
1335 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1336 { 'C', 'c'}, /* [no]canonicalize */
1337 { 'C', 'e' }, /* nocanonicalize, evaluate */
1338 { 'J', 'P', 'r','x' }, /* json,pairs,raw,verify */
1339 { 'M', 'T' }, /* mountpoint, target */
1340 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1341 { 'P','l','r','x' }, /* pairs,list,raw,verify */
1342 { 'p','x' }, /* poll,verify */
1343 { 'm','p','s' }, /* mtab,poll,fstab */
1344 { FINDMNT_OPT_PSEUDO
, FINDMNT_OPT_REAL
},
1347 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1349 setlocale(LC_ALL
, "");
1350 bindtextdomain(PACKAGE
, LOCALEDIR
);
1351 textdomain(PACKAGE
);
1352 close_stdout_atexit();
1354 /* default output format */
1357 while ((c
= getopt_long(argc
, argv
,
1358 "AabCcDd:ehiJfF:o:O:p::PklmM:nN:rst:uvRS:T:Uw:Vx",
1359 longopts
, NULL
)) != -1) {
1361 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1374 flags
|= FL_NOCACHE
;
1377 flags
|= FL_CANONICALIZE
;
1384 if (!strcmp(optarg
, "forward"))
1385 direction
= MNT_ITER_FORWARD
;
1386 else if (!strcmp(optarg
, "backward"))
1387 direction
= MNT_ITER_BACKWARD
;
1390 _("unknown direction '%s'"), optarg
);
1393 flags
|= FL_EVALUATE
;
1402 flags
|= FL_FIRSTONLY
;
1405 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1408 disable_columns_truncate();
1413 case FINDMNT_OPT_OUTPUT_ALL
:
1414 for (ncolumns
= 0; ncolumns
< ARRAY_SIZE(infos
); ncolumns
++) {
1415 if (is_tabdiff_column(ncolumns
))
1417 columns
[ncolumns
] = ncolumns
;
1421 set_match(COL_OPTIONS
, optarg
);
1425 nactions
= string_to_idarray(optarg
,
1426 actions
, ARRAY_SIZE(actions
),
1427 poll_action_name_to_id
);
1438 case 'm': /* mtab */
1439 tabtype
= TABTYPE_MTAB
;
1442 case 's': /* fstab */
1443 tabtype
= TABTYPE_FSTAB
;
1446 case 'k': /* kernel (mountinfo) */
1447 tabtype
= TABTYPE_KERNEL
;
1450 set_match(COL_FSTYPE
, optarg
);
1453 flags
&= ~FL_TREE
; /* disable the default */
1454 flags
|= FL_RAW
; /* enable raw */
1457 flags
&= ~FL_TREE
; /* disable the default */
1460 flags
|= FL_NOHEADINGS
;
1463 tabtype
= TABTYPE_KERNEL
;
1464 tabfiles
= append_pid_tabfile(tabfiles
, &ntabfiles
,
1465 strtou32_or_err(optarg
,
1466 _("invalid TID argument")));
1469 flags
|= FL_NOFSROOT
;
1472 flags
|= FL_SUBMOUNTS
;
1475 set_source_match(optarg
);
1476 flags
|= FL_NOSWAPMATCH
;
1479 flags
|= FL_STRICTTARGET
;
1482 set_match(COL_TARGET
, optarg
);
1483 flags
|= FL_NOSWAPMATCH
;
1489 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1494 case FINDMNT_OPT_VERBOSE
:
1495 flags
|= FL_VERBOSE
;
1497 case FINDMNT_OPT_TREE
:
1500 case FINDMNT_OPT_PSEUDO
:
1503 case FINDMNT_OPT_REAL
:
1510 print_version(EXIT_SUCCESS
);
1512 errtryhelp(EXIT_FAILURE
);
1516 if (!ncolumns
&& (flags
& FL_DF
)) {
1517 add_column(columns
, ncolumns
++, COL_SOURCE
);
1518 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1519 add_column(columns
, ncolumns
++, COL_SIZE
);
1520 add_column(columns
, ncolumns
++, COL_USED
);
1521 add_column(columns
, ncolumns
++, COL_AVAIL
);
1522 add_column(columns
, ncolumns
++, COL_USEPERC
);
1523 add_column(columns
, ncolumns
++, COL_TARGET
);
1526 /* default columns */
1528 if (flags
& FL_POLL
)
1529 add_column(columns
, ncolumns
++, COL_ACTION
);
1531 add_column(columns
, ncolumns
++, COL_TARGET
);
1532 add_column(columns
, ncolumns
++, COL_SOURCE
);
1533 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1534 add_column(columns
, ncolumns
++, COL_OPTIONS
);
1537 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1538 &ncolumns
, column_name_to_id
) < 0)
1539 return EXIT_FAILURE
;
1542 tabtype
= verify
? TABTYPE_FSTAB
: TABTYPE_KERNEL
;
1544 if ((flags
& FL_POLL
) && ntabfiles
> 1)
1545 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1547 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1548 errx(EXIT_FAILURE
, _(
1549 "options --target and --source can't be used together "
1550 "with command line element that is not an option"));
1553 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1555 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1557 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1558 /* don't care about submounts if list all mounts */
1559 flags
&= ~FL_SUBMOUNTS
;
1561 if (!(flags
& FL_SUBMOUNTS
) && ((flags
& FL_FIRSTONLY
)
1562 || get_match(COL_TARGET
)
1563 || get_match(COL_SOURCE
)
1564 || get_match(COL_MAJMIN
)))
1567 if (!(flags
& FL_NOSWAPMATCH
) &&
1568 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1570 * Check if we can swap source and target, it's
1571 * not possible if the source is LABEL=/UUID=
1573 const char *x
= get_match(COL_SOURCE
);
1575 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1576 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1577 flags
|= FL_NOSWAPMATCH
;
1581 * initialize libmount
1585 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1589 if (tabtype
== TABTYPE_MTAB
&& tab_is_kernel(tb
))
1590 tabtype
= TABTYPE_KERNEL
;
1592 istree
= tab_is_tree(tb
);
1593 if (istree
&& force_tree
)
1596 if ((flags
& FL_TREE
) && (ntabfiles
> 1 || !istree
))
1599 if (!(flags
& FL_NOCACHE
)) {
1600 cache
= mnt_new_cache();
1602 warn(_("failed to initialize libmount cache"));
1605 mnt_table_set_cache(tb
, cache
);
1607 if (tabtype
!= TABTYPE_KERNEL
)
1608 cache_set_targets(cache
);
1611 if (flags
& FL_UNIQ
)
1612 mnt_table_uniq_fs(tb
, MNT_UNIQ_KEEPTREE
, uniq_fs_target_cmp
);
1615 rc
= verify_table(tb
);
1620 * initialize libsmartcols
1622 scols_init_debug(0);
1623 table
= scols_new_table();
1625 warn(_("failed to allocate output table"));
1628 scols_table_enable_raw(table
, !!(flags
& FL_RAW
));
1629 scols_table_enable_export(table
, !!(flags
& FL_EXPORT
));
1630 scols_table_enable_json(table
, !!(flags
& FL_JSON
));
1631 scols_table_enable_ascii(table
, !!(flags
& FL_ASCII
));
1632 scols_table_enable_noheadings(table
, !!(flags
& FL_NOHEADINGS
));
1634 if (flags
& FL_JSON
)
1635 scols_table_set_name(table
, "filesystems");
1637 for (i
= 0; i
< ncolumns
; i
++) {
1638 struct libscols_column
*cl
;
1639 int fl
= get_column_flags(i
);
1640 int id
= get_column_id(i
);
1642 if (!(flags
& FL_TREE
))
1643 fl
&= ~SCOLS_FL_TREE
;
1645 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1646 warnx(_("%s column is requested, but --poll "
1647 "is not enabled"), get_column_name(i
));
1650 cl
= scols_table_new_column(table
, get_column_name(i
),
1651 get_column_whint(i
), fl
);
1653 warn(_("failed to allocate output column"));
1657 if (flags
& FL_JSON
) {
1662 if (!(flags
& FL_BYTES
))
1669 scols_column_set_json_type(cl
, SCOLS_JSON_NUMBER
);
1672 scols_column_set_json_type(cl
, SCOLS_JSON_STRING
);
1679 * Fill in data to the output table
1681 if (flags
& FL_POLL
) {
1682 /* poll mode (accept the first tabfile only) */
1683 rc
= poll_table(tb
, tabfiles
? *tabfiles
: _PATH_PROC_MOUNTINFO
, timeout
, table
, direction
);
1685 } else if ((flags
& FL_TREE
) && !(flags
& FL_SUBMOUNTS
)) {
1687 rc
= create_treenode(table
, tb
, NULL
, NULL
);
1689 /* whole list of sub-tree */
1690 rc
= add_matching_lines(tb
, table
, direction
);
1693 && tabtype
== TABTYPE_KERNEL
1694 && (flags
& FL_NOSWAPMATCH
)
1695 && !(flags
& FL_STRICTTARGET
)
1696 && get_match(COL_TARGET
)) {
1698 * Found nothing, maybe the --target is regular file,
1699 * try it again with extra functionality for target
1702 enable_extra_target_match(tb
);
1703 rc
= add_matching_lines(tb
, table
, direction
);
1708 * Print the output table for non-poll modes
1710 if (!rc
&& !(flags
& FL_POLL
))
1711 scols_print_table(table
);
1713 scols_unref_table(table
);
1715 mnt_unref_table(tb
);
1716 mnt_unref_cache(cache
);
1722 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;