]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/detect-virt/detect-virt.c
Merge pull request #9801 from yuwata/analyze-cleanups
[thirdparty/systemd.git] / src / detect-virt / detect-virt.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
07faed4f 2
07faed4f 3#include <errno.h>
cac72f7a 4#include <getopt.h>
3f6fd1ba
LP
5#include <stdbool.h>
6#include <stdlib.h>
07faed4f 7
37ec0fdd 8#include "alloc-util.h"
39824629 9#include "string-table.h"
37ec0fdd 10#include "terminal-util.h"
07faed4f 11#include "util.h"
b52aae1d 12#include "virt.h"
cac72f7a
LP
13
14static bool arg_quiet = false;
15static enum {
16 ANY_VIRTUALIZATION,
17 ONLY_VM,
d21be5ff
LP
18 ONLY_CONTAINER,
19 ONLY_CHROOT,
299a34c1 20 ONLY_PRIVATE_USERS,
cac72f7a
LP
21} arg_mode = ANY_VIRTUALIZATION;
22
37ec0fdd
LP
23static 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
cac72f7a
LP
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"
d21be5ff 37 " -r --chroot Detect whether we are run in a chroot() environment\n"
299a34c1 38 " --private-users Only detect whether we are running in a user namespace\n"
601185b4 39 " -q --quiet Don't output anything, just set return value\n"
39824629 40 " --list List all known and detectable types of virtualization\n"
37ec0fdd
LP
41 "\nSee the %s for details.\n"
42 , program_invocation_short_name
43 , link
44 );
45
46 return 0;
cac72f7a
LP
47}
48
49static int parse_argv(int argc, char *argv[]) {
50
51 enum {
299a34c1
ZJS
52 ARG_VERSION = 0x100,
53 ARG_PRIVATE_USERS,
39824629 54 ARG_LIST,
cac72f7a
LP
55 };
56
57 static const struct option options[] = {
299a34c1
ZJS
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' },
39824629 65 { "list", no_argument, NULL, ARG_LIST },
eb9da376 66 {}
cac72f7a
LP
67 };
68
69 int c;
70
71 assert(argc >= 0);
72 assert(argv);
73
d21be5ff 74 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
cac72f7a
LP
75
76 switch (c) {
77
78 case 'h':
37ec0fdd 79 return help();
cac72f7a
LP
80
81 case ARG_VERSION:
3f6fd1ba 82 return version();
cac72f7a
LP
83
84 case 'q':
85 arg_quiet = true;
86 break;
87
88 case 'c':
89 arg_mode = ONLY_CONTAINER;
90 break;
91
299a34c1
ZJS
92 case ARG_PRIVATE_USERS:
93 arg_mode = ONLY_PRIVATE_USERS;
94 break;
95
cac72f7a
LP
96 case 'v':
97 arg_mode = ONLY_VM;
98 break;
99
d21be5ff
LP
100 case 'r':
101 arg_mode = ONLY_CHROOT;
102 break;
103
39824629
LP
104 case ARG_LIST:
105 DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX);
106 return 0;
107
cac72f7a
LP
108 case '?':
109 return -EINVAL;
110
111 default:
eb9da376 112 assert_not_reached("Unhandled option");
cac72f7a 113 }
cac72f7a
LP
114
115 if (optind < argc) {
9e6a555a 116 log_error("%s takes no arguments.", program_invocation_short_name);
cac72f7a
LP
117 return -EINVAL;
118 }
119
120 return 1;
121}
07faed4f
LP
122
123int main(int argc, char *argv[]) {
9e6a555a 124 int r;
07faed4f
LP
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
cac72f7a
LP
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
75f86906
LP
139 case ONLY_VM:
140 r = detect_vm();
141 if (r < 0) {
9e6a555a 142 log_error_errno(r, "Failed to check for VM: %m");
cac72f7a
LP
143 return EXIT_FAILURE;
144 }
145
cac72f7a 146 break;
cac72f7a
LP
147
148 case ONLY_CONTAINER:
75f86906 149 r = detect_container();
cac72f7a 150 if (r < 0) {
da927ba9 151 log_error_errno(r, "Failed to check for container: %m");
cac72f7a
LP
152 return EXIT_FAILURE;
153 }
154
cac72f7a
LP
155 break;
156
d21be5ff
LP
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
299a34c1
ZJS
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
75f86906
LP
175 case ANY_VIRTUALIZATION:
176 default:
177 r = detect_virtualization();
cac72f7a 178 if (r < 0) {
75f86906 179 log_error_errno(r, "Failed to check for virtualization: %m");
cac72f7a
LP
180 return EXIT_FAILURE;
181 }
182
cac72f7a 183 break;
07faed4f
LP
184 }
185
ac0d6edf 186 if (!arg_quiet)
75f86906
LP
187 puts(virtualization_to_string(r));
188
9e6a555a 189 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
07faed4f 190}