简体   繁体   English

从Mac OS X Yosemite上的启动守护程序挂载磁盘

[英]Mount disk from launch daemon on Mac OS X Yosemite

i am trying to mount /private/tmp as ram disk. 我正在尝试挂载/ private / tmp作为ram磁盘。 I have this "ramfs.sh" script, which i found from the internet: 我有这个“ramfs.sh”脚本,我从互联网上找到:

#!/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. 但是我从LaunchDemon运行它有问题。 I have this contents in the file "/Library/LaunchDaemons/com.kalugin.ramfs-for-db.plist": 我在“/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. 但是“mount”没有显示挂载在disk1上的/ private / tmp。 Logs show only this: "Initialized /dev/rdisk1 as a 1024 MB case-insensitive HFS Plus volume". 日志仅显示:“初始化/ dev / rdisk1为1024 MB不区分大小写的HFS Plus卷”。

So definitely script is executed during system start up, but looks like mount command does not work. 所以在系统启动期间肯定会执行脚本,但看起来像mount命令不起作用。 Any ideas? 有任何想法吗? Thank you. 谢谢。

EDIT 编辑

I added some "echo" in script and make "mount" verbose. 我在脚本中添加了一些“echo”并使“mount”详细。 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? 但看起来在启动“tmp”文件夹被覆盖?

EDIT2 EDIT2

Looks like everything is fine, except something unmounts my mounted disk on system start up. 看起来一切都很好,除了在系统启动时卸载我挂载的磁盘。 Also someone noticed this behavior too link . 也有人注意到这种行为也链接

Updated Answer 更新的答案

I note you are trying to mount your RAMdisk at /private/tmp . 我注意到你正在尝试将你的RAMdisk挂载到/private/tmp I can't point to any concrete evidence, but that is just not a good idea as /tmp is a system directory. 我不能指出任何具体的证据,但这不是一个好主意,因为/tmp是一个系统目录。 I would make a directory under /tmp , like /tmp/RAMdisk or even at the filesystem root in /RAMDisk . 我会在/tmp下创建一个目录,比如/tmp/RAMdisk ,甚至是/RAMDisk中的文件系统根目录。

Original Answer 原始答案

I think the problem is that /sbin is not in your PATH, so the script can't find mount . 我认为问题是/sbin不在你的PATH中,所以脚本找不到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, ie that news_hfs failed to run, and that too, is located in /sbin . 同样缺少TYPE ,这表示磁盘上没有创建文件系统,即news_hfs无法运行,而且也位于/sbin

I solved my problem by following plist file: 我通过以下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.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. 看起来像“RunAtLoad”是不够的,或者它不起作用,我不知道。 But with "KeepAlive" it is working fine. 但是使用“KeepAlive”它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM