Skip to content

Instantly share code, notes, and snippets.

@snnwolf
Forked from Roman2K/mount-tmp.sh
Last active November 13, 2015 22:46
Show Gist options
  • Select an option

  • Save snnwolf/cf035ceb5c6cc9e98e05 to your computer and use it in GitHub Desktop.

Select an option

Save snnwolf/cf035ceb5c6cc9e98e05 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
size=1024 # MB
mount_point=$HOME/tmp
name=$(basename "$mount_point")
usage() {
echo "usage: $(basename "$0") [mount | umount | remount | check | orphan]" \
"(default: mount)" >&2
}
process_argv() {
[ $# -ge 0 -a $# -le 1 ] || { usage; exit 1; }
case "$1" in
"") cmd_mount ;;
"mount") cmd_mount ;;
"umount") cmd_umount ;;
"remount") cmd_remount ;;
"check") cmd_check ;;
"orphan") cmd_orphan ;;
*) usage ;;
esac
}
cmd_check() {
is_mounted
}
cmd_mount() {
if is_mounted; then
echo "Already mounted at $mount_point" >&2
return 1
fi
sectors=$(( $size * 1024 * 1024 / 512 ))
dev=$(hdiutil attach -nomount ram://$sectors) \
&& newfs_hfs -v "$name" $dev \
&& mount -t hfs -o nobrowse $dev "$mount_point" \
&& echo "Hello, world!" > "$mount_point"/hello_world \
&& echo "Mounted $name ($size MB) at $mount_point"
}
cmd_umount() {
dev=$(df "$mount_point" | tail -1 | awk '{ print $1 }')
[ $dev ] || return 1
umount "$mount_point" && hdiutil detach "$dev"
}
cmd_remount() {
if is_mounted; then cmd_umount else true; fi \
&& cmd_mount
}
cmd_orphan() {
hdiutil info | egrep 'image-path\s+: ram://' -A14 | egrep '^/dev/disk\d+\s*$'
return 0
}
is_mounted() {
mount | grep -q " on $mount_point "
}
process_argv "$@"

Mounting a RAMFS disk at boot in Mac OS X По ссылке инструкция создания tmpfs устарела, потому берем только загрузочный скрипт.

  • Open Terminal (Applications -> Utilities), type “sudo su” and enter your user’s password.
  • Create a new file in /Library/LaunchDaemons, like this: “nano -w /Library/LaunchDaemons/com.yeri.ramfs.plist” (you can rename yeri to whatever you like)
  • And insert following content (ctrl+x to save – y – [enter]):
<?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.yeri.ramfs</string>
        <key>ProgramArguments</key>
        <array>
            <string>/var/root/ramfs.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>
  • create a second file in /var/root/, like this: “nano -w /var/root/ramfs.sh“ And insert following content: (можно пропустил - устарело)
#!/bin/bash
ramfs_size_mb=64
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}
  • chmod +x ramfs.sh and reboot (не забываем перекреститься/постучать по дереву или в бубен). Check in Terminal with “mount” or “df -h” is everything is fine. To hide the disk icon on your desktop, check my old blog post.
  • In case Mac didn’t do so already, you might want to link /tmp to /private/tmp: “rm -r /tmp && ln -s /private/tmp /tmp“. Should be it !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment