Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 31 votes, 4.97 average. Display Modes
Old 11-19-2007, 06:34 AM   #1
adrinux
Registered User
 
Join Date: Nov 2007
Posts: 2
Red face Moving Swap to another partition, Leopard redux

I was going to submit this as a tip, but it could really do with more testing, and probably more work.

Those of us who used a previous tip to move swap to a different partition http:
//www.macosxhints.com/article.php?story=20050601094611565&query=move%2Bswap have found ourselves scratching our heads in Leopard, we can't modify /etc/rc because it doesn't exist any more!

So now we have to use Launchd. Swapfiles in OSX are written by dynamic_pager and there's a launchd plist from that in the system folder: /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

It defines the default swap location /var/vm, and we can change that to use our prefered swap partition. Personally I also dislike the exponentially increasing swapfile size Tiger and Leopard use (64, 128, 256, 512, 1024 MB and so on), so I also define a set size swapfile (156MB, remember Panther used 80MB by default) by passing command line options in the plist.

My /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist ends up looking like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
                <key>Label</key>
                <string>com.apple.dynamic_pager</string>
                <key>ProgramArguments</key>
                <array>
                     <string>/sbin/dynamic_pager</string>
                     <string>-F</string>
                     <string>/Volumes/Swap/.vm/swapfile</string>
                     <string>-H</string>
                     <string>10000000</string>
                     <string>-L</string>
                     <string>183000000</string>
                     <string>-S</string>
                     <string>163410000</string>
                </array>
                <key>OnDemand</key>
                <false/>
</dict>
</plist>
WARNING If you're thinking of changing those numbers please read 'man dynamic_pager' first and note the requirements for the relative sizes of those three numbers. If you just want to move swap you can leave those command line options out:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
                <key>Label</key>
                <string>com.apple.dynamic_pager</string>
                <key>ProgramArguments</key>
                <array>
                     <string>/sbin/dynamic_pager</string>
                     <string>-F</string>
                     <string>/Volumes/Swap/.vm/swapfile</string>
                </array>
                <key>OnDemand</key>
                <false/>
</dict>
</plist>
After rebooting we can check where swapfiles are now being written:
Code:
ps -wax | grep dynamic_pager -m1
And check the size:
Code:
ls -al /Volumes/Swap/.vm
which gives this in my case:
Code:
-rw------T   1 root    wheel   163410000 12 Nov 15:55 swapfile0
So I manually cleaned out the old swapfile:
Code:
sudo rm -rf /var/vm/swapfile0
I also checked the logs:
Code:
grep Swap /var/log/system.log | tail
Which gives:
Code:
Nov 16 10:59:32 localhost com.apple.dynamic_pager[40]: dynamic_pager: cannot create paging file /Volumes/Swap/.vm/swapfile0!
Nov 16 10:59:32 localhost com.apple.launchd[1] (com.apple.dynamic_pager[40]): Exited with exit code: 1
Nov 16 10:59:32 localhost com.apple.launchd[1] (com.apple.dynamic_pager): Throttling respawn: Will start in 7 seconds
Nov 16 10:59:33 localhost /System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow[34]: Login Window Application Started
Nov 16 10:59:34 localhost com.apple.launchd[1] (com.apple.dynamic_pager): Throttling respawn: Will start in 5 seconds
Nov 16 10:59:34: --- last message repeated 1 time ---
Nov 16 10:59:34 localhost kextd[10]: writing kernel link data to /var/run/mach.sym
Nov 16 10:59:34 localhost com.apple.launchd[1] (com.apple.dynamic_pager): Throttling respawn: Will start in 4 seconds
What this tells us is that the problem with with the swap partition not being mounted when dynamic_pager is first launched still exists in Leopard, however launchd simply respawns dynamic_pager in 8-10 seconds and the swap partition is found.

Notes
  • I've only tested this on a powerpc machine, however I don't see why this wouldn't work just as well on an intel mac, YMMV, be warned, make a backup etc.
  • I been using this for several days, it works fine across normal restarts and shutdowns.
  • I haven't tested if it works after a forced reboot, for obvious reasons.
  • I have worries about what would happen if the swap partition failed to mount, due to disk failure for instance. Again, I'm reluctant to disconect the disk to see what happens, maybe someone else can test that It should be fine if dynamic_pager has a hard coded default, IIRC from Tiger, it does.

