chiark / gitweb /
util: modernize readlink_malloc() a bit
[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>Incompatibility with Auditing</title>
148
149                 <para>Note that the kernel auditing subsystem is
150                 currently broken when used together with
151                 containers. We hence recommend turning it off entirely
152                 by booting with <literal>audit=0</literal> on the
153                 kernel command line, or by turning it off at kernel
154                 build time. If auditing is enabled in the kernel,
155                 operating systems booted in an nspawn container might
156                 refuse log-in attempts.</para>
157         </refsect1>
158
159         <refsect1>
160                 <title>Options</title>
161
162                 <para>If option <option>-b</option> is specified, the
163                 arguments are used as arguments for the init
164                 binary. Otherwise, <replaceable>COMMAND</replaceable>
165                 specifies the program to launch in the container, and
166                 the remaining arguments are used as arguments for this
167                 program. If <option>-b</option> is not used and no
168                 arguments are specifed, a shell is launched in the
169                 container.</para>
170
171                 <para>The following options are understood:</para>
172
173                 <variablelist>
174                         <varlistentry>
175                                 <term><option>-h</option></term>
176                                 <term><option>--help</option></term>
177
178                                 <listitem><para>Prints a short help
179                                 text and exits.</para></listitem>
180                         </varlistentry>
181
182                         <varlistentry>
183                                 <term><option>--version</option></term>
184
185                                 <listitem><para>Prints a version string
186                                 and exits.</para></listitem>
187                         </varlistentry>
188
189                         <varlistentry>
190                                 <term><option>-D</option></term>
191                                 <term><option>--directory=</option></term>
192
193                                 <listitem><para>Directory to use as
194                                 file system root for the namespace
195                                 container. If omitted, the current
196                                 directory will be
197                                 used.</para></listitem>
198                         </varlistentry>
199
200                         <varlistentry>
201                                 <term><option>-b</option></term>
202                                 <term><option>--boot</option></term>
203
204                                 <listitem><para>Automatically search
205                                 for an init binary and invoke it
206                                 instead of a shell or a user supplied
207                                 program. If this option is used,
208                                 arguments specified on the command
209                                 line are used as arguments for the
210                                 init binary. This option may not be
211                                 combined with
212                                 <option>--share-system</option>.
213                                 </para></listitem>
214                         </varlistentry>
215
216                         <varlistentry>
217                                 <term><option>-u</option></term>
218                                 <term><option>--user=</option></term>
219
220                                 <listitem><para>Run the command
221                                 under specified user, create home
222                                 directory and cd into it. As rest
223                                 of systemd-nspawn, this is not
224                                 the security feature and limits
225                                 against accidental changes only.
226                                 </para></listitem>
227                         </varlistentry>
228
229                         <varlistentry>
230                                 <term><option>-M</option></term>
231                                 <term><option>--machine=</option></term>
232
233                                 <listitem><para>Sets the machine name
234                                 for this container. This name may be
235                                 used to identify this container on the
236                                 host, and is used to initialize the
237                                 container's hostname (which the
238                                 container can choose to override,
239                                 however). If not specified, the last
240                                 component of the root directory of the
241                                 container is used.</para></listitem>
242                         </varlistentry>
243
244                         <varlistentry>
245                                 <term><option>--slice=</option></term>
246
247                                 <listitem><para>Make the container
248                                 part of the specified slice, instead
249                                 of the
250                                 <filename>machine.slice</filename>.</para>
251                                 </listitem>
252                         </varlistentry>
253
254                         <varlistentry>
255                                 <term><option>-Z</option></term>
256                                 <term><option>--selinux-context=</option></term>
257
258                                 <listitem><para>Sets the SELinux
259                                 security context to be used to label
260                                 processes in the container.</para>
261                                 </listitem>
262                         </varlistentry>
263
264                         <varlistentry>
265                                 <term><option>-L</option></term>
266                                 <term><option>--selinux-apifs-context=</option></term>
267
268                                 <listitem><para>Sets the SELinux security
269                                 context to be used to label files in
270                                 the virtual API file systems in the
271                                 container.</para>
272                                 </listitem>
273                         </varlistentry>
274
275                         <varlistentry>
276                                 <term><option>--uuid=</option></term>
277
278                                 <listitem><para>Set the specified UUID
279                                 for the container. The init system
280                                 will initialize
281                                 <filename>/etc/machine-id</filename>
282                                 from this if this file is not set yet.
283                                 </para></listitem>
284                         </varlistentry>
285
286                         <varlistentry>
287                                 <term><option>--private-network</option></term>
288
289                                 <listitem><para>Turn off networking in
290                                 the container. This makes all network
291                                 interfaces unavailable in the
292                                 container, with the exception of the
293                                 loopback device.</para></listitem>
294                         </varlistentry>
295
296                         <varlistentry>
297                                 <term><option>--read-only</option></term>
298
299                                 <listitem><para>Mount the root file
300                                 system read-only for the
301                                 container.</para></listitem>
302                         </varlistentry>
303
304                         <varlistentry>
305                                 <term><option>--capability=</option></term>
306
307                                 <listitem><para>List one or more
308                                 additional capabilities to grant the
309                                 container. Takes a comma-separated
310                                 list of capability names, see
311                                 <citerefentry><refentrytitle>capabilities</refentrytitle><manvolnum>7</manvolnum></citerefentry>
312                                 for more information. Note that the
313                                 following capabilities will be granted
314                                 in any way: CAP_CHOWN,
315                                 CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH,
316                                 CAP_FOWNER, CAP_FSETID, CAP_IPC_OWNER,
317                                 CAP_KILL, CAP_LEASE,
318                                 CAP_LINUX_IMMUTABLE,
319                                 CAP_NET_BIND_SERVICE,
320                                 CAP_NET_BROADCAST, CAP_NET_RAW,
321                                 CAP_SETGID, CAP_SETFCAP, CAP_SETPCAP,
322                                 CAP_SETUID, CAP_SYS_ADMIN,
323                                 CAP_SYS_CHROOT, CAP_SYS_NICE,
324                                 CAP_SYS_PTRACE, CAP_SYS_TTY_CONFIG,
325                                 CAP_SYS_RESOURCE, CAP_SYS_BOOT,
326                                 CAP_AUDIT_WRITE,
327                                 CAP_AUDIT_CONTROL.</para></listitem>
328                         </varlistentry>
329
330                         <varlistentry>
331                                 <term><option>--drop-capability=</option></term>
332
333                                 <listitem><para>Specify one or more
334                                 additional capabilities to drop for
335                                 the container. This allows running the
336                                 container with fewer capabilities than
337                                 the default (see above).</para></listitem>
338                         </varlistentry>
339
340                         <varlistentry>
341                                 <term><option>--link-journal=</option></term>
342
343                                 <listitem><para>Control whether the
344                                 container's journal shall be made
345                                 visible to the host system. If enabled,
346                                 allows viewing the container's journal
347                                 files from the host (but not vice
348                                 versa). Takes one of
349                                 <literal>no</literal>,
350                                 <literal>host</literal>,
351                                 <literal>guest</literal>,
352                                 <literal>auto</literal>. If
353                                 <literal>no</literal>, the journal is
354                                 not linked. If <literal>host</literal>,
355                                 the journal files are stored on the
356                                 host file system (beneath
357                                 <filename>/var/log/journal/<replaceable>machine-id</replaceable></filename>)
358                                 and the subdirectory is bind-mounted
359                                 into the container at the same
360                                 location. If <literal>guest</literal>,
361                                 the journal files are stored on the
362                                 guest file system (beneath
363                                 <filename>/var/log/journal/<replaceable>machine-id</replaceable></filename>)
364                                 and the subdirectory is symlinked into the host
365                                 at the same location. If
366                                 <literal>auto</literal> (the default),
367                                 and the right subdirectory of
368                                 <filename>/var/log/journal</filename>
369                                 exists, it will be bind mounted
370                                 into the container. If the
371                                 subdirectory does not exist, no
372                                 linking is performed. Effectively,
373                                 booting a container once with
374                                 <literal>guest</literal> or
375                                 <literal>host</literal> will link the
376                                 journal persistently if further on
377                                 the default of <literal>auto</literal>
378                                 is used.</para></listitem>
379                         </varlistentry>
380
381                         <varlistentry>
382                                 <term><option>-j</option></term>
383
384                                 <listitem><para>Equivalent to
385                                 <option>--link-journal=guest</option>.</para></listitem>
386                         </varlistentry>
387
388                         <varlistentry>
389                                 <term><option>--bind=</option></term>
390                                 <term><option>--bind-ro=</option></term>
391
392                                 <listitem><para>Bind mount a file or
393                                 directory from the host into the
394                                 container. Either takes a path
395                                 argument -- in which case the
396                                 specified path will be mounted from
397                                 the host to the same path in the
398                                 container --, or a colon-separated
399                                 pair of paths -- in which case the
400                                 first specified path is the source in
401                                 the host, and the second path is the
402                                 destination in the container. The
403                                 <option>--bind-ro=</option> option
404                                 creates read-only bind
405                                 mount.</para></listitem>
406                         </varlistentry>
407
408                         <varlistentry>
409                                 <term><option>--setenv=</option></term>
410
411                                 <listitem><para>Specifies an
412                                 environment variable assignment to
413                                 pass to the init process in the
414                                 container, in the format
415                                 <literal>NAME=VALUE</literal>. This
416                                 may be used to override the default
417                                 variables or to set additional
418                                 variables. This parameter may be used
419                                 more than once.</para></listitem>
420                         </varlistentry>
421
422                         <varlistentry>
423                                 <term><option>-q</option></term>
424                                 <term><option>--quiet</option></term>
425
426                                 <listitem><para>Turns off any status
427                                 output by the tool itself. When this
428                                 switch is used, then the only output
429                                 by nspawn will be the console output
430                                 of the container OS itself.</para></listitem>
431                         </varlistentry>
432
433                         <varlistentry>
434                                 <term><option>--share-system</option></term>
435
436                                 <listitem><para>Allows the container
437                                 to share certain system facilities
438                                 with the host. More specifically, this
439                                 turns off PID namespacing, UTS
440                                 namespacing and IPC namespacing, and
441                                 thus allows the guest to see and
442                                 interact more easily with processes
443                                 outside of the container. Note that
444                                 using this option makes it impossible
445                                 to start up a full Operating System in
446                                 the container, as an init system
447                                 cannot operate in this mode. It is
448                                 only useful to run specific programs
449                                 or applications this way, without
450                                 involving an init system in the
451                                 container. This option implies
452                                 <option>--register=no</option>. This
453                                 option may not be combined with
454                                 <option>--boot</option>.</para></listitem>
455                         </varlistentry>
456
457                         <varlistentry>
458                                 <term><option>--register=</option></term>
459
460                                 <listitem><para>Controls whether the
461                                 container is registered with
462                                 <citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. Takes
463                                 a boolean argument, defaults to
464                                 <literal>yes</literal>. This option
465                                 should be enabled when the container
466                                 runs a full Operating System (more
467                                 specifically: an init system), and is
468                                 useful to ensure the container is
469                                 accesible via
470                                 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
471                                 and shown by tools such as
472                                 <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>1</manvolnum></citerefentry>. If
473                                 the container does not run an init
474                                 system it is recommended to set this
475                                 option to <literal>no</literal>. Note
476                                 that <option>--share-system</option>
477                                 implies
478                                 <option>--register=no</option>.
479                                 </para></listitem>
480                         </varlistentry>
481                 </variablelist>
482
483         </refsect1>
484
485         <refsect1>
486                 <title>Example 1</title>
487
488                 <programlisting># yum -y --releasever=19 --nogpg --installroot=/srv/mycontainer --disablerepo='*' --enablerepo=fedora install systemd passwd yum fedora-release vim-minimal
489 # systemd-nspawn -bD /srv/mycontainer</programlisting>
490
491                 <para>This installs a minimal Fedora distribution into
492                 the directory <filename noindex='true'>/srv/mycontainer/</filename> and
493                 then boots an OS in a namespace container in
494                 it.</para>
495         </refsect1>
496
497         <refsect1>
498                 <title>Example 2</title>
499
500                 <programlisting># debootstrap --arch=amd64 unstable ~/debian-tree/
501 # systemd-nspawn -D ~/debian-tree/</programlisting>
502
503                 <para>This installs a minimal Debian unstable
504                 distribution into the directory
505                 <filename>~/debian-tree/</filename> and then spawns a
506                 shell in a namespace container in it.</para>
507         </refsect1>
508
509         <refsect1>
510                 <title>Example 3</title>
511
512                 <programlisting># pacstrap -c -d ~/arch-tree/ base
513 # systemd-nspawn -bD ~/arch-tree/</programlisting>
514
515                 <para>This installs a mimimal Arch Linux distribution into
516                 the directory <filename>~/arch-tree/</filename> and then
517                 boots an OS in a namespace container in it.</para>
518         </refsect1>
519
520         <refsect1>
521                 <title>Example 4</title>
522
523                 <programlisting># mv ~/arch-tree /var/lib/container/arch
524 # systemctl enable systemd-nspawn@arch.service
525 # systemctl start systemd-nspawn@arch.service</programlisting>
526
527                 <para>This makes the Arch Linux container part of the
528                 <filename>multi-user.target</filename> on the host.
529                 </para>
530         </refsect1>
531
532         <refsect1>
533                 <title>Example 5</title>
534
535                 <programlisting># btrfs subvolume snapshot / /.tmp
536 # systemd-nspawn --private-network -D /.tmp -b</programlisting>
537
538                 <para>This runs a copy of the host system in a
539                 btrfs snapshot.</para>
540         </refsect1>
541
542         <refsect1>
543                 <title>Example 6</title>
544
545                 <programlisting># chcon system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -R /srv/container
546 # 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>
547
548                 <para>This runs a container with SELinux sandbox security contexts.</para>
549         </refsect1>
550
551         <refsect1>
552                 <title>Exit status</title>
553
554                 <para>The exit code of the program executed in the
555                 container is returned.</para>
556         </refsect1>
557
558         <refsect1>
559                 <title>See Also</title>
560                 <para>
561                         <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
562                         <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
563                         <citerefentry><refentrytitle>yum</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
564                         <citerefentry><refentrytitle>debootstrap</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
565                         <citerefentry><refentrytitle>pacman</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
566                         <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
567                         <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
568                 </para>
569         </refsect1>
570
571 </refentry>