Mount a Temporary File System with systemd-mount

systemd-mount creates a mount as a proper systemd unit, one you can inspect, restart or tear down, instead of a bare kernel call. The commands use systemd 255, the version installed on the reference machine, and take about five minutes if you already have a suitable empty directory.

Before you start

You need a Linux system running systemd and the systemd package. Creating a system mount normally needs root privileges, so the examples use sudo. Pick a directory that is safe to use as a mount point: any files already there will be hidden while the mount is active, then reappear once you unmount.

This is a runtime change, not a permanent /etc/fstab entry, and it is not a substitute for a managed mount unit when the mount has to survive a reboot. Do not point it at a directory containing files that a running service must keep seeing.

1. Choose and inspect the mount point

Use an explicit path below /mnt. The command will create the directory if it needs to, but checking first is what stops you mounting over data you actually wanted.

mount_point=/mnt/example-tmpfs
sudo findmnt --target "$mount_point" || true
sudo ls -la "$mount_point" 2>/dev/null || true

If findmnt reports something already there, stop and pick a different path. If the directory already holds files, make a note: those files get hidden temporarily, not deleted.

Checkpoint: the chosen path is either absent or an empty, unmounted directory.

2. Create the transient tmpfs

Run systemd-mount with --tmpfs and the mount point. Naming it is optional but useful later when you go looking for the resulting unit; here it is scratch.

sudo systemd-mount --tmpfs scratch "$mount_point"

systemd-mount submits the job through the system service manager rather than calling the mount operation directly. Without --no-block it waits for the mount unit to finish starting, and a successful command returns status zero. Worth knowing: the top-level directory mode comes from the caller's umask, which differs from the kernel's usual sticky, world-writable tmpfs default.

To create an ordinary filesystem mount instead, give the source and destination as two arguments. This example uses a placeholder device and is not meant to be run as written:

sudo systemd-mount --type=ext4 /dev/EXAMPLE_DEVICE "$mount_point"

With two arguments, the source is not probed unless you add --discover. Give it a single source argument instead and you get a different mode entirely: systemd probes the device or image itself and picks a directory below /run/media/system/. That distinction is an easy way to end up mounting something somewhere you did not expect.

Checkpoint: the command has returned successfully before you continue.

3. Verify both the mount and its unit

Check the kernel's view first, then ask systemd for the generated mount unit. The path-to-unit conversion turns /mnt/example-tmpfs into mnt-example\x2dtmps.mount, which is exactly why querying by path with systemctl beats trying to spell that name yourself.

findmnt --target "$mount_point" --output TARGET,SOURCE,FSTYPE,OPTIONS
systemctl status --no-pager --full -- "$mount_point"

Typical findmnt output shows the target path with tmpfs as the filesystem type, and the status command should show an active mount unit. If it failed, read the unit's recent log and status before you try again:

systemctl is-active --quiet -- "$mount_point" && echo mounted
systemctl --no-pager --full status -- "$mount_point"
journalctl --no-pager -u "$(systemd-escape --path --suffix=mount "$mount_point")" -n 30

For a quick write test, create a file and read it back. This only touches the temporary filesystem, not the host filesystem underneath it.

printf '%s\n' 'transient mount check' | sudo tee "$mount_point/check.txt" >/dev/null
sudo cat "$mount_point/check.txt"

4. Use an automount when access should trigger the mount

Add --automount=yes when you want the path backed by the filesystem on first access rather than immediately. An automount point and its mount unit are two separate units, and the optional idle timeout detaches the backing filesystem after the last access.

sudo systemd-mount --automount=yes --timeout-idle-sec=60s \
  /dev/EXAMPLE_DEVICE /mnt/example-data

The device must exist when using --discover, including the implicit discovery in the one-argument form. For removable devices, discovery can also pick automounting, a one-second idle timeout, and binding the automount unit to the device's lifetime. Do not assume a removable device will behave like a fixed disk.

Writing a persistent .automount unit instead? Its [Automount] section needs an absolute Where=, and the filename has to represent that path: /home/example maps to home-example.automount, and a matching home-example.mount must exist. Keep network ordering requirements on the mount unit rather than the automount unit; adding After=network-online.target to the automount can create an ordering cycle.

5. Remove the transient mount

Unmount by giving the same mount point to systemd-umount, which is just an alias for systemd-mount --umount.

sudo systemd-umount "$mount_point"
findmnt --target "$mount_point" || echo "unmounted"
sudo ls -la "$mount_point" 2>/dev/null || true

Before unmounting, get out of any shell whose current directory is inside the mount and stop programs using files there. A busy mount will refuse to stop, or take longer than expected to release. If you set up an automount, unmounting the mount point stops both the mount and automount units together. Do not delete the directory until findmnt confirms it is no longer a mount point.

There is no reboot recovery step here: transient units are not a permanent configuration, so if you need the mount again, just run the creation command again. If a failed transient unit lingers, inspect it and clear only its recorded failure state with sudo systemctl reset-failed once you understand the cause.

Done means