Moving var, tmp Off the Root in FreeBSD

One one of the first things I do on a newly installed FreeBSD system is to move /var and /tmp to under /usr. Since I usually allocate about 4Gb for the root slice and the rest of a disk—usually several hundred gigabytes—goes to /usr (well, there’s also the swap slice that takes few gigabytes) having /var and /tmp there is more comfortable as some log files, database files, or some temp files can sometimes grow to multi-gigabyte size and exhaust the root space.

Below is a simple procedure to move the /var to /usr/var and /tmp to /usr/var/tmp. This is best to do early on in a new system installation since many services tend to hook into /tmp and/or /var, and may thus lock files in those directories making the move more difficult. If you’re making this move on an established system, at least stop all the services that might interfere with the process (such as database services). It might even be a good idea to boot into a single user mode (if you do so, remember to correctly mount your disks before proceeding). I usually do this early in a new system install, before installing any major services, or at least before scripting them to run.

  1. Move /var to /usr/var
    mkdir /usr/var
    cd /var
    tar cvf - . | (cd /usr/var; tar xvf - )
    cd /
    chflags -R noschg /var
    rm -rf /var
    ln -s /usr/var /var
    
  2. Move /tmp to /usr/var/tmp
    mkdir /usr/var/tmp
    cd /tmp
    tar cvf - . | (cd /usr/var/tmp; tar xvf - )
    cd /
    chflags -R noschg /tmp
    rm -rf /tmp
    ln -s /usr/var/tmp /tmp
    chmod -h 777 /tmp
    chmod 1777 /usr/var/tmp
    

2 thoughts on “Moving var, tmp Off the Root in FreeBSD”

  1. If you have default FreeBSD installation and one of your slices are mounted under /tmp you must first umount it and then delete the old /tmp before linking new one

  2. That’s a good point. In new installations I generally just create a slice for boot (often much bigger than what is really needed, perhaps 10Gb, since the disks are usually very large), swap according to expected maximum memory the system will ever have installed (easier to plan ahead, and again, the disks are huge so few gigabytes here or there don’t really make a difference), and one for /usr. That way /tmp and /var are placed in the boot slice by the initial installation, and then I proceed to move them to under /usr as described in my post above.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.