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

10.4: Move swap files to another drive System 10.4
Tiger only hintIn Tiger, unlike Panther, one must explicitly mount drives to make them available at the point in /etc/rc where virtual memory is set up. The catch-22 here is that one can only use BSD device names of the form /dev/disk1s10 at this early stage of the boot process, but BSD device names are not consistent across boots.

An idea from Markus Hitter is to use pdisk to inspect properties of unmounted drives by BSD device name. I refined this idea by using md5 to fingerprint the drive:
swaphash=`pdisk /dev/disk${swapdisk} -dump 2>/dev/null | \
grep -v '/dev/disk' | md5 -q`
Still, two drives might be identical twins intended for different purposes, so one can check for a hidden file .enablevm as confirmation. This issue can be circumvented by choosing a unique volume size to be used only for swap partitions. I have updated my web page, Moving the swapfile in Mac OS X 10.4 (Tiger), to explain all of this. I also provide a Perl script which automates this system modification. This script has been 100% reliable for me.

Read the rest of the hint for a long list of swap-related tips and tricks...

Here are a bunch of resources on the topic of moving swap:
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (5 votes cast)
 
[40,781 views]  

10.4: Move swap files to another drive | 22 comments | Create New Account
Click here to return to the '10.4: Move swap files to another drive' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Move swap files to another drive
Authored by: metiure on Feb 10, '06 01:14:12PM

What is the point with moving the swap file to another drive?
Speed?
Tiger is so quick, why bother...

Vic



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 10, '06 06:46:21PM

If you are on a standard 1 hd, 1 partition, apple setup, and you're completely satisfied with Tiger's speed, then this hint is not for you. This hint is not meant to convince you that moving your swap file is necessary - its not.

This hint is for those of us who are trying to squeeze every last ounce of performance out of our computers without having to void the warranty, or spend more money on faster hardware. Or, for those of us who have already spend the money on faster hardware - this will help us to make sure to get more bang for our buck by squeezing every last ounce of performance out of our upgrades.

Again, this hint is not meant to convince you that moving your swap file is necessary - its not. If you are on a standard 1 hd, 1 partition, apple setup, and you're completely satisfied with Tiger's speed, then this hint is not for you.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 10, '06 06:26:44PM

Interesting, I'm not having any issues with my relocated swap in Tiger. I'm using the same process that we worked out for Panther.

Maybe its because my swap partition is on the same physical drive as my boot partition?

I have two internal 10k rpm u160 scsi drives:

-HD1, internal SCSI (via PCI card) bus1:
--partition1: swap (vm)
--partition2: MacOSX (boot)
-HD2, internal SCSI (via PCI card) bus2:
--partition1: scratch (for photoshop scratch and ~/Library/Caches)
--partition2: users (for /Users)
-HD3, external Firewire (via onboard port) bus3:
--partition1: av (for ~/Movies and ~/Music)

I'm using a DP1.25GHz G4 Tower running 10.4.4 w/all updates, and I haven't had any problems relocating my swap using the Panther method.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 10, '06 07:02:51PM

To maximize performance: you typically want your swap/vm to be on the fastest part of the HD, and you want your vm files to be unfragmented. You can accomplish this by partitioning your boot drive into two partitions, the first (the first partition is faster) for vm/swap, and the second for your boot disk.

You also typically want your scratch to be on the fastest part of the HD, and you want your scratch files to be unfragmented. Basically scratch is vm for specific applications (such as photoshop). You also want your scratch partition on a separate HD (and a separate bus if you can) from your vm and boot disk (your boot disk also contains the application). But it is advantageous to keep your scratch and your data storage on the same HD because an inner-drive copy is going to be faster than an inter-drive copy (once photoshop is done with a file - which is temporarily stored in scratch - it then will write the file to a more permanent file).

Of course, if you can have multiple HDs (and possibly have multiple busses) you might want to consider RAID0 (striping) for even more speed.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 10, '06 07:11:28PM

