Move tmp folder from '/' partition to mounted partition (/mnt)

24,414

Solution 1

You can bind the /tmp mount point to /mnt/tmp:

sudo mount -B /tmp /mnt/tmp

Solution 2

Moving a "/tmp" partition requires some extra not suggested in this wrong answer https://askubuntu.com/a/371628/298086.

Moving data implies erase data from original partition once cloned to destination one, what is absolutelly not performed by a bind mount.

RECOMENDATION: Read this brilliant post if you wanna understand what a bind mount is https://unix.stackexchange.com/a/198591

If you give a try to the right mount option ("MOVE", not bind):

mount -M /tmp /mnt/tmp

I'm convinced it will fail reporting that "tmp is a shared mountpoint" (what indeed means "I cannot move a mountpoint if still being any process using it")

The "answer" you are looking for, may require stopping and restarting services using/accessing /tmp, before moving content.

You can list those services/processes by running:

lsof +D /tmp/

So once you listed what is actually accessing/using /tmp, is when you can reallly decide "how to act".

In my opinion most safe way passes-by

  1. Stopping all services accessing /tmp (if you can do that)
  2. Copying entirely /tmp contents to a new place
  3. Editing /etc/fstab and changing /tmp mount point physical location (no matter if is a bind or device mount)
  4. Restarting the system to perform the remount

But there is another way what does not require system restart, but is not safer as previous one. It is described here and consists in umounting /tmp in a lazy way, what should allow you to execute mount -M

This two links will be helpful for your demand:

Share:
24,414

Related videos on Youtube

Programster
Author by

Programster

Updated on September 18, 2022

Comments

  • Programster
    Programster over 1 year

    I have an Amazon EC2 instance which has given me a tiny '/' partition and a large '/mnt' partition. As such, I have moved my mysql data-dir over to the /mnt partition. However I am now having issues with the /tmp folder running out of space on my massive join queries and am trying to also move /tmp to /mnt/tmp. I tried to do this with a symlink but that results in the mysql service being unable to start.

    Please advise on how to move the storage of /tmp over to /mnt (/dev/xvdb)

  • Kevin Willock
    Kevin Willock over 10 years
    One thing you have to be very aware of is that the /mnt partition is ephemeral storage. If you stop your instance (or it degrades and AWS moves it), the data in /mnt will be lost. It is recommended (especially for your MySQL) to mount an EBS volume, and use that for data you want to be persistent.
  • Braiam
    Braiam over 10 years
    @KevinWillock I think that his /mnt (by the size) is persistent (or he mounted something there which is persistent) since he is actually saving data there. BTW, you may actually want to ping him (using @) since I'm almost sure didn't got pinged.
  • Programster
    Programster over 10 years
    @Braiam I didn't get pinged but checked this thread again by luck. Kevin would be correct that this is ephemeral storage. I know that this is highly unsafe but was done deliberately for testing performance of using local storage. If I was to use such a system, I would be making sure to replicate to an EBS volume or RDS itself and backing up from there. I am well aware of the risks.
  • hithwen
    hithwen about 10 years
    Do I need to execute that command on every reboot?
  • Matthieu
    Matthieu over 7 years
    @hithwen yes, but you can automate it in a startup script if really needed.
  • jitbit
    jitbit over 6 years
    Wait a second. Shouldn't it be the other way around? mount --bind /mnt/tmp /tmp
  • Braiam
    Braiam over 6 years
  • jitbit
    jitbit over 6 years
    @Braiam my point exactly! mount -B olddir newdir makes olddir accessible at newdir Also see this: serverfault.com/questions/427626/how-to-mount-tmp-in-mnt-on-‌​ec2
  • muru
    muru almost 4 years
    I second @jitbit - this should be mount -B /mnt/tmp /tmp, so that access to /tmp uses /mnt/tmp. WIih the current bind, access to /mnt/tmp uses whatever was mounted on /tmp` originally, which is not what's asked for.