Install DMG Files using Shell Script
  • 05 Sep 2024
  • 5 Minutes to read
  • PDF

Install DMG Files using Shell Script

  • PDF

Article summary

Scalefusion offers support to distribute Apple VPP apps and PKG (product archive packages) natively using the Dashboard. Most Enterprise apps use either of the above mechanisms for distribution in an enterprise scenario. However, there might be certain cases where the developer has made the installer available in the form of a DMG (Disk Image File).

In such cases where only a DMG file is available, IT Admins can use the script described below and deploy it via Scalefusion MDM agent to remotely install the DMG. Please follow the steps below to use this method.

Note:
  1. This method uses a script to install the DMG file; though Scalefusion has tested these scripts, please validate the scripts on a test machine before deploying them on all your managed devices.
  2. Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts

Prerequisites

  1. Deploy Scalefusion MDM Agent.
  2. Scalefusion subscription plan with access to Content Management
    DMG files that wrap a PKG file that, when extracted, contains a PKG file cannot be distributed by this mechanism. We suggest to use the PKG file directly in such cases.

Step 1: Generate a Download URL for the DMG file

The first step is to generate a download URL for the DMG file that needs to be installed. You can use any of your servers (if you have one already) or a storage provider like Amazon S3 or Cloudflare R2 to upload your DMG and generate a public download URL.

In case you don't have a storage provider that you use, you can also use Scalefusion's Content Management to upload your DMG files and obtain a DMG URL. Follow the steps below to upload the file to Scalefusion's content management and generate the download URL,

  1. Navigate to Content Management > Content and click Upload File to upload the DMG.
  2. Wait for the DMG file to be uploaded and to appear in the list of files. Once the file appears, click on the file to view the file details window.
  3. Click on the 3 dots, and from the menu, select Copy Download Link
  4. Now you can open TextEdit (or any text editor) on your Mac/PC and paste the copied URL to your local machine. The URL would look like below,
    URL
    https://mobilock.s3.amazonaws.com/uploads/mobile_file/content/5947/rustdesk-1.1.9.dmg

