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/>.
31 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 " -q --quiet Don't output anything, just set return value\n"
46 , program_invocation_short_name);
49 static int parse_argv(int argc, char *argv[]) {
55 static const struct option options[] = {
56 { "help", no_argument, NULL, 'h' },
57 { "version", no_argument, NULL, ARG_VERSION },
58 { "container", no_argument, NULL, 'c' },
59 { "vm", optional_argument, NULL, 'v' },
60 { "quiet", no_argument, NULL, 'q' },
69 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
79 puts(SYSTEMD_FEATURES);
87 arg_mode = ONLY_CONTAINER;
98 assert_not_reached("Unhandled option");
102 log_error("%s takes no arguments.",
103 program_invocation_short_name);
110 int main(int argc, char *argv[]) {
111 const char *id = NULL;
112 int retval = EXIT_SUCCESS;
115 /* This is mostly intended to be used for scripts which want
116 * to detect whether we are being run in a virtualized
117 * environment or not */
119 log_parse_environment();
122 r = parse_argv(argc, argv);
124 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
128 case ANY_VIRTUALIZATION: {
131 v = detect_virtualization(&id);
133 log_error_errno(v, "Failed to check for virtualization: %m");
137 retval = v != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
142 r = detect_container(&id);
144 log_error_errno(r, "Failed to check for container: %m");
148 retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
154 log_error_errno(r, "Failed to check for vm: %m");
158 retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
163 puts(id ? id : "none");