Move swap to another partition, revisited again

Jun 17, '05 09:26:00AM

Contributed by: club60.org

Tiger only hintThis hint is based on several prior hints and comments published earlier (see references at the end), and is proven to work under Tiger. Before proceeding, please make sure you are familiar with the following terms: virtual memory, swapfile, root, sudo, terminal. You'll also need a separate partition on which to store the swap files.

This hint makes virtual memory start a bit later so that your swap volume is already mounted, thus eliminating the problems stated in referenced hints. If the swap volume fails to mount, then the boot volume will be used.

Let's start by making a backup of /etc/rc. If something goes wrong, you'll be able to boot from CD or single-user mode, start Terminal, and restore the original file):

sudo cp /etc/rc /etc/rc.saved
Now create a new text file with the following contents (replace the word Swap after the equal sign in the first line with the name of your volume:
swapvolume=Swap

RMRF_ITEMS="${swapdir}/swap*"
if [ ! -d /Volumes/${swapvolume}/.Trashes ]; then
    swapcount=1
    echo "Waiting for ${swapvolume} to mount"
    while [ "$swapcount" -le 10 ]; do
        sleep 1
        if [ -d /Volumes/${swapvolume}/.Trashes ]; then
            echo "${swapvolume} mounted after $swapcount seconds"
            break
        fi
        swapcount=`expr $swapcount + 1`
    done
fi

if [ -d /Volumes/${swapvolume}/.Trashes ]; then
    swapdir=/Volumes/${swapvolume}/.vm
    echo "Using ${swapdir} for swapfile"
else
    echo "Unable to use ${swapvolume} for swapfile"
fi
Save this file to your Desktop as rc.txt and issue the following commands in terminal:
sudo cp ~/Desktop/rc.txt /etc/rc.swap
sudo chown root:wheel /etc/rc.swap
Now we are ready to edit /etc/rc. Type the following line in your terminal to launch TextEdit as root (you may choose another method of course):
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/rc
Find the line (184):
swapdir=/private/var/vm
Insert these three lines of code after that line:
echo Removing $RMRF_ITEMS
rm -rf $RMRF_ITEMS
if [ -f /etc/rc.swap ]; then . /etc/rc.swap; fi # inserted locally
Now select this entire block of code...
if [ -f /etc/rc.swap ]; then . /etc/rc.swap; fi # inserted locally
if [ "${NetBoot}" = "-N" ]; then
    sh /etc/rc.netboot setup_vm ${swapdir}
fi

if [ ! -d ${swapdir} ]; then
    echo "Creating default swap directory"
    mkdir -p -m 755 ${swapdir}
    chown root:wheel ${swapdir}
else
    RMRF_ITEMS="${RMRF_ITEMS} ${swapdir}/swap*"
fi
            
echo Removing $RMRF_ITEMS
rm -rf $RMRF_ITEMS

if [ ${ENCRYPTSWAP:=-NO-} = "-YES-" ]; then
    encryptswap="-E"
else
    encryptswap=""
fi
/sbin/dynamic_pager ${encryptswap} -F ${swapdir}/swapfile
... and move it down to appear just before this line:
/usr/sbin/update
Save the file, reboot, and enjoy.

P.S. To see what directory is used for swap files, you may type the following in Terminal:
ps -wax | grep dynamic_pager -m1
References: [robg adds: There's some debate over the benefits of moving swap, but for those of you who wish to do so, this may be the best solution yet posted here (though I haven't tried it myself). I have marked this hint "Tiger only" for now -- if someone running 10.3 can test it, please let me know if it works for you...]

Comments (30)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050601094611565