]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <getopt.h> | |
5 | #include <stdbool.h> | |
6 | #include <stdlib.h> | |
7 | ||
8 | #include "alloc-util.h" | |
9 | #include "main-func.h" | |
10 | #include "pretty-print.h" | |
11 | #include "string-table.h" | |
12 | #include "util.h" | |
13 | #include "virt.h" | |
14 | ||
15 | static bool arg_quiet = false; | |
16 | static enum { | |
17 | ANY_VIRTUALIZATION, | |
18 | ONLY_VM, | |
19 | ONLY_CONTAINER, | |
20 | ONLY_CHROOT, | |
21 | ONLY_PRIVATE_USERS, | |
22 | } arg_mode = ANY_VIRTUALIZATION; | |
23 | ||
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 | ||
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" | |
38 | " -r --chroot Detect whether we are run in a chroot() environment\n" | |
39 | " --private-users Only detect whether we are running in a user namespace\n" | |
40 | " -q --quiet Don't output anything, just set return value\n" | |
41 | " --list List all known and detectable types of virtualization\n" | |
42 | "\nSee the %s for details.\n" | |
43 | , program_invocation_short_name | |
44 | , link | |
45 | ); | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
50 | static int parse_argv(int argc, char *argv[]) { | |
51 | ||
52 | enum { | |
53 | ARG_VERSION = 0x100, | |
54 | ARG_PRIVATE_USERS, | |
55 | ARG_LIST, | |
56 | }; | |
57 | ||
58 | static const struct option options[] = { | |
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' }, | |
66 | { "list", no_argument, NULL, ARG_LIST }, | |
67 | {} | |
68 | }; | |
69 | ||
70 | int c; | |
71 | ||
72 | assert(argc >= 0); | |
73 | assert(argv); | |
74 | ||
75 | while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0) | |
76 | ||
77 | switch (c) { | |
78 | ||
79 | case 'h': | |
80 | return help(); | |
81 | ||
82 | case ARG_VERSION: | |
83 | return version(); | |
84 | ||
85 | case 'q': | |
86 | arg_quiet = true; | |
87 | break; | |
88 | ||
89 | case 'c': | |
90 | arg_mode = ONLY_CONTAINER; | |
91 | break; | |
92 | ||
93 | case ARG_PRIVATE_USERS: | |
94 | arg_mode = ONLY_PRIVATE_USERS; | |
95 | break; | |
96 | ||
97 | case 'v': | |
98 | arg_mode = ONLY_VM; | |
99 | break; | |
100 | ||
101 | case 'r': | |
102 | arg_mode = ONLY_CHROOT; | |
103 | break; | |
104 | ||
105 | case ARG_LIST: | |
106 | DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX); | |
107 | return 0; | |
108 | ||
109 | case '?': | |
110 | return -EINVAL; | |
111 | ||
112 | default: | |
113 | assert_not_reached("Unhandled option"); | |
114 | } | |
115 | ||
116 | if (optind < argc) | |
117 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
118 | "%s takes no arguments.", | |
119 | program_invocation_short_name); | |
120 | ||
121 | return 1; | |
122 | } | |
123 | ||
124 | static int run(int argc, char *argv[]) { | |
125 | int r; | |
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 | ||
131 | log_show_color(true); | |
132 | log_parse_environment(); | |
133 | log_open(); | |
134 | ||
135 | r = parse_argv(argc, argv); | |
136 | if (r <= 0) | |
137 | return r; | |
138 | ||
139 | switch (arg_mode) { | |
140 | case ONLY_VM: | |
141 | r = detect_vm(); | |
142 | if (r < 0) | |
143 | return log_error_errno(r, "Failed to check for VM: %m"); | |
144 | break; | |
145 | ||
146 | case ONLY_CONTAINER: | |
147 | r = detect_container(); | |
148 | if (r < 0) | |
149 | return log_error_errno(r, "Failed to check for container: %m"); | |
150 | break; | |
151 | ||
152 | case ONLY_CHROOT: | |
153 | r = running_in_chroot(); | |
154 | if (r < 0) | |
155 | return log_error_errno(r, "Failed to check for chroot() environment: %m"); | |
156 | return !r; | |
157 | ||
158 | case ONLY_PRIVATE_USERS: | |
159 | r = running_in_userns(); | |
160 | if (r < 0) | |
161 | return log_error_errno(r, "Failed to check for user namespace: %m"); | |
162 | return !r; | |
163 | ||
164 | case ANY_VIRTUALIZATION: | |
165 | default: | |
166 | r = detect_virtualization(); | |
167 | if (r < 0) | |
168 | return log_error_errno(r, "Failed to check for virtualization: %m"); | |
169 | break; | |
170 | } | |
171 | ||
172 | if (!arg_quiet) | |
173 | puts(virtualization_to_string(r)); | |
174 | ||
175 | return r == VIRTUALIZATION_NONE; | |
176 | } | |
177 | ||
178 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |