A fix for swap partitions in the wrong locations

Jun 03, '02 09:03:52AM

Contributed by: EatingPie

I have been using a swap file on a separate partition for almost a year now. I've actually tried a couple of the supplied implementations provided here at Mac OSX Hints. HOWEVER!! There is a bug in all the implementations I've tried, and I *believe* I've FINALLY found a fix! Note that I am dealing with a swap on a separate partition, not separate disk.

The Bug

If you ever force reboot (CTRL-CMD-POWER) OSX, your swap partition ends up mounted AFTER virtual memory starts. This means you now have "/Volumes/Swap" actually on your main partition running your virtual memory swap files, and a separate, useless partitition "/Volumes/Swap 1" just sitting there.

There is a "by-hand" way of fixing this, requiring some fudging and an additional reboot. But I've FINALLY found an automatic solution. Read the rest of the article for both the "by hand" and "automatic" solutions...

First, let me tell you the "by-hand" solution, in case you fall in the "faint of heart" or "just plain careful" category.

NOTE: In all examples I assume you've called your swap partitition Swap, for the swap device (obtained via "df") I use /dev/disk0s11. You will need to run df1 to find yours and replace my references with your actual device path.

Fix 1 (Easy)

It should not matter what method you used to create your swap partition, this should work on all of those provided. Quit all applications except the terminal. Now in the terminal, do your thing:

 % ls /Volumes
Ganymede Swap Swap 1
% sudo rm -rf /Volumes/Swap
% sudo mv "/Volumes/Swap 1" /Volumes/Swap
Reboot your system via the Apple Menu. Quick!!!

Fix 2 (Harder)

Now for the more harrowing, but very automated solution. These are the full instructions to using a separate partition as your swap (except the part about creating the partition already).

(1) Run df to find the device of your Swap partition:
% df
/dev/disk0s10 55743576 25548296 30195280 45% /Volumes/Ganymede
/dev/disk0s11 6550752 176968 6373784 2% /Volumes/Swap
My Swap partition is /dev/disk0s11. Whatever you get from df, use that in the next step.

(2) With the above info, add an entry to /etc/fstab (or just create the file if it isn't there):
/dev/disk0s11 /Volumes/Swap hfs rw 1 2
(3) You need to edit /etc/rc. Here is the original section on Swap (Virtual Memory):
...
ConsoleMessage "Starting virtual memory"

swapdir=/private/var/vm

# Make sure the swapfile exists
if [ ! -d ${swapdir} ]; then
ConsoleMessage "Creating default swap directory"
mount -uw /
mkdir -p -m 755 ${swapdir}
chown root:wheel ${swapdir}
else
rm -rf ${swapdir}/swap*
fi
...
We simply want to add a few things to the above code segment, and also change the single line naming the swapdir:
...
ConsoleMessage "Starting virtual memory"

# BEGIN CHANGE
#
# Make sure Swap is not mounted in case of hard reboot
# Then remove the directories if they exists
#
umount -v /Volumes/Swap*
if [ -d /Volumes/Swap ]; then
ConsoleMessage "*** DELETING extant Swap directory(s) ***"
mount -uw /
rm -rf /Volumes/Swap*
fi

#
# This fscks the disks and mounts them correctly
#
/sbin/autodiskmount

# HERE IS THE OTHER LINE TO CHANGE, so the swap file goes
# on your swap volume.
swapdir=/Volumes/Swap/private/var/vm

# END CHANGE

# Make sure the swapfile exists
if [ ! -d ${swapdir} ]; then
ConsoleMessage "Creating default swap directory"
mount -uw /
mkdir -p -m 755 ${swapdir}
chown root:wheel ${swapdir}
else
rm -rf ${swapdir}/swap*
fi
...
Note that everything below the new "swapdir=..." line is the original /etc/rc code section.

Explanation

If you have an invalid /Volumes/Swap directory, it stays there forever and ever... or until you remove it. First you
unmount all /Volumes/Swap partitions (/Volumes/Swap 1, /Volumes/Swap 2, etc.). Then you remove the directories themselves associated with those partitions. Unmounting keeps you from actually removing the stuff on the partitions themselves!

Next, comes the part that drove me nuts.

If you hard-boot (CMD-OPT-POWER) your Mac, Unix leaves all the disk partitions marked as "dirty." But you can't mount a
dirty disk until it is checked and marked "clean." Unfortunately, this checking/cleaning happens *after* virtual memory gets started, so you will NEVER be able to mount /Volumes/Swap prior to virtual memory. Unless, of course, you do another clean reboot. But that often isn't enough! Since the "/Volumes/Swap" was created by the system during the hard boot, it stays there, and your swap will always get mounted at "/Volumes/Swap 1" until you delete "/Volumes/Swap." (Did you understand that?)

OSX actually provides a command to both fsck and mount all the partitions correctly. This normally happens some time after virtual memory gets started (too late!), so we simply make it happen *before*. That's what the "/sbin/autodiskmount" is for.

There are two obvious questions here. The first: is it safe to move the autodiskmount to occur earlier in the boot process? I've checked this, and it works fine *for me* (translation: probably, but I'm not positive).

Second, is there a more robust or easier way to accomplish this task: ie, running an fsck directly and then doing a mount of your Swap volume? Almost surely! This is Unix after all. And I am certainly open to comments and suggestions on the above code.

Hope this helps.

[Editor's note: I have tried Fix #1, but have not yet tried Fix #2.]

Comments (13)


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