]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
07faed4f | 2 | |
07faed4f | 3 | #include <errno.h> |
cac72f7a | 4 | #include <getopt.h> |
3f6fd1ba LP |
5 | #include <stdbool.h> |
6 | #include <stdlib.h> | |
07faed4f | 7 | |
37ec0fdd | 8 | #include "alloc-util.h" |
d6b4d1c7 | 9 | #include "build.h" |
5e332028 | 10 | #include "main-func.h" |
294bf0c3 | 11 | #include "pretty-print.h" |
39824629 | 12 | #include "string-table.h" |
b52aae1d | 13 | #include "virt.h" |
cac72f7a LP |
14 | |
15 | static bool arg_quiet = false; | |
16 | static enum { | |
17 | ANY_VIRTUALIZATION, | |
18 | ONLY_VM, | |
d21be5ff LP |
19 | ONLY_CONTAINER, |
20 | ONLY_CHROOT, | |
299a34c1 | 21 | ONLY_PRIVATE_USERS, |
cac72f7a LP |
22 | } arg_mode = ANY_VIRTUALIZATION; |
23 | ||
37ec0fdd LP |
24 | static int help(void) { |
25 | _cleanup_free_ char *link = NULL; | |
26 | int r; | |
27 | ||
28 | r = terminal_urlify_man("systemd-detect-virt", "1", &link); | |
29 | if (r < 0) | |
30 | return log_oom(); | |
31 | ||
cac72f7a LP |
32 | printf("%s [OPTIONS...]\n\n" |
33 | "Detect execution in a virtualized environment.\n\n" | |
34 | " -h --help Show this help\n" | |
35 | " --version Show package version\n" | |
36 | " -c --container Only detect whether we are run in a container\n" | |
37 | " -v --vm Only detect whether we are run in a VM\n" | |
d21be5ff | 38 | " -r --chroot Detect whether we are run in a chroot() environment\n" |
299a34c1 | 39 | " --private-users Only detect whether we are running in a user namespace\n" |
601185b4 | 40 | " -q --quiet Don't output anything, just set return value\n" |
39824629 | 41 | " --list List all known and detectable types of virtualization\n" |
bc556335 DDM |
42 | "\nSee the %s for details.\n", |
43 | program_invocation_short_name, | |
44 | link); | |
37ec0fdd LP |
45 | |
46 | return 0; | |
cac72f7a LP |
47 | } |
48 | ||
49 | static int parse_argv(int argc, char *argv[]) { | |
50 | ||
51 | enum { | |
299a34c1 ZJS |
52 | ARG_VERSION = 0x100, |
53 | ARG_PRIVATE_USERS, | |
39824629 | 54 | ARG_LIST, |
cac72f7a LP |
55 | }; |
56 | ||
57 | static const struct option options[] = { | |
299a34c1 ZJS |
58 | { "help", no_argument, NULL, 'h' }, |
59 | { "version", no_argument, NULL, ARG_VERSION }, | |
60 | { "container", no_argument, NULL, 'c' }, | |
61 | { "vm", no_argument, NULL, 'v' }, | |
62 | { "chroot", no_argument, NULL, 'r' }, | |
63 | { "private-users", no_argument, NULL, ARG_PRIVATE_USERS }, | |
64 | { "quiet", no_argument, NULL, 'q' }, | |
39824629 | 65 | { "list", no_argument, NULL, ARG_LIST }, |
eb9da376 | 66 | {} |
cac72f7a LP |
67 | }; |
68 | ||
69 | int c; | |
70 | ||
71 | assert(argc >= 0); | |
72 | assert(argv); | |
73 | ||
d21be5ff | 74 | while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0) |
cac72f7a LP |
75 | |
76 | switch (c) { | |
77 | ||
78 | case 'h': | |
37ec0fdd | 79 | return help(); |
cac72f7a LP |
80 | |
81 | case ARG_VERSION: | |
3f6fd1ba | 82 | return version(); |
cac72f7a LP |
83 | |
84 | case 'q': | |
85 | arg_quiet = true; | |
86 | break; | |
87 | ||
88 | case 'c': | |
89 | arg_mode = ONLY_CONTAINER; | |
90 | break; | |
91 | ||
299a34c1 ZJS |
92 | case ARG_PRIVATE_USERS: |
93 | arg_mode = ONLY_PRIVATE_USERS; | |
94 | break; | |
95 | ||
cac72f7a LP |
96 | case 'v': |
97 | arg_mode = ONLY_VM; | |
98 | break; | |
99 | ||
d21be5ff LP |
100 | case 'r': |
101 | arg_mode = ONLY_CHROOT; | |
102 | break; | |
103 | ||
39824629 | 104 | case ARG_LIST: |
1b86c7c5 | 105 | DUMP_STRING_TABLE(virtualization, Virtualization, _VIRTUALIZATION_MAX); |
39824629 LP |
106 | return 0; |
107 | ||
cac72f7a LP |
108 | case '?': |
109 | return -EINVAL; | |
110 | ||
111 | default: | |
04499a70 | 112 | assert_not_reached(); |
cac72f7a | 113 | } |
cac72f7a | 114 | |
baaa35ad ZJS |
115 | if (optind < argc) |
116 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
117 | "%s takes no arguments.", | |
118 | program_invocation_short_name); | |
cac72f7a LP |
119 | |
120 | return 1; | |
121 | } | |
07faed4f | 122 | |
bdab0984 | 123 | static int run(int argc, char *argv[]) { |
1b86c7c5 | 124 | Virtualization v; |
9e6a555a | 125 | int r; |
07faed4f LP |
126 | |
127 | /* This is mostly intended to be used for scripts which want | |
128 | * to detect whether we are being run in a virtualized | |
129 | * environment or not */ | |
130 | ||
d2acb93d | 131 | log_setup(); |
cac72f7a LP |
132 | |
133 | r = parse_argv(argc, argv); | |
134 | if (r <= 0) | |
bdab0984 | 135 | return r; |
cac72f7a LP |
136 | |
137 | switch (arg_mode) { | |
75f86906 | 138 | case ONLY_VM: |
1b86c7c5 LP |
139 | v = detect_vm(); |
140 | if (v < 0) | |
141 | return log_error_errno(v, "Failed to check for VM: %m"); | |
cac72f7a | 142 | break; |
cac72f7a LP |
143 | |
144 | case ONLY_CONTAINER: | |
1b86c7c5 LP |
145 | v = detect_container(); |
146 | if (v < 0) | |
147 | return log_error_errno(v, "Failed to check for container: %m"); | |
cac72f7a LP |
148 | break; |
149 | ||
d21be5ff LP |
150 | case ONLY_CHROOT: |
151 | r = running_in_chroot(); | |
bdab0984 ZJS |
152 | if (r < 0) |
153 | return log_error_errno(r, "Failed to check for chroot() environment: %m"); | |
154 | return !r; | |
d21be5ff | 155 | |
299a34c1 ZJS |
156 | case ONLY_PRIVATE_USERS: |
157 | r = running_in_userns(); | |
bdab0984 ZJS |
158 | if (r < 0) |
159 | return log_error_errno(r, "Failed to check for user namespace: %m"); | |
160 | return !r; | |
299a34c1 | 161 | |
75f86906 LP |
162 | case ANY_VIRTUALIZATION: |
163 | default: | |
1b86c7c5 LP |
164 | v = detect_virtualization(); |
165 | if (v < 0) | |
166 | return log_error_errno(v, "Failed to check for virtualization: %m"); | |
cac72f7a | 167 | break; |
07faed4f LP |
168 | } |
169 | ||
ac0d6edf | 170 | if (!arg_quiet) |
1b86c7c5 | 171 | puts(virtualization_to_string(v)); |
75f86906 | 172 | |
1b86c7c5 | 173 | return v == VIRTUALIZATION_NONE; |
07faed4f | 174 | } |
bdab0984 ZJS |
175 | |
176 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |