Skip to content

Instantly share code, notes, and snippets.

@aserhat
Last active March 23, 2024 14:51
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save aserhat/91c1d5633d395d45dc8e5ab12c6b4767 to your computer and use it in GitHub Desktop.
Save aserhat/91c1d5633d395d45dc8e5ab12c6b4767 to your computer and use it in GitHub Desktop.
QEMU and HVF
# Summary
A few notes I took to see if I could use MacOS as Hypevirsor in a similar fashion to Linux
I wanted to see how few addons were needed instead of using Parallels, Virtual Box, VM Fsion etc.
The idea is to use QEMU, Hypervisor Framework (https://developer.apple.com/documentation/hypervisor) and some custom host networking.
# Installations
brew install qemu (For controlling Hypervisor Framework)
brew install cdrtools (For making cloud init iso's)
http://tuntaposx.sourceforge.net/download.xhtml (For customer tap based networking)
# Helpful Links (Mostly for the networking stuff)
https://davidhamann.de/2017/04/19/sharing-vpn-on-macos/
https://github.com/ckujau/scripts/blob/master/qemu-tap.sh
https://superuser.com/questions/596095/how-do-i-bridge-a-connection-from-wi-fi-to-tap-on-mac-os-x-for-the-emulator-qe
https://blog.san-ss.com.ar/2016/04/setup-nat-network-for-qemu-macosx
https://linuxconfig.org/how-to-configure-static-ip-address-on-ubuntu-18-04-bionic-beaver-linux
https://www.techrepublic.com/article/how-to-configure-a-static-ip-address-in-ubuntu-server-18-04/
http://osxdaily.com/2015/06/24/load-unload-kernel-extensions-mac-os-x/
#Static Builds
## Creata disk
qemu-img create -f qcow2 server1.img 10G
## Install the OS onto the disk
qemu-system-x86_64 -M accel=hvf --cpu host -hda server1.img -cdrom ubuntu-18.04.2-live-server-amd64.iso -boot d -m 2048
## Boot the server
qemu-system-x86_64 -M accel=hvf --cpu host server1.img -m 2048
## Boot the server with some custom networking
## This requires some additional configuration on the host to make work.
sudo qemu-system-x86_64 -M accel=hvf --cpu host server1.img -m 2048 -netdev tap,id=tap0,script=no,downscript=no -device e1000,netdev=tap0
## This requires some scripts at /etc/qemu-ifup and /etc/qemu-ifdown
sudo qemu-system-x86_64 -M accel=hvf --cpu host server1.img -m 2048 -net nic,model=virtio -net tap,br=/dev/bridge1,id=tap0
# Cloud Based Images
## Create the meta-data and user-data files (samples are the other files in this gist)
## Create the init iso
mkisofs -output init.iso -volid cidata -joliet -rock {user-data,meta-data}
## Boot the cloud image
qemu-system-x86_64 -M accel=hvf --cpu host -hda ubuntu-18.04-server-cloudimg-amd64.img --cdrom init.iso -boot d -m 2048
## Working on making this work with custom networking this was a pain to do without having ssh access to login to the server and test stuff out.
## Once the network is all settled I can work on headless most and some automation to bring up and down the VM's via Ansible.
# sample cloud init meta-data file, everything below this line.
instance-id: k8sm1
local-hostname: k8sm1
# sample cloud init user-data file, everything below this line.
#cloud-config
#obviously for demo purposes only, would want to inject ssh keys here.
#sets the ubuntu user password, root user password and allows password ssh vs key based ssh.
hostname: k8sm1
fqdn: k8sm1
ssh_pwauth: True
password: password
chpasswd:
list: |
root:password
expire: False
@vaibhav-kaushal
Copy link

I guess this is for an Intel based Mac, is it? Or did it work on an M1 machine?

@aserhat
Copy link
Author

aserhat commented Jun 20, 2021

@vaibhav-kaushal. This is from Intel based MAC.

@desrod
Copy link

desrod commented Dec 26, 2021

I can also confirm, hvf support does not work in qemu under M1 chipsets using x86_64 emulation, using the version of qemu in brew (6.2.0) nor built from upstream git source with --enable-hvf. That support does get compiled into the aarch64 qemu binary, but not the x86_64 qemu binary.

Likewise UTM, adding the -accel hvf flag throws a warning, because the embedded qemu that ships with UTM, also does not support hvf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment