The .dmg disk image is a particularly tricky format. The main reason for this is that a .dmg file must be mounted by the macOS file system before the installation files contained within it can be examined and installed. For this reason, Addigy recommends that you remove the .pkg file from within the .dmg and upload it individually when at all possible.
However, sometimes the software will require installation from within the .dmg volume. In this case, you will need to generate your installation script. The other steps for this Smart Software should mirror the .pkg installation described in our article Creating Smart Software.
Setting the Groundwork
In our example, we’ll be installing Firefox. Specifically, the Firefox 75.0.dmg has a ".app" file inside of the .dmg file.
Start by entering the basic information of the Smart Software item, and upload the .dmg file.
Installation Command
The next step is to configure the installation script. To install from a .dmg we need to accomplish these three steps:
- Mount the .dmg file.
- Install files and software from the mounted volume.
- Unmount the .dmg file.
Steps 1 and 3 are generally the same for every .dmg installation; all that needs to be changed are the paths for the commands to work.
Below is what will be used as the installation script. Be sure to edit the variables by inserting the relevant information between the double quotes. The variables are:
- dmg_file
- app_name
- volume_path
- destination
#!/bin/bash
# Set the paths and variables
dmg_file="/Library/Addigy/ansible/packages/Firefox (75.0)/Firefox 75.0.dmg"
app_name="Firefox.app"
volume_path="/Volumes/Firefox"
destination="/Applications"
# Removes the trailing slash if it exists
app_name="${app_name%/}"
volume_path="${volume_path%/}"
destination="${destination%/}"
# Mount the DMG and copy the application
hdiutil attach -nobrowse "$dmg_file"
cp -R "$volume_path/$app_name" "$destination"
# Detach the DMG
hdiutil detach "$volume_path"
If you're unsure where to get the Smart Software directory used for the dmg_file variable, you can find it in the Installation section of your Smart Software item.
Condition Script
The Custom Conditional Command for this software is quite simple. It checks to see if the software we want to install is already in the correct folder.
#!/bin/bash
if [ -e '/Applications/Firefox.app' ]; then
echo "file found in /Applications folder; skipping installation"
exit 1
else
echo "file not found in /Applications folder; attempting installation"
exit 0
fi
The Custom Condition Command for your Smart Software may vary depending on your needs, and if you would like to check for other things, go ahead and check out our article here: Overview: Conditions for Install (Condition Scripts)
FAQ
Question: How would I get proper directory information?
Great question!
Let's use Firefox as an example again. When you expand a DMG file, there will be another file within it. To know what volume is being mounted, you'd need to drag that file into a terminal and get that path.
For example, when expanding Firefox's DMG file, a .app file is returned:
Here is a quick gif on how to get the volume path of a .app file using the terminal:
From there, you'd add cp -R (This command will copy a file) in front of /Volumes/Firefox/Firefox.app and then the directory you want to copy the .app file to, in most cases, it will be the /Applications directory.
Question: What if there's a .pkg file inside of the DMG?
If your DMG houses a pkg, you will want to just extract that pkg as normal and upload that into Smart Software. From there, an installation command will automatically be offered for use. Additional steps on this can be found here.
Question: How do I know what directory to detach?
The last line of the example includes the command to detach the DMG file after the files are run. Referring to our Firefox example, the volume we'd want to detach is /Volumes/Firefox/.
The volume that you include there will always be what's before the .pkg or .app.