Step 2: Create the Script

  1. Create a file on your Mac, for example, download_install_dmg.sh and open it to a text editor of your choice like TextEdit
  2. Copy the contents below.
    Shell
    #!/bin/bash
    
    # Insert the DMG download url below
    DownloadUrl="DMG_download_url"
    
    ##### DONOT EDIT BELOW CODE #####
    
    
    regex='^https.*.dmg$'
    if [[ $DownloadUrl =~ $regex ]]; then
        echo "URL points to direct DMG download"
        validLink="True"
    else
        echo "Searching headers for download links"
        urlHead=$(curl -s --head "$DownloadUrl")
        locationSearch=$(echo "$urlHead" | grep https:)
        if [ -n "$locationSearch" ]; then
            locationRaw=$(echo "$locationSearch" | awk '{print $2}')
            locationFormatted="$(echo "${locationRaw}" | tr -d '[:space:]')"
            regex='^https.*'
            if [[ $locationFormatted =~ $regex ]]; then
                echo "Download link found"
                DownloadUrl="$locationFormatted"
            else
                echo "No https location download link found in headers"
                exit 1
            fi
        else
            echo "No location download link found in headers"
            exit 1
        fi
    fi
    # Create Temp Folder
    DATE=$(date '+%Y-%m-%d-%H-%M-%S')
    TempFolder="Download-$DATE"
    mkdir -p "/tmp/$TempFolder"
    # Navigate to Temp Folder
    cd "/tmp/$TempFolder" || exit
    # Download File into Temp Folder
    curl -s -O "$DownloadUrl"
    # Capture name of Download File
    DownloadFile="$(ls)"
    echo "Downloaded $DownloadFile to /tmp/$TempFolder"
    # Verify DMG File
    regex='\.dmg$'
    if [[ $DownloadFile =~ $regex ]]; then
        DMGFile="$DownloadFile"
        echo "DMG File Found: $DMGFile"
    else
        echo "File: $DownloadFile is not a DMG"
        rm -r "/tmp/$TempFolder"
        echo "Deleted /tmp/$TempFolder"
        exit 1
    fi
    # Mount DMG File (-nobrowse prevents the volume from popping up in Finder)
    hdiutilAttach=$(hdiutil attach "/tmp/$TempFolder/$DMGFile" -nobrowse)
    echo "Used hdiutil to mount $DMGFile"
    err=$?
    if [ ${err} -ne 0 ]; then
        echo "Could not mount $DMGFile Error: ${err}"
        rm -r "/tmp/$TempFolder"
        echo "Deleted /tmp/$TempFolder"
        exit 1
    fi
    regex='\/Volumes\/.*'
    if [[ $hdiutilAttach =~ $regex ]]; then
        DMGVolume="${BASH_REMATCH[0]}"
        echo "Located DMG Volume: $DMGVolume"
    else
        echo "DMG Volume not found"
        rm -r "/tmp/$TempFolder"
        echo "Deleted /tmp/$TempFolder"
        exit 1
    fi
    # Identify the mount point for the DMG file
    DMGMountPoint=$(hdiutil info | grep "$DMGVolume" | awk '{ print $1 }')
    echo "Located DMG Mount Point: $DMGMountPoint"
    # Capture name of App file
    cd "$DMGVolume" || exit
    AppName=$(ls | grep '.app')
    cd ~ || exit
    echo "Located App: $AppName"
    # Test to ensure App is not already installed
    ExistingSearch=$(find "/Applications/" -name "$AppName" -depth 1)
    if [ -n "$ExistingSearch" ]; then
        echo "$AppName already present in /Applications folder"
        hdiutil detach "$DMGMountPoint"
        echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
        rm -r "/tmp/$TempFolder"
        echo "Deleted /tmp/$TempFolder"
        exit 1
    else
        echo "$AppName not present in /Applications folder"
    fi
    DMGAppPath=$(find "$DMGVolume" -name "*.app" -depth 1)
    # Copy the contents of the DMG file to /Applications/
    # Preserves all file attributes and ACLs
    cp -pPR "$DMGAppPath" /Applications/
    err=$?
    if [ ${err} -ne 0 ]; then
        echo "Could not copy $DMGAppPath Error: ${err}"
        hdiutil detach "$DMGMountPoint"
        echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
        rm -r "/tmp/$TempFolder"
        echo "Deleted /tmp/$TempFolder"
        exit 1
    fi
    echo "Copied $DMGAppPath to /Applications"
    # Unmount the DMG file
    hdiutil detach "$DMGMountPoint"
    echo "Used hdiutil to detach $DMGFile from $DMGMountPoint"
    err=$?
    if [ ${err} -ne 0 ]; then
        echo "Could not detach DMG: $DMGMountPoint Error: ${err}"
    fi
    # Remove Temp Folder and download
    rm -r "/tmp/$TempFolder"
    echo "Deleted /tmp/$TempFolder"
    
     
    

  3. Now replace the phrase "DMG_download_url" with the download URL generated in Step 1.
    Shell
    # Insert the DMG download url below
    DownloadUrl="DMG_download_url"
    
     
    
  4. Save the file on your Mac.

Step 3: Deploy the Script

Please use our help document on deploying scripts on macOS devices to upload and publish the script to your managed Mac devices. For easy reference, here are the steps,

  1. Navigate to Application Management > Enterprise Store& upload the script as shown below,
    1. Enter Script Name: Enter a name for the script file
    2. Upload Script File: Upload the script file created in Step 2
    3. Run Script as signed-in user: Select Yes from the dropdown
    4. Click Save
  2. Now you can Publish the script to the device profiles where you want to install this application.
  3. Once the script has been deployed, you can View Status- from the side panel that appears once you click on the uploaded script file. Here are sample screenshots,
    1. Successful Installation
    2. Invalid Download URL
    3. Failed to Install DMG File
If you would like to install a DMG from a local file on the managed device, Download the script here and replace the ENTER_PATH_NAME phrase with the path of the local file on the managed Mac device.


Was this article helpful?