Skip to content

Commit 3322130

Browse files
qmonnetborkmann
authored andcommitted
tools: bpftool: add an option to prevent auto-mount of bpffs, tracefs
In order to make life easier for users, bpftool automatically attempts to mount the BPF virtual file system, if it is not mounted already, before trying to pin objects in it. Similarly, it attempts to mount tracefs if necessary before trying to dump the trace pipe to the console. While mounting file systems on-the-fly can improve user experience, some administrators might prefer to avoid that. Let's add an option to block these mount attempts. Note that it does not prevent automatic mounting of tracefs by debugfs for the "bpftool prog tracelog" command. Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent be3245e commit 3322130

File tree

7 files changed

+31
-2
lines changed

7 files changed

+31
-2
lines changed

tools/bpf/bpftool/Documentation/bpftool-map.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ OPTIONS
128128
-f, --bpffs
129129
Show file names of pinned maps.
130130

131+
-n, --nomount
132+
Do not automatically attempt to mount any virtual file system
133+
(such as tracefs or BPF virtual file system) when necessary.
134+
131135
EXAMPLES
132136
========
133137
**# bpftool map show**

tools/bpf/bpftool/Documentation/bpftool-prog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ OPTIONS
161161
-m, --mapcompat
162162
Allow loading maps with unknown map definitions.
163163

164+
-n, --nomount
165+
Do not automatically attempt to mount any virtual file system
166+
(such as tracefs or BPF virtual file system) when necessary.
167+
164168
EXAMPLES
165169
========
166170
**# bpftool prog show**

tools/bpf/bpftool/Documentation/bpftool.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ OPTIONS
6060
-m, --mapcompat
6161
Allow loading maps with unknown map definitions.
6262

63+
-n, --nomount
64+
Do not automatically attempt to mount any virtual file system
65+
(such as tracefs or BPF virtual file system) when necessary.
66+
6367

6468
SEE ALSO
6569
========

tools/bpf/bpftool/common.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ int mount_bpffs_for_pin(const char *name)
177177
/* nothing to do if already mounted */
178178
goto out_free;
179179

180+
if (block_mount) {
181+
p_err("no BPF file system found, not mounting it due to --nomount option");
182+
err = -1;
183+
goto out_free;
184+
}
185+
180186
err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
181187
if (err) {
182188
err_str[ERR_MAX_LEN - 1] = '\0';

tools/bpf/bpftool/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ json_writer_t *json_wtr;
2424
bool pretty_output;
2525
bool json_output;
2626
bool show_pinned;
27+
bool block_mount;
2728
int bpf_flags;
2829
struct pinned_obj_table prog_table;
2930
struct pinned_obj_table map_table;
@@ -313,6 +314,7 @@ int main(int argc, char **argv)
313314
{ "version", no_argument, NULL, 'V' },
314315
{ "bpffs", no_argument, NULL, 'f' },
315316
{ "mapcompat", no_argument, NULL, 'm' },
317+
{ "nomount", no_argument, NULL, 'n' },
316318
{ 0 }
317319
};
318320
int opt, ret;
@@ -321,13 +323,14 @@ int main(int argc, char **argv)
321323
pretty_output = false;
322324
json_output = false;
323325
show_pinned = false;
326+
block_mount = false;
324327
bin_name = argv[0];
325328

326329
hash_init(prog_table.table);
327330
hash_init(map_table.table);
328331

329332
opterr = 0;
330-
while ((opt = getopt_long(argc, argv, "Vhpjfm",
333+
while ((opt = getopt_long(argc, argv, "Vhpjfmn",
331334
options, NULL)) >= 0) {
332335
switch (opt) {
333336
case 'V':
@@ -354,6 +357,9 @@ int main(int argc, char **argv)
354357
case 'm':
355358
bpf_flags = MAPS_RELAX_COMPAT;
356359
break;
360+
case 'n':
361+
block_mount = true;
362+
break;
357363
default:
358364
p_err("unrecognized option '%s'", argv[optind - 1]);
359365
if (json_output)

tools/bpf/bpftool/main.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
#define HELP_SPEC_PROGRAM \
4545
"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
4646
#define HELP_SPEC_OPTIONS \
47-
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} | {-m|--mapcompat}"
47+
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} |\n" \
48+
"\t {-m|--mapcompat} | {-n|--nomount} }"
4849
#define HELP_SPEC_MAP \
4950
"MAP := { id MAP_ID | pinned FILE }"
5051

@@ -85,6 +86,7 @@ extern const char *bin_name;
8586
extern json_writer_t *json_wtr;
8687
extern bool json_output;
8788
extern bool show_pinned;
89+
extern bool block_mount;
8890
extern int bpf_flags;
8991
extern struct pinned_obj_table prog_table;
9092
extern struct pinned_obj_table map_table;

tools/bpf/bpftool/tracelog.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ static bool get_tracefs_pipe(char *mnt)
9191
if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
9292
goto exit_found;
9393

94+
if (block_mount)
95+
return false;
96+
9497
p_info("could not find tracefs, attempting to mount it now");
9598
/* Most of the time, tracefs is automatically mounted by debugfs at
9699
* /sys/kernel/debug/tracing when we try to access it. If we could not

0 commit comments

Comments
 (0)