I do however feel like this could be a little more robust. Cleaning up old swapfiles in the original location should be automatic. It might be better to wait for the swap partition to mount before running dynamic pager.
In theory it should be possible to write a script for this, and create a launchd plist to run the script, and we could disable the dynamic_pager.plist. But because this just works, I'm not so motivated to do further work

* The benefit of moving swap to a different disk partition has been debated before, so please keep your comments useful, we really don't need yet another 'why bother' comment thread :P

Hope this helps and I look forward to seeing how other people have dealt with this.
adrinux is offline   Reply With Quote
Old 11-20-2007, 10:47 AM   #2
cmdavis3rd
Registered User
 
Join Date: Nov 2007
Posts: 1
Thank you very much for this. We have several 2.66 MacPros that are going to get upgraded to Leopard. One of our test machines (2.66, 3GB RAM) was crawling to a halt while running several intensive applications, ie. Photoshop, Illustrator, Parallels running Rhino, and a few others. This machine ran just fine in Tiger.

I formatted a 120GB SATA drive, named it Swap, and then used terminal to create a .vm folder on that volume. I changed the .plist to point to the new volume.

Things seem to be all well and the system is running at what feels like a normal pace. It is also using more swap space overall, which is fine since it has 109GB of formatted drive space to fill if needed (not likely, but...).

Anyhow, after less than a day of testing I would say that things seem fine. I would also say the question of if this works on a intel has been answered.

Carl
cmdavis3rd is offline   Reply With Quote
Old 11-21-2007, 09:36 AM   #3
adrinux
Registered User
 
Join Date: Nov 2007
Posts: 2
You're welcome

Just answered another of my questions. Opened up my G4 and pulled out the power cable to the disk with my swap partition on. It started up fine - but with no swap! system.log showed launchd repeatedly trying to start dynamic_pager which obviously couldn't find the swap partition, so it just kept re-spawning ad infinitum.

Personally I'm happy with this as is. I monitor all my partitions current sizes on the desktop with geektool, so it'd be pretty obvious to me that swap had gone awol before anything went wrong.

Maybe I'll get around to putting together a script eventually...
adrinux is offline   Reply With Quote
Old 11-29-2007, 12:05 PM   #4
tokamac
Prospect
 
Join Date: Nov 2007
Posts: 2
Hi,
I tried your method (Mac OS X Leopard 10.5.1 on Power Mac G5 2x2 GHz with two SATA disks) and it seemed to work fine at first.
I say "it seemed at first" because in fact the virtual memory was still located on the same disk (Macintosh HD) althoug on a different mount point, labelled exactly like the GUI name of my second partition!

I have two separated disks in my Power Mac, each with one journaled HFS+ partition:
- The partition of the first disk is named Macintosh HD and has Leopard on it.
- The partition of the second disk is named Data.

So in the file /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
instead of the line
<string>/private/var/vm/swapfile</string>
I wrote
<string>/Volumes/Data/.vm/swapfile</string>

Of course after making the .vm folder on Data. I was careful enough to give correct permissions on the partition Data, the directory .vm and the file com.apple.dynamic_pager.plist. I even auto corrected permissions with Disk Utility before restarting my Mac.

After reboot, Terminal told me everything was fine from launchd process to swapfile0 creation in the new location.
BUT!
Although the name of my second disk still appeared as "Data" in Finder, the name had changed to "Data 1" in Terminal and Disk Utility, and for all System calls. The .vm folder I created on it did not contained any swapfile.
I went to /Volumes/ and I saw why. A new mount point named "Data" had been automatically created in the form of a folder containing a new .vm directory, on the first disk Macintosh HD.

So it seemed instead of re-spawning, waiting for the Data partition to mount, my Leopard decided to create this mount point on my boot volume.

I tried to correct the problem by trashing the false Data mount point, renaming correctly my partition back from "Data 1" to "Data" and assigning a fix mount point and fix mount name for it with its UUID in /etc/fstab.

