Search code examples
macosencryptionterminalaesosx-mavericks

HFS+ Mount Encrypted programmatically


I'm writing a little utility to manage mounted drives, but I have run into a small issue when mounting a encrypted disk, The program tells me theres no mountable file systems when I run:

echo "password" | hdiutil attach -stdinpass /dev/disk2s2

I have tried using mount_hfs , however the mount_hfs only pops up with the system password prompt. I want to use my programs' database, not the Apple keychain for the encryption code. I've seen many ways to do this exact process, but on OS X Mavericks, I'm unsure how to correct the error. I have also tried as one web page suggested, remove the "-stdinpass" parameter, as that worked for them.

How do I mount a encrypted disk using terminal utilities on OS X Mavericks. If it helps, the disk that I am testing this on is AES encrypted.

Here is a sample:

set passList to words of (do shell script "cat pwd.lst")
repeat with i in passList
    do shell script "Testing password: " & i
    do shell script "echo " & quoted form of i & " | hdiutil attach -stdinpass     /dev/disk2s2"

end repeat

Solution

  • hdiutil is for managing disk images, not native volumes -- it has a few features that work with native volumes as well, but this isn't one of them. In general, you want diskutil for working with actual disks. In the case of encrypted volumes, they're managed by CoreStorage, so you actually need diskutil cs and its subcommands. The command you need is something like:

    echo "password" | diskutil cd UnlockVolume 3F34630B-FAD5-4210-8812-973C158C9892 -stdinpass
    

    ... where 3F34630B-FAD5-4210-8812-973C158C9892 is the UUID of the volume you want to mount. If you don't know that UUID, just the /dev entry, you have some work to do. Core Storage is set up to take one or more physical volumes (e.g. /dev/disk2s2), merge them into a logical volume group, and them create logical volumes within that group. You can get the UUID of the logical volume group with:

    lvguuid="$(diskutil info /dev/disk2s2 | awk '/LVG UUID:/ {print $3}')"
    

    Unfortunately, you then have to figure out the logical volume UUID, and I think to do that you have to run diskutil cs list (maybe with the -plist option), and parse through the mess that results, looking for the logical volume(s) in that group. Ick.