Access linux partitions from macos using qemu

2023-09-01

usual disclaimer: no one is responsible for any damage/loss by using this

First step:

On macos install brew (if not installed) and with it install qemu

brew install qemu

and

brew install wget

Download Alpine Linux aarc64 virtual iso image from https://alpinelinux.org/downloads/

Similar to standard. Slimmed down kernel. Optimized for virtual systems.)

create virtual disk image if want to install it and use, though iso image could be used without installation

Download ARM (aarch64) Virtual Machine Firmware EFI from https://dl-cdn.alpinelinux.org/alpine/edge/community/aarch64/

/opt/homebrew/bin/wget https://dl-cdn.alpinelinux.org/alpine/edge/community/aarch64/aavmf-0.0.202302-r1.apk

currently file name is https://dl-cdn.alpinelinux.org/alpine/edge/community/aarch64/aavmf-0.0.202302-r1.apk

(probably firmware from other distros could be used)

Unpack it with tar xf $avmf-file-name (Alpine packages could be extracted with tar)

tar xf aavmf-0.0.202302-r1.apk

and copy QEMU_EFI.fd from usr/share/AAVMVF to current dir

cp usr/share/AAVMF/QEMU_EFI.fd .

Find linux partitions on macos with diskutil list command

in this example it is disk0s6 and added to qemu -drive paramater bellow

run qemu with (just example, parameters could be changed as desired for RAM, CPU, virtual disks ...) optionally add path qemu binary

qemu should be invoked with 'sudo' - sudo ./start-qemu-script.sh for example

if this doesn't work then with su command become root

sudo su

sample script

/opt/homebrew/bin/qemu-system-aarch64 -machine virt,accel=hvf,highmem=off -cpu cortex-a57 -smp cores=4 -m 1G \
 -nic user,model=virtio -device virtio-rng-pci \
 -bios QEMU_EFI.fd \
 -rtc base=utc,clock=host \
 -drive if=virtio,id=hd0,format=raw,file=alpine-virt-3.18.3-aarch64.iso\
 -drive if=virtio,id=hd1,format=raw,file=/dev/disk0s6 \
 -device qemu-xhci -device usb-mouse \
 -device usb-kbd \
 -serial stdio \
 -net user \
 -device virtio-gpu-pci

Linux partition will be device /dev/vdb

it could be mounted with command:

mount /dev/vdb /mnt

also, more partitions could be specified in qemu parameters. for example another one can be added as:

-drive if=virtio,id=hd2,format=raw,file=/dev/disk0s7 \

and it will be /dev/vdc

It is possible to create VM qemu disk image when this iso is booted by creating file, for example:

qemu-img create -f raw rescue.img 1G

name and size could be different but 1GB size is quite enough to install some rescue tools on it.

How to install Alpine Linux is documented at https://docs.alpinelinux.org/user-handbook/0.1a/index.html