The result was a partition keeping its correct name "Data" after reboot, and no new weird mount point creation anymore.
But the Virtual Memory is now disabled. None swapfile is made inside /Volumes/Data/.vm/ although Terminal says everything is fine (dynamic_pager, launchd, system.log). No re-spawing, and a ps -wax | grep dynamic_pager -ml shows the good location where the swap files should be written.

If I go back to the default setup (aka /private/var/vm/) the virtual memory works fine again.

Do you have any idea about what's going on?
tokamac is offline   Reply With Quote
Old 11-30-2007, 09:24 AM   #5
tokamac
Prospect
 
Join Date: Nov 2007
Posts: 2
Nevermind, It works now.

I have launched the uninstall command of the Perl script swapfile.pl (from Dave Bayer) that I installed previously in Tiger before upgrading to Leopard. Perhaps the AutomountDisksWithoutUserLogin interfered with the Leopard method.

While rebooting I zapped the PRAM four times (Command-alt-P-R). I don't know which one made the tip work, but now it works
tokamac is offline   Reply With Quote
Old 12-02-2007, 08:46 AM   #6
adrinux
Registered User
 
Join Date: Nov 2007
Posts: 2
Glad you got it sorted, I'd say it probably was the old script causing issues...keep an eye on things though

I've got three IDE disks in my G4, it's had to see why the IDA/SATA difference would cause problems though.

I always assume having journaling switched on in my swap partition is a bad idea. But then I'm using a separate 5Gi partition purely for swap (on a disk that's used for little else). I also disable spotlight indexing on the swap partition, since it's pointless.

Good point about permissions on the new swap location! Since I was just pointing Leopard to the same location I was using in Tiger I didn't have to think about that
adrinux is offline   Reply With Quote
Old 12-13-2007, 11:40 AM   #7
adrianchapman
Prospect
 
Join Date: Dec 2007
Posts: 4
I have managed to do this on my Mac Pro with two Seagate SATA drives, but I have only been partially successful. I created a swap partition (called Swap) on the second drive but when I do a cold startup it would seem that this volume isn't mounted soon enough for it to be used as the swap partition, resulting in /Volumes containing an alias to "Swap 1" and a Folder containing the swap file on the boot drive.

If I perform a restart, everything works perfectly,except I now have to delete the folder from /Volumes. This is 100% repeatable and I have even tried changing the physical locations of the two drives.

Any ideas?

Adrian
adrianchapman is offline   Reply With Quote
Old 12-13-2007, 12:46 PM   #8
nihad
Registered User
 
Join Date: Dec 2007
Posts: 1
The same thing is happening to me as well... "Swap 1" is created as partition "Swap" is not mounted at the point dynamic_pager is ran...

I wrote about it couple of weeks ago, but my post never got published... (hopefully this one will. i may be doing something wrong while posting)
nihad is offline   Reply With Quote
Old 01-02-2008, 07:47 AM   #9
adrianchapman
Prospect
 
Join Date: Dec 2007
Posts: 4
Problem solved. When I created the Swap partition I made Owner/Group root/wheel. I changed this to root/admin and all is well.
adrianchapman is offline   Reply With Quote
Old 01-03-2008, 04:40 AM   #10
adrianchapman
Prospect
 
Join Date: Dec 2007
Posts: 4
My optimism was premature, for some reason I am back to square one
adrianchapman is offline   Reply With Quote
Old 01-17-2008, 07:35 PM   #11
walk_n_wind
Registered User
 
Join Date: Jan 2008
Posts: 1
Move swap to a flash device?

Does anyone think it would be beneficial to move the swap to a USB stick? I've been looking for ways to speed up my iBook G4 (with Leopard), and I was thinking about using this method to place the swap on something small and portable (and hopefully fast)...

I'll give this a try asap : )
walk_n_wind is offline   Reply With Quote
Old 01-26-2008, 11:51 AM   #12
marcushedenstrom
Prospect
 
Join Date: Jan 2008
Location: Göteborg, Sweden
Posts: 3
Actually, I think the issue is in fact NOT that the dedicated swap volume is not yet mounted at the point of dynamic_pager starting up. Rather, the issue is that the mount point directory in '/Volumes/' fails to unmount, or being removed properly, at shutdown.

