]> git.proxmox.com Git - systemd.git/blame - src/detect-virt/detect-virt.c
New upstream version 236
[systemd.git] / src / detect-virt / detect-virt.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
663996b3
MS
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
663996b3 21#include <errno.h>
663996b3 22#include <getopt.h>
6300502b
MP
23#include <stdbool.h>
24#include <stdlib.h>
663996b3
MS
25
26#include "util.h"
27#include "virt.h"
663996b3
MS
28
29static bool arg_quiet = false;
30static enum {
31 ANY_VIRTUALIZATION,
32 ONLY_VM,
db2df898
MP
33 ONLY_CONTAINER,
34 ONLY_CHROOT,
8a584da2 35 ONLY_PRIVATE_USERS,
663996b3
MS
36} arg_mode = ANY_VIRTUALIZATION;
37
5eef597e 38static void help(void) {
663996b3
MS
39 printf("%s [OPTIONS...]\n\n"
40 "Detect execution in a virtualized environment.\n\n"
41 " -h --help Show this help\n"
42 " --version Show package version\n"
43 " -c --container Only detect whether we are run in a container\n"
44 " -v --vm Only detect whether we are run in a VM\n"
db2df898 45 " -r --chroot Detect whether we are run in a chroot() environment\n"
8a584da2 46 " --private-users Only detect whether we are running in a user namespace\n"
5eef597e
MP
47 " -q --quiet Don't output anything, just set return value\n"
48 , program_invocation_short_name);
663996b3
MS
49}
50
51static int parse_argv(int argc, char *argv[]) {
52
53 enum {
8a584da2
MP
54 ARG_VERSION = 0x100,
55 ARG_PRIVATE_USERS,
663996b3
MS
56 };
57
58 static const struct option options[] = {
8a584da2
MP
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' },
60f067b4 66 {}
663996b3
MS
67 };
68
69 int c;
70
71 assert(argc >= 0);
72 assert(argv);
73
db2df898 74 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
663996b3
MS
75
76 switch (c) {
77
78 case 'h':
5eef597e
MP
79 help();
80 return 0;
663996b3
MS
81
82 case ARG_VERSION:
6300502b 83 return version();
663996b3
MS
84
85 case 'q':
86 arg_quiet = true;
87 break;
88
89 case 'c':
90 arg_mode = ONLY_CONTAINER;
91 break;
92
8a584da2
MP
93 case ARG_PRIVATE_USERS:
94 arg_mode = ONLY_PRIVATE_USERS;
95 break;
96
663996b3
MS
97 case 'v':
98 arg_mode = ONLY_VM;
99 break;
100
db2df898
MP
101 case 'r':
102 arg_mode = ONLY_CHROOT;
103 break;
104
663996b3
MS
105 case '?':
106 return -EINVAL;
107
108 default:
60f067b4 109 assert_not_reached("Unhandled option");
663996b3 110 }
663996b3
MS
111
112 if (optind < argc) {
6300502b 113 log_error("%s takes no arguments.", program_invocation_short_name);
663996b3
MS
114 return -EINVAL;
115 }
116
117 return 1;
118}
119
120int main(int argc, char *argv[]) {
60f067b4 121 int r;
663996b3
MS
122
123 /* This is mostly intended to be used for scripts which want
124 * to detect whether we are being run in a virtualized
125 * environment or not */
126
127 log_parse_environment();
128 log_open();
129
130 r = parse_argv(argc, argv);
131 if (r <= 0)
132 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
133
134 switch (arg_mode) {
135
6300502b
MP
136 case ONLY_VM:
137 r = detect_vm();
138 if (r < 0) {
139 log_error_errno(r, "Failed to check for VM: %m");
663996b3
MS
140 return EXIT_FAILURE;
141 }
142
663996b3 143 break;
663996b3
MS
144
145 case ONLY_CONTAINER:
6300502b 146 r = detect_container();
663996b3 147 if (r < 0) {
f47781d8 148 log_error_errno(r, "Failed to check for container: %m");
663996b3
MS
149 return EXIT_FAILURE;
150 }
151
663996b3
MS
152 break;
153
db2df898
MP
154 case ONLY_CHROOT:
155 r = running_in_chroot();
156 if (r < 0) {
157 log_error_errno(r, "Failed to check for chroot() environment: %m");
158 return EXIT_FAILURE;
159 }
160
161 return r ? EXIT_SUCCESS : EXIT_FAILURE;
162
8a584da2
MP
163 case ONLY_PRIVATE_USERS:
164 r = running_in_userns();
165 if (r < 0) {
166 log_error_errno(r, "Failed to check for user namespace: %m");
167 return EXIT_FAILURE;
168 }
169
170 return r ? EXIT_SUCCESS : EXIT_FAILURE;
171
6300502b
MP
172 case ANY_VIRTUALIZATION:
173 default:
174 r = detect_virtualization();
663996b3 175 if (r < 0) {
6300502b 176 log_error_errno(r, "Failed to check for virtualization: %m");
663996b3
MS
177 return EXIT_FAILURE;
178 }
179
663996b3
MS
180 break;
181 }
182
183 if (!arg_quiet)
6300502b 184 puts(virtualization_to_string(r));
663996b3 185
6300502b 186 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
663996b3 187}