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