4 * Copyright (C) 2010,2011 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
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
33 #define USE_UNSTABLE_LIBMOUNT_API
36 #include "pathnames.h"
43 FL_EVALUATE
= (1 << 1),
44 FL_CANONICALIZE
= (1 << 2),
45 FL_FIRSTONLY
= (1 << 3),
47 FL_NOSWAPMATCH
= (1 << 6),
48 FL_NOFSROOT
= (1 << 7),
49 FL_SUBMOUNTS
= (1 << 8),
69 const char *name
; /* header */
70 double whint
; /* width hint (N < 1 is in percent of termwidth) */
71 int truncate
; /* boolean */
72 const char *match
; /* pattern for match_func() */
75 /* columns descriptions */
76 struct colinfo infos
[__NCOLUMNS
] = {
77 [COL_SOURCE
] = { "SOURCE", 0.25, FALSE
},
78 [COL_TARGET
] = { "TARGET", 0.30, FALSE
},
79 [COL_FSTYPE
] = { "FSTYPE", 0.10, TRUE
},
80 [COL_OPTIONS
] = { "OPTIONS", 0.10, TRUE
},
81 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, TRUE
},
82 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, TRUE
},
83 [COL_LABEL
] = { "LABEL", 0.10, FALSE
},
84 [COL_UUID
] = { "UUID", 36, FALSE
},
85 [COL_MAJMIN
] = { "MAJ:MIN", 6, FALSE
},
92 /* array with IDs of enabled columns */
93 int columns
[__NCOLUMNS
];
97 struct libmnt_cache
*cache
;
99 static int get_column_id(int num
)
101 assert(num
< ncolumns
);
102 assert(columns
[num
] < __NCOLUMNS
);
106 static struct colinfo
*get_column_info(int num
)
108 return &infos
[ get_column_id(num
) ];
111 static const char *column_id_to_name(int id
)
113 assert(id
< __NCOLUMNS
);
114 return infos
[id
].name
;
117 static const char *get_column_name(int num
)
119 return get_column_info(num
)->name
;
122 static float get_column_whint(int num
)
124 return get_column_info(num
)->whint
;
127 static int get_column_truncate(int num
)
129 return get_column_info(num
)->truncate
;
132 static const char *get_match(int id
)
134 assert(id
< __NCOLUMNS
);
135 return infos
[id
].match
;
138 static void set_match(int id
, const char *match
)
140 assert(id
< __NCOLUMNS
);
141 infos
[id
].match
= match
;
145 * "findmnt" without any filter
147 static int is_listall_mode(void)
149 return (!get_match(COL_SOURCE
) &&
150 !get_match(COL_TARGET
) &&
151 !get_match(COL_FSTYPE
) &&
152 !get_match(COL_OPTIONS
));
156 * findmnt --first-only <devname|TAG=|mountpoint>
158 * ... it works like "mount <devname|TAG=|mountpoint>"
160 static int is_mount_compatible_mode(void)
162 if (!get_match(COL_SOURCE
))
163 return 0; /* <devname|TAG=|mountpoint> is required */
164 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
165 return 0; /* cannot be restricted by -t or -O */
166 if (!(flags
& FL_FIRSTONLY
))
167 return 0; /* we have to return the first entry only */
172 static void set_all_columns_truncate(int set
)
176 for (i
= 0; i
< __NCOLUMNS
; i
++)
177 infos
[i
].truncate
= set
;
181 * converts @name to column ID
183 static int column_name_to_id(const char *name
, size_t namesz
)
187 for (i
= 0; i
< __NCOLUMNS
; i
++) {
188 const char *cn
= column_id_to_name(i
);
190 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
193 warnx(_("unknown column: %s"), name
);
197 /* Returns LABEL or UUID */
198 static const char *get_tag(struct libmnt_fs
*fs
, const char *tagname
)
200 const char *t
, *v
, *res
;
202 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
205 res
= mnt_fs_get_source(fs
);
207 res
= mnt_resolve_spec(res
, cache
);
209 res
= mnt_cache_find_tag_value(cache
, res
, tagname
);
215 /* reads FS data from libmount
216 * TODO: add function that will deallocate data allocated by get_data()
218 static const char *get_data(struct libmnt_fs
*fs
, int num
)
220 const char *str
= NULL
;
222 switch(get_column_id(num
)) {
225 const char *root
= mnt_fs_get_root(fs
);
227 str
= mnt_fs_get_srcpath(fs
);
229 if (str
&& (flags
& FL_CANONICALIZE
))
230 str
= mnt_resolve_path(str
, cache
);
232 str
= mnt_fs_get_source(fs
);
234 if (str
&& (flags
& FL_EVALUATE
))
235 str
= mnt_resolve_spec(str
, cache
);
237 if (root
&& str
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/")) {
240 if (asprintf(&tmp
, "%s[%s]", str
, root
) > 0)
246 str
= mnt_fs_get_target(fs
);
249 str
= mnt_fs_get_fstype(fs
);
252 str
= mnt_fs_strdup_options(fs
);
254 case COL_VFS_OPTIONS
:
255 str
= mnt_fs_get_vfs_options(fs
);
258 str
= mnt_fs_get_fs_options(fs
);
261 str
= get_tag(fs
, "UUID");
264 str
= get_tag(fs
, "LABEL");
268 dev_t devno
= mnt_fs_get_devno(fs
);
272 if (tt_flags
& TT_FL_RAW
)
273 rc
= asprintf(&tmp
, "%u:%u",
274 major(devno
), minor(devno
));
276 rc
= asprintf(&tmp
, "%3u:%-3u",
277 major(devno
), minor(devno
));
288 /* adds one line to the output @tab */
289 static struct tt_line
*add_line(struct tt
*tt
, struct libmnt_fs
*fs
,
290 struct tt_line
*parent
)
293 struct tt_line
*line
= tt_add_line(tt
, parent
);
296 warn(_("failed to add line to output"));
299 for (i
= 0; i
< ncolumns
; i
++)
300 tt_line_set_data(line
, i
, get_data(fs
, i
));
302 tt_line_set_userdata(line
, fs
);
306 static int has_line(struct tt
*tt
, struct libmnt_fs
*fs
)
310 list_for_each(p
, &tt
->tb_lines
) {
311 struct tt_line
*ln
= list_entry(p
, struct tt_line
, ln_lines
);
312 if ((struct libmnt_fs
*) ln
->userdata
== fs
)
318 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
319 static int create_treenode(struct tt
*tt
, struct libmnt_table
*tb
,
320 struct libmnt_fs
*fs
, struct tt_line
*parent_line
)
322 struct libmnt_fs
*chld
= NULL
;
323 struct libmnt_iter
*itr
= NULL
;
324 struct tt_line
*line
;
328 /* first call, get root FS */
329 if (mnt_table_get_root_fs(tb
, &fs
))
333 } else if ((flags
& FL_SUBMOUNTS
) && has_line(tt
, fs
))
336 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
340 line
= add_line(tt
, fs
, parent_line
);
345 * add all children to the output table
347 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
348 if (create_treenode(tt
, tb
, chld
, line
))
358 static int parser_errcb(struct libmnt_table
*tb
, const char *filename
, int line
)
360 warn(_("%s: parse error at line %d"), filename
, line
);
364 /* calls libmount fstab/mtab/mountinfo parser */
365 static struct libmnt_table
*parse_tabfile(const char *path
)
368 struct libmnt_table
*tb
= mnt_new_table();
371 warn(_("failed to initialize libmount tab"));
375 mnt_table_set_parser_errcb(tb
, parser_errcb
);
377 if (!strcmp(path
, _PATH_MNTTAB
))
378 rc
= mnt_table_parse_fstab(tb
, NULL
);
379 else if (!strcmp(path
, _PATH_MOUNTED
))
380 rc
= mnt_table_parse_mtab(tb
, NULL
);
382 rc
= mnt_table_parse_file(tb
, path
);
386 warn(_("can't read: %s"), path
);
392 /* filter function for libmount (mnt_table_find_next_fs()) */
393 static int match_func(struct libmnt_fs
*fs
, void *data
)
395 int rc
= flags
& FL_INVERT
? 1 : 0;
398 m
= get_match(COL_TARGET
);
399 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
402 m
= get_match(COL_SOURCE
);
403 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
406 m
= get_match(COL_FSTYPE
);
407 if (m
&& !mnt_fs_match_fstype(fs
, m
))
410 m
= get_match(COL_OPTIONS
);
411 if (m
&& !mnt_fs_match_options(fs
, m
))
417 /* iterate over filesystems in @tb */
418 static struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
419 struct libmnt_iter
*itr
)
421 struct libmnt_fs
*fs
= NULL
;
423 if (is_listall_mode()) {
427 mnt_table_next_fs(tb
, itr
, &fs
);
429 } else if (is_mount_compatible_mode()) {
431 * Look up for FS in the same way how mount(8) searchs in fstab
435 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
436 mnt_iter_get_direction(itr
));
438 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
439 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
440 mnt_iter_get_direction(itr
));
443 * Look up for all matching entries
445 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
446 * findmnt [-l] <spec> [-O <options>] [-t <types>]
449 mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
);
452 !(flags
& FL_NOSWAPMATCH
) &&
453 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
455 /* swap 'spec' and target. */
456 set_match(COL_TARGET
, get_match(COL_SOURCE
));
457 set_match(COL_SOURCE
, NULL
);
458 mnt_reset_iter(itr
, -1);
467 static int add_matching_lines(struct libmnt_table
*tb
,
468 struct tt
*tt
, int direction
)
470 struct libmnt_iter
*itr
= NULL
;
471 struct libmnt_fs
*fs
;
472 int nlines
= 0, rc
= -1;
474 itr
= mnt_new_iter(direction
);
476 warn(_("failed to initialize libmount iterator"));
480 while((fs
= get_next_fs(tb
, itr
))) {
481 if ((tt_flags
& TT_FL_TREE
) || (flags
& FL_SUBMOUNTS
))
482 rc
= create_treenode(tt
, tb
, fs
, NULL
);
484 rc
= !add_line(tt
, fs
, NULL
);
488 if (flags
& FL_FIRSTONLY
)
490 flags
|= FL_NOSWAPMATCH
;
500 static void __attribute__((__noreturn__
)) usage(FILE *out
)
507 " %1$s [options] <device> | <mountpoint>\n"
508 " %1$s [options] <device> <mountpoint>\n"
509 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
510 program_invocation_short_name
);
514 " -s, --fstab search in static table of filesystems\n"
515 " -m, --mtab search in table of mounted filesystems\n"
516 " -k, --kernel search in kernel table of mounted \n"
517 " filesystems (default)\n\n"
519 " -c, --canonicalize canonicalize printed paths\n"
520 " -d, --direction <word> search direction - 'forward' or 'backward'\n"
521 " -e, --evaluate print all TAGs (LABEL/UUID) evaluated\n"
522 " -f, --first-only print the first found filesystem only\n"
523 " -h, --help print this help\n"
524 " -i, --invert invert sense of matching\n"
525 " -l, --list use list format output\n"
526 " -n, --noheadings don't print headings\n"
527 " -u, --notruncate don't truncate text in columns\n"
528 " -O, --options <list> limit the set of filesystems by mount options\n"
529 " -o, --output <list> output columns\n"
530 " -r, --raw use raw format output\n"
531 " -a, --ascii use ascii chars for tree formatting\n"
532 " -t, --types <list> limit the set of filesystem by FS types\n"
533 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
534 " -R, --submounts print all submount for the matching filesystems\n"
535 " -S, --source <string> device, LABEL= or UUID=device\n"
536 " -T, --target <string> mountpoint\n\n"));
539 fprintf(out
, _("\nAvailable columns:\n"));
541 for (i
= 0; i
< __NCOLUMNS
; i
++) {
543 fprintf(out
, " %-12s", infos
[i
].name
);
544 if (i
&& !((i
+1) % 3))
549 fprintf(out
, _("\nFor more information see findmnt(1).\n"));
551 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
554 static void __attribute__((__noreturn__
))
555 errx_mutually_exclusive(const char *opts
)
557 errx(EXIT_FAILURE
, "%s %s", opts
, _("options are mutually exclusive"));
560 int main(int argc
, char *argv
[])
563 struct libmnt_table
*tb
= NULL
;
564 char *tabfile
= NULL
;
565 int direction
= MNT_ITER_FORWARD
;
569 struct tt
*tt
= NULL
;
571 struct option longopts
[] = {
572 { "ascii", 0, 0, 'a' },
573 { "canonicalize", 0, 0, 'c' },
574 { "direction", 1, 0, 'd' },
575 { "evaluate", 0, 0, 'e' },
576 { "first-only", 0, 0, 'f' },
577 { "fstab", 0, 0, 's' },
578 { "help", 0, 0, 'h' },
579 { "invert", 0, 0, 'i' },
580 { "kernel", 0, 0, 'k' },
581 { "list", 0, 0, 'l' },
582 { "mtab", 0, 0, 'm' },
583 { "noheadings", 0, 0, 'n' },
584 { "notruncate", 0, 0, 'u' },
585 { "options", 1, 0, 'O' },
586 { "output", 1, 0, 'o' },
587 { "raw", 0, 0, 'r' },
588 { "types", 1, 0, 't' },
589 { "fsroot", 0, 0, 'v' },
590 { "submounts", 0, 0, 'R' },
591 { "source", 1, 0, 'S' },
592 { "target", 1, 0, 'T' },
597 assert(ARRAY_SIZE(columns
) == __NCOLUMNS
);
599 setlocale(LC_ALL
, "");
600 bindtextdomain(PACKAGE
, LOCALEDIR
);
603 /* default enabled columns */
604 columns
[ncolumns
++] = COL_TARGET
;
605 columns
[ncolumns
++] = COL_SOURCE
;
606 columns
[ncolumns
++] = COL_FSTYPE
;
607 columns
[ncolumns
++] = COL_OPTIONS
;
609 /* default output format */
610 tt_flags
|= TT_FL_TREE
;
612 while ((c
= getopt_long(argc
, argv
,
613 "acd:ehifo:O:klmnrst:uvRS:T:", longopts
, NULL
)) != -1) {
616 tt_flags
|= TT_FL_ASCII
;
619 flags
|= FL_CANONICALIZE
;
622 if (!strcmp(optarg
, "forward"))
623 direction
= MNT_ITER_FORWARD
;
624 else if (!strcmp(optarg
, "backward"))
625 direction
= MNT_ITER_BACKWARD
;
628 _("unknown direction '%s'"), optarg
);
631 flags
|= FL_EVALUATE
;
640 flags
|= FL_FIRSTONLY
;
643 set_all_columns_truncate(FALSE
);
646 if (tt_parse_columns_list(optarg
, columns
, &ncolumns
,
651 set_match(COL_OPTIONS
, optarg
);
655 errx_mutually_exclusive("--{fstab,mtab,kernel}");
656 tabfile
= _PATH_MOUNTED
;
657 tt_flags
&= ~TT_FL_TREE
;
659 case 's': /* fstab */
661 errx_mutually_exclusive("--{fstab,mtab,kernel}");
662 tabfile
= _PATH_MNTTAB
;
663 tt_flags
&= ~TT_FL_TREE
;
665 case 'k': /* kernel (mountinfo) */
667 errx_mutually_exclusive("--{fstab,mtab,kernel}");
668 tabfile
= _PATH_PROC_MOUNTINFO
;
671 set_match(COL_FSTYPE
, optarg
);
674 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
675 tt_flags
|= TT_FL_RAW
; /* enable raw */
678 if (tt_flags
& TT_FL_RAW
)
679 errx_mutually_exclusive("--{raw,list}");
681 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
684 tt_flags
|= TT_FL_NOHEADINGS
;
687 flags
|= FL_NOFSROOT
;
690 flags
|= FL_SUBMOUNTS
;
693 set_match(COL_SOURCE
, optarg
);
694 flags
|= FL_NOSWAPMATCH
;
697 set_match(COL_TARGET
, optarg
);
698 flags
|= FL_NOSWAPMATCH
;
707 tabfile
= _PATH_PROC_MOUNTINFO
;
709 if (access(tabfile
, R_OK
)) { /* old kernel? */
710 tabfile
= _PATH_PROC_MOUNTS
;
711 tt_flags
&= ~TT_FL_TREE
;
715 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
716 errx(EXIT_FAILURE
, _(
717 "options --target and --source can't be used together "
718 "with command line element that is not an option"));
721 set_match(COL_SOURCE
, argv
[optind
++]); /* dev/tag/mountpoint */
723 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
725 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
726 /* don't care about submounts if list all mounts */
727 flags
&= ~FL_SUBMOUNTS
;
729 if (!(flags
& FL_SUBMOUNTS
) &&
730 (!is_listall_mode() || (flags
& FL_FIRSTONLY
)))
731 tt_flags
&= ~TT_FL_TREE
;
733 if (!(flags
& FL_NOSWAPMATCH
) &&
734 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
736 * Check if we can swap source and target, it's
737 * not possible if the source is LABEL=/UUID=
739 const char *x
= get_match(COL_SOURCE
);
741 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5))
742 flags
|= FL_NOSWAPMATCH
;
746 * initialize libmount
750 tb
= parse_tabfile(tabfile
);
754 cache
= mnt_new_cache();
756 warn(_("failed to initialize libmount cache"));
759 mnt_table_set_cache(tb
, cache
);
762 * initialize output formatting (tt.h)
764 tt
= tt_new_table(tt_flags
);
766 warn(_("failed to initialize output table"));
770 for (i
= 0; i
< ncolumns
; i
++) {
771 int fl
= get_column_truncate(i
) ? TT_FL_TRUNC
: 0;
773 if (get_column_id(i
) == COL_TARGET
&& (tt_flags
& TT_FL_TREE
))
776 if (!tt_define_column(tt
, get_column_name(i
),
777 get_column_whint(i
), fl
)) {
778 warn(_("failed to initialize output column"));
784 * Fill in data to the output table
786 if ((tt_flags
& TT_FL_TREE
) && is_listall_mode())
788 rc
= create_treenode(tt
, tb
, NULL
, NULL
);
790 /* whole lits of sub-tree */
791 rc
= add_matching_lines(tb
, tt
, direction
);
794 * Print the output table
802 mnt_free_cache(cache
);
804 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;