On the hardware side, more RAM should always be your first upgrade, so you can avoid VM swapping as much as possible. Faster spinning HDs (more Rpm) with more lanes for traffic (more MBps) are also a good upgrade (although they can get expensive) so that when you do swap, at least it won't take as long. Besides, with a faster HD, everything is faster, from boot-up, to app launch, to file reads, to file writes.

Another way to keep your system Snappy™ is to keep your files and filesystem de-fragmented. Tiger has some limited de-fragmentation built in.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: DavidRavenMoon on Feb 14, '06 07:59:45AM

Just keep in mind that the performance you might gain from having a VM partition on the same physical drive as your OS, as far as not having fragmented files, is (probably) lost by the drive head having to travel farther.

This is true of partitions in general. You keep files organized and unfragmented (maybe), but you make the drive do more work.

You also have to make sure the VM partition is large enough. Five times the installed RAM is ideal... double is the bare minimum.

And that brings me to the last point... load up your Mac with enough RAM and you won't have to worry about swap files. I have 1.5 GB on this G5 (my work machine), and right now, with a bunch of apps open, including Photoshop CS2, Illustrator CS2, Quark 6.5, Acrobat 7 Pro, Classic, Safari, Entourage, etc. I have one 64 MB swap file. Putting that file on another partition, or drive for that matter, won't speed up a thing!

:)

---
G4/Digital Audio/1GHz, 1 GB, Mac OS X 10.4.4
www.david-schwab.com
www.imanicoppola.net



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 14, '06 06:07:05PM

Forgive me, but I'm not following you. How would reading a non-continuous stream of data be just as fast as reading a continuous stream?

In a continuous stream of data, the read-head does not need to jump around the platter. Every time the read-head jumps around the platter, your performance is decremented. If you have a really large file that is in many different chunks in different places of your drive's platter, then that theoretical decrement of performance increases in magnitude. If you have several large files that you are currently using, that are in many different chunks in different places of your drive's platter, then your read-head is doing a virtual square dance, and you have to wait for it to finish the Do-si-do. That's not even considering the overhead of having a file-system b-tree that's so bogged with extents branches that it looks like willow tree.

I'm not saying that any of this matters for most users - it doesn't matter for most users. But this hint isn't for most users.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 14, '06 06:36:44PM

I'm not sure where you have come up with the idea of having 5x RAM as a size for your VM partition. I have always read that 2x RAM is ideal.

"Choosing the right amount of swap space can be a bit of an art. A good rule of thumb is that your swap space should be two or three times as much as the available physical memory (RAM). You should also have at least 64 MB of swap, so if you have less than 32 MB of RAM in your computer then set the swap amount to 64 MB." - FreeBSD install guide

"Your swap partitions should total at least twice the size of the amount of RAM memory you have installed in your machine." - vmunix install guide

"Many people follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. This rule is nonsense. On a modern system, that's a LOT of swap, most people prefer that their systems never swap. Use what is appropriate for your needs... your swap space should be [at least] slightly larger than the amount of main memory you are likely to ever have in the system." - OpenBSD FAQ

I used to have 1.5 GB of RAM and I used to use swap a bunch. I have 3 GB of dedicated swap space, but I've never hit the ceiling on that. Since I've upgraded to 2 GB of RAM, I almost never go beyond the initial 64 MB swapfile. But occasionally I do. And when I do, I'm usually on a deadline, and I need things to move as quickly as possible.

I think that you may be confusing swap space with scratch space. Applications like photoshop use scratch files (basically VM for a specific application). And yes, for photoshop, it is recommended that you allocate at least 5x - of your largest file size that you'd ever work with in photoshop. So if you work with 5 200 MB photoshop files concurrently (200MB is a really big file - for comparison a 7.1 megapixel RAW file is about 10 MB), you should have 5 GB of dedicated scratch space. But you should not typically have your swap and scratch in the same place. In fact, no only do you want them on different partitions, but if you can, you want them to be on different drives, or better yet, different busses.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: syzygies on Feb 11, '06 10:12:06AM

