]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
Merge pull request #2589 from keszybz/resolve-tool-2
[thirdparty/systemd.git] / src / detect-virt / detect-virt.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24
25 #include "util.h"
26 #include "virt.h"
27
28 static bool arg_quiet = false;
29 static enum {
30 ANY_VIRTUALIZATION,
31 ONLY_VM,
32 ONLY_CONTAINER,
33 ONLY_CHROOT,
34 } arg_mode = ANY_VIRTUALIZATION;
35
36 static void help(void) {
37 printf("%s [OPTIONS...]\n\n"
38 "Detect execution in a virtualized environment.\n\n"
39 " -h --help Show this help\n"
40 " --version Show package version\n"
41 " -c --container Only detect whether we are run in a container\n"
42 " -v --vm Only detect whether we are run in a VM\n"
43 " -r --chroot Detect whether we are run in a chroot() environment\n"
44 " -q --quiet Don't output anything, just set return value\n"
45 , program_invocation_short_name);
46 }
47
48 static int parse_argv(int argc, char *argv[]) {
49
50 enum {
51 ARG_VERSION = 0x100
52 };
53
54 static const struct option options[] = {
55 { "help", no_argument, NULL, 'h' },
56 { "version", no_argument, NULL, ARG_VERSION },
57 { "container", no_argument, NULL, 'c' },
58 { "vm", no_argument, NULL, 'v' },
59 { "chroot", no_argument, NULL, 'r' },
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, "hqcvr", options, NULL)) >= 0)
70
71 switch (c) {
72
73 case 'h':
74 help();
75 return 0;
76
77 case ARG_VERSION:
78 return version();
79
80 case 'q':
81 arg_quiet = true;
82 break;
83
84 case 'c':
85 arg_mode = ONLY_CONTAINER;
86 break;
87
88 case 'v':
89 arg_mode = ONLY_VM;
90 break;
91
92 case 'r':
93 arg_mode = ONLY_CHROOT;
94 break;
95
96 case '?':
97 return -EINVAL;
98
99 default:
100 assert_not_reached("Unhandled option");
101 }
102
103 if (optind < argc) {
104 log_error("%s takes no arguments.", program_invocation_short_name);
105 return -EINVAL;
106 }
107
108 return 1;
109 }
110
111 int main(int argc, char *argv[]) {
112 int r;
113
114 /* This is mostly intended to be used for scripts which want
115 * to detect whether we are being run in a virtualized
116 * environment or not */
117
118 log_parse_environment();
119 log_open();
120
121 r = parse_argv(argc, argv);
122 if (r <= 0)
123 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
124
125 switch (arg_mode) {
126
127 case ONLY_VM:
128 r = detect_vm();
129 if (r < 0) {
130 log_error_errno(r, "Failed to check for VM: %m");
131 return EXIT_FAILURE;
132 }
133
134 break;
135
136 case ONLY_CONTAINER:
137 r = detect_container();
138 if (r < 0) {
139 log_error_errno(r, "Failed to check for container: %m");
140 return EXIT_FAILURE;
141 }
142
143 break;
144
145 case ONLY_CHROOT:
146 r = running_in_chroot();
147 if (r < 0) {
148 log_error_errno(r, "Failed to check for chroot() environment: %m");
149 return EXIT_FAILURE;
150 }
151
152 return r ? EXIT_SUCCESS : EXIT_FAILURE;
153
154 case ANY_VIRTUALIZATION:
155 default:
156 r = detect_virtualization();
157 if (r < 0) {
158 log_error_errno(r, "Failed to check for virtualization: %m");
159 return EXIT_FAILURE;
160 }
161
162 break;
163 }
164
165 if (!arg_quiet)
166 puts(virtualization_to_string(r));
167
168 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
169 }