Skip to content

Instantly share code, notes, and snippets.

@motionbug
Created May 23, 2025 12:58
Show Gist options
  • Select an option

  • Save motionbug/1d05c9df74dfc2a4ec16f1d3fbb04372 to your computer and use it in GitHub Desktop.

Select an option

Save motionbug/1d05c9df74dfc2a4ec16f1d3fbb04372 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# Function to clone a Tart VM and set specific properties.
#
# This function will:
# 1. Clone a base VM image to a new VM name.
# 2. Set the new VM to use a random serial number, a random MAC address,
# and display the rEFIt boot manager on startup.
#
# Usage:
# starttart <VM_BASE_NAME> <NEW_VM_NAME>
#
# Arguments:
# VM_BASE_NAME: The name of the Tart VM to use as the base for cloning (e.g., "macos-sonoma-vanilla").
# NEW_VM_NAME: The name for the new VM that will be created (e.g., "my-dev-vm").
#
# Example:
# starttart "macos-sonoma-vanilla" "my-dev-vm"
starttart() {
# --- Configuration: VM Names ---
# The VM base name and the new VM name are taken from function arguments.
# If you prefer to set default values or hardcode them here, you can modify these lines.
# For example:
# local default_vm_base="macos-sonoma-vanilla"
# local default_new_vm_name="default-new-vm"
# local vm_base_name="${1:-$default_vm_base}"
# local new_vm_name="${2:-$default_new_vm_name}"
local vm_base_name="$1"
local new_vm_name="$2"
# Step 1: Check prerequisites (tart installed)
if ! command -v tart &> /dev/null; then
echo "Error: 'tart' command not found in PATH. Please install Tart: https://tart.run" >&2
return 1
fi
# Step 2: Validate arguments
if [[ -z "$vm_base_name" ]]; then
echo "Error: Missing VM_BASE_NAME (first argument). Usage: starttart <BASE_VM> <NEW_VM>" >&2
return 1
fi
if [[ -z "$new_vm_name" ]]; then
echo "Error: Missing NEW_VM_NAME (second argument). Usage: starttart <BASE_VM> <NEW_VM>" >&2
return 1
fi
# Step 3: Clone the VM
echo "Cloning VM: $vm_base_name to $new_vm_name"
if tart clone "$vm_base_name" "$new_vm_name"; then
echo "VM '$new_vm_name' cloned successfully from '$vm_base_name'."
else
echo "Error: Failed to clone VM from '$vm_base_name' to '$new_vm_name'." >&2
# Additional check: does the VM already exist?
if tart list | grep -q "^${new_vm_name} "; then
echo "Note: A VM named '$new_vm_name' already exists." >&2
fi
return 1
fi
echo ""
# Step 4: Set VM properties
echo "Setting properties for VM '$new_vm_name' (--random-serial, --random-mac, --display-refit)"
if tart set "$new_vm_name" --random-serial --random-mac --display-refit; then
echo "Properties for VM '$new_vm_name' set successfully."
else
echo "Oh no! The sacred ritual of setting properties failed for VM '$new_vm_name'." >&2
echo "The VM '$new_vm_name' was cloned, but its configuration failed." >&2
echo "You may need to run the 'tart set' command manually:" >&2
echo " tart set \"$new_vm_name\" --random-serial --random-mac --display-refit" >&2
return 1
fi
echo ""
echo " Hooray! The VM '$new_vm_name' has been successfully forged and configured! "
echo ""
echo "Behold! The arcane details of your new creation, '$new_vm_name':"
if ! tart get "$new_vm_name" --format text; then
echo ""
echo "==========================="
echo " Hmm, a slight hiccup. Couldn't automatically fetch details for '$new_vm_name'."
echo " You can try manually with: tart get \"$new_vm_name\" --format text"
echo "==========================="
echo ""
fi
echo "Your Tart VM '$new_vm_name' is ready for adventure!"
return 0
}
# Usage Instructions for starttart
#
# 1. Save this script (e.g., as starttart_function.sh).
# 2. To use in your current terminal session, run:
# source /path/to/starttart_function.sh
# 3. To load automatically in every session, add the following to your ~/.zshrc:
# source /path/to/starttart_function.sh
# 4. After sourcing, use the function as follows:
# starttart <BASE_VM_NAME> <NEW_VM_NAME>
# Example:
# starttart "macos-sonoma-vanilla" "my-dev-vm"
#
# If you see 'command not found: starttart', ensure the script is sourced in your current session.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment