As part of my testing workflow, I’ve been using VMs running on a ESXi server running ESXi 6.5. To help me quickly build those VMs, I have been using a script named esxi_macos_vm_creation.sh for building VMs. This script is forked from Tamas Piros’s auto-create script for standing up Linux VMs on free ESXi:
https://github.com/tpiros/auto-create
My fork of the auto-create script is designed to create and configure virtual machines with Apple operating systems as the guest OS, hosted on a VMware ESXi server running on Apple hardware. The script assumes that the virtual machines are built using copied VMDK disk files, where the VMDK files are generated by AutoDMG and vfuse. For more details, see below the jump.
Building VMDK files
In the process I’m following, the workflow to generate the VMDK files works like this:
1. Create disk image using AutoDMG. The disk image includes a firstboot package, which includes tools to configure the VM on its first startup.
2. Once the disk image is generated, use vfuse to create VMDK files using a command similar to the one shown below:
3. vfuse will create a VM for VMware Fusion. To access the VMDK files, right-click on the VM and select Show Package Contents.
4. The VMDK files will be inside. The conversion process will create two files:
- filename.vmdk
- filename-flat.vmdk
This is expected behavior and both of these files will be needed.
Uploading VMDK files
Once the VMDK files are created and available, they need to be uploaded to a convenient location on an ESXi datastore. For information on how to upload these files, see the link below from the VMware documentation for vSphere:
Upload Files to Datastores:
https://pubs.vmware.com/vsphere-65/index.jsp?topic=%2Fcom.vmware.vsphere.storage.doc%2FGUID-58D77EA5-50D9-4A8E-A15A-D7B3ABA11B87.html
The esxi_macos_vm_creation script will need to be stored on the ESXi server, so also upload it to a convenient location on an ESXi datastore.
Running the script
This script is designed to create and configure virtual machines running Apple operating systems, hosted on a VMware ESXi server running on Apple hardware. The script assumes that the virtual machines are built using copied VMDK disk files. In this case, the VMDK files will be the disk images created by AutoDMG and converted to VMDK files using vfuse.
Note: When creating the VM, you will need to provide the location of the filename.vmdk file . As long as filename.vmdk and filename-flat.vmdk are both available in the same directory on the datastore, ESXi will automatically also reference and include filename-flat.vmdk when creating the VM.
The script is designed to be stored on an ESXi datastore and run from the ESXi server’s command line interface, which is why it needed to be uploaded to an ESXi datastore in the previous step.
Usage:
/path/to/esxi_macos_vm_creation.sh -n -d -c -h -i -o -r -s -v -p
Options:
- -n: Name of VM (required)
- -d: Location of a VMDK disk file (required). Location must be in this format – /vmfs/volumes/datastore_number_here/path/to/filename_here.vmdk
- -c: Number of virtual CPUs
- -h: VMware Hardware Version
- -i: Location of an ISO image. Location must be in this format – /vmfs/volumes/datastore_number_here/path/to/iso_file.iso
- -o: Apple OS version
- -r: RAM size in MB
- -s: Disk size in GB
- -v: VNC port between 5900 and 5909
- -p: VNC password. Maximum password length is eight characters.
The script has several default variables set up:
ESXi datastore location for new VMs: /vmfs/volumes/datastore1
Number of processors for new VMs: 2
Amount of RAM for new VMs: 4096 MBs
Hard drive size for new VMs: 40 GB
Guest OS: darwin14-64 (this is the OS setting for OS X Yosemite.)
Hardware Version: 11 (this is the maximum Hardware Version available for ESXi 6.0.x)
The ESXi datastore value can be changed by editing the DATASTORE variable in the script. The other default values are overridden if alternate values are enabled by the script’s available options.
Examples
To set up a VM specifying only the VM name and VMDK location:
/path/to/esxi_macos_vm_creation.sh -n VM_Name_Goes_Here -d /vmfs/volumes/datastore_number_here/path/to/filename_here.vmdk
The output should appear similar to that shown below:
computername:~ username$ ssh root@esxi.demo.com
Password:
The time and date of this login have been sent to the system logs.
VMware offers supported, powerful system administration tools. Please
see www.vmware.com/go/sysadmintools for details.
The ESXi Shell can be disabled by an administrative user. See the
vSphere Security documentation for more information.
[root@esxi:~] /path/to/esxi_macos_vm_creation.sh -n macOSVM -d /vmfs/volumes/datastore1/template/filename.vmdk
Destination disk format: VMFS thin-provisioned
Cloning disk '/vmfs/volumes/datastore1/template/filename.vmdk'...
Clone: 100% done.
Powering on VM:
The Virtual Machine is now configured and the VM has been started up. The VM is set to use the following configuration:
Name: macOSVM
CPU: 2
RAM: 4096
Guest OS: darwin14-64
Hardware Version: 11
Hard drive size: 40
No ISO added.
VNC not enabled.
[root@esxi:~]
To set up a VM using a name with spaces and/or special characters, add quotation marks to the VM name:
/path/to/esxi_macos_vm_creation.sh -n "VM's Name Goes Here!" -d /vmfs/volumes/datastore_number_here/path/to/filename_here.vmdk
Other flags can be added as needed:
To set up a VM and add more CPUs:
/path/to/esxi_macos_vm_creation.sh -n VM_Name_Goes_Here -c 4 -d /vmfs/volumes/datastore_number_here/path/to/filename_here.vmdk
To set up a VM and enable VNC on port 5901 with the password set to the word password:
/path/to/esxi_macos_vm_creation.sh -n VM_Name_Goes_Here -d /vmfs/volumes/datastore_number_here/path/to/filename_here.vmdk -v 5901 -p password
To set up a VM named MacOS VM 10.12 using a VDMK stored on /vmfs/volumes/datastore1/template and named macos-vm.vmdk with 4 CPUs, 8 GBs of RAM, a 52 GB hard drive, set to HW Version 13, guest OS set to macOS Sierra and VNC enabled on port 5902 with the VNC password set to the word password:
/path/to/esxi_macos_vm_creation.sh -n "MacOS VM 10.12" -d /vmfs/volumes/datastore1/template/macos-vm.vmdk -c 4 -r 8192 -s 52 -h 13 -o darwin16-64 -v 5902 -p password
The output should appear similar to what’s shown below:
computername:~ username$ ssh root@esxi.demo.com
Password:
The time and date of this login have been sent to the system logs.
VMware offers supported, powerful system administration tools. Please
see www.vmware.com/go/sysadmintools for details.
The ESXi Shell can be disabled by an administrative user. See the
vSphere Security documentation for more information.
[root@esxi:~] /path/to/esxi_macos_vm_creation.sh -n "MacOS VM 10.12" -d /vmfs/volumes/datastore1/template/macos-vm.vmdk -c 4 -r 8192 -s 52 -h 13 -o darwin16-64 -v 5902 -p password
Destination disk format: VMFS thin-provisioned
Cloning disk '/vmfs/volumes/datastore1/template/macos-vm.vmdk'...
Clone: 100% done.
Grow: 100% done.
Powering on VM:
The Virtual Machine is now configured and the VM has been started up. The VM is set to use the following configuration:
Name: MacOS VM 10.12
CPU: 4
RAM: 8192
Guest OS: darwin16-64
Hardware Version: 13
Hard drive size: 52
No ISO added.
VNC Port: 5902
VNC Password: password
[root@esxi:~]
The script is available below. It is also available from GitHub at the following address:
https://github.com/rtrouton/esxi_macos_vm_creation