1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
7 #include "confidential-virt.h"
10 #include "pretty-print.h"
11 #include "string-table.h"
14 static bool arg_quiet
= false;
22 } arg_mode
= ANY_VIRTUALIZATION
;
24 static int help(void) {
25 _cleanup_free_
char *link
= NULL
;
28 r
= terminal_urlify_man("systemd-detect-virt", "1", &link
);
32 printf("%s [OPTIONS...]\n\n"
33 "Detect execution in a virtualized environment.\n\n"
34 " -h --help Show this help\n"
35 " --version Show package version\n"
36 " -c --container Only detect whether we are run in a container\n"
37 " -v --vm Only detect whether we are run in a VM\n"
38 " -r --chroot Detect whether we are run in a chroot() environment\n"
39 " --private-users Only detect whether we are running in a user namespace\n"
40 " --cvm Only detect whether we are run in a confidential VM\n"
41 " -q --quiet Don't output anything, just set return value\n"
42 " --list List all known and detectable types of virtualization\n"
43 " --list-cvm List all known and detectable types of confidential \n"
45 "\nSee the %s for details.\n",
46 program_invocation_short_name
,
52 static int parse_argv(int argc
, char *argv
[]) {
62 static const struct option options
[] = {
63 { "help", no_argument
, NULL
, 'h' },
64 { "version", no_argument
, NULL
, ARG_VERSION
},
65 { "container", no_argument
, NULL
, 'c' },
66 { "vm", no_argument
, NULL
, 'v' },
67 { "chroot", no_argument
, NULL
, 'r' },
68 { "private-users", no_argument
, NULL
, ARG_PRIVATE_USERS
},
69 { "quiet", no_argument
, NULL
, 'q' },
70 { "cvm", no_argument
, NULL
, ARG_CVM
},
71 { "list", no_argument
, NULL
, ARG_LIST
},
72 { "list-cvm", no_argument
, NULL
, ARG_LIST_CVM
},
81 while ((c
= getopt_long(argc
, argv
, "hqcvr", options
, NULL
)) >= 0)
96 arg_mode
= ONLY_CONTAINER
;
99 case ARG_PRIVATE_USERS
:
100 arg_mode
= ONLY_PRIVATE_USERS
;
108 arg_mode
= ONLY_CHROOT
;
112 DUMP_STRING_TABLE(virtualization
, Virtualization
, _VIRTUALIZATION_MAX
);
120 DUMP_STRING_TABLE(confidential_virtualization
, ConfidentialVirtualization
, _CONFIDENTIAL_VIRTUALIZATION_MAX
);
127 assert_not_reached();
131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
132 "%s takes no arguments.",
133 program_invocation_short_name
);
138 static int run(int argc
, char *argv
[]) {
140 ConfidentialVirtualization c
;
143 /* This is mostly intended to be used for scripts which want
144 * to detect whether we are being run in a virtualized
145 * environment or not */
149 r
= parse_argv(argc
, argv
);
157 return log_error_errno(v
, "Failed to check for VM: %m");
161 v
= detect_container();
163 return log_error_errno(v
, "Failed to check for container: %m");
167 r
= running_in_chroot();
169 return log_error_errno(r
, "Failed to check for chroot() environment: %m");
172 case ONLY_PRIVATE_USERS
:
173 r
= running_in_userns();
175 return log_error_errno(r
, "Failed to check for user namespace: %m");
179 c
= detect_confidential_virtualization();
181 return log_error_errno(c
, "Failed to check for confidential virtualization: %m");
183 puts(confidential_virtualization_to_string(c
));
184 return c
== CONFIDENTIAL_VIRTUALIZATION_NONE
;
186 case ANY_VIRTUALIZATION
:
188 v
= detect_virtualization();
190 return log_error_errno(v
, "Failed to check for virtualization: %m");
194 puts(virtualization_to_string(v
));
196 return v
== VIRTUALIZATION_NONE
;
199 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);