Mount disk from launch daemon on Mac OS X Yosemite

1.8k views Asked by At

i am trying to mount /private/tmp as ram disk. I have this "ramfs.sh" script, which i found from the internet:

#!/bin/bash
ramfs_size_mb=1024
mount_point=/private/tmp

ramfs_size_sectors=$((${ramfs_size_mb}*1024*1024/512))
ramdisk_dev=`hdid -nomount ram://${ramfs_size_sectors}`
newfs_hfs -v 'Volatile HD' ${ramdisk_dev}
mkdir -p ${mount_point}
mount -o noatime -t hfs ${ramdisk_dev} ${mount_point}
chown root:wheel ${mount_point}
chmod 1777 ${mount_point}

It is working fine, if i run it manually from terminal. However i have problem running it from LaunchDemon. I have this contents in the file "/Library/LaunchDaemons/com.kalugin.ramfs-for-db.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.kalugin.ramfs-for-db</string>
        <key>Program</key>
        <string>/var/root/ramfs.sh</string>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardOutPath</key>
        <string>/var/log/ramfs_for_db.log</string>
        <key>StandardErrorPath</key>
        <string>/var/log/ramfs_for_db_error.log</string>
        <key>Debug</key>
        <true/>
    </dict>
</plist>

After system load i have:

/dev/disk1
    #:                       TYPE NAME                    SIZE       IDENTIFIER
    0:                            Volatile HD            *1.1 GB     disk1 

But "mount" doesn't show /private/tmp as mounted on disk1. Logs show only this: "Initialized /dev/rdisk1 as a 1024 MB case-insensitive HFS Plus volume".

So definitely script is executed during system start up, but looks like mount command does not work. Any ideas? Thank you.

EDIT

I added some "echo" in script and make "mount" verbose. Here is output:

Creating ram disk...
Initialized /dev/rdisk1 as a 1024 MB case-insensitive HFS Plus volume
Mounting ram disk...
/dev/disk1 on /private/tmp (hfs, local, noatime)
Setting permissions...

So looks like script is doing fine, and even mounted disk. But looks like during boot "tmp" folder is overwritten?

EDIT2

Looks like everything is fine, except something unmounts my mounted disk on system start up. Also someone noticed this behavior too link.

2

There are 2 answers

0
Clickbeetle On BEST ANSWER

I solved my problem by following plist file:

<?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.local.ramdisk</string>
                <key>Program</key>
                <string>/usr/libexec/ramdisk.sh</string>
                <key>RunAtLoad</key>
                <true/>
                 <key>KeepAlive</key>
                <dict>
                        <key>PathState</key>
                        <dict>
                                <key>/private/tmp/ram</key>
                                <false/>
                        </dict>
                </dict>
                <key>StandardOutPath</key>
                <string>/var/log/ramdisk.log</string>
        </dict>
</plist>

Looks like "RunAtLoad" is not enough or it doesn't work, i don't know. But with "KeepAlive" it is working fine.

8
Mark Setchell On

Updated Answer

I note you are trying to mount your RAMdisk at /private/tmp. I can't point to any concrete evidence, but that is just not a good idea as /tmp is a system directory. I would make a directory under /tmp, like /tmp/RAMdisk or even at the filesystem root in /RAMDisk.

Original Answer

I think the problem is that /sbin is not in your PATH, so the script can't find mount. Try adding this as the second line of your script:

export PATH="/sbin:$PATH"

Likewise the TYPE is missing, which indicates that no filesystem was created on your disk, i.e. that news_hfs failed to run, and that too, is located in /sbin.