]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
07faed4f LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
8 | under the terms of the GNU Lesser General Public License as published by |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
07faed4f LP |
10 | (at your option) any later version. |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 15 | Lesser General Public License for more details. |
07faed4f | 16 | |
5430f7f2 | 17 | You should have received a copy of the GNU Lesser General Public License |
07faed4f LP |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
19 | ***/ | |
20 | ||
07faed4f | 21 | #include <errno.h> |
cac72f7a | 22 | #include <getopt.h> |
3f6fd1ba LP |
23 | #include <stdbool.h> |
24 | #include <stdlib.h> | |
07faed4f LP |
25 | |
26 | #include "util.h" | |
b52aae1d | 27 | #include "virt.h" |
cac72f7a LP |
28 | |
29 | static bool arg_quiet = false; | |
30 | static enum { | |
31 | ANY_VIRTUALIZATION, | |
32 | ONLY_VM, | |
d21be5ff LP |
33 | ONLY_CONTAINER, |
34 | ONLY_CHROOT, | |
299a34c1 | 35 | ONLY_PRIVATE_USERS, |
cac72f7a LP |
36 | } arg_mode = ANY_VIRTUALIZATION; |
37 | ||
601185b4 | 38 | static void help(void) { |
cac72f7a LP |
39 | printf("%s [OPTIONS...]\n\n" |
40 | "Detect execution in a virtualized environment.\n\n" | |
41 | " -h --help Show this help\n" | |
42 | " --version Show package version\n" | |
43 | " -c --container Only detect whether we are run in a container\n" | |
44 | " -v --vm Only detect whether we are run in a VM\n" | |
d21be5ff | 45 | " -r --chroot Detect whether we are run in a chroot() environment\n" |
299a34c1 | 46 | " --private-users Only detect whether we are running in a user namespace\n" |
601185b4 ZJS |
47 | " -q --quiet Don't output anything, just set return value\n" |
48 | , program_invocation_short_name); | |
cac72f7a LP |
49 | } |
50 | ||
51 | static int parse_argv(int argc, char *argv[]) { | |
52 | ||
53 | enum { | |
299a34c1 ZJS |
54 | ARG_VERSION = 0x100, |
55 | ARG_PRIVATE_USERS, | |
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' }, | |
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': | |
601185b4 ZJS |
79 | help(); |
80 | return 0; | |
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 | ||
cac72f7a LP |
105 | case '?': |
106 | return -EINVAL; | |
107 | ||
108 | default: | |
eb9da376 | 109 | assert_not_reached("Unhandled option"); |
cac72f7a | 110 | } |
cac72f7a LP |
111 | |
112 | if (optind < argc) { | |
9e6a555a | 113 | log_error("%s takes no arguments.", program_invocation_short_name); |
cac72f7a LP |
114 | return -EINVAL; |
115 | } | |
116 | ||
117 | return 1; | |
118 | } | |
07faed4f LP |
119 | |
120 | int main(int argc, char *argv[]) { | |
9e6a555a | 121 | int r; |
07faed4f LP |
122 | |
123 | /* This is mostly intended to be used for scripts which want | |
124 | * to detect whether we are being run in a virtualized | |
125 | * environment or not */ | |
126 | ||
cac72f7a LP |
127 | log_parse_environment(); |
128 | log_open(); | |
129 | ||
130 | r = parse_argv(argc, argv); | |
131 | if (r <= 0) | |
132 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
133 | ||
134 | switch (arg_mode) { | |
135 | ||
75f86906 LP |
136 | case ONLY_VM: |
137 | r = detect_vm(); | |
138 | if (r < 0) { | |
9e6a555a | 139 | log_error_errno(r, "Failed to check for VM: %m"); |
cac72f7a LP |
140 | return EXIT_FAILURE; |
141 | } | |
142 | ||
cac72f7a | 143 | break; |
cac72f7a LP |
144 | |
145 | case ONLY_CONTAINER: | |
75f86906 | 146 | r = detect_container(); |
cac72f7a | 147 | if (r < 0) { |
da927ba9 | 148 | log_error_errno(r, "Failed to check for container: %m"); |
cac72f7a LP |
149 | return EXIT_FAILURE; |
150 | } | |
151 | ||
cac72f7a LP |
152 | break; |
153 | ||
d21be5ff LP |
154 | case ONLY_CHROOT: |
155 | r = running_in_chroot(); | |
156 | if (r < 0) { | |
157 | log_error_errno(r, "Failed to check for chroot() environment: %m"); | |
158 | return EXIT_FAILURE; | |
159 | } | |
160 | ||
161 | return r ? EXIT_SUCCESS : EXIT_FAILURE; | |
162 | ||
299a34c1 ZJS |
163 | case ONLY_PRIVATE_USERS: |
164 | r = running_in_userns(); | |
165 | if (r < 0) { | |
166 | log_error_errno(r, "Failed to check for user namespace: %m"); | |
167 | return EXIT_FAILURE; | |
168 | } | |
169 | ||
170 | return r ? EXIT_SUCCESS : EXIT_FAILURE; | |
171 | ||
75f86906 LP |
172 | case ANY_VIRTUALIZATION: |
173 | default: | |
174 | r = detect_virtualization(); | |
cac72f7a | 175 | if (r < 0) { |
75f86906 | 176 | log_error_errno(r, "Failed to check for virtualization: %m"); |
cac72f7a LP |
177 | return EXIT_FAILURE; |
178 | } | |
179 | ||
cac72f7a | 180 | break; |
07faed4f LP |
181 | } |
182 | ||
ac0d6edf | 183 | if (!arg_quiet) |
75f86906 LP |
184 | puts(virtualization_to_string(r)); |
185 | ||
9e6a555a | 186 | return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE; |
07faed4f | 187 | } |