Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Last active October 23, 2024 03:34
Show Gist options
  • Save pythoninthegrass/8073e5e3b24f385c9d9b712f6f243982 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/8073e5e3b24f385c9d9b712f6f243982 to your computer and use it in GitHub Desktop.
sysctl.conf stand-in on macOS 10.15+

com.startup.sysctl.plist

As of at least macOS Catalina 10.15.3, /etc/sysctl.conf values are no longer respected and/or the file straight up doesn't exist (confirmed on macOS Monterey 12.4.)

Ran across a fio shm error: failed to setup shm segment while benchmarking SSDs. The fix was to set shmmni to 4096. The following has to happen:

  • Disable SIP
    • Disclaimer from Apple

      Warning

      Disable SIP only temporarily to perform necessary tasks, and reenable it as soon as possible. Failure to reenable SIP when you are done testing leaves your computer vulnerable to malicious code.

  • Download com.startup.sysctl.plist
  • Move to /Library/LaunchDaemons/com.startup.sysctl.plist
  • Load the PLIST after a few housekeeping items
    # sanity check
    sysctl -a | grep shm
    
    # set permissions
    sudo chown root:wheel /Library/LaunchDaemons/com.startup.sysctl.plist
    
    # validate key-value pairs
    plutil /Library/LaunchDaemons/com.startup.sysctl.plist
    
    # load plist
    sudo launchctl bootstrap system /Library/LaunchDaemons/com.startup.sysctl.plist
    
    # check logs
    tail -f /tmp/sysctl.out
    tail -f /tmp/sysctl.err
    
    # recheck sysctl values
    sysctl -a | grep shm
  • If the shmmni or other values didn't change, a reboot is in order
    • Known good output when values are changed successfully
      λ tail -f /tmp/sysctl.out
      kern.sysv.shmmax: 4194304 -> 4194304
      kern.sysv.shmmin: 1 -> 1
      kern.sysv.shmmni: 32 -> 4096
      kern.sysv.shmseg: 8 -> 8
      kern.sysv.shmall: 1024 -> 1024
      λ sysctl -a | grep shm
      kern.sysv.shmmax: 4194304
      kern.sysv.shmmin: 1
      kern.sysv.shmmni: 4096
      kern.sysv.shmseg: 8
      kern.sysv.shmall: 1024
      security.mac.posixshm_enforce: 1
      security.mac.sysvshm_enforce: 1
      

Sources

macos - Values from sysctl -A don't match /etc/sysctl.conf even after restart - Unix & Linux Stack Exchange

Setting shared memory in Catalina | Apple Developer Forums

c++ - Shared memory "Too many open files" but ipcs doesn't show many allocations - Stack Overflow

kernel - How do I increase the max open files in macOS Big Sur? - Super User

A launchd Tutorial

macos - Values from sysctl -A don't match /etc/sysctl.conf even after restart - Unix & Linux Stack Exchange

macos - "No space left on drive" when trying to allocate more shared memory - Stack Overflow

<?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">
<!-- DISABLE SIP TO USE: macOS Recovery > Utilities > Terminal > `csrutil disable` > Reboot -->
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.startup.sysctl</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>StandardErrorPath</key>
<string>/private/tmp/sysctl.err</string>
<key>StandardOutPath</key>
<string>/private/tmp/sysctl.out</string>
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/sysctl</string>
<string>-w</string>
<string>kern.sysv.shmmax=4194304</string>
<string>kern.sysv.shmmin=1</string>
<string>kern.sysv.shmmni=32</string>
<string>kern.sysv.shmseg=8</string>
<string>kern.sysv.shmall=1024</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment