Skip to content

Implement tmpfs file system for /tmp that writes to volatile memory instead of persisting to disk #6999

Closed

Description

@fuomag9

Is your feature request related to a problem? Please describe.
I'd love to be able to use /tmp like I would on linux machines, as a tmpfs file system. At the moment the tmpfs file system on WSL 2 is actually implemented by mapping it to disk instead of writing to RAM. This results in files not being cleaned with restarts (e.g. wsl --shutdown) that accumulate over time. Furthermore, this can result in performance issues with code that relies on temp files since they will be actually written to disk.

Describe the solution you'd like
Implement the tmpfs by writing to volatile memory

Describe alternatives you've considered
At least autoclean /tmp in the meantime at the startup of the distro instead of relying on the user to do it.

Activity

  1. changed the title [-]Implement true tmpfs file system for /tmp instead of persisting to disk[/-] [+]Implement tmpfs file system for /tmp that writes to volatile memory instead of persisting to disk[/+] on May 27, 2021
  2. therealkenc commented on May 27, 2021

    @therealkenc

    like I would on linux machines, as a tmpfs file system

    Label "feature" loophole, but /tmp is not a tmpfs on Real Linux either (on Debian derived distros, anyway). This is for all intents by-design/linux-behavior. If you want /tmp to be tmpfs, you can mount a tmpfs at /tmp. Test for the tmpfs mount in .bashrc, and if it isn't there, mount it.

    At least autoclean /tmp in the meantime

    That much is dupe #1278 ➡️ #344 (➡️ #994). Fire up cron or just call your tmp cleaning script directly from .bashrc.

  3. xploSEoF commented on Jun 14, 2021

    @xploSEoF

    As almost all linux machine hardening guides suggest, the /tmp drive should always be mounted as tmpfs with a few additional settings.

    As per the recommendations here, doing this directly in the .bashrc isn't ideal but works:

    mount|grep -E "^[^ ]* on /tmp " >/dev/null
    if [ "$?" != "0" ];then
            echo "Mounting tmp"
            sudo mount -t tmpfs tmpfs /tmp -o noexec,defaults,nodev,nosuid,noatime,size=256m
    fi
    

    I've also added this answer to the only stack overflow question I can find on the matter: https://superuser.com/questions/1170939/simulate-reboot-to-clear-tmp-on-the-windows-linux-subsystem/1656653#1656653

  4. massimosala commented on Aug 26, 2021

    @massimosala

    /etc/fstab ... this seems to be creating a new /tmp mount for each bash terminal

    Liam Le Brun (@xploSEoF) Liam could you please write how to test it?

  5. added a commit that references this issue on Nov 1, 2021
  6. mati865 commented on Nov 15, 2022

    @mati865

    Since addition of Systemd this can be easily enabled using instruction from this post https://askubuntu.com/a/1416156/588206:

    sudo systemctl enable /usr/share/systemd/tmp.mount
    
  7. xploSEoF commented on Nov 21, 2022

    @xploSEoF

    /etc/fstab ... this seems to be creating a new /tmp mount for each bash terminal

    Liam Le Brun (@xploSEoF) Liam could you please write how to test it?

    Wow, late reply, sorry.

    It's simple. Open a terminal, then put a file into the /tmp drive, even with something as simple as touch /tmp/test then launch another, create another file, and see that neither has the others file.

    There are a number of other ways to confirm, and that could be specific to WSL1 - I'm unfortunately stuck on WSL1 due to using features unavailable on WSL2.

  8. OneBlue commented on Jun 4, 2024

    @OneBlue
    Collaborator

    Indeed you can use either systemd or fstab to use a tmpfs for /tmp. Closing since this is user-configurable.

  9. Nahor commented on Jun 23, 2024

    @Nahor

    Indeed you can use either systemd or fstab to use a tmpfs for /tmp. Closing since this is user-configurable

    Note that currently (WSL 2.2.4.0, and since 2.0.12.0), doing so will break GUI applications, because the new tmpfs mount will hide the /tmp/.X11-unix that WSL creates at startup (microsoft/wslg#1156).

  10. darkvertex commented on Aug 21, 2024

    @darkvertex

    Indeed you can use either systemd or fstab to use a tmpfs for /tmp. Closing since this is user-configurable

    Note that currently (WSL 2.2.4.0, and since 2.0.12.0), doing so will break GUI applications, because the new tmpfs mount will hide the /tmp/.X11-unix that WSL creates at startup (microsoft/wslg#1156).

    Nahor In the interest of saving others from this unfortunate bad default behaviour who find this GH issue, an easy manual fix is to create /etc/tmpfiles.d/wslg.conf as root with the following contents:

    #  This file is part of the debianisation of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU General Public License as published by
    #  the Free Software Foundation; either version 2 of the License, or
    #  (at your option) any later version.
    
    # See tmpfiles.d(5) for details
    
    # Type Path           Mode UID  GID  Age Argument
    L+     /tmp/.X11-unix -    -    -    -   /mnt/wslg/.X11-unix
    

    and then wsl --shutdown and start it again to see the effect.

    This results in the symlink being created, and it happens after tmpfs is mounted by systemd.

  11. wang1zhen commented on Aug 22, 2024

    @wang1zhen

    Indeed you can use either systemd or fstab to use a tmpfs for /tmp. Closing since this is user-configurable

    Note that currently (WSL 2.2.4.0, and since 2.0.12.0), doing so will break GUI applications, because the new tmpfs mount will hide the /tmp/.X11-unix that WSL creates at startup (microsoft/wslg#1156).

    Nahor In the interest of saving others from this unfortunate bad default behaviour who find this GH issue, an easy manual fix is to create /etc/tmpfiles.d/wslg.conf as root with the following contents:

    #  This file is part of the debianisation of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU General Public License as published by
    #  the Free Software Foundation; either version 2 of the License, or
    #  (at your option) any later version.
    
    # See tmpfiles.d(5) for details
    
    # Type Path           Mode UID  GID  Age Argument
    L+     /tmp/.X11-unix -    -    -    -   /mnt/wslg/.X11-unix
    

    and then wsl --shutdown and start it again to see the effect.

    This results in the symlink being created, and it happens after tmpfs is mounted by systemd.

    Thanks so much for the snippets. Maybe off-topic, but should this be included in the distribution provided by Microsoft Store for future fix?

  12. py563 commented on Sep 30, 2024

    @py563

    thanks, this works.

  13. biggestsonicfan commented on Jan 1, 2025

    @biggestsonicfan

    Alan Fregtman (@darkvertex) Wonderful tip, thank you! I find it a bit odd that with systemd the /etc/wsl.conf autmount setting isn't honored by the wslg mounting, but this worked a treat.

  14. koyae commented on Jan 28, 2025

    @koyae

    Does this mean that tmpfs has a real implementation now (actually in RAM)? I'm aware the implementation was simply faked as of 2017, but I would appreciate proper confirmation regarding any changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions