]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
Merge pull request #1359 from jengelh/ue
[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 <errno.h>
23 #include <getopt.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26
27 #include "util.h"
28 #include "virt.h"
29
30 static bool arg_quiet = false;
31 static enum {
32 ANY_VIRTUALIZATION,
33 ONLY_VM,
34 ONLY_CONTAINER,
35 ONLY_CHROOT,
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 " -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);
48 }
49
50 static int parse_argv(int argc, char *argv[]) {
51
52 enum {
53 ARG_VERSION = 0x100
54 };
55
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' },
63 {}
64 };
65
66 int c;
67
68 assert(argc >= 0);
69 assert(argv);
70
71 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
72
73 switch (c) {
74
75 case 'h':
76 help();
77 return 0;
78
79 case ARG_VERSION:
80 return version();
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 'r':
95 arg_mode = ONLY_CHROOT;
96 break;
97
98 case '?':
99 return -EINVAL;
100
101 default:
102 assert_not_reached("Unhandled option");
103 }
104
105 if (optind < argc) {
106 log_error("%s takes no arguments.", program_invocation_short_name);
107 return -EINVAL;
108 }
109
110 return 1;
111 }
112
113 int main(int argc, char *argv[]) {
114 int r;
115
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 */
119
120 log_parse_environment();
121 log_open();
122
123 r = parse_argv(argc, argv);
124 if (r <= 0)
125 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
126
127 switch (arg_mode) {
128
129 case ONLY_VM:
130 r = detect_vm();
131 if (r < 0) {
132 log_error_errno(r, "Failed to check for VM: %m");
133 return EXIT_FAILURE;
134 }
135
136 break;
137
138 case ONLY_CONTAINER:
139 r = detect_container();
140 if (r < 0) {
141 log_error_errno(r, "Failed to check for container: %m");
142 return EXIT_FAILURE;
143 }
144
145 break;
146
147 case ONLY_CHROOT:
148 r = running_in_chroot();
149 if (r < 0) {
150 log_error_errno(r, "Failed to check for chroot() environment: %m");
151 return EXIT_FAILURE;
152 }
153
154 return r ? EXIT_SUCCESS : EXIT_FAILURE;
155
156 case ANY_VIRTUALIZATION:
157 default:
158 r = detect_virtualization();
159 if (r < 0) {
160 log_error_errno(r, "Failed to check for virtualization: %m");
161 return EXIT_FAILURE;
162 }
163
164 break;
165 }
166
167 if (!arg_quiet)
168 puts(virtualization_to_string(r));
169
170 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
171 }