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?
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.
- Start “Automator”. Create an “Application” workflow
- 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”.
- Search for “Run Shell Script” action. Double click or drag it into the workflow to add.
- 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"
- 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
- Verify UTM starts the VM.
- Save the Automator workflow as “UTM Start Home Assistant.app”
-
Open “Login Items” in “Users & Groups” settings
-
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)
- Restart the computer. The first time the workflow runs there will be another permissions prompt, approve. Afterwards it shouldn’t prompt anymore.
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
@ 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
Is there any good way to start this when the Mac powers on in Seqoia? (without requiring login)
As far as I see, it’s not possible to run UI apps (like UTM) without a user logs in. Here how I worked around the issue:
- First, you need to enable “Start up automaticaly after a failure” in Energy settings. Also enable “Prevent automatic sleeping…” and disable “Put hard disks to sleep…”.
- I created a new user named “homelab” (standart user, not admin), enabled auto login for this user, prepared HAOS with UTM in it. (If you use FileVault, you can not enable autologin. So I sacrificed disk encryption )
- Also keep relevant settings in UTM to prevent computer to go sleep,
- I put the script below in “Script Editor” app, and saved it as an Application (File → Save as, Application):
#!/usr/bin/osascript
delay 10
tell application "UTM"
set vm to virtual machine named "Home Assistant OS"
start vm
end tell
- I added this “application” to the login items in settings.
Now when an unintended restart happens (like a power failure or OS upgrade) homelab account automatically logs in, opens UTM and runs VM named “Home Assistant OS”. I also kept auto lock settings fairly short, so mac goes to lock screen in 1 minute.
I think this is the best we can with macos for now