2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 static bool arg_quiet
= false;
35 } arg_mode
= ANY_VIRTUALIZATION
;
37 static void help(void) {
38 printf("%s [OPTIONS...]\n\n"
39 "Detect execution in a virtualized environment.\n\n"
40 " -h --help Show this help\n"
41 " --version Show package version\n"
42 " -c --container Only detect whether we are run in a container\n"
43 " -v --vm Only detect whether we are run in a VM\n"
44 " -r --chroot Detect whether we are run in a chroot() environment\n"
45 " --private-users Only detect whether we are running in a user namespace\n"
46 " -q --quiet Don't output anything, just set return value\n"
47 , program_invocation_short_name
);
50 static int parse_argv(int argc
, char *argv
[]) {
57 static const struct option options
[] = {
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' },
73 while ((c
= getopt_long(argc
, argv
, "hqcvr", options
, NULL
)) >= 0)
89 arg_mode
= ONLY_CONTAINER
;
92 case ARG_PRIVATE_USERS
:
93 arg_mode
= ONLY_PRIVATE_USERS
;
101 arg_mode
= ONLY_CHROOT
;
108 assert_not_reached("Unhandled option");
112 log_error("%s takes no arguments.", program_invocation_short_name
);
119 int main(int argc
, char *argv
[]) {
122 /* This is mostly intended to be used for scripts which want
123 * to detect whether we are being run in a virtualized
124 * environment or not */
126 log_parse_environment();
129 r
= parse_argv(argc
, argv
);
131 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
138 log_error_errno(r
, "Failed to check for VM: %m");
145 r
= detect_container();
147 log_error_errno(r
, "Failed to check for container: %m");
154 r
= running_in_chroot();
156 log_error_errno(r
, "Failed to check for chroot() environment: %m");
160 return r
? EXIT_SUCCESS
: EXIT_FAILURE
;
162 case ONLY_PRIVATE_USERS
:
163 r
= running_in_userns();
165 log_error_errno(r
, "Failed to check for user namespace: %m");
169 return r
? EXIT_SUCCESS
: EXIT_FAILURE
;
171 case ANY_VIRTUALIZATION
:
173 r
= detect_virtualization();
175 log_error_errno(r
, "Failed to check for virtualization: %m");
183 puts(virtualization_to_string(r
));
185 return r
!= VIRTUALIZATION_NONE
? EXIT_SUCCESS
: EXIT_FAILURE
;