Mount home sparsebundle when SSH

Solution 1:

Mount

SSH lets you create a ~/.ssh/rc that will be executed right after the environment has been set up, but before any shell will be active (so beware of that; search for "sshrc" in man ssh(8) for more informations).

So, to mount your FileVault partition you can add this line to your ~/.ssh/rc file:

hdiutil attach /Users/$USER/$USER.sparsebundle -mountpoint /Users/$USER

Don't worry, your ~/.ssh/rc will be available to the system even before mounting the FileVault partition.

Unmount

Now you have to ensure the unmount at logout, and only if you are connected via ssh. To do that you can use your shell's logout script. These are the logout script paths for the 3 most used shells on Macs (feel free to add more in the comments):

  • bash: ~/.bash_logout
  • zsh: ~/.zlogout
  • (t)csh: ~/.logout

Open (or create) the appropriate file and add:

if [[ -n $SSH_CONNECTION ]]
    then hdiutil detach /Users/$USER/$USER.sparsebundle
fi