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