MacOS in a VM

A history of my personal computing hardware

I had a series of desktops and laptops between 1997 and 2005. I borrowed a Sony Vaio laptop for a while from a good friend, then purchased some older used ThinkPad, then a new Asus latop where I ran Linux. I made the switch to MacOS around 2005. Iwent from an Asus laptop running Linux to one of those beautiful PowerBook G4 laptops. I got mine used on eBay, and when Apple released the first Intel based MacBooks I sold that powerbook (on eBay) and ordered a shiny new white MacBook in 2006. I still have that first generation intel Laptop today, and it still works!

Some time went by and in 2011 I purchased new MacBook Pro laptops for me and my wife. Hers still works and mine does too, but around 2018 the AMD GPU died and the screen output become unsuable. It is now only useable via SSH or Screen Sharing.

I purchased a 2018 MacBook Pro with the aweful butterfly keys, only because I had too. Nothing else was available. Because of known issues with the butternfly keys, I reluctantly purchased a mid-spec MacBook Pro. This was the worst ~$2,000 laptop I have had the displeasure of owning. Everything about it worked fine, except for the keys.

In December 2021 I purchased an Apple Silicon (M1) MacBook Air to replace the 2018 MBP, which I traded in to Costco for $500. Good riddance to that thing!

Time Machine Backups

Apple has time machine and that was running on the MBP backing up to a Samba share on my Ubuntu+ZFS NAS. Unfortunately with the new MB Air, time machine could not restore from this backup and I had to manually copy things over. Eventually I would get Time Machine running on the M1 MBA.

Setting up a Linux hypervisor

This setup is running Ubuntu 22.04 LTS on a headless (no monitor or windowing system) 1-Liter PC (1LPC) with an Intel Core i5-8500T, 16GB RAM, and a fast 500GB NVMe SSD. You can configure a similar system using my ubuntu_hypervisor role+playbook.

Clone repo and configure

Clone the kholia/OSX-KVM repo.
Do the KVM config preparation (link):
KVM may need the following tweak on the host machine to work.

echo 1 | sudo tee /sys/module/kvm/parameters/ignore_msrs

To make this change permanent, you may use the following command.

sudo cp kvm.conf /etc/modprobe.d/kvm.conf  # for intel boxes only, after cloning the repo below

Install QEMU and other packages.

sudo apt-get install qemu uml-utilities virt-manager git \
    wget libguestfs-tools p7zip-full make dmg2img -y

Restart the Linux OS or restart the libvirt services. This will shutdown any existing/running VMs so don't do this on a production server.

Follow the remaining steps of:
Download the OS image with (I used 11.7 since 11.6 is what I was running on the 2018 MBP)
Convert the OS image with dmg2img dmg2img -i BaseSystem.dmg BaseSystem.img
* Create the disk for qemu to use as the OS qemu-img create -f qcow2 mac_hdd_ng.img 128G

The next step is to run which starts qemu-system-x86_64 with a bunch of MacOS specific flags. It also requires an X11 server to be running. Since I am running this headless I modified the script to include the -vnc flag which enables VNC with basic password support:

    -vnc :0,password=on -k en-us
    -monitor stdio
    -device VGA,vgamem_mb=128

Now run the script from your ssh terminal into your KVM/Libvirt enabled Ubuntu 20.04 or 22.04 server. Right after you run the script you will need to hit enter to get to the (qemu) command prompt. Set a VNC password by entering change vnc password. Press Return, then enter your password for the VNC session.

I can now use a VNC client such as the one MacOS provides to connect to hostname:5900 with the password I set in the (qemu) terminal. NOTE: The mouse will not work initially in the VNC session where you are presented with the disks to boot. Use the keyboard to navigate. Tab, arrow keys, and Return/Enter.

Install MacOS

Since this is a new disk it needs to be erased and the OS installed. I wont cover the OS installation here, but its pretty standard. Click through the dialogs. This will take around an hour to complete depending on hardware.

Restoring from Time Machine

During first boot we are given the migration assistant option to restore from a backup. I added my samba server with credentials and was presented with my old laptop's final time machine backups from December 2021. Pick the most recent and restore.

Getting the VM to boot with virsh and at boot, and keep a VNC socket open

With the VM shutdown copy the repo directory to an appropriate location (e.g. /var/lib/libvirt/).
Adjust the paths mentioned in the macOS-libvirt-Catalina.xml file to match the new location.
Modify the XML <graphics> lines to enable VNC per Scott Lowe's blog post. It will look something like:

<graphics type='vnc' port='-1' autoport='yes' listen='' passwd='my_insecure_password'/>

This will let you connect with a VNC client to port 5900, or whatever is next available if there are other VMs running with VNC enabled.