Skip to content

Instantly share code, notes, and snippets.

@carlashley
Last active October 6, 2017 03:17
Show Gist options
  • Save carlashley/04edf87c92cbacb74fe096d3b521c73e to your computer and use it in GitHub Desktop.
Save carlashley/04edf87c92cbacb74fe096d3b521c73e to your computer and use it in GitHub Desktop.
Imports a VirtualBox OVA for currently logged in user.
#!/bin/sh
# This will import a VirtualBox OVA file for the currently logged in user; by default, when `VBoxManage import` is run in
# root environment, it will import the VM into a folder in the root user home directory.
# Handy for enabling self-service installs of a VM.
# The script is _very_ basic and doesn't have any form of version management/removal/upgrade capability, but this
# should be relatively easily expandable with the right `VBoxManage` commands.
#
# This example is for use in a package that doesn't actually install the OVA on disk, but imports it direct from the .pkg
# file. Essentially 'payload free', but not ;)
# If you're using an Apple flat distribution .pkg, there are some paths that are handy to know.
# The `resources` path is the location of resources placed inside the .pkg when the .pkg is built.
# It's not necessary to use this, as you might want to "install" the OVA file to a location on the target machine
# if the OVA is going to be imported by multiple users on the same machine.
# In that case, just remove $(dirname $0) and replace it with the right path to the VM.
resources=$(dirname $0)
target_vol=$3
package_bundle_id=$INSTALL_PKG_SESSION_ID
# OVA file
ova_file="virtualmachine.ova"
# Use a pyobjc call to get actual user. This works with scripts run in root context to get the actual user.
# Credit: https://macmule.com/2014/11/19/how-to-get-the-currently-logged-in-user-in-a-more-apple-approved-way/
current_user=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
# Set the VBOX_USER_HOME path to reflect the correct user for importing.
VBOX_USER_PATH=/Users/${current_user}
# Import the OVA as the ${current_user}
/usr/bin/su -l ${current_user} -c "/usr/local/bin/VBoxManage import ${resources}/${ova_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment