Need guidance for error in script

marcusbjerknes
New Contributor II

Hi,

I'm publishing VirtualBox and separate OS Appliance OVA's in Self Service. VirtualBox is installed nicely on client machines, but the OS Appliance is not. In the script it seems to get stuck at the stage where the appliance is imported and the script returns an error. I'd appreciate if anyone could take a look and give me some pointers on what is wrong?

OS appliance is packaged inside a DMG.

Funny thing is that the command "sudo -u $USER VBoxManage import "path to ova...." is working fine when running it when the dmg is mounted manually and visible under /Volumes.

#!/bin/bash
########################################################################################

# DEFINE VARIABLES & READ IN PARAMETERS

#########################################################################################

# HARDCODED VALUES SET HERE

Variables set by Casper - To manually override, remove the comment for the given variable

targetDrive="" # Casper will pass this parameter as "Target Drive" if left commented out

computerName="" # Casper will pass this parameter as "Computer Name" if left commented out

userName="" # Casper will pass this parameter as "User Name" if left commented out. Usernames

can only be passed if the script is triggered at login, logout, or by Self Service

Variables used for logging

logFile="/private/var/log/importOSApplianceToVBox.log"

Variables used by this script.

dmgName=""

CHECK TO SEE IF A VALUE WERE PASSED IN FOR PARAMETERS AND ASSIGN THEM

if [ "$1" != "" ] && [ "$targetDrive" == "" ]; then targetDrive="$1"
fi

if [ "$2" != "" ] && [ "$computerName" == "" ]; then computerName="$2"
fi

if [ "$3" != "" ] && [ "$userName" == "" ]; then userName="$3"
fi

if [ "$4" != "" ] && [ "$dmgName" == "" ]; then dmgName="$4"
fi

########################################################################################

# LOGGING FUNCTION

########################################################################################

log () { echo $1 echo $(date "+%Y-%m-%d %H:%M:%S: ") $1 >> $logFile }

########################################################################################

# VARIABLE VERIFICATION FUNCTION

########################################################################################

verifyVariable () {
eval variableValue=$$1
if [ "$variableValue" != "" ]; then echo "Variable "$1" value is set to: $variableValue"
else echo "Variable "$1" is blank. Please assign a value to the variable." exit 1
fi
}

########################################################################################

# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE

########################################################################################

Verify Variables

verifyVariable dmgName

Mount the DMG

log "Mounting the DMG $dmgName..."
mountResult=/usr/bin/hdiutil attach -noautoopen -noverify -nobrowse /Library/Application Support/JAMF/Waiting Room/$dmgName
mountVolume=echo "$mountResult" | grep Volumes | awk '{print $3}'
mountDevice=echo "$mountResult" | grep disk | head -1 | awk '{print $1}'

if [ $? == 0 ]; then log " DMG mounted successfully as volume $mountVolume on device $mountDevice."
else log "There was an error mounting the DMG. Exit Code: $?"
fi

Find the OVA in the DMG

ovaName=ls $mountVolume | grep "ova"

if [ $? == 0 ]; then log " Found OVA file $ovaName on $mountVolume."
else log "No OVA file found. Exit Code: $?"
fi

Import the OVA wrapped inside the DMG

echo "Importing OS appliance $ovaName from mount path $mountVolume..."

sudo -u $USER VBoxManage import $mountVolume/$ovaName

if [ $? == 0 ]; then log " OS Appliance successfully imported."
else log "There was an error importing the OS appliance. Exit Code: $?"
fi

Unmount the DMG

echo "Unmounting disk $mountDevice..."
hdiutil detach $mountDevice -force

Delete the DMG

echo "Deleting $dmgName..."
/bin/rm /Library/Application Support/JAMF/Waiting Room/$dmgName

exit

1 ACCEPTED SOLUTION

SimonLovett
New Contributor III

Yup - in this context $USER I think will return 'root'.

From your script,

"userName=" # Casper will pass this parameter as "User Name" if left commented out. Usernames
can only be passed if the script is triggered at login, logout, or by Self Service, so you might simply be able to substitute $userName in place of $USER?

sudo -u $userName VBoxManage import $mountVolume/$ovaName

View solution in original post

6 REPLIES 6

SimonLovett
New Contributor III

In the line....

sudo -u $USER VBoxManage import $mountVolume/$ovaName

I can't see where you previously defined "$USER"?

I might be missing the definition, or misinterpreting the script, however that could be your problem :)

marcusbjerknes
New Contributor II

Thanks for your input! I only tested the script locally, so I'm an idiot... It's obvious that the variable $USER does not work in that context, running the policy from Self Service. So how should I go about to retrieve the logged in user and then define that user into the script line? Could I use the jamf predefined $3 for user, and if so, how?

SimonLovett
New Contributor III

Yup - in this context $USER I think will return 'root'.

From your script,

"userName=" # Casper will pass this parameter as "User Name" if left commented out. Usernames
can only be passed if the script is triggered at login, logout, or by Self Service, so you might simply be able to substitute $userName in place of $USER?

sudo -u $userName VBoxManage import $mountVolume/$ovaName

mm2270
Legendary Contributor III

If the script will only run from something like Self Service or via the login trigger, then using $3 should be fine. If you plan on using it in other ways, you can get the logged in user a few different ways. There is loggedInUser=$(stat -f%Su /dev/console) but there are longer but more accurate methods as well. The stat command hasn't really failed me in my testing.

marcusbjerknes
New Contributor II

I came to my senses and figured out how to solve it. It was a combination of two factors really, the USER definition which you pointed out, and the path to VBoxManage. The userName="{$3}" is defined earlier in the script, so I used that one and wrote the full path to vboxmanage, and now it's working!

sudo -u $userName /usr/local/bin/VBoxManage import $mountVolume/$ovaName

When I get home, I'll have a cold one...

marcusbjerknes
New Contributor II

Thanks!

@SimonCU: You beat me to it! Though I found out by myself...

@mm2270: The stat command will surely come in use in the future.

I'm a happy camper now! Cheers!