Auto-mount of the contained disk-image, when the external disk is mounted
I have one external SSD drive that automatically mounts a partition Backup. Inside Backup there is a sparse-bundle disk image called TimeMachine.
Backup is not mounted to /Volume
but to a specific folder (synced with a cloud service), via /etc/fstab
:
UUID=4E5D0103-741G-46DA-A79E-F923E5C951D1 /Users/myuser/Cloud\040Folder/Backup apfs rw 0 2
How can I trigger the automatic mount of TimeMachine
, when detecting that Backup
has been mounted?
Something like hdiutil attach /Users/myuser/Cloud\040Folder/Backup/TimeMachine.sparsebundle
, being run when detecting that /Users/myuser/Cloud\040Folder/Backup is present.
Maybe the /etc/auto_master
file might help? (I've never used it before)
Solution 1:
You can create a launchd LaunchDaemon definition with the key “StartOnMount” set to true. This will allow you to have your own script run whenever something is mounted.
Your own script should then just check if “Backup” is mounted without “TimeMachine” being mounted - and if so, mount “TimeMachine”.
You can use the “mount” command to get information on the currently mounted file systems.
Solution 2:
Since the other answer is a bit devoid of explicit/specific details, this is meant to show how it can be done.
In /etc/fstab
I've added the appropriate entry to have the target disk containing the TimeMachine.sparsebundle
file automatically mount to the target location, when inserted, as shown in your post.
As it's not clear yet you are using the -mountpoint
with hdiutil
the following assumes your not, since you removed it, however I will update the bash
code as necessary, if need be.
I have tested the following steps and code, and it works, to automatically have the TimeMachine.sparsebundle
file mounted when the target disk containing it is mounted.
1. Create the bash
script that will be executed whenever a disk is mounted.
In Terminal:
cd /Users/Shared
mkdir bin
cd bin
touch MountTimeMachineSparseBundle
open -e MountTimeMachineSparseBundle
- Copy and paste the example
bash
code into the opened document, replacing_you_
in thehdiutil
command with your short user name, save and close.
#!/bin/bash
if [ "Cloud Folder/Backup" == "$(mount | grep -o "Cloud Folder/Backup")" ]; then
if [ "/Volumes/TimeMachine" != "$(mount | grep -o "/Volumes/TimeMachine")" ]; then
hdiutil attach "/Users/_you_/Cloud Folder/Backup/TimeMachine.sparsebundle"
fi
fi
- Back in Terminal, make the
bash
script executable:
chmod +x MountTimeMachineSparseBundle
2. Create the .plist
file the daemon
will use to execute the bash
script used to automatically have the TimeMachine.sparsebundle
file mounted when the target drive containing it is mounted.
In Terminal:
cd /Library/LaunchDaemons
sudo nano com.my.MountTimeMachineSparseBundle.plist
- Copy and paste the example PLIST XML code into
nano
, save it (^O Enter), and exitnano
(^X).
<?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.my.MountTimeMachineSparseBundle </string>
<key>ProgramArguments</key>
<array>
<string>/Users/Shared/bin/MountTimeMachineSparseBundle</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>StartOnMount</key>
<true/>
</dict>
</plist>
Then:
sudo chmod 0444 com.my.MountTimeMachineSparseBundle.plist
sudo launchctl load com.my.MountTimeMachineSparseBundle.plist
Now when the target disk containing the TimeMachine.sparsebundle
file is mounted, the TimeMachine.sparsebundle
file will also be mounted.