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;
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 " -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
[]) {
56 static const struct option options
[] = {
57 { "help", no_argument
, NULL
, 'h' },
58 { "version", no_argument
, NULL
, ARG_VERSION
},
59 { "container", no_argument
, NULL
, 'c' },
60 { "vm", no_argument
, NULL
, 'v' },
61 { "chroot", no_argument
, NULL
, 'r' },
62 { "quiet", no_argument
, NULL
, 'q' },
71 while ((c
= getopt_long(argc
, argv
, "hqcvr", options
, NULL
)) >= 0)
87 arg_mode
= ONLY_CONTAINER
;
95 arg_mode
= ONLY_CHROOT
;
102 assert_not_reached("Unhandled option");
106 log_error("%s takes no arguments.", program_invocation_short_name
);
113 int main(int argc
, char *argv
[]) {
116 /* This is mostly intended to be used for scripts which want
117 * to detect whether we are being run in a virtualized
118 * environment or not */
120 log_parse_environment();
123 r
= parse_argv(argc
, argv
);
125 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
132 log_error_errno(r
, "Failed to check for VM: %m");
139 r
= detect_container();
141 log_error_errno(r
, "Failed to check for container: %m");
148 r
= running_in_chroot();
150 log_error_errno(r
, "Failed to check for chroot() environment: %m");
154 return r
? EXIT_SUCCESS
: EXIT_FAILURE
;
156 case ANY_VIRTUALIZATION
:
158 r
= detect_virtualization();
160 log_error_errno(r
, "Failed to check for virtualization: %m");
168 puts(virtualization_to_string(r
));
170 return r
!= VIRTUALIZATION_NONE
? EXIT_SUCCESS
: EXIT_FAILURE
;