Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fuomag9 opened this issue May 27, 2021 · 12 comments
Labels

Comments

@fuomag9
Copy link

fuomag9 commented May 27, 2021

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.

@fuomag9 fuomag9 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 May 27, 2021
@therealkenc
Copy link
Collaborator

therealkenc commented May 27, 2021

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.

@xploSEoF
Copy link

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

@massimosala
Copy link

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

@xploSEoF Liam could you please write how to test it?

codebymikey added a commit to codebymikey/wsld that referenced this issue Nov 1, 2021
Explicitly delete the lock files before starting wsld following the nbdd0121@c3a2bb7 commit.

WSL2 never deletes the tmp directory - microsoft/WSL#6999
@mati865
Copy link

mati865 commented Nov 15, 2022

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

@xploSEoF
Copy link

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

@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.

@OneBlue
Copy link
Collaborator

OneBlue commented Jun 4, 2024

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

@OneBlue OneBlue closed this as completed Jun 4, 2024
@Nahor
Copy link

Nahor commented Jun 23, 2024

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).

@darkvertex
Copy link

darkvertex commented Aug 21, 2024

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.

@wang1zhen
Copy link

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?

@py563
Copy link

py563 commented Sep 30, 2024

thanks, this works.

@biggestsonicfan
Copy link

@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.

@koyae
Copy link

koyae commented Jan 28, 2025

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
Labels
Projects
None yet
Development

No branches or pull requests