๐Ÿคฏ Unmount volume on launch

ยท

1 min read

So here is my setup, I have 2 volumes (macOS Monterey and macOS Ventura), and my goal is to have just the required volume for each macOS

1.png

So when launching macOS Monterey I just want the macOS Ventura volume unmounted.

1) Using diskutil it's very easy to force unmount a volume.

diskutil unmountDisk force /Volumes/macOS\ Ventura

2) Let's create a Launch Agent plist file at $HOME/Library/LaunchAgents and we will call it com.ruiaureliano.unmount.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">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.ruiaureliano.unmount</string>

        <key>RunAtLoad</key>
        <true />

        <key>StandardErrorPath</key>
        <string>/Users/ruiaureliano/.stderr.log</string>

        <key>StandardOutPath</key>
        <string>/Users/ruiaureliano/.stdout.log</string>

        <key>WorkingDirectory</key>
        <string>/Users/ruiaureliano</string>

        <key>ProgramArguments</key>
        <array>
            <string>/bin/sh</string>
            <string>.unmount-disk.sh</string>
        </array>

    </dict>
</plist>

4) Let's create the .unmount-disk.sh that will be executed on launch, it should be placed on WorkingDirectory that we defined on the list, in my case /Users/ruiaureliano/.unmount-disk.sh

#!/bin/sh
echo "macOS ventura unmount"
/usr/sbin/diskutil unmountDisk force /Volumes/macOS\ Ventura

3) Now we just load the plist file using launchctl tool.

 launchctl load $HOME/Library/LaunchAgents/com.ruiaureliano.unmount.plist

If we need to unload

 launchctl unload $HOME/Library/LaunchAgents/com.ruiaureliano.unmount.plist

Restart your Mac and voila ๐Ÿง™โ€โ™‚๏ธ๐Ÿง™โ€โ™‚๏ธ๐Ÿง™โ€โ™‚๏ธ

ย