1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 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 " -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", optional_argument
, NULL
, 'v' },
59 { "quiet", no_argument
, NULL
, 'q' },
68 while ((c
= getopt_long(argc
, argv
, "hqcv", options
, NULL
)) >= 0)
84 arg_mode
= ONLY_CONTAINER
;
95 assert_not_reached("Unhandled option");
99 log_error("%s takes no arguments.", program_invocation_short_name
);
106 int main(int argc
, char *argv
[]) {
109 /* This is mostly intended to be used for scripts which want
110 * to detect whether we are being run in a virtualized
111 * environment or not */
113 log_parse_environment();
116 r
= parse_argv(argc
, argv
);
118 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
125 log_error_errno(r
, "Failed to check for VM: %m");
132 r
= detect_container();
134 log_error_errno(r
, "Failed to check for container: %m");
140 case ANY_VIRTUALIZATION
:
142 r
= detect_virtualization();
144 log_error_errno(r
, "Failed to check for virtualization: %m");
152 puts(virtualization_to_string(r
));
154 return r
!= VIRTUALIZATION_NONE
? EXIT_SUCCESS
: EXIT_FAILURE
;