一个在 macOS 上创建 tmpfs 的脚本。

https://gist.github.com/Roman2K/3238fb441e298369198e

鉴于很多人都上不了 Gist,我就直接贴文件了。

亲测可用。

#!/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 "[email protected]"

我这没有特殊配置但是默认就开了tmpfs

tmpfs           3.9G   91M  3.8G    3% /dev/shm
tmpfs           3.9G     0  3.9G    0% /sys/fs/cgroup
tmpfs           8.0G   26M  8.0G    1% /tmp
tmpfs           787M   16K  787M    1% /run/user/1000

我这个xps只有8G内存,所以我还把 swap, zram都开了,希望内存读写能快点。

其实 macOS 不支持 tmpfs,这个脚本创建的是 RAM Disk,和 tmpfs 的主要区别是后者不能动态调节占用内存大小。