10.3 methods didn't work for me in 10.4. If one looks at the previous 'Hint' on this subject, the 10.3 methods didn't work in 10.4 for other people, either. That hint described moving the swap code to the end of /etc/rc, after the drives had a chance to mount, but also after a slew of other processes started without the benefit of virtual memory. I didn't like the idea of extensively editing /etc/rc, it's much cleaner to insert one line into /etc/rc, that does nothing if the helper file /etc/rc.swapfile isn't present.

What baffles me is why my new approach only works in 10.4, not also in 10.3. The snippets of code I experimented with and put together all come from ideas batted around since 10.0.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Feb 11, '06 08:55:44PM
uhh... just looked in my /etc/ directory, and I found some interesting "rc" files:
$ dir | grep rc
-rw-r--r--    1 root  wheel    187B Mar 20 18:39:12 2005 bashrc
-rw-r--r--    1 root  wheel    189B Mar 20 19:04:08 2005 csh.cshrc
-rw-r--r--    1 root  wheel    587B Aug 22 09:55:31 2005 efax.rc
-rw-r--r--    1 root  wheel    106B Mar 26 22:54:41 2005 mail.rc
-r--r--r--    1 root  wheel     11B Mar 20 19:03:29 2005 nanorc
-r--r--r--    1 root  wheel      7K Jul  4 23:46:54 2005 rc
-r--r--r--    1 root  wheel      7K Jun 10 00:43:49 2004 rc.10_3_4
-r--r--r--    1 root  wheel      8K Jan  1 12:08:47 2004 rc.bak
-rw-r--r--    1 root  wheel      1K Mar 23 20:49:08 2005 rc.common
-r--r--r--    1 root  wheel     10K Jan  1 21:29:19 2004 rc.*censored*ed
-rw-r--r--    1 root  wheel      4K Mar 23 20:49:08 2005 rc.netboot
-r--r--r--    1 root  wheel      7K Dec 29 01:45:11 2003 rc.orig
-r--r--r--    1 root  wheel      7K Jul  4 23:40:59 2005 rc.saved
-rw-r--r--    1 root  wheel    183B Mar 23 20:49:08 2005 rc.shutdown
-rw-r--r--    1 root  wheel    606B Jul  4 23:42:37 2005 rc.swap
-rw-r--r--    1 root  wheel      1K Mar 18 02:58:14 2004 rc.swapfile
That one that is censored starts with an f and rhymes with duck. Anyone else have this kind of variety in their etc folder?

