]> git.proxmox.com Git - systemd.git/blame - src/detect-virt/detect-virt.c
Merge tag 'upstream/229'
[systemd.git] / src / detect-virt / detect-virt.c
CommitLineData
663996b3
MS
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
663996b3 20#include <errno.h>
663996b3 21#include <getopt.h>
6300502b
MP
22#include <stdbool.h>
23#include <stdlib.h>
663996b3
MS
24
25#include "util.h"
26#include "virt.h"
663996b3
MS
27
28static bool arg_quiet = false;
29static enum {
30 ANY_VIRTUALIZATION,
31 ONLY_VM,
db2df898
MP
32 ONLY_CONTAINER,
33 ONLY_CHROOT,
663996b3
MS
34} arg_mode = ANY_VIRTUALIZATION;
35
5eef597e 36static void help(void) {
663996b3
MS
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"
db2df898 43 " -r --chroot Detect whether we are run in a chroot() environment\n"
5eef597e
MP
44 " -q --quiet Don't output anything, just set return value\n"
45 , program_invocation_short_name);
663996b3
MS
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' },
db2df898
MP
58 { "vm", no_argument, NULL, 'v' },
59 { "chroot", no_argument, NULL, 'r' },
663996b3 60 { "quiet", no_argument, NULL, 'q' },
60f067b4 61 {}
663996b3
MS
62 };
63
64 int c;
65
66 assert(argc >= 0);
67 assert(argv);
68
db2df898 69 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
663996b3
MS
70
71 switch (c) {
72
73 case 'h':
5eef597e
MP
74 help();
75 return 0;
663996b3
MS
76
77 case ARG_VERSION:
6300502b 78 return version();
663996b3
MS
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
db2df898
MP
92 case 'r':
93 arg_mode = ONLY_CHROOT;
94 break;
95
663996b3
MS
96 case '?':
97 return -EINVAL;
98
99 default:
60f067b4 100 assert_not_reached("Unhandled option");
663996b3 101 }
663996b3
MS
102
103 if (optind < argc) {
6300502b 104 log_error("%s takes no arguments.", program_invocation_short_name);
663996b3
MS
105 return -EINVAL;
106 }
107
108 return 1;
109}
110
111int main(int argc, char *argv[]) {
60f067b4 112 int r;
663996b3
MS
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
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
6300502b
MP
127 case ONLY_VM:
128 r = detect_vm();
129 if (r < 0) {
130 log_error_errno(r, "Failed to check for VM: %m");
663996b3
MS
131 return EXIT_FAILURE;
132 }
133
663996b3 134 break;
663996b3
MS
135
136 case ONLY_CONTAINER:
6300502b 137 r = detect_container();
663996b3 138 if (r < 0) {
f47781d8 139 log_error_errno(r, "Failed to check for container: %m");
663996b3
MS
140 return EXIT_FAILURE;
141 }
142
663996b3
MS
143 break;
144
db2df898
MP
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
6300502b
MP
154 case ANY_VIRTUALIZATION:
155 default:
156 r = detect_virtualization();
663996b3 157 if (r < 0) {
6300502b 158 log_error_errno(r, "Failed to check for virtualization: %m");
663996b3
MS
159 return EXIT_FAILURE;
160 }
161
663996b3
MS
162 break;
163 }
164
165 if (!arg_quiet)
6300502b 166 puts(virtualization_to_string(r));
663996b3 167
6300502b 168 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
663996b3 169}