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;
34 } arg_mode
= ANY_VIRTUALIZATION
;
36 static void help(void) {
37 printf("%s [OPTIONS...]\n\n"
38 "Detect execution in a virtualized environment.\n\n"
39 " -h --help Show this help\n"
40 " --version Show package version\n"
41 " -c --container Only detect whether we are run in a container\n"
42 " -v --vm Only detect whether we are run in a VM\n"
43 " -r --chroot Detect whether we are run in a chroot() environment\n"
44 " -q --quiet Don't output anything, just set return value\n"
45 , program_invocation_short_name
);
48 static int parse_argv(int argc
, char *argv
[]) {
54 static const struct option options
[] = {
55 { "help", no_argument
, NULL
, 'h' },
56 { "version", no_argument
, NULL
, ARG_VERSION
},
57 { "container", no_argument
, NULL
, 'c' },
58 { "vm", no_argument
, NULL
, 'v' },
59 { "chroot", no_argument
, NULL
, 'r' },
60 { "quiet", no_argument
, NULL
, 'q' },
69 while ((c
= getopt_long(argc
, argv
, "hqcvr", options
, NULL
)) >= 0)
85 arg_mode
= ONLY_CONTAINER
;
93 arg_mode
= ONLY_CHROOT
;
100 assert_not_reached("Unhandled option");
104 log_error("%s takes no arguments.", program_invocation_short_name
);
111 int main(int argc
, char *argv
[]) {
114 /* This is mostly intended to be used for scripts which want
115 * to detect whether we are being run in a virtualized
116 * environment or not */
118 log_parse_environment();
121 r
= parse_argv(argc
, argv
);
123 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
130 log_error_errno(r
, "Failed to check for VM: %m");
137 r
= detect_container();
139 log_error_errno(r
, "Failed to check for container: %m");
146 r
= running_in_chroot();
148 log_error_errno(r
, "Failed to check for chroot() environment: %m");
152 return r
? EXIT_SUCCESS
: EXIT_FAILURE
;
154 case ANY_VIRTUALIZATION
:
156 r
= detect_virtualization();
158 log_error_errno(r
, "Failed to check for virtualization: %m");
166 puts(virtualization_to_string(r
));
168 return r
!= VIRTUALIZATION_NONE
? EXIT_SUCCESS
: EXIT_FAILURE
;