]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / detect-virt / detect-virt.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7
8 #include "alloc-util.h"
9 #include "string-table.h"
10 #include "terminal-util.h"
11 #include "util.h"
12 #include "virt.h"
13
14 static bool arg_quiet = false;
15 static enum {
16 ANY_VIRTUALIZATION,
17 ONLY_VM,
18 ONLY_CONTAINER,
19 ONLY_CHROOT,
20 ONLY_PRIVATE_USERS,
21 } arg_mode = ANY_VIRTUALIZATION;
22
23 static int help(void) {
24 _cleanup_free_ char *link = NULL;
25 int r;
26
27 r = terminal_urlify_man("systemd-detect-virt", "1", &link);
28 if (r < 0)
29 return log_oom();
30
31 printf("%s [OPTIONS...]\n\n"
32 "Detect execution in a virtualized environment.\n\n"
33 " -h --help Show this help\n"
34 " --version Show package version\n"
35 " -c --container Only detect whether we are run in a container\n"
36 " -v --vm Only detect whether we are run in a VM\n"
37 " -r --chroot Detect whether we are run in a chroot() environment\n"
38 " --private-users Only detect whether we are running in a user namespace\n"
39 " -q --quiet Don't output anything, just set return value\n"
40 " --list List all known and detectable types of virtualization\n"
41 "\nSee the %s for details.\n"
42 , program_invocation_short_name
43 , link
44 );
45
46 return 0;
47 }
48
49 static int parse_argv(int argc, char *argv[]) {
50
51 enum {
52 ARG_VERSION = 0x100,
53 ARG_PRIVATE_USERS,
54 ARG_LIST,
55 };
56
57 static const struct option options[] = {
58 { "help", no_argument, NULL, 'h' },
59 { "version", no_argument, NULL, ARG_VERSION },
60 { "container", no_argument, NULL, 'c' },
61 { "vm", no_argument, NULL, 'v' },
62 { "chroot", no_argument, NULL, 'r' },
63 { "private-users", no_argument, NULL, ARG_PRIVATE_USERS },
64 { "quiet", no_argument, NULL, 'q' },
65 { "list", no_argument, NULL, ARG_LIST },
66 {}
67 };
68
69 int c;
70
71 assert(argc >= 0);
72 assert(argv);
73
74 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
75
76 switch (c) {
77
78 case 'h':
79 return help();
80
81 case ARG_VERSION:
82 return version();
83
84 case 'q':
85 arg_quiet = true;
86 break;
87
88 case 'c':
89 arg_mode = ONLY_CONTAINER;
90 break;
91
92 case ARG_PRIVATE_USERS:
93 arg_mode = ONLY_PRIVATE_USERS;
94 break;
95
96 case 'v':
97 arg_mode = ONLY_VM;
98 break;
99
100 case 'r':
101 arg_mode = ONLY_CHROOT;
102 break;
103
104 case ARG_LIST:
105 DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX);
106 return 0;
107
108 case '?':
109 return -EINVAL;
110
111 default:
112 assert_not_reached("Unhandled option");
113 }
114
115 if (optind < argc) {
116 log_error("%s takes no arguments.", program_invocation_short_name);
117 return -EINVAL;
118 }
119
120 return 1;
121 }
122
123 int main(int argc, char *argv[]) {
124 int r;
125
126 /* This is mostly intended to be used for scripts which want
127 * to detect whether we are being run in a virtualized
128 * environment or not */
129
130 log_parse_environment();
131 log_open();
132
133 r = parse_argv(argc, argv);
134 if (r <= 0)
135 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
136
137 switch (arg_mode) {
138
139 case ONLY_VM:
140 r = detect_vm();
141 if (r < 0) {
142 log_error_errno(r, "Failed to check for VM: %m");
143 return EXIT_FAILURE;
144 }
145
146 break;
147
148 case ONLY_CONTAINER:
149 r = detect_container();
150 if (r < 0) {
151 log_error_errno(r, "Failed to check for container: %m");
152 return EXIT_FAILURE;
153 }
154
155 break;
156
157 case ONLY_CHROOT:
158 r = running_in_chroot();
159 if (r < 0) {
160 log_error_errno(r, "Failed to check for chroot() environment: %m");
161 return EXIT_FAILURE;
162 }
163
164 return r ? EXIT_SUCCESS : EXIT_FAILURE;
165
166 case ONLY_PRIVATE_USERS:
167 r = running_in_userns();
168 if (r < 0) {
169 log_error_errno(r, "Failed to check for user namespace: %m");
170 return EXIT_FAILURE;
171 }
172
173 return r ? EXIT_SUCCESS : EXIT_FAILURE;
174
175 case ANY_VIRTUALIZATION:
176 default:
177 r = detect_virtualization();
178 if (r < 0) {
179 log_error_errno(r, "Failed to check for virtualization: %m");
180 return EXIT_FAILURE;
181 }
182
183 break;
184 }
185
186 if (!arg_quiet)
187 puts(virtualization_to_string(r));
188
189 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
190 }