macOS(Sequoia)でnoatimeに変更
macOSがAPFSになる前は、SSDで/(ルート)のマウントオプションにnoatimeを追加して使っていましたが、システムドライブがAPFSに変換されてからはnoatimeにする方法が当時はわからなかったため、長らくデフォルトのまま使用してきました。
macOSの大事な領域はそもそもreadonlyなのでnoatimeどころではないですね。
Linuxでは何年も前からrelatimeという改善された方式がデフォルトになっていますが、相変わらず「/etc/fstab」にnoatimeを追加するだけでrelatimeからnoatimeへの切り替えが行えます。
Windowsなんかも「fsutil behavior set disablelastaccess 0x1」というコマンドで最終アクセス日時の更新を全体で無効にできます。(Windows Serverはデフォルトで無効です)
macOSではrelatimeは無いようなので、そのあたり、デフォルトの設定で動作が改善されているかは不明です。自分的にはatimeは要らないので急に思い立って久しぶりに無効にしてみました。
設定した環境は
・macOS Sequoia 15.5
・iMac Retina 5K
です。
システムドライブに関しては、
https://apple.stackexchange.com/questions/410714/is-it-possible-to-add-the-noatime-mount-option-to-system-volumes-data
ここに書いてあるそのままで行けます。
起動時にLaunchDaemonsでマウントオプションを変更します。
sudo vi /Library/LaunchDaemons/local.noatime.system.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">
<!-- Install: copy to /Library/LaunchDaemons/, chown root:wheel and chmod 644 -->
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.noatime.system</string>
<key>ProgramArguments</key>
<array>
<string>/bin/zsh</string>
<string>-c</string>
<!-- first get the current mount options to see if there are any default options that we missed to provide here -->
<!-- as mount overwrites the default options except the ones it reads out from the filesystem table -->
<string>mnt='/System/Volumes/Data'; mount | grep -F " $mnt "; mount -vuwo nobrowse,noatime "$mnt"</string>
</array>
<key>StandardOutPath</key>
<string>/tmp/noatime.system.log</string>
<key>StandardErrorPath</key>
<string>/tmp/noatime.system.log</string>
<key>RunAtLoad</key>
<true/>
<key>LaunchOnlyOnce</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
ファイルを作成すると次回起動時から自動的に実行されます。
止める場合は削除すればOKです。
下記のコマンドで手動実行も可能です。
sudo launchctl bootstrap system /Library/LaunchDaemons/local.noatime.system.plist
動作しているかは
cat /tmp/noatime.system.log
を実行して
/dev/disk1s1 on /System/Volumes/Data (apfs, local, journaled, nobrowse, root data)
/dev/disk1s1 on /System/Volumes/Data (apfs, local, journaled, noatime, nobrowse, root data)
noatimeが追加され、デフォルトのオプションが消えていないか確認します。
1行目が元々のマウントオプションで、2行目が変更後のマウントオプションです。
システムドライブだけnoatimeにしたい場合はここまででOKなのですが、
内蔵ディスクのパーティションを分割している場合や、起動時に接続されている外付けディスクは起動時にLaunchDaemonsで処理できません。
システムドライブ以外は、ログイン後にマウントされるためです。
(もしかしたら暗号化されていない場合はLaunchDaemons処理できるかもしれません。未確認です。)
システムドライブ以外は、LaunchAgentsを使用してマウントオプションを変更します。
LaunchAgentsはログインしているユーザーとして実行されますが、sudoでmountコマンドを実行しなければならないので、sudoをパスワードをなしで実行できるようにする必要があります。
現在のユーザーを/etc/sudoers.dにワンライナーで追加するコマンド
echo "$USER ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$USER
mountだけ許可したい場合は下記でもよいと思います
echo "$USER ALL=(ALL) NOPASSWD: /sbin/mount" | sudo tee /etc/sudoers.d/$USER
ログイン時にLaunchAgentsでマウントオプションを変更します。
・内蔵ディスクをパーティション分割した「/Volumes/Data」
・外付けの「/Volumes/Backup」
というドライブがある想定です。
<?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>local.noatime.data</string>
<key>ProgramArguments</key>
<array>
<string>/bin/zsh</string>
<string>-c</string>
<!-- first get the current mount options to see if there are any default options that we missed to provide here -->
<!-- as mount overwrites the default options except the ones it reads out from the filesystem table -->
<string>sleep 15; mnt='/Volumes/Data'; mount | grep -F " $mnt "; sudo mount -vuwo noatime "$mnt"; mnt='/Volumes/Backup'; mount | grep -F " $mnt "; sudo mount -vuwo nodev,nosuid,noowners,noatime "$mnt";</string>
</array>
<key>StandardOutPath</key>
<string>/tmp/noatime.data.log</string>
<key>StandardErrorPath</key>
<string>/tmp/noatime.data.log</string>
<key>RunAtLoad</key>
<true/>
<key>LaunchOnlyOnce</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
ログイン後にOSから自動的にマウントされるまでに時間がかかるようなので、最初に「sleep」で待つようにしています。
「mount」コマンドは「sudo」で実行しています。
ドライブがもっとたくさんある場合はマウントオプションの変更を「;」で続けてください。「sleep」も増やす必要があるかもしれません。
こちらも、ファイルを作成すると次回起動時から自動的に実行されます。
止める場合は削除すればOKです。
下記のコマンドで手動実行も可能です。
launchctl load ~/Library/LaunchAgents/local.noatime.data.plist
動作しているかは、
cat /tmp/noatime.data.log
を実行して
/dev/disk1s2 on /Volumes/Data (apfs, local, journaled)
/dev/disk1s2 on /Volumes/Data (apfs, local, journaled, noatime)
/dev/disk3s1 on /Volumes/Backup (apfs, local, nodev, nosuid, journaled, noowners)
/dev/disk3s1 on /Volumes/Backup (apfs, local, nodev, nosuid, journaled, noowners, noatime)
noatimeが追加され、デフォルトのオプションが消えていないか確認します。
ドライブにつき1行目が元々のマウントオプションで、2行目が変更後のマウントオプションです。