4

TL;DR: How can I set up either via a bash command executed as user or a bash command executed only once as sudo a temporary directory in $HOME which has the same behavior as the global /tmp directory?

Having upgraded my system to ubuntu 20.04 I am stumbeling upon a problem related to the temporary directory /tmp, snap and chromium.

I have written a small bash script which translate some log file to an html file, which is then opened in chromium. The html files are stored in /tmp because they are large and it is very likely, that I won't need them any more in the near future / after the next reboot. All worked well on ubuntu 18.04.

I recently upgraded my system to ubuntu 20.04, where chromium is installed via snap and does not have access to /tmp because of some reasons I do not understand. Regardless, I have to accept this behavior (doesn't seem like anything is changing soon) and have to think of a work around. While searching the WWW I stumbled upon the idea of a user-specific temporary directory stored in $HOME, which would solve all my problems, because chromium/snap can access $HOME. However, I do not want to put much thought into creating and cleaning this tmp directory by myself. I am hoping of a way to easily create a $HOME/tmp directory which is cleaned up in the same manner as /tmp.

I already read about pam_tmpdir but couldn't find out if and how this could help. I know about mktemp but these files are not cleaned up on restart, if not created in /tmp. I thought about creating the file in /tmp/snap.chromium/tmp because chromium seems to have access to this, but user don't and the folder is only created at the first start up of chromium which is to late. Creating an entry in crontab with @reboot is an option, but I don't like it. And if I am not mistaken, /tmp cleaning can be changed so that clean up is not only performed on reboot.

Hopefully somebody reading this has a great idea how to solve my problem.

1
  • Does this hack sound good to you? askubuntu.com/a/1264341/1096597 Otherwise you could try to mount a tmpfs on your own in your desired subdir of home (just note that "tmpfs puts everything into the kernel internal caches and grows and shrinks to accommodate the files it contains and is able to swap unneeded pages out to swap space."). And finally - you could avoid using snap chrome ;) Also about mktmp - files in tmp dir are cleaned reagularly
    – Tooster
    Nov 23, 2022 at 13:01

1 Answer 1

0

I put a script named /etc/profile.d/tmp-user.sh which is

if [ "$EUID" -ne 0 ]; then
    if [[ ! ( -d /home/$USER/tmp ) ]]; then
        if [[ ! ( -d /tmp/tmp_$USER ) ]]; then
            mkdir /tmp/tmp_$USER
        fi
        ln -sv /tmp/tmp_$USER /home/$USER/tmp
    fi
    export TMPDIR=/home/$USER/tmp/
fi

It does not require root access to be launched so we can include it in our ~/.bash_profile for instance.

1
  • I don't think creating a symlink will allow to write to that directory without creating a bind mount. If it did, that would seem to be a giant hole in the snap's confinement mechanisms - what would be the difference between linking to /root?. Symlinks are resolved on filesystem level.
    – Tooster
    Nov 23, 2022 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.