chiark / gitweb /
man: systemd.service(5): clarify behavior of SuccessExitStatus
[elogind.git] / man / systemd-nspawn.xml
1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3         "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4
5 <!--
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 -->
23
24 <refentry id="systemd-nspawn">
25
26         <refentryinfo>
27                 <title>systemd-nspawn</title>
28                 <productname>systemd</productname>
29
30                 <authorgroup>
31                         <author>
32                                 <contrib>Developer</contrib>
33                                 <firstname>Lennart</firstname>
34                                 <surname>Poettering</surname>
35                                 <email>lennart@poettering.net</email>
36                         </author>
37                 </authorgroup>
38         </refentryinfo>
39
40         <refmeta>
41                 <refentrytitle>systemd-nspawn</refentrytitle>
42                 <manvolnum>1</manvolnum>
43         </refmeta>
44
45         <refnamediv>
46                 <refname>systemd-nspawn</refname>
47                 <refpurpose>Spawn a namespace container for debugging, testing and building</refpurpose>
48         </refnamediv>
49
50         <refsynopsisdiv>
51                 <cmdsynopsis>
52                         <command>systemd-nspawn</command>
53                         <arg choice="opt" rep="repeat">OPTIONS</arg>
54                         <arg choice="opt"><replaceable>COMMAND</replaceable>
55                         <arg choice="opt" rep="repeat">ARGS</arg>
56                         </arg>
57                 </cmdsynopsis>
58                 <cmdsynopsis>
59                         <command>systemd-nspawn</command>
60                         <arg choice="plain">-b</arg>
61                         <arg choice="opt" rep="repeat">OPTIONS</arg>
62                         <arg choice="opt" rep="repeat">ARGS</arg>
63                 </cmdsynopsis>
64         </refsynopsisdiv>
65
66         <refsect1>
67                 <title>Description</title>
68
69                 <para><command>systemd-nspawn</command> may be used to
70                 run a command or OS in a light-weight namespace
71                 container. In many ways it is similar to
72                 <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
73                 but more powerful since it fully virtualizes the file
74                 system hierarchy, as well as the process tree, the
75                 various IPC subsystems and the host and domain
76                 name.</para>
77
78                 <para><command>systemd-nspawn</command> limits access
79                 to various kernel interfaces in the container to
80                 read-only, such as <filename>/sys</filename>,
81                 <filename>/proc/sys</filename> or
82                 <filename>/sys/fs/selinux</filename>. Network
83                 interfaces and the system clock may not be changed
84                 from within the container. Device nodes may not be
85                 created. The host system cannot be rebooted and kernel
86                 modules may not be loaded from within the
87                 container.</para>
88
89                 <para>Note that even though these security precautions
90                 are taken <command>systemd-nspawn</command> is not
91                 suitable for secure container setups. Many of the
92                 security features may be circumvented and are hence
93                 primarily useful to avoid accidental changes to the
94                 host system from the container. The intended use of
95                 this program is debugging and testing as well as
96                 building of packages, distributions and software
97                 involved with boot and systems management.</para>
98
99                 <para>In contrast to
100                 <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry> <command>systemd-nspawn</command>
101                 may be used to boot full Linux-based operating systems
102                 in a container.</para>
103
104                 <para>Use a tool like
105                 <citerefentry><refentrytitle>yum</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
106                 <citerefentry><refentrytitle>debootstrap</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
107                 or
108                 <citerefentry><refentrytitle>pacman</refentrytitle><manvolnum>8</manvolnum></citerefentry>
109                 to set up an OS directory tree suitable as file system
110                 hierarchy for <command>systemd-nspawn</command>
111                 containers.</para>
112
113                 <para>Note that <command>systemd-nspawn</command> will
114                 mount file systems private to the container to
115                 <filename>/dev</filename>,
116                 <filename>/run</filename> and similar. These will
117                 not be visible outside of the container, and their
118                 contents will be lost when the container exits.</para>
119
120                 <para>Note that running two
121                 <command>systemd-nspawn</command> containers from the
122                 same directory tree will not make processes in them
123                 see each other. The PID namespace separation of the
124                 two containers is complete and the containers will
125                 share very few runtime objects except for the
126                 underlying file system. Use
127                 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
128                 <command>login</command> command to request an
129                 additional login prompt in a running container.</para>
130
131                 <para><command>systemd-nspawn</command> implements the
132                 <ulink
133                 url="http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface">Container
134                 Interface</ulink> specification.</para>
135
136                 <para>As a safety check
137                 <command>systemd-nspawn</command> will verify the
138                 existence of <filename>/etc/os-release</filename> in
139                 the container tree before starting the container (see
140                 <citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>). It
141                 might be necessary to add this file to the container
142                 tree manually if the OS of the container is too old to
143                 contain this file out-of-the-box.</para>
144         </refsect1>
145
146         <refsect1>
147                 <title>Options</title>
148
149                 <para>If option <option>-b</option> is specified, the
150                 arguments are used as arguments for the init
151                 binary. Otherwise, <replaceable>COMMAND</replaceable>
152                 specifies the program to launch in the container, and
153                 the remaining arguments are used as arguments for this
154                 program. If <option>-b</option> is not used and no
155                 arguments are specifed, a shell is launched in the
156                 container.</para>
157
158                 <para>The following options are understood:</para>
159
160                 <variablelist>
161                         <varlistentry>
162                                 <term><option>-h</option></term>
163                                 <term><option>--help</option></term>
164
165                                 <listitem><para>Prints a short help
166                                 text and exits.</para></listitem>
167                         </varlistentry>
168
169                         <varlistentry>
170                                 <term><option>--version</option></term>
171
172                                 <listitem><para>Prints a version string
173                                 and exits.</para></listitem>
174                         </varlistentry>
175
176                         <varlistentry>
177                                 <term><option>-q</option></term>
178                                 <term><option>--quiet</option></term>
179
180                                 <listitem><para>Turns off any status
181                                 output by the tool itself. When this
182                                 switch is used, then the only output
183                                 by nspawn will be the console output
184                                 of the container OS itself.</para></listitem>
185                         </varlistentry>
186
187                         <varlistentry>
188                                 <term><option>-D</option></term>
189                                 <term><option>--directory=</option></term>
190
191                                 <listitem><para>Directory to use as
192                                 file system root for the namespace
193                                 container. If omitted, the current
194                                 directory will be
195                                 used.</para></listitem>
196                         </varlistentry>
197
198                         <varlistentry>
199                                 <term><option>-b</option></term>
200                                 <term><option>--boot</option></term>
201
202                                 <listitem><para>Automatically search
203                                 for an init binary and invoke it
204                                 instead of a shell or a user supplied
205                                 program. If this option is used,
206                                 arguments specified on the command
207                                 line are used as arguments for the
208                                 init binary. This option may not be
209                                 combined with
210                                 <option>--share-system</option>.
211                                 </para></listitem>
212                         </varlistentry>
213
214                         <varlistentry>
215                                 <term><option>-u</option></term>
216                                 <term><option>--user=</option></term>
217
218                                 <listitem><para>Run the command
219                                 under specified user, create home
220                                 directory and cd into it. As rest
221                                 of systemd-nspawn, this is not
222                                 the security feature and limits
223                                 against accidental changes only.
224                                 </para></listitem>
225                         </varlistentry>
226
227                         <varlistentry>
228                                 <term><option>-M</option></term>
229                                 <term><option>--machine=</option></term>
230
231                                 <listitem><para>Sets the machine name
232                                 for this container. This name may be
233                                 used to identify this container on the
234                                 host, and is used to initialize the
235                                 container's hostname (which the
236                                 container can choose to override,
237                                 however). If not specified, the last
238                                 component of the root directory of the
239                                 container is used.</para></listitem>
240                         </varlistentry>
241
242                         <varlistentry>
243                                 <term><option>--uuid=</option></term>
244
245                                 <listitem><para>Set the specified UUID
246                                 for the container. The init system
247                                 will initialize
248                                 <filename>/etc/machine-id</filename>
249                                 from this if this file is not set yet.
250                                 </para></listitem>
251                         </varlistentry>
252
253                         <varlistentry>
254                                 <term><option>--slice=</option></term>
255
256                                 <listitem><para>Make the container
257                                 part of the specified slice, instead
258                                 of the default
259                                 <filename>machine.slice</filename>.</para>
260                                 </listitem>
261                         </varlistentry>
262
263                         <varlistentry>
264                                 <term><option>--private-network</option></term>
265
266                                 <listitem><para>Disconnect networking
267                                 of the container from the host. This
268                                 makes all network interfaces
269                                 unavailable in the container, with the
270                                 exception of the loopback device and
271                                 those specified with
272                                 <option>--network-interface=</option>
273                                 and configured ith
274                                 <option>--network-veth</option>. If
275                                 this option is specified the
276                                 CAP_NET_ADMIN capability will be added
277                                 to the set of capabilities the
278                                 container retains. The latter may be
279                                 disabled by using
280                                 <option>--drop-capability=</option>.</para></listitem>
281                         </varlistentry>
282
283                         <varlistentry>
284                                 <term><option>--network-interface=</option></term>
285
286                                 <listitem><para>Assign the specified
287                                 network interface to the
288                                 container. This will move the
289                                 specified interface from the calling
290                                 namespace and place it in the
291                                 container. When the container
292                                 terminates it is moved back to the
293                                 host namespace. Note that
294                                 <option>--network-interface=</option>
295                                 implies
296                                 <option>--private-network</option>. This
297                                 option may be used more than once to
298                                 add multiple network interfaces to the
299                                 container.</para></listitem>
300                         </varlistentry>
301
302                         <varlistentry>
303                                 <term><option>--network-veth</option></term>
304
305                                 <listitem><para>Create a virtual
306                                 ethernet link between host and
307                                 container. The host side of the
308                                 ethernet link will be available as
309                                 network interface named after the
310                                 container's name (as specified with
311                                 <option>--machine=</option>), prefixed
312                                 with <literal>ve-</literal>. The
313                                 container side of the the ethernet
314                                 link will be named
315                                 <literal>host0</literal>. Note that
316                                 <option>--network-veth</option>
317                                 implies
318                                 <option>--private-network</option>.</para></listitem>
319                         </varlistentry>
320
321                         <varlistentry>
322                                 <term><option>-Z</option></term>
323                                 <term><option>--selinux-context=</option></term>
324
325                                 <listitem><para>Sets the SELinux
326                                 security context to be used to label
327                                 processes in the container.</para>
328                                 </listitem>
329                         </varlistentry>
330
331                         <varlistentry>
332                                 <term><option>-L</option></term>
333                                 <term><option>--selinux-apifs-context=</option></term>
334
335                                 <listitem><para>Sets the SELinux security
336                                 context to be used to label files in
337                                 the virtual API file systems in the
338                                 container.</para>
339                                 </listitem>
340                         </varlistentry>
341
342                         <varlistentry>
343                                 <term><option>--capability=</option></term>
344
345                                 <listitem><para>List one or more
346                                 additional capabilities to grant the
347                                 container. Takes a comma-separated
348                                 list of capability names, see
349                                 <citerefentry><refentrytitle>capabilities</refentrytitle><manvolnum>7</manvolnum></citerefentry>
350                                 for more information. Note that the
351                                 following capabilities will be granted
352                                 in any way: CAP_CHOWN,
353                                 CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH,
354                                 CAP_FOWNER, CAP_FSETID, CAP_IPC_OWNER,
355                                 CAP_KILL, CAP_LEASE,
356                                 CAP_LINUX_IMMUTABLE,
357                                 CAP_NET_BIND_SERVICE,
358                                 CAP_NET_BROADCAST, CAP_NET_RAW,
359                                 CAP_SETGID, CAP_SETFCAP, CAP_SETPCAP,
360                                 CAP_SETUID, CAP_SYS_ADMIN,
361                                 CAP_SYS_CHROOT, CAP_SYS_NICE,
362                                 CAP_SYS_PTRACE, CAP_SYS_TTY_CONFIG,
363                                 CAP_SYS_RESOURCE, CAP_SYS_BOOT,
364                                 CAP_AUDIT_WRITE,
365                                 CAP_AUDIT_CONTROL. Also CAP_NET_ADMIN
366                                 is retained if
367                                 <option>--private-network</option> is
368                                 specified. If the special value
369                                 <literal>all</literal> is passed all
370                                 capabilities are
371                                 retained.</para></listitem>
372                         </varlistentry>
373
374                         <varlistentry>
375                                 <term><option>--drop-capability=</option></term>
376
377                                 <listitem><para>Specify one or more
378                                 additional capabilities to drop for
379                                 the container. This allows running the
380                                 container with fewer capabilities than
381                                 the default (see above).</para></listitem>
382                         </varlistentry>
383
384                         <varlistentry>
385                                 <term><option>--link-journal=</option></term>
386
387                                 <listitem><para>Control whether the
388                                 container's journal shall be made
389                                 visible to the host system. If enabled,
390                                 allows viewing the container's journal
391                                 files from the host (but not vice
392                                 versa). Takes one of
393                                 <literal>no</literal>,
394                                 <literal>host</literal>,
395                                 <literal>guest</literal>,
396                                 <literal>auto</literal>. If
397                                 <literal>no</literal>, the journal is
398                                 not linked. If <literal>host</literal>,
399                                 the journal files are stored on the
400                                 host file system (beneath
401                                 <filename>/var/log/journal/<replaceable>machine-id</replaceable></filename>)
402                                 and the subdirectory is bind-mounted
403                                 into the container at the same
404                                 location. If <literal>guest</literal>,
405                                 the journal files are stored on the
406                                 guest file system (beneath
407                                 <filename>/var/log/journal/<replaceable>machine-id</replaceable></filename>)
408                                 and the subdirectory is symlinked into the host
409                                 at the same location. If
410                                 <literal>auto</literal> (the default),
411                                 and the right subdirectory of
412                                 <filename>/var/log/journal</filename>
413                                 exists, it will be bind mounted
414                                 into the container. If the
415                                 subdirectory does not exist, no
416                                 linking is performed. Effectively,
417                                 booting a container once with
418                                 <literal>guest</literal> or
419                                 <literal>host</literal> will link the
420                                 journal persistently if further on
421                                 the default of <literal>auto</literal>
422                                 is used.</para></listitem>
423                         </varlistentry>
424
425                         <varlistentry>
426                                 <term><option>-j</option></term>
427
428                                 <listitem><para>Equivalent to
429                                 <option>--link-journal=guest</option>.</para></listitem>
430                         </varlistentry>
431
432                         <varlistentry>
433                                 <term><option>--read-only</option></term>
434
435                                 <listitem><para>Mount the root file
436                                 system read-only for the
437                                 container.</para></listitem>
438                         </varlistentry>
439
440                         <varlistentry>
441                                 <term><option>--bind=</option></term>
442                                 <term><option>--bind-ro=</option></term>
443
444                                 <listitem><para>Bind mount a file or
445                                 directory from the host into the
446                                 container. Either takes a path
447                                 argument -- in which case the
448                                 specified path will be mounted from
449                                 the host to the same path in the
450                                 container --, or a colon-separated
451                                 pair of paths -- in which case the
452                                 first specified path is the source in
453                                 the host, and the second path is the
454                                 destination in the container. The
455                                 <option>--bind-ro=</option> option
456                                 creates read-only bind
457                                 mount.</para></listitem>
458                         </varlistentry>
459
460                         <varlistentry>
461                                 <term><option>--setenv=</option></term>
462
463                                 <listitem><para>Specifies an
464                                 environment variable assignment to
465                                 pass to the init process in the
466                                 container, in the format
467                                 <literal>NAME=VALUE</literal>. This
468                                 may be used to override the default
469                                 variables or to set additional
470                                 variables. This parameter may be used
471                                 more than once.</para></listitem>
472                         </varlistentry>
473
474                         <varlistentry>
475                                 <term><option>--share-system</option></term>
476
477                                 <listitem><para>Allows the container
478                                 to share certain system facilities
479                                 with the host. More specifically, this
480                                 turns off PID namespacing, UTS
481                                 namespacing and IPC namespacing, and
482                                 thus allows the guest to see and
483                                 interact more easily with processes
484                                 outside of the container. Note that
485                                 using this option makes it impossible
486                                 to start up a full Operating System in
487                                 the container, as an init system
488                                 cannot operate in this mode. It is
489                                 only useful to run specific programs
490                                 or applications this way, without
491                                 involving an init system in the
492                                 container. This option implies
493                                 <option>--register=no</option>. This
494                                 option may not be combined with
495                                 <option>--boot</option>.</para></listitem>
496                         </varlistentry>
497
498                         <varlistentry>
499                                 <term><option>--register=</option></term>
500
501                                 <listitem><para>Controls whether the
502                                 container is registered with
503                                 <citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. Takes
504                                 a boolean argument, defaults to
505                                 <literal>yes</literal>. This option
506                                 should be enabled when the container
507                                 runs a full Operating System (more
508                                 specifically: an init system), and is
509                                 useful to ensure that the container is
510                                 accessible via
511                                 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
512                                 and shown by tools such as
513                                 <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>1</manvolnum></citerefentry>. If
514                                 the container does not run an init
515                                 system it is recommended to set this
516                                 option to <literal>no</literal>. Note
517                                 that <option>--share-system</option>
518                                 implies
519                                 <option>--register=no</option>.
520                                 </para></listitem>
521                         </varlistentry>
522
523                         <varlistentry>
524                                 <term><option>--keep-unit</option></term>
525
526                                 <listitem><para>Instead of creating a
527                                 transient scope unit to run the
528                                 container in, simply register the
529                                 service or scope unit
530                                 <command>systemd-nspawn</command> has
531                                 been invoked in in
532                                 <citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. This
533                                 has no effect if
534                                 <option>--register=no</option> is
535                                 used. This switch should be used if
536                                 <command>systemd-nspawn</command> is
537                                 invoked from within an a service unit,
538                                 and the service unit's sole purpose
539                                 is to run a single
540                                 <command>systemd-nspawn</command>
541                                 container. This option is not
542                                 available if run from a user
543                                 session.</para></listitem>
544                         </varlistentry>
545
546                 </variablelist>
547
548         </refsect1>
549
550         <refsect1>
551                 <title>Example 1</title>
552
553                 <programlisting># yum -y --releasever=19 --nogpg --installroot=/srv/mycontainer --disablerepo='*' --enablerepo=fedora install systemd passwd yum fedora-release vim-minimal
554 # systemd-nspawn -bD /srv/mycontainer</programlisting>
555
556                 <para>This installs a minimal Fedora distribution into
557                 the directory <filename noindex='true'>/srv/mycontainer/</filename> and
558                 then boots an OS in a namespace container in
559                 it.</para>
560         </refsect1>
561
562         <refsect1>
563                 <title>Example 2</title>
564
565                 <programlisting># debootstrap --arch=amd64 unstable ~/debian-tree/
566 # systemd-nspawn -D ~/debian-tree/</programlisting>
567
568                 <para>This installs a minimal Debian unstable
569                 distribution into the directory
570                 <filename>~/debian-tree/</filename> and then spawns a
571                 shell in a namespace container in it.</para>
572         </refsect1>
573
574         <refsect1>
575                 <title>Example 3</title>
576
577                 <programlisting># pacstrap -c -d ~/arch-tree/ base
578 # systemd-nspawn -bD ~/arch-tree/</programlisting>
579
580                 <para>This installs a mimimal Arch Linux distribution into
581                 the directory <filename>~/arch-tree/</filename> and then
582                 boots an OS in a namespace container in it.</para>
583         </refsect1>
584
585         <refsect1>
586                 <title>Example 4</title>
587
588                 <programlisting># mv ~/arch-tree /var/lib/container/arch
589 # systemctl enable systemd-nspawn@arch.service
590 # systemctl start systemd-nspawn@arch.service</programlisting>
591
592                 <para>This makes the Arch Linux container part of the
593                 <filename>multi-user.target</filename> on the host.
594                 </para>
595         </refsect1>
596
597         <refsect1>
598                 <title>Example 5</title>
599
600                 <programlisting># btrfs subvolume snapshot / /.tmp
601 # systemd-nspawn --private-network -D /.tmp -b</programlisting>
602
603                 <para>This runs a copy of the host system in a
604                 btrfs snapshot.</para>
605         </refsect1>
606
607         <refsect1>
608                 <title>Example 6</title>
609
610                 <programlisting># chcon system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -R /srv/container
611 # systemd-nspawn -L system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -Z system_u:system_r:svirt_lxc_net_t:s0:c0,c1 -D /srv/container /bin/sh</programlisting>
612
613                 <para>This runs a container with SELinux sandbox security contexts.</para>
614         </refsect1>
615
616         <refsect1>
617                 <title>Exit status</title>
618
619                 <para>The exit code of the program executed in the
620                 container is returned.</para>
621         </refsect1>
622
623         <refsect1>
624                 <title>See Also</title>
625                 <para>
626                         <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
627                         <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
628                         <citerefentry><refentrytitle>yum</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
629                         <citerefentry><refentrytitle>debootstrap</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
630                         <citerefentry><refentrytitle>pacman</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
631                         <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
632                         <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
633                 </para>
634         </refsect1>
635
636 </refentry>