当前位置:   article > 正文

macOS KVM 虚拟机_mac kvm

mac kvm

在内网要做macos的编译 CI流水线,需要在zstack平台装一台虚拟机(它使用的是KVM,类似openstack)

尝试了好几种方式,最后选择了3

  1. https://github.com/sickcodes/Docker-OSX之有试过docker里跑kvm的一个方法,确实可行,但不知道怎么设置网络,ping不通其他机器,作罢

  2. 参考之前做linux windows虚拟机的经验,以为vmware装一个macos,做出来的vmdk 再用qemu-img转成qcow2可以在zstack上跑,无法启动,失败

  3. https://github.com/foxlet/macOS-Simple-KVM这个可行,开始折腾了半天libvirtd 的vnc密码,后面发现空密码不行,必须要设置一个密码,才能vnc;其他就是一台物理机centos7上的qemu版本有点问题,跑不起来这个,换了一个zstack上的虚拟机,套娃跑这个虚拟机,可行,稍微调整一下配置文件里的qemu kvm二进制路径,usb设备等就行了

  4. 还是想在zstack上直接跑这个macos,毕竟zstack做快照、备份方便点,要不就得自己人工做;但折腾了一下发现卡在clover选磁盘这里,对比了下zstack libvirtd qemu的配置,与macos-simple-kvm的UEFI固件有区别,[Solved] Clover hangs on "4630 scan entries" | tonymacx86.com结合这个,推断确实zstack上不能直接跑macos

  5. 其实还有一种方式是gitlab runner的libvirtd exectuor方式,但只看到linux虚拟机的实践,mac和windows的没有看到

