Ok, this one falls under the category ‘just for fun while actually being very handy’: Create an iOS Shortcut to create a VM in ESXI 🙂

When troubleshooting and testing deployments, you need some virtual machines to make your life easy… Snapshotting them before you configure them is of course a smart thing to do, but there are situations where you just need to create a new VM and start over.

I already discussed the methods of creating a ‘DEP capable’, sorry Apple, I mean ‘Automated MDM enrollment capable’, VM on both Parallels and VMWare but what about ESXI? Well I’m not going to spend too much time on this because it’s actually exactly the same as VMWare Fusion!

Just like in VMWare Fusion the steps for ESXI are:

  • Create the VM. Save but do not boot it yet.
  • Edit settings and add the serialNumber.reflectHost, serialNumber, hw.model.reflectHost, hw.model and smbios.reflectHost parameters.
  • Boot and install macOS
  • STOP the VM to make the serialnumber and model hold
  • Boot the VM and enjoy Automated MDM enrollment
  • Make a snapthot on the first setup screen!
Before booting the VM for the first time, go to ‘edit settings’->’VM Options’->’Advanced’.
Add the serialNumber.reflectHost, serialNumber, hw.model.reflectHost, hw.model and smbios.reflectHost parameters.

Another way, if you have VMware Fusion Pro, is to create the VM in Fusion and upload it to ESXI, but this still requires uploading, waiting and loosing time… so there must be a better way of doing it! And yes there is!

Rich Trouten, aka ‘Der Flounder’, already did the heavy lifting on creating a script to create a VM in ESXI, based on a vFuse vmdk. Awesome, so let’s use that and see what we can do with it to make our life even more lazy, or let’s call it “efficient”. What if I would just have a magic button somewhere… just one button to create a VM in my ESXI. No Terminal, no ssh commands, no typing, just 1 button… and oh yes, of course I want it to do ‘Automated MDM enrollment as well’! A unicorn? Maybe, but the good news is, it’s possible, so let’s go!

First of all, have a look at vFuse. A full detail tutorial on vFuse will make this post a bit too long, so I’ll keep it at referring you to the vFuse Github and AutoDMG which you will also need to make vFuse work. The workflow is however very simple:

  • Download a copy of the macOS installer from the App Store
  • Make it a DMG with AutoDMG
  • Use vFsuse to make a Fusion VM

vFuse allows you to use a template (JSON), so you can quickly run the same command over and over again to create VM’s:

Use ‘vfuse -t /path/to/template.json’ to create the vFuse VM

At this moment you could just use your vFuse VM in VMWare fusion, or upload it to your ESXI. This is however what I want to avoid in order to gain more time. Hence the only thing I’m interested in right now is the vmdk files which vFuse creates, because we need those for the script made by Rich Trouten. While the vFuse VM will do ‘Automated MDM enrollment’ (serial number and model in the template), we’ll need to tweak Rich’s script a bit because we will not be using the .vmx file from vFuse.

I forked Rich’s Github and made the necessary changes here. I just added the serialnumber/model variables and updated a few references to macOS Mojave and ESXI 6.7 (Hardware version 14). Now we can continue with Rich’s tutorial.

First of all we’ll grab the vmdk files from within the vFuse package:

Navigate to the vFuse VM, ctrl-click and select ‘Show Package Contents’
You will need BOTH files. Copy them (or move in case you are not going to use this VM locally) out of the VM package as we’ll need to upload them to ESXI. The name of the files will depend on the name you gave the VM in vFuse.

Upload both files to your ESXI, following this KB. For instance into a folder called “Templates”. Next, add the serial number and model to the esxi_macos_vm_creation script and upload it to your ESXI as well. For instance into a folder called ‘Scripts’.

Don’t forget to name define your DATASTORE, Serial Number and Model in the script BEFORE uploading it to ESXI. Or any other settings such as CPU, RAM, etc…

Once both the vmdk files and the script are in place on the ESXI server, we can try it out with the following command:

/vmfs/volumes/DATASTORENAME/path/to/esxi_macos_vm_creation.sh -n VM_NAME_GOES_HERE -d /vmfs/volumes/DATASTORENUMBER/path/to/macos-vm.vmdk

Make sure you put the correct datastore name in the first path, and use the ‘Datastore number’ in the second path. To find the number of your datastore, ssh into the ESXI and have a look at /vmfs/volumes/ :

My SSD datastore with the number… hence my Terminal command to make a new VM called ‘macOS141DEP’ would be:

/vmfs/volumes/SSD/Scripts/esxi_macos_vm_creation.sh -n macOS141DEP -d /vmfs/volumes/5b6e08bb-4e1d2549-f33c-685b35d36842/Templates/macos-vm.vmdk

All credits to Rich Trouton for this very handy script! Love it! With the vmdk files and the script in place, the only thing I now need to do to create a new VM is:

  • ssh into the ESXI
  • run a oneliner in Terminal
  • grab a coffee while the vmdk is being cloned and the VM created…

How cool is that?! Very nice indeed! However, for this to work I need my Mac, open Terminal, ssh into ESXI, copy paste a command,…

Only then I can go grab a coffee… waiting for the script to complete. Not ‘time efficient’ enough for me. What if this could all be triggered while walking towards the coffee machine? With just a push on a button… 🙂

iOS Shortcuts to the rescue! 

Add the ‘ask for input’ and ‘Run Script over SSH’ tasks…
Add the onliner we used above… eh voila!
Name your VM…
Walk to the coffee machine, take your iPhone, push the button! The Shortcuts button that is, for the ☕️☕️☕️ you’ll need to push some buttons on the coffee machine as well…
Just walked back to my Mac, with my coffee and oh look a VM! Magic!

That’s it! A magic button in iOS to create a VM in ESXI. Yes, SSH needs to be enabled, but who cares on a test ESXI server right?

Now, whenever I think “Oh I need a VM to test”… right… just one button to press 🙂