MAC M1重启后自动启动虚拟机

Why

笔者在M1通过UTM setup了一个虚拟机,虚拟机开机的时候会自动启动我设置的服务,那如何让M1重启后自动启动虚拟机呢?

How

首先要找到通过命令启动虚拟机的方法:

  • UTM
    • /Applications/UTM.app/Contents/MacOS/utmctl start ubuntu --hide
  • Virtualbox
    • /Applications/VirtualBox.app/Contents/MacOS/VBoxManage startvm ubuntu --type headless

因为M1上virtualbox 不支持,我用的UTM去setup的虚拟机,具体过程详见 here

创建自启动plist文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cat /Users/haofan/Library/LaunchAgents/org.utm.launch.ubuntu.plist
<?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>Label</key>
<string>org.utm.launch.ubuntu.plist</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/UTM.app/Contents/MacOS/utmctl</string>
<string>start</string>
<string>ubuntu</string>
<string>--hide</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

测试:

1
launchctl load /Users/haofan/Library/LaunchAgents/org.utm.launch.ubuntu.plist

最后重启Mac后,虚拟机自动启动。

使用搜索:谷歌必应百度