[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: oink on Feb 12, '06 11:21:23PM

Getting this in the log:
...
/sbin/mount -vt hfs /dev/disk0s7 /Volumes/swap
mount_hfs: Resource busy
mount_hfs: Resource busy
mount_hfs: Resource busy
mount_hfs: Resource busy
mount_hfs: Resource busy
Unable to mount swap volume swap; using default configuration

My swap is on a different partition on the same drive. I have found nothing useful about the "busy resource", any pointers? Thanks


---
blurred visionary



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: oink on Feb 13, '06 06:48:56AM

Noticed that despite the log above, swapfile seems to be moved to the partition and is working:

% ps -wwax | grep dynamic_pager -m1
93 ?? Ss 0:00.00 /sbin/dynamic_pager -F /Volumes/swap/.vm/swapfile

Why does it say:
Unable to mount swap volume swap; using default configuration ?


---
blurred visionary



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: syzygies on Feb 13, '06 09:43:48AM

I'm baffled how you could see both of these responses on the same boot. Did you reboot? Can you post or send me your entire log file, if my reply doesn't make sense? If you study the code in /etc/rc.swapfile, you'll see that it is written to not set up the alternate swap location after the Unable to mount swap volume swap; using default configuration error message. On the other hand, the conditions causing the mount_hfs: Resource busy error message could vary from boot to boot. My guess is that your log file will show that you rebooted between seeing the error messages and seeing the swapfile move successfully.

The most likely way that you could be getting a mount_hfs: Resource busy error is if somehow the swap volume is mounting on its own. I haven't naturally observed this behavior myself, but I can artificially reproduce these messages by mounting the swap volume before this section of code.

I changed the logic of the mounting loop in /etc/rc.swapfile. I just posted the new version on my web site, which checks to see if the volume is mounted before each try at mounting it using the mount command. As a bonus, this revised code now also works with Panther, increasing the odds that it will also work unmodified with future versions of OS X.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: oink on Feb 13, '06 10:03:05AM

MY BAD! I just found two instances of:
if [ -f /etc/rc.swapfile ]; then . /etc/rc.swapfile; fi # inserted locally

in my hand edited rc file.

My log contains about 6 similar instances of these lines corresonponding to 6 reboots:
---------
Mon Feb 13 09:31:22 EST 2006
/dev/disk0 aafc5883ce0a2aea473cb0103381e017
/sbin/fsck -y /dev/disk0s7
/sbin/mount -vt hfs /dev/disk0s7 /Volumes/swap
/dev/disk0s7 on /Volumes/swap (local)
Using /dev/disk0s7 for swapfile

Mon Feb 13 09:31:24 EST 2006
/dev/disk0 aafc5883ce0a2aea473cb0103381e017
/sbin/fsck -y /dev/disk0s7
/sbin/mount -vt hfs /dev/disk0s7 /Volumes/swap
mount_hfs: Resource busy
mount_hfs: Resource busy
mount_hfs: Resource busy
mount_hfs: Resource busy
mount_hfs: Resource busy
Unable to mount swap volume swap; using default configuration
---------

I was overwhelmed by the error and never noticed the first part of the log which successfully created and used the swapfile on my swap partition.

Thank you again. And sorry for my unnecessary alarm.



---
blurred visionary



[ Reply to This | # ]
A simpler way to mount drives - 100%
Authored by: Cat2Mac on Feb 13, '06 08:49:03PM
Several ways to mount a specified disk @ boot have been suggested ... involving scripts, partition size, & other non-trivial ways of determining the unique identity of a drive/partition. Instead of these methods why not use a constant, unique identifier that is already present in each partition ... the UUID.

I have used the following method, with 100% success for about 18+ months on both 10.3 & 10.4, to mount a second SATA drive I added to my G5 PowerMac as the "/Users" volume:

1. Open Disk Utility.
2. Select the partition/volume (not physical drive) you want to mount & click Info.
3. Write down the value for "Universal Unique Identifier"
4. Edit /etc/fstab & add the following line:

UUID=xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx/mountpoint hfs rw 1 2

for example: I used "UUID=832760B0-DA18-3865-B22A-C86F16493D15 /Users hfs rw 1 2" to mount my second drive as my "/Users:" directory, putting all user files are on my other drive. (NOTE: The mount point must be a preexisting empty directory.)

5. Reboot.

This info was discussed in an earlier hint, quite a few months ago.

Hope this is helpfull...


[ Reply to This | # ]
A simpler way to mount drives - 100%
Authored by: syzygies on Feb 14, '06 03:12:18PM

Only after a half hour of experimenting did I realize that you never actually said that you tried this for moving the swapfile. Your confidence threw me.

While Apple is deprecating the use of fstab, your method does a great job of eventually mounting arbitrary volumes in arbitrary locations (as
does mounting by name rather than UUID), with two caveats:

1. The maids come in and sweep /Volumes clean between boots, so any mount point created there will be deleted before it can be used.

2. If one creates a mount point elsewhere, e.g. next to Apple's default location for swapfiles, then the volume does not mount in time to be used for virtual memory.

There's a perennial tug-of-war in hints land, between those who love one-liners and feel sorry anyone who just wasted hundreds of lines of code for the same task, and those who view robustly covering every case as a game, and are willing to codify their strategy using hundreds of lines of code.

If one writes for other people, one has to take the second point of view. A Perl script feels heavy-handed compared to proposing a few manual changes, but look at the one trouble report so far here: A manual editing error by someone who didn't use the Perl script.



[ Reply to This | # ]
A simpler way to mount drives - 100%
Authored by: TigerKR on Feb 14, '06 06:50:46PM

Your suggestion to use fstab will only work 100% of the time for single disk setups. If you have multiple disks, especially across multiple busses, then the disk numbering order won't necessarily be the same after every reboot.

Please see the following link for reference and scroll down to the bottom to read my post: http://www.macosxhints.com/article.php?story=20031104150206554

You can also scroll to the bottom of this page <http://www.xlr8yourmac.com/OSX/OS_x_swapfile_notes.html> and read the comments about using the fstab file.



[ Reply to This | # ]
A simpler way to mount drives - 100%
Authored by: Cat2Mac on Feb 14, '06 08:09:24PM

Each volume/partition has a unique, unchanging UUID.

If you call the volume X & it has a UUID of Y, & your mount point is /Z, then X is Y & will always mount at /Z. I believe the only time the UUID might change is when deleting & recreating the volume. (Note that I have only used this for permament drives, not external/unmountable firewire or USB. SCSI drives should also work.)

Also you can have as many volumes as you want ... just add as many volume definitions as you need to fstab. I only used one, but any number of volumes can be added. If you want 10 volumes then add 10 lines.



[ Reply to This | # ]
A simpler way to mount drives - 100%
Authored by: syzygies on Feb 15, '06 04:21:06AM

UUID fstab mounting also works fine with FireWire drives, you've just got to set the automount preferences right, via

defaults write /Library/Preferences/SystemConfiguration/autodiskmount AutomountDisksWithoutUserLogin -bool true

It won't work for moving the swapfile, because moving the swapfile comes too soon in the boot process. I'd read this widely, and in fact I stated it as a truism on my web page, but after first reading this post I did a double take, "Maybe this behavior changed and I didn't test it recently!?" No, still doesn't work for moving the swap.



[ Reply to This | # ]
Using the UUID to reference volumes
Authored by: Cat2Mac on Feb 15, '06 01:12:58PM
I must cede to your greater knowledge of the nuances of the boot process. My intention was primarily to bring attention to an intrinsic property (that everyone in the thread seemed to be ignoring) of any volume/partition that is:
  1. Generated & recorded on each volume during formatting
  2. UNIQUE to each volume
  3. Stable & unchanging (unless reformatted)
  4. Stays the same even if mounted on a different computer.
Rather than looking for combinations of ephemeral values to identify a volume, why don't you use the one value that is designed to be a unique value: the UUID.

Please let me know if there is some reason why the UUID can't be used to identfy a drive (whether used as a swap volume or some other purpose),

[ Reply to This | # ]
Using the UUID to reference volumes
Authored by: TigerKR on Feb 15, '06 05:01:30PM

This thread is about setting up a swap partition.

You can't use an UUID to setup a swap partition because swap is setup before ANY UUID information is read/used; UUID only comes into play only AFTER the swap is happily swapping along already.

Therefore, this thread is ignoring UUID discussions because this thread is about setting up a swap partition.



[ Reply to This | # ]
10.4: Move swap files to another drive
Authored by: TigerKR on Mar 03, '06 12:04:58AM

I had the pleasure of doing a format and reinstall on a tower, and I used the perl script to move the swap to a different partition. However, it did not work on the first go around. I had to uninstall it, and then install it again, but it did work the second time, and its quite nice to have this excellent perl script do all of the heavy lifting. Thanks Dave.



[ Reply to This | # ]