]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / detect-virt / detect-virt.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <getopt.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10
11 #include "string-table.h"
12 #include "util.h"
13 #include "virt.h"
14
15 static bool arg_quiet = false;
16 static enum {
17 ANY_VIRTUALIZATION,
18 ONLY_VM,
19 ONLY_CONTAINER,
20 ONLY_CHROOT,
21 ONLY_PRIVATE_USERS,
22 } arg_mode = ANY_VIRTUALIZATION;
23
24 static void help(void) {
25 printf("%s [OPTIONS...]\n\n"
26 "Detect execution in a virtualized environment.\n\n"
27 " -h --help Show this help\n"
28 " --version Show package version\n"
29 " -c --container Only detect whether we are run in a container\n"
30 " -v --vm Only detect whether we are run in a VM\n"
31 " -r --chroot Detect whether we are run in a chroot() environment\n"
32 " --private-users Only detect whether we are running in a user namespace\n"
33 " -q --quiet Don't output anything, just set return value\n"
34 " --list List all known and detectable types of virtualization\n"
35 , program_invocation_short_name);
36 }
37
38 static int parse_argv(int argc, char *argv[]) {
39
40 enum {
41 ARG_VERSION = 0x100,
42 ARG_PRIVATE_USERS,
43 ARG_LIST,
44 };
45
46 static const struct option options[] = {
47 { "help", no_argument, NULL, 'h' },
48 { "version", no_argument, NULL, ARG_VERSION },
49 { "container", no_argument, NULL, 'c' },
50 { "vm", no_argument, NULL, 'v' },
51 { "chroot", no_argument, NULL, 'r' },
52 { "private-users", no_argument, NULL, ARG_PRIVATE_USERS },
53 { "quiet", no_argument, NULL, 'q' },
54 { "list", no_argument, NULL, ARG_LIST },
55 {}
56 };
57
58 int c;
59
60 assert(argc >= 0);
61 assert(argv);
62
63 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
64
65 switch (c) {
66
67 case 'h':
68 help();
69 return 0;
70
71 case ARG_VERSION:
72 return version();
73
74 case 'q':
75 arg_quiet = true;
76 break;
77
78 case 'c':
79 arg_mode = ONLY_CONTAINER;
80 break;
81
82 case ARG_PRIVATE_USERS:
83 arg_mode = ONLY_PRIVATE_USERS;
84 break;
85
86 case 'v':
87 arg_mode = ONLY_VM;
88 break;
89
90 case 'r':
91 arg_mode = ONLY_CHROOT;
92 break;
93
94 case ARG_LIST:
95 DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX);
96 return 0;
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 ONLY_PRIVATE_USERS:
157 r = running_in_userns();
158 if (r < 0) {
159 log_error_errno(r, "Failed to check for user namespace: %m");
160 return EXIT_FAILURE;
161 }
162
163 return r ? EXIT_SUCCESS : EXIT_FAILURE;
164
165 case ANY_VIRTUALIZATION:
166 default:
167 r = detect_virtualization();
168 if (r < 0) {
169 log_error_errno(r, "Failed to check for virtualization: %m");
170 return EXIT_FAILURE;
171 }
172
173 break;
174 }
175
176 if (!arg_quiet)
177 puts(virtualization_to_string(r));
178
179 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
180 }