I've got a separate ATA/100 drive hooked up inside my Powermac G4 MDD, in addition to the main OS drive which sits on a SATA bus (PCI-controller). I've created a 4 GiB volume on the ATA100-drive, and named it "swap".

If I do not attempt to change the swap directory at all, i.e. leaving the dynamic_pager.plist unmodified, this volume mounts as '/Volumes/swap' as intended. If I then go ahead and change the swap directory path to '/Volumes/swap/vm/swapfile', everything works after the _first_ subsequent boot process. The swap volume mounts at '/Volumes/swap' with the swapfiles on it. No '/Volumes/swap 1' exists at this point.

However, if I reboot once more, the swap volume will mount at '/Volumes/swap 1' instead, and the swapfiles are placed in a regular directory at '/Volumes/swap', and not on the dedicated swap volume.

What I think is happening, is that the '/Volumes/swap' mountpoint is not cleared out of the way properly at shutdown, thus forcing the swap volume to mount as "swap 1" ever thereafter.

If I manually remove the '/Volumes/swap' directory before rebooting, everything gets setup properly after reboot, with the swapfiles on the swap volume mounted at '/Volumes/swap' and there is no "swap 1" in sight.
marcushedenstrom is offline   Reply With Quote
Old 01-26-2008, 04:26 PM   #13
marcushedenstrom
Prospect
 
Join Date: Jan 2008
Location: Göteborg, Sweden
Posts: 3
Okay, I tried something else. I removed the dynamic_pager property list from '/System/Library/LaunchDaemons/', to avoid launchd from intefering with the process in any way.

Then I added these two lines to 'rc.common', way down at the end of the file to make it execute as late as possible.

Code:
rm /Volumes/swap/vm/swapfile*
dynamic_pager -F /Volumes/swap/vm/swapfile
And hey, it works!! I've rebooted numerous times, and every single time I end up with "swap" properly mounted, with one initial swapfile created on it. Having two gigs of memory in this machine, I'll have to wait and see if more swapfiles are created as they become nessecary. But I assume they will.

What do you think? Is this a sensible way of moving the swap files, or have I overlooked any important aspect?
marcushedenstrom is offline   Reply With Quote
Old 02-19-2008, 03:07 AM   #14
E_James
Prospect
 
Join Date: Feb 2008
Posts: 11
After a little research and experimentation, here is my solution:

I modified the 'com.apple.dynamic_pager.plist' file so that it launches a shell script '/sbin/dynamic_pager_init' instead of simply launching '/sbin/dynamic_pager'. The shell script will not launch the dynamic_pager until the swap volume has mounted properly. This solution makes use of the fact that the Apple's launchd process will keep executing the dynamic_pager_init script every 2 - 10 seconds until the dynamic_pager is running.

/System/Library/LaunchDaemons/com.apple.dynamic_pager.plist:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
                <key>Label</key>
                <string>com.apple.dynamic_pager</string>
                <key>ProgramArguments</key>
                <array>
                     <string>/sbin/dynamic_pager_init</string>
                </array>
                <key>OnDemand</key>
                <false/>
</dict>
</plist>
/sbin/dynamic_pager_init:
Code:
#!/bin/bash
#launch Apple's dynamic_pager only when the swap volume is mounted

if [ "x`df -H | grep /Volumes/Swap`" = "x" ]; then
        echo "Waiting for Swap volume to mount";
else
        echo "Launching dynamic pager on volume Swap";
        /sbin/dynamic_pager -F /Volumes/Swap/.vm/swapfile;
fi
I am relatively new to shell scripting and unix commands, so there may be better ways of implementing the shell script. I have a feeling that 'df -H | grep...' command is not the most efficient solution, but it does work on my system.

I would love to get some feedback on this approach.
E_James is offline   Reply With Quote
Old 02-25-2008, 07:28 PM   #15
Anonymous Prime
Prospect
 
Join Date: Feb 2008
Posts: 3
E James: You may want to have a way for your setup to "fail gracefully"-- After ten attempts or 30 seconds or so, give up and let dynamic_pager load under its default settings.
Anonymous Prime is offline   Reply With Quote
Old 02-25-2008, 11:21 PM   #16
Anonymous Prime
Prospect
 
Join Date: Feb 2008
Posts: 3
Well, I tried it out anyway, and my fears were confirmed. I keep getting this in the system log:

