Autostart a UTM VM on MacOS

Hello, I use a HAOS image running on an Intel Mac mini to get domotic services at home.
I had a look at UTM official documentation + posted in the Discord support channel to get help, but I still don’t know how to make sure the HAOS VM can start automatically after a reboot of the hardware.
Tried the shortcut method (with utm://start?name=xxx command) and a utmctrl script, but neither of the two method actually launched the VM after a restart.
Currently digging into a plist option, but I am a bit lost when using the launchd command to register the script I created and located in /System/Library/LaunchDaemons
Does anyone have an example of someting actually ok to autostart his/her VM?

1 Like

Make an apple script app that contains
tell application “UTM”
set vm to virtual machine named “Your server”
— start a vm
start vm
end tell

Set Up the Script to Run on Login
Open System Settings (or System Preferences on older macOS versions).
Navigate to General > Login Items.
Click the + button under the “Open at Login” section.
Find and select the application you just saved

Thanks @Johangransby. I also tried this Apple Script method. Unfortunatly it doesn’t start the VM (as the oher solutions described) and it would need someone to log in, this not in my scope.

UTM deprecated the URI scheme due to security issues in v4.5. I switched to using Automator with the UTM CLI instead.

  1. Start “Automator”. Create an “Application” workflow

  1. Search for “Launch Application” action. Double click or drag it into the workflow to add. Select “Other” from the dropdown and then find “UTM.app”.

  1. Search for “Run Shell Script” action. Double click or drag it into the workflow to add.

image

  1. Change the shell script to (replace "Home Assistant" with the name of your Home Assistant VM)
/Applications/UTM.app/Contents/MacOS/utmctl start "Home Assistant"

  1. To test the workflow, make sure UTM is not running or stop the VM. Click “Run” command in the top right of Automator and approve the permission prompt

  1. Verify UTM starts the VM.

  1. Save the Automator workflow as “UTM Start Home Assistant.app”

  1. Open “Login Items” in “Users & Groups” settings

  2. Drag or add the “UTM Start Home Assistant” application into the Login Items (the list UI might be buggy and doesn’t show it immediately, switch between the “Password” and “Login Items” tabs to refresh the list)

  1. Restart the computer. The first time the workflow runs there will be another permissions prompt, approve. Afterwards it shouldn’t prompt anymore.
1 Like

Do you if it is possible to also have UTM connect a specific USB device - in my case the Conbee III stick I use for ZHA?

Nice I will tell this, thanks. I am afraid your method + setting an auto-login user is the only way to have UTM and the VM restarted after a reboot.

Well, did the search myself, and it is possible :slight_smile:

1 Like

@ longzheng
Thanks for the instructions!
I figured out that UTM was started but the Home Assistant VM not.
I added a 5 sec pause before the shell script … which solved the problem

Here’s a way to run a script at startup to do all your startup tasks that will even run after a power outage with no user intervention. First in the Mac’s settings, autostart to a specific user. Then place the following .plist in /users/<your-ha-username>/library/launchagents

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <false/>
        <key>Label</key>
        <string>ha.startup_tasks.org</string>
        <key>Program</key>
        <string>/usr/bin/osascript</string>
        <key>ProgramArguments</key>
        <array>
                <string>osascript</string>
                <string>/Users/ha-username/<path-you-want>/startup_tasks.scpt</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/startup_tasks_error</string>
        <key>StandardOutPath</key>
        <string>/tmp/startup_tasks_out</string>
</dict>

Then put whatever you want in the script named startup_tasks.scpt. Here’s mine, which uses AppleScript to start the UTM instance, hide iStatistica and connect Airfoil to my speakers:

#!/usr/bin/osascript

delay 10

tell application "UTM"
	set vm to virtual machine named "ha_vm"
	start vm
end tell

tell application "Finder"
	set visible of every process whose visible is true and name is "iStatistica Pro" to false
end tell

tell application "Airfoil"
    launch
    activate
	delay 15
	get every speaker
	connect to (every speaker whose id contains "@Bedroom")
	connect to (every speaker whose id contains "@Bath")
	connect to (every speaker whose id contains "@Near")
	connect to (every speaker whose id contains "@Far")
	disconnect from (every speaker whose id contains "@Beosound")
end tell