]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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 "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 static int help(void) {
25 _cleanup_free_ char *link = NULL;
26 int r;
27
28 r = terminal_urlify_man("systemd-detect-virt", "1", &link);
29 if (r < 0)
30 return log_oom();
31
32 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 return 0;
48 }
49
50 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 assert(argc >= 0);
73 assert(argv);
74
75 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
76
77 switch (c) {
78
79 case 'h':
80 return help();
81
82 case ARG_VERSION:
83 return version();
84
85 case 'q':
86 arg_quiet = true;
87 break;
88
89 case 'c':
90 arg_mode = ONLY_CONTAINER;
91 break;
92
93 case ARG_PRIVATE_USERS:
94 arg_mode = ONLY_PRIVATE_USERS;
95 break;
96
97 case 'v':
98 arg_mode = ONLY_VM;
99 break;
100
101 case 'r':
102 arg_mode = ONLY_CHROOT;
103 break;
104
105 case ARG_LIST:
106 DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX);
107 return 0;
108
109 case '?':
110 return -EINVAL;
111
112 default:
113 assert_not_reached("Unhandled option");
114 }
115
116 if (optind < argc)
117 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
118 "%s takes no arguments.",
119 program_invocation_short_name);
120
121 return 1;
122 }
123
124 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 log_parse_environment();
132 log_open();
133
134 r = parse_argv(argc, argv);
135 if (r <= 0)
136 return r;
137
138 switch (arg_mode) {
139 case ONLY_VM:
140 r = detect_vm();
141 if (r < 0)
142 return log_error_errno(r, "Failed to check for VM: %m");
143 break;
144
145 case ONLY_CONTAINER:
146 r = detect_container();
147 if (r < 0)
148 return log_error_errno(r, "Failed to check for container: %m");
149 break;
150
151 case ONLY_CHROOT:
152 r = running_in_chroot();
153 if (r < 0)
154 return log_error_errno(r, "Failed to check for chroot() environment: %m");
155 return !r;
156
157 case ONLY_PRIVATE_USERS:
158 r = running_in_userns();
159 if (r < 0)
160 return log_error_errno(r, "Failed to check for user namespace: %m");
161 return !r;
162
163 case ANY_VIRTUALIZATION:
164 default:
165 r = detect_virtualization();
166 if (r < 0)
167 return log_error_errno(r, "Failed to check for virtualization: %m");
168 break;
169 }
170
171 if (!arg_quiet)
172 puts(virtualization_to_string(r));
173
174 return r == VIRTUALIZATION_NONE;
175 }
176
177 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);