2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY % entities SYSTEM
"custom-entities.ent" >
7 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
9 <refentry id=
"systemd-vmspawn" conditional=
"ENABLE_VMSPAWN"
10 xmlns:
xi=
"http://www.w3.org/2001/XInclude">
13 <title>systemd-vmspawn
</title>
14 <productname>systemd
</productname>
18 <refentrytitle>systemd-vmspawn
</refentrytitle>
19 <manvolnum>1</manvolnum>
23 <refname>systemd-vmspawn
</refname>
24 <refpurpose>Spawn an OS in a virtual machine
</refpurpose>
29 <command>systemd-vmspawn
</command>
30 <arg choice=
"opt" rep=
"repeat">OPTIONS
</arg>
31 <arg choice=
"opt" rep=
"repeat">ARGS
</arg>
36 <title>Description
</title>
37 <para><command>systemd-vmspawn
</command> may be used to start a virtual machine from an OS image. In many ways it is similar to
<citerefentry>
38 <refentrytitle>systemd-nspawn
</refentrytitle><manvolnum>1</manvolnum></citerefentry>, but
39 launches a full virtual machine instead of using namespaces.
</para>
41 <para>File descriptors for
<filename>/dev/kvm
</filename> and
<filename>/dev/vhost-vsock
</filename> can be
42 passed to
<command>systemd-vmspawn
</command> via systemd's native socket passing interface (see
43 <citerefentry><refentrytitle>sd_listen_fds
</refentrytitle><manvolnum>3</manvolnum></citerefentry> for
44 details about the precise protocol used and the order in which the file descriptors are passed), these
45 file descriptors must be passed with the names
<literal>kvm
</literal> and
<literal>vhost-vsock
</literal>
48 <para>Note: on Ubuntu/Debian derivatives systemd-vmspawn requires the user to be in the
49 <literal>kvm
</literal> group to use the VSOCK options.
</para>
53 <title>Options
</title>
55 <para>The excess arguments are passed as extra kernel command line arguments using SMBIOS.
</para>
57 <para>The following options are understood:
</para>
62 <term><option>-q
</option></term>
63 <term><option>--quiet
</option></term>
65 <listitem><para>Turns off any status output by the tool
66 itself. When this switch is used, the only output from vmspawn
67 will be the console output of the Virtual Machine OS itself.
</para>
69 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
75 <title>Image Options
</title>
79 <term><option>-D
</option></term>
80 <term><option>--directory=
</option></term>
82 <listitem><para>Directory to use as file system root for the virtual machine.
</para>
84 <para>One of either
<option>--directory=
</option> or
<option>--image=
</option> must be specified.
85 If neither are specified
<option>--directory=.
</option> is assumed.
</para>
87 <para>Note: If mounting a non-root owned directory you may require
<option>--private-users=
</option>
88 to map into the user's subuid namespace. An example of how to use
<constant>/etc/subuid
</constant>
89 for this is given later.
</para>
91 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
96 <term><option>-i
</option></term>
97 <term><option>--image=
</option></term>
99 <listitem><para>Root file system disk image (or device node) for the virtual machine.
</para>
101 <xi:include href=
"version-info.xml" xpointer=
"v255"/>
108 <title>Host Configuration
</title>
112 <term><option>--cpus=
<replaceable>CPUS
</replaceable></option></term>
114 <listitem><para>The number of CPUs to start the virtual machine with.
115 Defaults to
1.
</para>
117 <xi:include href=
"version-info.xml" xpointer=
"v255"/>
122 <term><option>--ram=
<replaceable>BYTES
</replaceable></option></term>
124 <listitem><para>The amount of memory to start the virtual machine with.
125 Defaults to
2G.
</para>
127 <xi:include href=
"version-info.xml" xpointer=
"v255"/>
132 <term><option>--kvm=
<replaceable>BOOL
</replaceable></option></term>
134 <listitem><para>If
<option>--kvm=
</option> is not specified KVM support will be
135 detected automatically. If true, KVM is always used, and if false, KVM is never used.
</para>
137 <xi:include href=
"version-info.xml" xpointer=
"v255"/></listitem>
141 <term><option>--vsock=
<replaceable>BOOL
</replaceable></option></term>
143 <listitem><para>If
<option>--vsock=
</option> is not specified VSOCK networking support will be
144 detected automatically. If true, VSOCK networking is always used, and if false, VSOCK networking is never used.
</para>
146 <xi:include href=
"version-info.xml" xpointer=
"v255"/></listitem>
150 <term><option>--vsock-cid=
<replaceable>CID
</replaceable></option></term>
153 <para>Sets the specific CID to use for the guest.
154 Valid CIDs are in the range
<constant>3</constant> to
<constant>4294967294</constant> (
<constant>0xFFFF_FFFE
</constant>).
155 CIDs outside of this range are reserved. By default vmspawn will attempt to derive a CID for the guest derived from the machine name,
156 falling back to a random CID if this CID is taken.
</para>
158 <xi:include href=
"version-info.xml" xpointer=
"v255"/>
163 <term><option>--tpm=
<replaceable>BOOL
</replaceable></option></term>
166 <para>If
<option>--tpm=
</option> is not specified vmspawn will detect the presence of
<citerefentry project='debian'
>
167 <refentrytitle>swtpm
</refentrytitle><manvolnum>8</manvolnum></citerefentry> and use it if available.
168 If yes is specified
<citerefentry project='debian'
><refentrytitle>swtpm
</refentrytitle><manvolnum>8</manvolnum></citerefentry>
169 is always used, and if no is set
<citerefentry project='debian'
><refentrytitle>swtpm
</refentrytitle>
170 <manvolnum>8</manvolnum></citerefentry> is never used.
</para>
172 <para>Note: the virtual TPM used may change in future.
</para>
174 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
179 <term><option>--linux=
<replaceable>PATH
</replaceable></option></term>
182 <para>Set the linux kernel image to use for direct kernel boot.
183 If a directory type image is used and
<option>--linux=
</option> was omitted, vmspawn will search for boot loader entries
185 <ulink url=
"https://uapi-group.org/specifications/specs/boot_loader_specification">Boot Loader Specification
</ulink> assuming
186 XBOOTLDR to be located at /boot and ESP to be /efi respectively.
187 If no kernel was installed into the image then the image will fail to boot.
</para>
189 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
194 <term><option>--initrd=
<replaceable>PATH
</replaceable></option></term>
197 <para>Set the initrd to use for direct kernel boot.
198 If the
<option>--linux=
</option> supplied is a
199 <ulink url=
"https://uapi-group.org/specifications/specs/boot_loader_specification">Boot Loader Specification
</ulink>
200 Type #
2 entry, then this argument is not required.
201 If no initrd was installed into the image then the image will fail to boot.
</para>
203 <para><option>--initrd=
</option> can be specified multiple times and vmspawn will merge them together.
</para>
205 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
210 <term><option>-n
</option></term>
211 <term><option>--network-tap
</option></term>
214 <para>Create a TAP device to network with the virtual machine.
</para>
216 <para>Note: root privileges are required to use TAP networking.
218 <citerefentry><refentrytitle>systemd-networkd
</refentrytitle><manvolnum>8</manvolnum></citerefentry>
219 must be running and correctly set up on the host to provision the host interface. The relevant
220 <literal>.network
</literal> file can be found at
221 <filename>/usr/lib/systemd/network/
80-vm-vt.network
</filename>.
224 <xi:include href=
"version-info.xml" xpointer=
"v255"/>
229 <term><option>--network-user-mode
</option></term>
231 <listitem><para>Use user mode networking.
</para>
233 <xi:include href=
"version-info.xml" xpointer=
"v255"/></listitem>
237 <term><option>--firmware=
<replaceable>PATH
</replaceable></option></term>
239 <listitem><para>Takes an absolute path, or a relative path beginning with
240 <filename>./
</filename>. Specifies a JSON firmware definition file, which allows selecting the
241 firmware to boot in the VM. If not specified a suitable firmware is automatically discovered. If the
242 special string
<literal>list
</literal> is specified lists all discovered firmwares.
</para>
244 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
248 <term><option>--discard-disk=
<replaceable>BOOL
</replaceable></option></term>
250 <listitem><para>Controls whether qemu processes discard requests from the VM.
251 This prevents long running VMs from using more disk space than required.
252 This is enabled by default.
</para>
254 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
258 <term><option>--secure-boot=
<replaceable>BOOL
</replaceable></option></term>
260 <listitem><para>Configure whether to search for firmware which supports Secure Boot.
</para>
262 <para>If the option is not specified the first firmware which is detected will be used.
263 If the option is set to yes then the first firmware with Secure Boot support will be selected.
264 If no is specified then the first firmware without Secure Boot will be selected.
</para>
266 <xi:include href=
"version-info.xml" xpointer=
"v255"/></listitem>
272 <title>System Identity Options
</title>
276 <term><option>-M
</option></term>
277 <term><option>--machine=
</option></term>
279 <listitem><para>Sets the machine name for this virtual machine. This
280 name may be used to identify this virtual machine during its runtime
281 (for example in tools like
282 <citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
285 <xi:include href=
"version-info.xml" xpointer=
"v255"/>
290 <term><option>--uuid=
</option></term>
292 <listitem><para>Set the specified UUID for the virtual machine. The
293 init system will initialize
294 <filename>/etc/machine-id
</filename> from this if this file is
295 not set yet. Note that this option takes effect only if
296 <filename>/etc/machine-id
</filename> in the virtual machine is
299 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
305 <title>Property Options
</title>
309 <term><option>--register=
</option></term>
311 <listitem><para>Controls whether the virtual machine is registered with
312 <citerefentry><refentrytitle>systemd-machined
</refentrytitle><manvolnum>8</manvolnum></citerefentry>. Takes a
313 boolean argument, which defaults to
<literal>yes
</literal> when running as root, and
<literal>no
</literal> when
314 running as a regular user. This ensures that the virtual machine is accessible via
315 <citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>
317 <para>Note: root privileges are required to use this option as registering with
318 <citerefentry><refentrytitle>systemd-machined
</refentrytitle><manvolnum>8</manvolnum></citerefentry>
319 requires privileged D-Bus method calls.
</para>
321 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
327 <title>User Namespacing Options
</title>
331 <term><option>--private-users=
<replaceable>UID_SHIFT[:UID_RANGE]
</replaceable></option></term>
333 <listitem><para>Controls user namespacing under
<option>--directory=
</option>.
335 <citerefentry><refentrytitle>virtiofsd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
336 is instructed to map user and group ids (UIDs and GIDs).
337 This involves mapping the private UIDs/GIDs used in the virtual machine (starting with the virtual machine's
338 root user
0 and up) to a range of UIDs/GIDs on the host that are not used for other purposes (usually in the
339 range beyond the host's UID/GID
65536).
</para>
341 <para>If one or two colon-separated numbers are specified, user namespacing is turned on.
<replaceable>UID_SHIFT
</replaceable>
342 specifies the first host UID/GID to map,
<replaceable>UID_RANGE
</replaceable> is optional and specifies number of host
343 UIDs/GIDs to assign to the virtual machine. If
<replaceable>UID_RANGE
</replaceable> is omitted,
65536 UIDs/GIDs are assigned.
</para>
345 <para>When user namespaces are used, the GID range assigned to each virtual machine is always chosen identical to the
348 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
354 <title>Mount Options
</title>
359 <term><option>--bind=
<replaceable>PATH
</replaceable></option></term>
360 <term><option>--bind-ro=
<replaceable>PATH
</replaceable></option></term>
362 <listitem><para>Mount a directory from the host into the virtual machine. Takes one of: a path
363 argument — in which case the specified path will be mounted from the host to the same path in the virtual machine, or
364 a colon-separated pair of paths — in which case the first specified path is the source in the host, and the
365 second path is the destination in the virtual machine. If the source path is not absolute, it is resolved
366 relative to the current working directory. The
<option>--bind-ro=
</option> option creates read-only bind mounts.
367 Backslash escapes are interpreted, so
<literal>\:
</literal> may be used to embed colons in either path.
368 This option may be specified multiple times for creating multiple independent bind mount points.
</para>
370 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
374 <term><option>--extra-drive=
<replaceable>PATH
</replaceable></option></term>
376 <listitem><para>Takes a disk image or block device on the host and supplies it to the virtual machine as another drive.
</para>
378 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
385 <title>Integration Options
</title>
389 <term><option>--forward-journal=
<replaceable>FILE|DIR
</replaceable></option></term>
391 <listitem><para>Forward the virtual machine's journal to the host.
392 <citerefentry><refentrytitle>systemd-journal-remote
</refentrytitle><manvolnum>8</manvolnum></citerefentry>
393 is currently used to receive the guest VM's forwarded journal entries. This option determines where
394 this journal is saved on the host and has the same semantics as
395 <option>-o
</option>/
<option>--output
</option> described in
396 <citerefentry><refentrytitle>systemd-journal-remote
</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
</para>
398 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
403 <term><option>--pass-ssh-key=
<replaceable>BOOL
</replaceable></option></term>
405 <listitem><para>By default an SSH key is generated to allow
<command>systemd-vmspawn
</command> to open
406 a D-Bus connection to the VM's systemd bus. Setting this to
"no" will disable SSH key generation.
</para>
408 <para>The generated keys are ephemeral. That is they are valid only for the current invocation of
<command>systemd-vmspawn
</command>,
409 and are typically not persisted.
</para>
411 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
416 <term><option>--ssh-key-type=
<replaceable>TYPE
</replaceable></option></term>
418 <listitem><para>Configures the type of SSH key to generate, see
419 <citerefentry project=
"man-pages"><refentrytitle>ssh-keygen
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
420 for more information.
</para>
422 <para>By default
<literal>ed25519
</literal> keys are generated, however
<literal>rsa
</literal> keys
423 may also be useful if the VM has a particularly old version of
<command>sshd
</command>.
</para>
425 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
432 <title>Input/Output Options
</title>
436 <term><option>--console=
<replaceable>MODE
</replaceable></option></term>
438 <listitem><para>Configures how to set up the console of the VM. Takes one of
439 <literal>interactive
</literal>,
<literal>read-only
</literal>,
<literal>native
</literal>,
440 <literal>gui
</literal>. Defaults to
<literal>interactive
</literal>.
<literal>interactive
</literal>
441 provides an interactive terminal interface to the VM.
<literal>read-only
</literal> is similar, but
442 is strictly read-only, i.e. does not accept any input from the user.
<literal>native
</literal> also
443 provides a TTY-based interface, but uses qemu native implementation (which means the qemu monitor
444 is available).
<literal>gui
</literal> shows the qemu graphical UI.
</para>
446 <xi:include href=
"version-info.xml" xpointer=
"v256"/></listitem>
450 <term><option>--background=
<replaceable>COLOR
</replaceable></option></term>
452 <listitem><para>Change the terminal background color to the specified ANSI color as long as the VM
453 runs. The color specified should be an ANSI X3.64 SGR background color, i.e. strings such as
454 <literal>40</literal>,
<literal>41</literal>, …,
<literal>47</literal>,
<literal>48;
2;…
</literal>,
455 <literal>48;
5;…
</literal>. See
<ulink
456 url=
"https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters">ANSI
457 Escape Code (Wikipedia)
</ulink> for details. Assign an empty string to disable any coloring. This
458 only has an effect in
<option>--console=interactive
</option> and
459 <option>--console=read-only
</option> modes.
</para>
461 <xi:include href=
"version-info.xml" xpointer=
"v256"/>
468 <title>Credentials
</title>
472 <term><option>--load-credential=
<replaceable>ID
</replaceable>:
<replaceable>PATH
</replaceable></option></term>
473 <term><option>--set-credential=
<replaceable>ID
</replaceable>:
<replaceable>VALUE
</replaceable></option></term>
475 <listitem><para>Pass a credential to the virtual machine. These two options correspond to the
476 <varname>LoadCredential=
</varname> and
<varname>SetCredential=
</varname> settings in unit files. See
477 <citerefentry><refentrytitle>systemd.exec
</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
478 details about these concepts, as well as the syntax of the option's arguments.
</para>
480 <para>In order to embed binary data into the credential data for
<option>--set-credential=
</option>,
481 use C-style escaping (i.e.
<literal>\n
</literal> to embed a newline, or
<literal>\x00
</literal> to
482 embed a
<constant>NUL
</constant> byte). Note that the invoking shell might already apply unescaping
483 once, hence this might require double escaping!
</para>
485 <xi:include href=
"version-info.xml" xpointer=
"v255"/></listitem>
489 </refsect2><refsect2>
493 <xi:include href=
"standard-options.xml" xpointer=
"no-pager" />
494 <xi:include href=
"standard-options.xml" xpointer=
"help" />
495 <xi:include href=
"standard-options.xml" xpointer=
"version" />
500 <xi:include href=
"common-variables.xml" />
503 <title>Examples
</title>
506 <title>Run an Arch Linux VM image generated by mkosi
</title>
509 $ mkosi -d arch -p systemd -p linux --autologin -o image.raw -f build
510 $ systemd-vmspawn --image=image.raw
515 <title>Import and run a Fedora
39 Cloud image using machinectl
</title>
519 -O https://download.fedoraproject.org/pub/fedora/linux/releases/
&fedora_latest_version;/Cloud/x86_64/images/Fedora-Cloud-Base-
&fedora_latest_version;-
&fedora_cloud_release;.x86_64.raw.xz \
520 -O https://download.fedoraproject.org/pub/fedora/linux/releases/
&fedora_latest_version;/Cloud/x86_64/images/Fedora-Cloud-
&fedora_latest_version;-
&fedora_cloud_release;-x86_64-CHECKSUM \
521 -O https://fedoraproject.org/fedora.gpg
522 $ gpgv --keyring ./fedora.gpg Fedora-Cloud-
&fedora_latest_version;-
&fedora_cloud_release;-x86_64-CHECKSUM
523 $ sha256sum -c Fedora-Cloud-
&fedora_latest_version;-
&fedora_cloud_release;-x86_64-CHECKSUM
524 # machinectl import-raw Fedora-Cloud-Base-
&fedora_latest_version;-
&fedora_cloud_release;.x86_64.raw.xz fedora-
&fedora_latest_version;-cloud
525 # systemd-vmspawn -M fedora-
&fedora_latest_version;-cloud
530 <title>Build and run systemd's system image and forward the VM's journal to a local file
</title>
535 -D mkosi.output/system \
536 --private-users $(grep $(whoami) /etc/subuid | cut -d: -f2) \
537 --linux mkosi.output/system.efi \
538 --forward-journal=vm.journal \
542 <para>Note: this example also uses a kernel command line argument to ensure SELinux isn't started in enforcing mode.
</para>
546 <title>SSH into a running VM using
<command>systemd-ssh-proxy
</command></title>
550 $ my_vsock_cid=
3735928559
552 -D mkosi.output/system \
553 --private-users $(grep $(whoami) /etc/subuid | cut -d: -f2) \
554 --linux mkosi.output/system.efi \
555 --vsock-cid $my_vsock_cid \
557 $ ssh root@vsock/$my_vsock_cid -i /run/user/$UID/systemd/vmspawn/machine-*-system-ed25519
563 <title>Exit status
</title>
565 <para>If an error occurred the value errno is propagated to the return code.
566 If EXIT_STATUS is supplied by the running image that is returned.
567 Otherwise EXIT_SUCCESS is returned.
</para>
571 <title>See Also
</title>
572 <para><simplelist type=
"inline">
573 <member><citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
574 <member><citerefentry project='debian'
><refentrytitle>mkosi
</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
575 <member><citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
576 <member><citerefentry><refentrytitle>importctl
</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
577 <member><ulink url=
"https://uapi-group.org/specifications/specs/boot_loader_specification">Boot Loader Specification
</ulink></member>