Listing in Terminal the last mounted volume

Attach(1)ing different copies of the same dmg file create in /Volumes folders representing each image. Let the first image be called Image; the subsequents will receive an incrementing number in their tail, e.g. Image 1, Image 2 and so on.

However, detaching Image 1 and then attaching another instance will recycle the the numerical id, so that my last image will actually be Image 1.

This means that I cannot rely on ls(1)'s output (by default sorted lexicographically) to determine the last mounted image. I don't seem to have more success with ls -U or diskutil's output either.

I may very well be missing something. Is there any way to reliably determine in bash the latest mounted image? Thank you.


Solution 1:

The last line from the output of mount would be the last filesystem mounted.

Solution 2:

Use grep "Image_Volume_Name" /var/log/system.log. Replace Image_Volume_Name by the volume name of the mounted image (in the example below dnscrypt-pkg).

Oct 18 03:34:49 host kernel[0]: hfs: mounted dnscrypt-pkg on device disk7s1
Oct 18 03:34:49 host.sld.tld mds[70]: (Normal) Volume: volume:0x7f7fe6814c00 ********** Bootstrapped Creating a default store:1 SpotLoc:(null) SpotVerLoc:(null) occlude:0 /Volumes/dnscrypt-pkg 1
Oct 18 03:34:53 host kernel[0]: hfs: mounted dnscrypt-pkg on device disk8s1
Oct 18 03:34:53 host.sld.tld mds[70]: (Normal) Volume: volume:0x7f7fe7012c00 ********** Bootstrapped Creating a default store:1 SpotLoc:(null) SpotVerLoc:(null) occlude:0 /Volumes/dnscrypt-pkg 4
Oct 18 03:49:00 host kernel[0]: hfs: mounted dnscrypt-pkg on device disk5s1 
Oct 18 03:49:00 host.sld.tld mds[70]: (Normal) Volume: volume:0x7f7fe6883800 ********** Bootstrapped Creating a default store:1 SpotLoc:(null) SpotVerLoc:(null) occlude:0 /Volumes/dnscrypt-pkg 2

This works in older system (in the example above 10.9.5).

In Sierra you have to "pipe" the disk identifier found in grep "Image_Volume_Name" /var/log/system.log of the last mounted disk to hdituil:

hdiutil info | grep disk5s1
/dev/disk5s1    48465300-0000-11AA-AA11-00306543ECAC    /Volumes/dnscrypt-pkg 2

The bigger picture: use the -volname switch in hdiutil to rename the volume name of the mounted image while creating the dmg file. Then you don't have to deal with myriads of /Volumes/Image Xs