1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
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
10 (at your option) any later version.
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
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 static bool arg_quiet
= false;
36 } arg_mode
= ANY_VIRTUALIZATION
;
38 static void help(void) {
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"
45 " -r --chroot Detect whether we are run in a chroot() environment\n"
46 " --private-users Only detect whether we are running in a user namespace\n"
47 " -q --quiet Don't output anything, just set return value\n"
48 , program_invocation_short_name
);
51 static int parse_argv(int argc
, char *argv
[]) {
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' },
74 while ((c
= getopt_long(argc
, argv
, "hqcvr", options
, NULL
)) >= 0)
90 arg_mode
= ONLY_CONTAINER
;
93 case ARG_PRIVATE_USERS
:
94 arg_mode
= ONLY_PRIVATE_USERS
;
102 arg_mode
= ONLY_CHROOT
;
109 assert_not_reached("Unhandled option");
113 log_error("%s takes no arguments.", program_invocation_short_name
);
120 int main(int argc
, char *argv
[]) {
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 */
127 log_parse_environment();
130 r
= parse_argv(argc
, argv
);
132 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
139 log_error_errno(r
, "Failed to check for VM: %m");
146 r
= detect_container();
148 log_error_errno(r
, "Failed to check for container: %m");
155 r
= running_in_chroot();
157 log_error_errno(r
, "Failed to check for chroot() environment: %m");
161 return r
? EXIT_SUCCESS
: EXIT_FAILURE
;
163 case ONLY_PRIVATE_USERS
:
164 r
= running_in_userns();
166 log_error_errno(r
, "Failed to check for user namespace: %m");
170 return r
? EXIT_SUCCESS
: EXIT_FAILURE
;
172 case ANY_VIRTUALIZATION
:
174 r
= detect_virtualization();
176 log_error_errno(r
, "Failed to check for virtualization: %m");
184 puts(virtualization_to_string(r
));
186 return r
!= VIRTUALIZATION_NONE
? EXIT_SUCCESS
: EXIT_FAILURE
;