Code:
com.apple.launchd[1] (com.apple.dynamic_pager[446]): posix_spawnp("/sbin/dynamic_pager_init", ...): Permission denied
com.apple.launchd[1] (com.apple.dynamic_pager[446]): Exited with exit code: 1
com.apple.launchd[1] (com.apple.dynamic_pager): Throttling respawn: Will start in 10 seconds
It just keeps doing that every 10 seconds, until I replace com.apple.dynamic_pager.plist with the original, repair permissions, and restart.

Repairing permissions in Disk Utility doesn't help (although it does fix com.apple.dynamic_pager.plist). I've tried changing permissions in Get Info and with chmod in Terminal, but I have little idea of what I'm doing there.
Anonymous Prime is offline   Reply With Quote
Old 02-28-2008, 01:04 AM   #17
E_James
Prospect
 
Join Date: Feb 2008
Posts: 11
Thank you. I should have made a note of the permissions on that 'dynamic_pager_init' file.

Just execute the following two commands in a terminal window. You will be asked for your password because of the 'sudo' command.
sudo chmod a+x-w /sbin/dynamic_pager_init
sudo chown root:wheel /sbin/dynamic_pager_init
The first command (chmod) adds the 'execute' permission (+x) to the file so that it can be run as a command, and removes the 'write' permission (-w) so that it can not be edited accidentally. The second command (chown) makes 'root' the owner of the file, and makes 'wheel' its group. This matches most of the other daemons and scripts in the '/sbin' directory.

You can verify that the file has been set up properly by typing the following:
ls -al /sbin/dynamic_pager_init
Which should return something like:
-r-xr-xr-x 1 root wheel 281 19 Feb 01:38 dynamic_pager_init
I hope that helps. Please let me know if you run into any more difficulties.
E_James is offline   Reply With Quote
Old 03-06-2008, 08:58 PM   #18
Anonymous Prime
Prospect
 
Join Date: Feb 2008
Posts: 3
Smile

Sorry I took so long to respond; I got sidetracked with other things shortly after I made those responses.

Anyway, I got it to work after a struggle. Apparently you can't just make dynamic_pager_init in TextEdit, use Onyx to make Finder show hidden/system items, move D_P_I over to /sbin and change the permissions/owner. Doing that leaves the file with a "@" in the permissions, like this:
-r-xr-xr-x@ 1 root wheel 281 19 Feb 01:38 dynamic_pager_init
I haven't been able to find out what that means, except that it indicates something that prevents the file from being found:
2008.03.06 08:26:37p com.apple.launchd[1] (com.apple.dynamic_pager[671]) posix_spawnp("/sbin/dynamic_pager_init", ...): No such file or directory
I got around that by creating D_P_I directly in /sbin with pico, and then changing permissions/owner as you specified. All is well, for the moment.

Thank you.

Last edited by Anonymous Prime; 03-06-2008 at 09:02 PM.
Anonymous Prime is offline   Reply With Quote
Old 03-07-2008, 02:55 AM   #19
E_James
Prospect
 
Join Date: Feb 2008
Posts: 11
Glad to hear that it is working for you, now!

The "@" symbol in the file permissions has something to do with access control lists (ACLs), which control extended privilege settings. I'm not sure why that would prevent you from finding the initialization script. Perhaps one of those settings prevents spotlight from indexing the file?
E_James is offline   Reply With Quote
Old 03-22-2008, 01:31 PM   #20
chmood
Prospect
 
Join Date: Mar 2008
Posts: 3
Quote:
Originally Posted by marcushedenstrom
Okay, I tried something else. I removed the dynamic_pager property list from '/System/Library/LaunchDaemons/', to avoid launchd from intefering with the process in any way.

Then I added these two lines to 'rc.common', way down at the end of the file to make it execute as late as possible.

Code:
rm /Volumes/swap/vm/swapfile*
dynamic_pager -F /Volumes/swap/vm/swapfile

Very interested in how this turned out - Marcus, can you update? Any glitches or work-arounds you've had to deal with?

Also, I'd like very much to know where you put dynamic_pager.plist when you removed it from LaunchDaemons[/i].
chmood is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:52 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.