2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2011-02-21 21:48:59 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2012-04-12 14:45:50 +02:00
|
|
|
#include <getopt.h>
|
2015-09-23 03:01:06 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2011-02-21 21:48:59 +01:00
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
#include "alloc-util.h"
|
2018-05-22 12:12:44 +02:00
|
|
|
#include "string-table.h"
|
2018-08-09 10:32:31 +02:00
|
|
|
#include "terminal-util.h"
|
2011-02-21 21:48:59 +01:00
|
|
|
#include "util.h"
|
2011-09-23 17:00:33 +02:00
|
|
|
#include "virt.h"
|
2012-04-12 14:45:50 +02:00
|
|
|
|
|
|
|
static bool arg_quiet = false;
|
|
|
|
static enum {
|
|
|
|
ANY_VIRTUALIZATION,
|
|
|
|
ONLY_VM,
|
2015-10-27 00:07:54 +01:00
|
|
|
ONLY_CONTAINER,
|
|
|
|
ONLY_CHROOT,
|
2016-10-21 05:41:21 +02:00
|
|
|
ONLY_PRIVATE_USERS,
|
2012-04-12 14:45:50 +02:00
|
|
|
} arg_mode = ANY_VIRTUALIZATION;
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-detect-virt", "1", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2012-04-12 14:45:50 +02:00
|
|
|
printf("%s [OPTIONS...]\n\n"
|
|
|
|
"Detect execution in a virtualized environment.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
|
|
|
" -c --container Only detect whether we are run in a container\n"
|
|
|
|
" -v --vm Only detect whether we are run in a VM\n"
|
2015-10-27 00:07:54 +01:00
|
|
|
" -r --chroot Detect whether we are run in a chroot() environment\n"
|
2016-10-21 05:41:21 +02:00
|
|
|
" --private-users Only detect whether we are running in a user namespace\n"
|
2014-08-02 17:12:21 +02:00
|
|
|
" -q --quiet Don't output anything, just set return value\n"
|
2018-05-22 12:12:44 +02:00
|
|
|
" --list List all known and detectable types of virtualization\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2012-04-12 14:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
2016-10-21 05:41:21 +02:00
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_PRIVATE_USERS,
|
2018-05-22 12:12:44 +02:00
|
|
|
ARG_LIST,
|
2012-04-12 14:45:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
2016-10-21 05:41:21 +02:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "container", no_argument, NULL, 'c' },
|
|
|
|
{ "vm", no_argument, NULL, 'v' },
|
|
|
|
{ "chroot", no_argument, NULL, 'r' },
|
|
|
|
{ "private-users", no_argument, NULL, ARG_PRIVATE_USERS },
|
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
2018-05-22 12:12:44 +02:00
|
|
|
{ "list", no_argument, NULL, ARG_LIST },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2012-04-12 14:45:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2015-10-27 00:07:54 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
|
2012-04-12 14:45:50 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2012-04-12 14:45:50 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2012-04-12 14:45:50 +02:00
|
|
|
|
|
|
|
case 'q':
|
|
|
|
arg_quiet = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
arg_mode = ONLY_CONTAINER;
|
|
|
|
break;
|
|
|
|
|
2016-10-21 05:41:21 +02:00
|
|
|
case ARG_PRIVATE_USERS:
|
|
|
|
arg_mode = ONLY_PRIVATE_USERS;
|
|
|
|
break;
|
|
|
|
|
2012-04-12 14:45:50 +02:00
|
|
|
case 'v':
|
|
|
|
arg_mode = ONLY_VM;
|
|
|
|
break;
|
|
|
|
|
2015-10-27 00:07:54 +01:00
|
|
|
case 'r':
|
|
|
|
arg_mode = ONLY_CHROOT;
|
|
|
|
break;
|
|
|
|
|
2018-05-22 12:12:44 +02:00
|
|
|
case ARG_LIST:
|
|
|
|
DUMP_STRING_TABLE(virtualization, int, _VIRTUALIZATION_MAX);
|
|
|
|
return 0;
|
|
|
|
|
2012-04-12 14:45:50 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
2013-11-06 18:28:39 +01:00
|
|
|
assert_not_reached("Unhandled option");
|
2012-04-12 14:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc) {
|
2015-09-23 19:40:13 +02:00
|
|
|
log_error("%s takes no arguments.", program_invocation_short_name);
|
2012-04-12 14:45:50 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-02-21 21:48:59 +01:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2015-09-23 19:40:13 +02:00
|
|
|
int r;
|
2011-02-21 21:48:59 +01:00
|
|
|
|
|
|
|
/* This is mostly intended to be used for scripts which want
|
|
|
|
* to detect whether we are being run in a virtualized
|
|
|
|
* environment or not */
|
|
|
|
|
2012-04-12 14:45:50 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
|
|
|
|
switch (arg_mode) {
|
|
|
|
|
2015-09-07 13:42:47 +02:00
|
|
|
case ONLY_VM:
|
|
|
|
r = detect_vm();
|
|
|
|
if (r < 0) {
|
2015-09-23 19:40:13 +02:00
|
|
|
log_error_errno(r, "Failed to check for VM: %m");
|
2012-04-12 14:45:50 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ONLY_CONTAINER:
|
2015-09-07 13:42:47 +02:00
|
|
|
r = detect_container();
|
2012-04-12 14:45:50 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to check for container: %m");
|
2012-04-12 14:45:50 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2015-10-27 00:07:54 +01:00
|
|
|
case ONLY_CHROOT:
|
|
|
|
r = running_in_chroot();
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "Failed to check for chroot() environment: %m");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
|
2016-10-21 05:41:21 +02:00
|
|
|
case ONLY_PRIVATE_USERS:
|
|
|
|
r = running_in_userns();
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "Failed to check for user namespace: %m");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
|
2015-09-07 13:42:47 +02:00
|
|
|
case ANY_VIRTUALIZATION:
|
|
|
|
default:
|
|
|
|
r = detect_virtualization();
|
2012-04-12 14:45:50 +02:00
|
|
|
if (r < 0) {
|
2015-09-07 13:42:47 +02:00
|
|
|
log_error_errno(r, "Failed to check for virtualization: %m");
|
2012-04-12 14:45:50 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2011-02-21 21:48:59 +01:00
|
|
|
}
|
|
|
|
|
2012-04-12 15:10:28 +02:00
|
|
|
if (!arg_quiet)
|
2015-09-07 13:42:47 +02:00
|
|
|
puts(virtualization_to_string(r));
|
|
|
|
|
2015-09-23 19:40:13 +02:00
|
|
|
return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
|
2011-02-21 21:48:59 +01:00
|
|
|
}
|