Skip to content

Commit d21be5f

Browse files
committed
detect-virt: add new --chroot switch to detect chroot() environments
1 parent 7f4b3c5 commit d21be5f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

man/systemd-detect-virt.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
</row>
140140

141141
<row>
142-
<entry morerows="5">container</entry>
142+
<entry morerows="5">Container</entry>
143143
<entry><varname>openvz</varname></entry>
144144
<entry>OpenVZ/Virtuozzo</entry>
145145
</row>
@@ -196,6 +196,18 @@
196196
hardware virtualization).</para></listitem>
197197
</varlistentry>
198198

199+
<varlistentry>
200+
<term><option>-r</option></term>
201+
<term><option>--chroot</option></term>
202+
203+
<listitem><para>Detect whether invoked in a
204+
<citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
205+
environment. In this mode no output is written, but the return
206+
value indicates whether the process was invoked in a
207+
<function>chroot()</function>
208+
environment or not.</para></listitem>
209+
</varlistentry>
210+
199211
<varlistentry>
200212
<term><option>-q</option></term>
201213
<term><option>--quiet</option></term>
@@ -221,7 +233,8 @@
221233
<title>See Also</title>
222234
<para>
223235
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
224-
<citerefentry><refentrytitle>systemd-nspawn</refentrytitle><manvolnum>1</manvolnum></citerefentry>
236+
<citerefentry><refentrytitle>systemd-nspawn</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
237+
<citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
225238
</para>
226239
</refsect1>
227240

src/detect-virt/detect-virt.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ static bool arg_quiet = false;
3131
static enum {
3232
ANY_VIRTUALIZATION,
3333
ONLY_VM,
34-
ONLY_CONTAINER
34+
ONLY_CONTAINER,
35+
ONLY_CHROOT,
3536
} arg_mode = ANY_VIRTUALIZATION;
3637

3738
static void help(void) {
@@ -41,6 +42,7 @@ static void help(void) {
4142
" --version Show package version\n"
4243
" -c --container Only detect whether we are run in a container\n"
4344
" -v --vm Only detect whether we are run in a VM\n"
45+
" -r --chroot Detect whether we are run in a chroot() environment\n"
4446
" -q --quiet Don't output anything, just set return value\n"
4547
, program_invocation_short_name);
4648
}
@@ -55,7 +57,8 @@ static int parse_argv(int argc, char *argv[]) {
5557
{ "help", no_argument, NULL, 'h' },
5658
{ "version", no_argument, NULL, ARG_VERSION },
5759
{ "container", no_argument, NULL, 'c' },
58-
{ "vm", optional_argument, NULL, 'v' },
60+
{ "vm", no_argument, NULL, 'v' },
61+
{ "chroot", no_argument, NULL, 'r' },
5962
{ "quiet", no_argument, NULL, 'q' },
6063
{}
6164
};
@@ -65,7 +68,7 @@ static int parse_argv(int argc, char *argv[]) {
6568
assert(argc >= 0);
6669
assert(argv);
6770

68-
while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
71+
while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
6972

7073
switch (c) {
7174

@@ -88,6 +91,10 @@ static int parse_argv(int argc, char *argv[]) {
8891
arg_mode = ONLY_VM;
8992
break;
9093

94+
case 'r':
95+
arg_mode = ONLY_CHROOT;
96+
break;
97+
9198
case '?':
9299
return -EINVAL;
93100

@@ -137,6 +144,15 @@ int main(int argc, char *argv[]) {
137144

138145
break;
139146

147+
case ONLY_CHROOT:
148+
r = running_in_chroot();
149+
if (r < 0) {
150+
log_error_errno(r, "Failed to check for chroot() environment: %m");
151+
return EXIT_FAILURE;
152+
}
153+
154+
return r ? EXIT_SUCCESS : EXIT_FAILURE;
155+
140156
case ANY_VIRTUALIZATION:
141157
default:
142158
r = detect_virtualization();

0 commit comments

Comments
 (0)