Last active
February 7, 2024 19:26
-
-
Save Roman2K/3238fb441e298369198e to your computer and use it in GitHub Desktop.
tmpfs for OS X - https://github.com/Roman2K/bin/blob/master/mount-tmp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" |
The only thing that didn't work for me is echo "Hello, world!" > "$mount_point"/hello_world
😝
In bash, !
has a special meaning even inside double quotes. Use single quotes:
echo 'Hello, world!' > "$mount_point"/hello_world
Or skip it altogether, it's just a demonstration.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This did not work for me on a mac. This one did: https://gist.github.com/koshigoe/822455