使用macOS-Simple-KVM,把仓库克隆,进入目录:

  1. 外网找一台ubuntu22的机器 装好qemu等依赖

  2. ./jumpstart.sh --catalina 下载苹果的catalina镜像 BaseSystem.img

  3. 创建一个空磁盘 qemu-img create -f qcow2 MyDisk.qcow2 256G

  4. 给basic.sh 添加两行, 然后运行basic.sh 安装macos后关虚拟机, 设置用户名密码(比如设置用户是user,密码是12345)

  1. -drive id=SystemDisk,if=none,file=MyDisk.qcow2 \
  2. -device ide-hd,bus=sata.4,drive=SystemDisk \
  1. https://github.com/foxlet/macOS-Simple-KVM/issues/566按照这个这个编辑ESP.qcow2中的config.plist 让clover自动选磁盘(diskutil list 拿到 /dev/disk3 vm后面的一个名称 ),不然每次启动还要去选择从哪里启动

  2. 把项目整个目录拿到内网,找个欧拉2003sp3的虚拟机(其他较新的系统也行,centos7似乎有问题),装好livirtd以及qemu-kvm一堆程序, 整个项目目录记得chown成qemu用户的,放到比如/opt/os下(qemu能访问到), 运行 ./make.sh --add,  再用virsh edit macOS-Simple-KVM添加磁盘

  3. 配置类似(根据你qemu的版本,需要调整路径等,微调),这里设置qemu的vnc密码为123

  1. msu@msu-System-Product-Name:~$ sudo cat /etc/libvirt/qemu/macOS-Simple-KVM.xml
  2. [sudo] password for msu:
  3. <!--
  4. WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
  5. OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  6. virsh edit macOS-Simple-KVM
  7. or other application using the libvirt API.
  8. -->
  9. <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  10. <name>macOS-Simple-KVM</name>
  11. <uuid>d06d502a-904a-4b34-847d-debf1a3d76c7</uuid>
  12. <memory unit='KiB'>2097152</memory>
  13. <currentMemory unit='KiB'>2097152</currentMemory>
  14. <vcpu placement='static'>4</vcpu>
  15. <os>
  16. <type arch='x86_64' machine='pc-q35-6.2'>hvm</type>
  17. <loader readonly='yes' type='pflash'>/media/msu/本地磁盘/macos/macOS-Simple-KVM/firmware/OVMF_CODE.fd</loader>
  18. <nvram>/media/msu/本地磁盘/macos/macOS-Simple-KVM/firmware/OVMF_VARS-1024x768.fd</nvram>
  19. <boot dev='hd'/>
  20. </os>
  21. <features>
  22. <acpi/>
  23. <apic/>
  24. <vmport state='off'/>
  25. </features>
  26. <cpu mode='custom' match='exact' check='none'>
  27. <model fallback='forbid'>qemu64</model>
  28. <topology sockets='1' dies='1' cores='4' threads='1'/>
  29. </cpu>
  30. <clock offset='utc'>
  31. <timer name='rtc' tickpolicy='catchup'/>
  32. <timer name='pit' tickpolicy='delay'/>
  33. <timer name='hpet' present='no'/>
  34. </clock>
  35. <on_poweroff>destroy</on_poweroff>
  36. <on_reboot>restart</on_reboot>
  37. <on_crash>destroy</on_crash>
  38. <pm>
  39. <suspend-to-mem enabled='no'/>
  40. <suspend-to-disk enabled='no'/>
  41. </pm>
  42. <devices>
  43. <emulator>/usr/bin/qemu-system-x86_64</emulator>
  44. <disk type='file' device='disk'>
  45. <driver name='qemu' type='qcow2'/>
  46. <source file='/media/msu/本地磁盘/macos/macOS-Simple-KVM/ESP.qcow2'/>
  47. <target dev='sda' bus='sata'/>
  48. <address type='drive' controller='0' bus='0' target='0' unit='0'/>
  49. </disk>
  50. <disk type='file' device='disk'>
  51. <driver name='qemu' type='raw'/>
  52. <source file='/media/msu/本地磁盘/macos/macOS-Simple-KVM/BaseSystem.img'/>
  53. <target dev='sdb' bus='sata'/>
  54. <address type='drive' controller='0' bus='0' target='0' unit='1'/>
  55. </disk>
  56. <disk type='file' device='disk'>
  57. <driver name='qemu' type='qcow2'/>
  58. <source file='/media/msu/本地磁盘/macos/macOS-Simple-KVM/MyDisk.qcow2'/>
  59. <target dev='sdc' bus='sata'/>
  60. <address type='drive' controller='0' bus='0' target='0' unit='2'/>
  61. </disk>
  62. <controller type='usb' index='0' model='ich9-ehci1'>
  63. <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x7'/>
  64. </controller>
  65. <controller type='usb' index='0' model='ich9-uhci1'>
  66. <master startport='0'/>
  67. <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x0' multifunction='on'/>
  68. </controller>
  69. <controller type='usb' index='0' model='ich9-uhci2'>
  70. <master startport='2'/>
  71. <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x1'/>
  72. </controller>
  73. <controller type='usb' index='0' model='ich9-uhci3'>
  74. <master startport='4'/>
  75. <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x2'/>
  76. </controller>
  77. <controller type='sata' index='0'>
  78. <address type='pci' domain='0x0000' bus='0x00' slot='0x1f' function='0x2'/>
  79. </controller>
  80. <controller type='pci' index='0' model='pcie-root'/>
  81. <interface type='network'>
  82. <mac address='52:54:00:92:d4:7b'/>
  83. <source network='default'/>
  84. <model type='e1000-82545em'/>
  85. <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
  86. </interface>
  87. <serial type='pty'>
  88. <target type='isa-serial' port='0'>
  89. <model name='isa-serial'/>
  90. </target>
  91. </serial>
  92. <console type='pty'>
  93. <target type='serial' port='0'/>
  94. </console>
  95. <input type='mouse' bus='usb'>
  96. <address type='usb' bus='0' port='1'/>
  97. </input>
  98. <input type='keyboard' bus='usb'>
  99. <address type='usb' bus='0' port='2'/>
  100. </input>
  101. <input type='mouse' bus='ps2'/>
  102. <input type='keyboard' bus='ps2'/>
  103. <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us' passwd='123'>
  104. <listen type='address' address='0.0.0.0'/>
  105. </graphics>
  106. <sound model='ich9'>
  107. <address type='pci' domain='0x0000' bus='0x00' slot='0x1b' function='0x0'/>
  108. </sound>
  109. <audio id='1' type='spice'/>
  110. <video>
  111. <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
  112. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
  113. </video>
  114. <redirdev bus='usb' type='spicevmc'>
  115. <address type='usb' bus='0' port='3'/>
  116. </redirdev>
  117. <redirdev bus='usb' type='spicevmc'>
  118. <address type='usb' bus='0' port='4'/>
  119. </redirdev>
  120. <memballoon model='none'/>
  121. </devices>
  122. <qemu:commandline>
  123. <qemu:arg value='-cpu'/>
  124. <qemu:arg value='Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+pcid,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,check'/>
  125. <qemu:arg value='-device'/>
  126. <qemu:arg value='isa-applesmc,osk=ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc'/>
  127. <qemu:arg value='-smbios'/>
  128. <qemu:arg value='type=2'/>
  129. </qemu:commandline>
  130. </domain>
  1. 最后virsh start macOS-Simple-KVM 跑起来

  2. 网络是nat的 在宿主机上 删除libvirtd的规则 sudo iptables -D LIBVIRT_FWI 2, 与10的iptables配置冲突了

  3. 在mac里增加屏幕共享(vnc),远程访问(ssh)添加转发规则 ,宿主机2222端口转发到虚拟机22端口,5999端口转发到虚拟机5900端口, 10.113.6.118为宿主机IP;qemu自身的vnc监听在宿主机的5900端口,但这个vnc不太好用,鼠标太飘

  1. iptables -t nat -A PREROUTING -p tcp -d 10.113.6.118/32 --dport 2222 -j DNAT --to-destination 192.168.122.186:22
  2. iptables -t nat -A PREROUTING -p tcp -d 10.113.6.118/32 --dport 5999 -j DNAT --to-destination 192.168.122.186:5900

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/973328
推荐阅读
相关标签
  

闽ICP备14008679号