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