]>
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" |
5e332028 | 9 | #include "main-func.h" |
294bf0c3 | 10 | #include "pretty-print.h" |
39824629 | 11 | #include "string-table.h" |
07faed4f | 12 | #include "util.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" |
37ec0fdd LP |
42 | "\nSee the %s for details.\n" |
43 | , program_invocation_short_name | |
44 | , link | |
45 | ); | |
46 | ||
47 | return 0; | |
cac72f7a LP |
48 | } |
49 | ||
50 | static int parse_argv(int argc, char *argv[]) { | |
51 | ||
52 | enum { | |
299a34c1 ZJS |
53 | ARG_VERSION = 0x100, |
54 | ARG_PRIVATE_USERS, | |
39824629 | 55 | ARG_LIST, |
cac72f7a LP |
56 | }; |
57 | ||
58 | static const struct option options[] = { | |
299a34c1 ZJS |
59 | { "help", no_argument, NULL, 'h' }, |
60 | { "version", no_argument, NULL, ARG_VERSION }, | |
61 | { "container", no_argument, NULL, 'c' }, | |
62 | { "vm", no_argument, NULL, 'v' }, | |
63 | { "chroot", no_argument, NULL, 'r' }, | |
64 | { "private-users", no_argument, NULL, ARG_PRIVATE_USERS }, | |
65 | { "quiet", no_argument, NULL, 'q' }, | |
39824629 | 66 | { "list", no_argument, NULL, ARG_LIST }, |
eb9da376 | 67 | {} |
cac72f7a LP |
68 | }; |
69 | ||
70 | int c; | |
71 | ||
72 | assert(argc >= 0); | |
73 | assert(argv); | |
74 | ||
d21be5ff | 75 | while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0) |
cac72f7a LP |
76 | |
77 | switch (c) { | |
78 | ||
79 | case 'h': | |
37ec0fdd | 80 | return help(); |
cac72f7a LP |
81 | |
82 | case ARG_VERSION: | |
3f6fd1ba | 83 | return version(); |
cac72f7a LP |
84 | |
85 | case 'q': | |
86 | arg_quiet = true; | |
87 | break; | |
88 | ||
89 | case 'c': | |
90 | arg_mode = ONLY_CONTAINER; | |
91 | break; | |
92 | ||
299a34c1 ZJS |
93 | case ARG_PRIVATE_USERS: |
94 | arg_mode = ONLY_PRIVATE_USERS; | |
95 | break; | |
96 | ||
cac72f7a LP |
97 | case 'v': |
98 | arg_mode = ONLY_VM; | |
99 | break; | |
100 | ||
d21be5ff LP |
101 | case 'r': |
102 | arg_mode = ONLY_CHROOT; | |
103 | break; | |
104 | ||
39824629 LP |
105 | case ARG_LIST: |
106 | DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX); | |
107 | return 0; | |
108 | ||
cac72f7a LP |
109 | case '?': |
110 | return -EINVAL; | |
111 | ||
112 | default: | |
eb9da376 | 113 | assert_not_reached("Unhandled option"); |
cac72f7a | 114 | } |
cac72f7a | 115 | |
baaa35ad ZJS |
116 | if (optind < argc) |
117 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
118 | "%s takes no arguments.", | |
119 | program_invocation_short_name); | |
cac72f7a LP |
120 | |
121 | return 1; | |
122 | } | |
07faed4f | 123 | |
bdab0984 | 124 | static int run(int argc, char *argv[]) { |
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 | ||
41d1f469 | 131 | log_setup_cli(); |
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 LP |
138 | case ONLY_VM: |
139 | r = detect_vm(); | |
bdab0984 ZJS |
140 | if (r < 0) |
141 | return log_error_errno(r, "Failed to check for VM: %m"); | |
cac72f7a | 142 | break; |
cac72f7a LP |
143 | |
144 | case ONLY_CONTAINER: | |
75f86906 | 145 | r = detect_container(); |
bdab0984 ZJS |
146 | if (r < 0) |
147 | return log_error_errno(r, "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: | |
164 | r = detect_virtualization(); | |
bdab0984 ZJS |
165 | if (r < 0) |
166 | return log_error_errno(r, "Failed to check for virtualization: %m"); | |
cac72f7a | 167 | break; |
07faed4f LP |
168 | } |
169 | ||
ac0d6edf | 170 | if (!arg_quiet) |
75f86906 LP |
171 | puts(virtualization_to_string(r)); |
172 | ||
bdab0984 | 173 | return r == VIRTUALIZATION_NONE; |
07faed4f | 174 | } |
bdab0984 ZJS |
175 | |
176 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |