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/>.
32 static bool arg_quiet = false;
37 } arg_mode = ANY_VIRTUALIZATION;
39 static int help(void) {
41 printf("%s [OPTIONS...]\n\n"
42 "Detect execution in a virtualized environment.\n\n"
43 " -h --help Show this help\n"
44 " --version Show package version\n"
45 " -c --container Only detect whether we are run in a container\n"
46 " -v --vm Only detect whether we are run in a VM\n"
47 " -q --quiet Don't output anything, just set return value\n",
48 program_invocation_short_name);
53 static int parse_argv(int argc, char *argv[]) {
59 static const struct option options[] = {
60 { "help", no_argument, NULL, 'h' },
61 { "version", no_argument, NULL, ARG_VERSION },
62 { "container", no_argument, NULL, 'c' },
63 { "vm", optional_argument, NULL, 'v' },
64 { "quiet", no_argument, NULL, 'q' },
73 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0) {
82 puts(SYSTEMD_FEATURES);
90 arg_mode = ONLY_CONTAINER;
101 assert_not_reached("Unhandled option");
113 int main(int argc, char *argv[]) {
114 const char *id = NULL;
115 int retval = EXIT_SUCCESS;
118 /* This is mostly intended to be used for scripts which want
119 * to detect whether we are being run in a virtualized
120 * environment or not */
122 log_parse_environment();
125 r = parse_argv(argc, argv);
127 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
131 case ANY_VIRTUALIZATION: {
134 v = detect_virtualization(&id);
136 log_error("Failed to check for virtualization: %s", strerror(-v));
140 retval = v != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
145 r = detect_container(&id);
147 log_error("Failed to check for container: %s", strerror(-r));
151 retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
157 log_error("Failed to check for vm: %s", strerror(-r));
161 retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
166 puts(id ? id : "none");