]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
Merge pull request #1180 from evverx/dot-from-to-alias-handling
[thirdparty/systemd.git] / src / detect-virt / detect-virt.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <errno.h>
25 #include <getopt.h>
26
27 #include "util.h"
28 #include "virt.h"
29 #include "build.h"
30
31 static bool arg_quiet = false;
32 static enum {
33 ANY_VIRTUALIZATION,
34 ONLY_VM,
35 ONLY_CONTAINER
36 } arg_mode = ANY_VIRTUALIZATION;
37
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);
47 }
48
49 static int parse_argv(int argc, char *argv[]) {
50
51 enum {
52 ARG_VERSION = 0x100
53 };
54
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' },
61 {}
62 };
63
64 int c;
65
66 assert(argc >= 0);
67 assert(argv);
68
69 while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
70
71 switch (c) {
72
73 case 'h':
74 help();
75 return 0;
76
77 case ARG_VERSION:
78 puts(PACKAGE_STRING);
79 puts(SYSTEMD_FEATURES);
80 return 0;
81
82 case 'q':
83 arg_quiet = true;
84 break;
85
86 case 'c':
87 arg_mode = ONLY_CONTAINER;
88 break;
89
90 case 'v':
91 arg_mode = ONLY_VM;
92 break;
93
94 case '?':
95 return -EINVAL;
96
97 default:
98 assert_not_reached("Unhandled option");
99 }
100
101 if (optind < argc) {
102 log_error("%s takes no arguments.",
103 program_invocation_short_name);
104 return -EINVAL;
105 }
106
107 return 1;
108 }
109
110 int main(int argc, char *argv[]) {
111 int retval = EXIT_SUCCESS, r;
112
113 /* This is mostly intended to be used for scripts which want
114 * to detect whether we are being run in a virtualized
115 * environment or not */
116
117 log_parse_environment();
118 log_open();
119
120 r = parse_argv(argc, argv);
121 if (r <= 0)
122 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
123
124 switch (arg_mode) {
125
126 case ONLY_VM:
127 r = detect_vm();
128 if (r < 0) {
129 log_error_errno(r, "Failed to check for vm: %m");
130 return EXIT_FAILURE;
131 }
132
133 break;
134
135 case ONLY_CONTAINER:
136 r = detect_container();
137 if (r < 0) {
138 log_error_errno(r, "Failed to check for container: %m");
139 return EXIT_FAILURE;
140 }
141
142 break;
143
144 case ANY_VIRTUALIZATION:
145 default:
146 r = detect_virtualization();
147 if (r < 0) {
148 log_error_errno(r, "Failed to check for virtualization: %m");
149 return EXIT_FAILURE;
150 }
151
152 break;
153 }
154
155 if (!arg_quiet)
156 puts(virtualization_to_string(r));
157
158 retval = r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
159
160 return retval;
161 }