| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "ansi-color.h" |
| 5 |
#include "build.h" |
| 6 |
#include "confidential-virt.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "log.h" |
| 9 |
#include "main-func.h" |
| 10 |
#include "options.h" |
| 11 |
#include "pretty-print.h" |
| 12 |
#include "string-table.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 |
ONLY_CVM, |
| 23 |
} arg_mode = ANY_VIRTUALIZATION; |
| 24 |
|
| 25 |
static int help(void) { |
| 26 |
_cleanup_free_ char *link = NULL; |
| 27 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 28 |
int r; |
| 29 |
|
| 30 |
r = terminal_urlify_man("systemd-detect-virt", "1", &link); |
| 31 |
if (r < 0) |
| 32 |
return log_oom(); |
| 33 |
|
| 34 |
r = option_parser_get_help_table(&options); |
| 35 |
if (r < 0) |
| 36 |
return r; |
| 37 |
|
| 38 |
printf("%s [OPTIONS...]\n" |
| 39 |
"\n%sDetect execution in a virtualized environment.%s\n" |
| 40 |
"\nOptions:\n", |
| 41 |
program_invocation_short_name, |
| 42 |
ansi_highlight(), |
| 43 |
ansi_normal()); |
| 44 |
r = table_print_or_warn(options); |
| 45 |
if (r < 0) |
| 46 |
return r; |
| 47 |
|
| 48 |
printf("\nSee the %s for details.\n", link); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
static int parse_argv(int argc, char *argv[]) { |
| 53 |
assert(argc >= 0); |
| 54 |
assert(argv); |
| 55 |
|
| 56 |
OptionParser opts = { argc, argv }; |
| 57 |
|
| 58 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 59 |
switch (c) { |
| 60 |
OPTION_COMMON_HELP: |
| 61 |
return help(); |
| 62 |
|
| 63 |
OPTION_COMMON_VERSION: |
| 64 |
return version(); |
| 65 |
|
| 66 |
OPTION('q', "quiet", NULL, "Don't output anything, just set return value"): |
| 67 |
arg_quiet = true; |
| 68 |
break; |
| 69 |
|
| 70 |
OPTION('c', "container", NULL, "Only detect whether we are run in a container"): |
| 71 |
arg_mode = ONLY_CONTAINER; |
| 72 |
break; |
| 73 |
|
| 74 |
OPTION_LONG("private-users", NULL, "Only detect whether we are running in a user namespace"): |
| 75 |
arg_mode = ONLY_PRIVATE_USERS; |
| 76 |
break; |
| 77 |
|
| 78 |
OPTION('v', "vm", NULL, "Only detect whether we are run in a VM"): |
| 79 |
arg_mode = ONLY_VM; |
| 80 |
break; |
| 81 |
|
| 82 |
OPTION('r', "chroot", NULL, "Detect whether we are run in a chroot() environment"): |
| 83 |
arg_mode = ONLY_CHROOT; |
| 84 |
break; |
| 85 |
|
| 86 |
OPTION_LONG("list", NULL, "List all known and detectable types of virtualization"): |
| 87 |
return DUMP_STRING_TABLE(virtualization, Virtualization, _VIRTUALIZATION_MAX); |
| 88 |
|
| 89 |
OPTION_LONG("cvm", NULL, "Only detect whether we are run in a confidential VM"): |
| 90 |
arg_mode = ONLY_CVM; |
| 91 |
return 1; |
| 92 |
|
| 93 |
OPTION_LONG("list-cvm", NULL, "List all known and detectable types of confidential virtualization"): |
| 94 |
return DUMP_STRING_TABLE(confidential_virtualization, ConfidentialVirtualization, _CONFIDENTIAL_VIRTUALIZATION_MAX); |
| 95 |
} |
| 96 |
|
| 97 |
if (option_parser_get_n_args(&opts) > 0) |
| 98 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 99 |
"%s takes no arguments.", |
| 100 |
program_invocation_short_name); |
| 101 |
|
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
static int run(int argc, char *argv[]) { |
| 106 |
Virtualization v; |
| 107 |
ConfidentialVirtualization c; |
| 108 |
int r; |
| 109 |
|
| 110 |
/* This is mostly intended to be used for scripts which want |
| 111 |
* to detect whether we are being run in a virtualized |
| 112 |
* environment or not */ |
| 113 |
|
| 114 |
log_setup(); |
| 115 |
|
| 116 |
r = parse_argv(argc, argv); |
| 117 |
if (r <= 0) |
| 118 |
return r; |
| 119 |
|
| 120 |
switch (arg_mode) { |
| 121 |
case ONLY_VM: |
| 122 |
v = detect_vm(); |
| 123 |
if (v < 0) |
| 124 |
return log_error_errno(v, "Failed to check for VM: %m"); |
| 125 |
break; |
| 126 |
|
| 127 |
case ONLY_CONTAINER: |
| 128 |
v = detect_container(); |
| 129 |
if (v < 0) |
| 130 |
return log_error_errno(v, "Failed to check for container: %m"); |
| 131 |
break; |
| 132 |
|
| 133 |
case ONLY_CHROOT: |
| 134 |
r = running_in_chroot(); |
| 135 |
if (r < 0) |
| 136 |
return log_error_errno(r, "Failed to check for chroot() environment: %m"); |
| 137 |
return !r; |
| 138 |
|
| 139 |
case ONLY_PRIVATE_USERS: |
| 140 |
r = running_in_userns(); |
| 141 |
if (r < 0) |
| 142 |
return log_error_errno(r, "Failed to check for user namespace: %m"); |
| 143 |
return !r; |
| 144 |
|
| 145 |
case ONLY_CVM: |
| 146 |
c = detect_confidential_virtualization(); |
| 147 |
if (c < 0) |
| 148 |
return log_error_errno(c, "Failed to check for confidential virtualization: %m"); |
| 149 |
if (!arg_quiet) |
| 150 |
puts(confidential_virtualization_to_string(c)); |
| 151 |
return c == CONFIDENTIAL_VIRTUALIZATION_NONE; |
| 152 |
|
| 153 |
case ANY_VIRTUALIZATION: |
| 154 |
default: |
| 155 |
v = detect_virtualization(); |
| 156 |
if (v < 0) |
| 157 |
return log_error_errno(v, "Failed to check for virtualization: %m"); |
| 158 |
} |
| 159 |
|
| 160 |
if (!arg_quiet) |
| 161 |
puts(virtualization_to_string(v)); |
| 162 |
|
| 163 |
return v == VIRTUALIZATION_NONE; |
| 164 |
} |
| 165 |
|
| 166 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |