Skip to content

Instantly share code, notes, and snippets.

@trenskow
Last active April 23, 2025 11:02
Show Gist options
  • Save trenskow/39fd464ff52aa95cad83e912e34dea39 to your computer and use it in GitHub Desktop.
Save trenskow/39fd464ff52aa95cad83e912e34dea39 to your computer and use it in GitHub Desktop.

virtually.sh

Small bash script for lauching virtual MacOS instance using Tart.

Usage

echo "Hello, World!" | ./virtually.sh
./virtually.sh < my-script.sh

How it works

Clones an existing VM, boots it, SSH, performs command, shuts down, deletes clone.

Setup VM

Make user admin able to sudo without password. Add SSH key to authorized_keys.

#!env bash
VM_ORIGIN=$1
if [ -z "${VM_ORIGIN}" ]; then
VM_ORIGIN="macos-sequoia-xcode"
fi
if ! tart get ${VM_ORIGIN} 2>&1 > /dev/null; then
echo "Not found: ${VM_ORIGIN}"
exit 1
fi
VM_NAME="${VM_ORIGIN}-$(cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w 50 | head -n 1)"
tart clone macos-sequoia-xcode ${VM_NAME}
tart run ${VM_NAME} --no-graphics 2>&1 > /dev/null &
PID=$(echo $!)
while ! tart ip ${VM_NAME} &> /dev/null; do
sleep 1
done
IP=$(tart ip ${VM_NAME})
while ! ping -t 1 -c 1 ${IP} &> /dev/null; do
sleep 1
done
ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no admin@${IP}
CODE=$?
ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no admin@${IP} -t "sudo shutdown -h now" > /dev/null 2>&1 &
wait ${PID}
tart delete ${VM_NAME}
exit ${CODE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment