Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A fix for swap partitions in the wrong locations System
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.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[12,341 views]  

A fix for swap partitions in the wrong locations | 13 comments | Create New Account
Click here to return to the 'A fix for swap partitions in the wrong locations' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Amazing!
Authored by: deppix on Jun 03, '02 09:57:34AM

You're my hero! I had observed the same problems you have and posted in the MacAch (http://arstechnica.infopop.net/OpenTopic/page?a=frm&s=50009562&f=8300945231), but noone knew an answer. Since then I've figured out solution 1 myself, but never got to solution 2. You're the man, thank you so much!

deppix



[ Reply to This | # ]
Cool fix!
Authored by: donvy on Jun 03, '02 10:25:19AM

Great solution! Thanks! As stable as Mac OS X is, we still end up crashing once in a while, and I dread having to reboot twice just to fix the swap partition problem.

What I normally did was use a variant of Fix #1: Log into the console as root, go to single user mode, manually unmount /Volumes/swap 1 and do a mount -a, then exit back. At least I didn\'t have to reboot all the way. But your way is better! Way better!



[ Reply to This | # ]
Additonal mysterious line in rc
Authored by: bluehz on Jun 03, '02 10:51:27AM

For some reason my /etc/rc has an additional line in it listed directly after the last line of the script shown - before the "Start System Preferences"

The line is:
dynamic_pager -H 40000000 -L 160000000 -S 80000000 -F ${swapdir}/swapfile

Is this normal and just not listed? What is this line for? Possibly it was added by SwapCop which I have used in the past.

Just curious...



[ Reply to This | # ]
dynamic_pager daemon
Authored by: sjk on Jun 03, '02 06:41:38PM
The dynamic_pager process dynamically manages VM swap files. See:

System Daemons

... for a brief description.

People tinkering with swap configurations might remove or modify the /etc/rc entry; I've left it as-is on my iBook.


[ Reply to This | # ]
Additonal mysterious line in rc
Authored by: EatingPie on Jun 05, '02 02:00:19PM

That line is most definitely there. I left it out for brevity. It is
actually what starts the Virtual Memory swap stuff going.

So you're in there.



[ Reply to This | # ]
Moving the autodiskmount
Authored by: WillyT on Jun 03, '02 11:04:38AM

I have my swap partition on a separate drive and moving the autodiskmount ahead of the swap is the only way I can do this "fairly" consistently. Yes it works for me.
Willy



[ Reply to This | # ]
is it just me?
Authored by: JayBee on Jun 03, '02 12:05:19PM

This is going to sound daft, but what is the point of Fix #1? It requires a reboot and... well, on my system, a straight reboot will do exactly the same thing. Swap gets mounted back at /Volumes/Swap.

Mounts on to folders seem to work quite sensibly. I used to have my home folder on a separate partition, but my actual /Users/username folder was a link to my /Volumes/Users/username directory. If the fstab mounted the Users partition at /Users, all well and good. If not, the functionality was still there, as the mount just mounted ON TOP OF the directory - it didn\'t clobber it.

The same thing happens on my Swap partition. Or maybe I\'ve installed something I\'ve just forgotten about...



[ Reply to This | # ]
is it just me? Well.. Depends...
Authored by: EatingPie on Jun 05, '02 02:23:26PM

Yes, in some of the recommended implementations of "Adding a Swap Partition," another straight reboot (via Apple Menu) automagically fixes the "Swap 1" problem. I had the same behaviour before myself.

Before what? You ask!

Before I got a new drive and created *3* partitions instead of just 2.

I *think* this is the reason it works for you, and not me (not me now). That's my theory anyway.



[ Reply to This | # ]
Problem ?
Authored by: drsmithy on Jun 03, '02 06:10:41PM

I have to resort to the three finger salute reasonably often (more often than I'd like) and I've never seen this occur. Is it just me ?



[ Reply to This | # ]
BAD FIX!!!
Authored by: ashevin on Jun 04, '02 07:32:55AM

# This fscks the disks and mounts them correctly
#
/sbin/autodiskmount <<<<----------- BAD BAD BAD
# 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
...

There is a reason that the autodiskmount is only done after all the fstab partitions have been checked. The proper fix is to place an entry in /etc/fstab and let the default /etc/rc fsck code handle checking and mounting it.

The real way to use a seperate swap partition is to use a raw partition for swap. Check the manpage for fstab for details. Using an automounted HFS partition just to hold swap files is stupid, IMNSHO.

- Avi



[ Reply to This | # ]
BAD FIX!!!
Authored by: bhines on Jun 04, '02 05:53:47PM

Using a separate partition, period for swap is stupid. :) If you must move the swap file (and i don't agree that there's a point), put it on another physical disk.



[ Reply to This | # ]
BAD FIX!!! ... No, Should still be SAFE!!
Authored by: EatingPie on Jun 05, '02 02:16:17PM
There is a reason that the autodiskmount is only done after all the fstab partitions have been checked. The proper fix is to place an entry in /etc/fstab and let the default /etc/rc fsck code handle checking and mounting it.
Let's talk about this... In my post, I state that the /etc/rc fsck is either (a) not working right, (b) done too late, or (c) not done in /etc/rc. I lean toward (c) since I don't even see an fsck in my /etc/rc script!! More importantly, the autodiskmount *does* fsck your disk for you!! That's how the thing gets mounted... It checkd your disk (fsck? Okay, I don't know for sure, but it performs the SAME function as fsck by checking and cleaning), marks the disk as clean, and then mounts it. This is according to the man page on autodiskmount. So, this method, according to documentation is safe... and it works!
The real way to use a seperate swap partition is to use a raw partition for swap. Check the manpage for fstab for details. Using an automounted HFS partition just to hold swap files is stupid, IMNSHO.
This is really a different issue. The common way to doing a seperate swap partition *is* with an HFS/UFS partition. We can certainly discuss quality of methods: your "raw" method vs. the more common "automount" method, but , as I said, it's a different issue. Then again, if your name "Avi" refers to you being Avi Tevanian, I throw in the towel right now!! -Pie

[ Reply to This | # ]
SwapCop
Authored by: Anonymous on Jun 12, '02 06:31:56AM
Hi,
I suggested to the author of SwapCop, J. Schier if he could change the way SwapCop works to the one suggested in this hint.

In response, he asked me to post his comment:


Comment by J. Schrier, author of Swap Cop ( http://homepage.mac.com/jschrier ), in response to an email from Ralf Welter pointing to this article:

"Actually, the method used in this article is incorrect (although it is the one used in earlier versions of Swap Cop). The OS X/Darwin disk system does not have fixed mount points like many other Unices, and so as a result, setting fstab mounts does *not* guarantee that you get the disk you want, resulting in "/swap/disk/name 1", type problems. The better way is to use the method implemented in the current (1.1.2) version of Swap Cop, which is described more fully at http://www.xlr8yourmac.com/OSX/OS_x_swapfile_notes.html

Regards,
--js

[ Reply to This | # ]