Wednesday, April 20, 2011

在MACOSX中設置RAMDISK

換了SSD後,為了空間,(因為經費有限,只能買128G的SSD)
考量了SSD使用年限,於是找了一下設置RAMDISK的方式。

設置方式如下:
1. 啟用「noatime」:新增檔案「/Library/LaunchDaemons/com.nullvision.noatime.plist」,檔案內容如下。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.nullvision.noatime</string>
<key>ProgramArguments</key>
<array>
<string>mount</string>
<string>-vuwo</string>
<string>noatime</string>
<string>/</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

2. 變更檔案權限。
sudo chown root:wheel /Library/LaunchDaemons/com.nullvision.noatime.plist

3. 重開機。
重開機後,執行
$ mount | grep " / "
/dev/disk0s2 on / (hfs, local, journaled, noatime)
應該會有noatime的字串出現

4. 新增「RamFS」的目錄,與修改目錄權限。
cd /System/Library/StartupItems
mkdir RamFS
sudo chown -R root:wheel RamFS
sudo chmod -R u+rwX,g+rX,o+rX RamFS

5. 新增檔案「/System/Library/StartupItems/RamFS/RamFS」,檔案內容如下。
#!/bin/sh
# Create a RAM disk with same perms as mountpoint

RAMDisk() {
mntpt=$1
rdsize=$(($2*1024*1024/512))
echo "Creating RamFS for $mntpt"
# Create the RAM disk.
dev=`hdik -drivekey system-image=yes -nomount ram://$rdsize`
# Successfull creation…
if [ $? -eq 0 ] ; then
# Create HFS on the RAM volume.
newfs_hfs $dev
# Store permissions from old mount point.
eval `/usr/bin/stat -s $mntpt`
# Mount the RAM disk to the target mount point.
mount -t hfs -o union -o nobrowse $dev $mntpt
# Restore permissions like they were on old volume.
chown $st_uid:$st_gid $mntpt
chmod $st_mode $mntpt
fi
}

# Test for arguments.
if [ -z $1 ]; then
echo "Usage: $0 [start|stop|restart] "
exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common

StartService () {
ConsoleMessage "Starting RamFS disks..."
#這邊可以設定RAMDISK的SIZE。512表示是512MB的意思。
RAMDisk /private/tmp 512
RAMDisk /var/run 64
#RAMDisk /var/db 256
#mkdir -m 1777 /var/db/mds
}
StopService () {
ConsoleMessage "Stopping RamFS disks, nothing will be done here..."
# diskutil unmount /private/tmp /private/var/run
# diskutil unmount /private/var/run
}

RestartService () {
ConsoleMessage "Restarting RamFS disks, nothing will be done here..."
}

RunService "$1"

6. 新增檔案「/System/Library/StartupItems/RamFS/StartupParameters.plist」,檔案內容如下。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>Description</key>
<string>RamFS Disks Manager</string>
<key>OrderPreference</key>
<string>Early</string>
<key>Provides</key>
<array>
<string>RamFS</string>
</array>
<key>Uses</key>
<array>
<string>Disks</string>
</array>
</dict>
</plist>

7. 把「~/Library/Caches」移到RAMDISK中。(可做可不做,重開機的話,Browser的cache,cookie,history都會消失)
cd ~/Library
mv Caches Caches.old
ln -s /private/tmp Caches

8. 把hibernate的功能關掉:hibernate檔案的SIZE是依照記憶體大小來配置。所以如果RAM是4G,hibernate的檔案就是4G。而MACOSX只有在電池快沒電時,才會使用到hibernate的功能。用到的機會幾乎等於0,建議把hibernate的功能拿掉,可以節省不少空間。方式如下:
$ sudo pmset -a hibernatemode 0
$ sudo rm /var/vm/sleepimage

9. 重開機

==
Reference:
Mac OS X SSD tweaks
Mac OS X SSD tweaks (revised for 10.6.5 – thanks to Pierre)

No comments:

Post a Comment