Skip to content

Instantly share code, notes, and snippets.

@jamesy0ung
Last active April 5, 2025 02:01
Show Gist options
  • Save jamesy0ung/b411143c15d3c8296684c4987e7c9894 to your computer and use it in GitHub Desktop.
Save jamesy0ung/b411143c15d3c8296684c4987e7c9894 to your computer and use it in GitHub Desktop.

Phase 1: Prepare Kernel (Linux aarch64 Build VM)

  1. Download & Extract Kernel:

    wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.85.tar.xz
    tar xvJf linux-6.6.85.tar.xz
    cd linux-6.6.85
  2. Configure & Compile Kernel:

    make defconfig
    make kvm_guest.config
    make -j$(nproc) # Build using all available cores
    # Kernel image created at: arch/arm64/boot/Image
  3. Package Compiled Source & Prepare for Copy:

    # Go back to the parent directory
    cd ..
    # Create an uncompressed tarball (matches later step)
    tar cf linux-6.6.85-compiled.tar linux-6.6.85
    # Note the location of the kernel Image: linux-6.6.85/arch/arm64/boot/Image
    # Make sure SSH is running on this Linux VM if using scp: sudo systemctl enable ssh --now
    # Find this VM's IP address: ip a

Phase 2: Transfer to Host & Prepare Image (Mac Host)

  1. Copy Kernel Files from Linux VM to Mac:

    • (Run these commands on your Mac Host terminal)
    • (Replace linux_vm_user and LINUX_VM_IP with your Linux VM's username and IP address)
    • (Alternatively, use Shared Folders if your VM software supports it)
    # Copy the tarball
    scp linux_vm_user@LINUX_VM_IP:~/linux-6.6.85-compiled.tar .
    
    # Copy the kernel Image file (ensure the path is correct)
    scp linux_vm_user@LINUX_VM_IP:~/linux-6.6.85/arch/arm64/boot/Image .
  2. Resize VM Image: (Ensure the Image file and linux-6.6.85-compiled.tar are in your current directory on the Mac)

    qemu-img resize 2024-11-19-raspios-bookworm-arm64-lite.img 16G

Phase 3: Initial VM Setup (Boot to Shell on Mac Host)

  1. Set Initial Password (Boot to Shell): (Using the Image file copied to your Mac)
    qemu-system-aarch64 -machine virt -cpu cortex-a72 -smp 6 -m 4G \
        -kernel Image \
        -append "root=/dev/vda2 rw init=/bin/bash rootfstype=ext4 rw panic=0 console=ttyAMA0" \
        -drive format=raw,file=2024-11-19-raspios-bookworm-arm64-lite.img,if=none,id=hd0,cache=writeback \
        -device virtio-blk,drive=hd0,bootindex=0 \
        -netdev user,id=mynet,hostfwd=tcp::2222-:22 \
        -device virtio-net-pci,netdev=mynet \
        -monitor telnet:127.0.0.1:5555,server,nowait
    
    # --- Inside QEMU Shell ---
    passwd pi
    # (Enter and confirm new password)
    sync
    exit
    # --- QEMU will exit ---

Phase 4: Configure Running VM (Boot Normally on Mac Host)

  1. Boot VM Normally:

    qemu-system-aarch64 -machine virt,accel=hvf -cpu cortex-a72 -smp 6 -m 4G \
        -kernel Image \
        -append "root=/dev/vda2 rootfstype=ext4 rw panic=0 console=ttyAMA0" \
        -drive format=raw,file=2024-11-19-raspios-bookworm-arm64-lite.img,if=none,id=hd0,cache=writeback \
        -device virtio-blk,drive=hd0,bootindex=0 \
        -netdev user,id=mynet,hostfwd=tcp::2222-:22 \
        -device virtio-net-pci,netdev=mynet \
        -monitor telnet:127.0.0.1:5555,server,nowait
  2. Enable SSH & Connect (Mac Host):

    • Inside the VM (via QEMU console), ensure SSH is enabled (sudo systemctl enable ssh --now).
    • From your Mac terminal:
      ssh-copy-id -p 2222 pi@localhost
      ssh -p 2222 pi@localhost
      # You are now SSH'd into the VM. Run subsequent commands here.
  3. Resize Filesystem (Inside VM via SSH):

    sudo parted /dev/vda # Then: print -> resizepart 2 100% -> quit
    sudo resize2fs /dev/vda2
  4. Install Dependencies (Inside VM via SSH):

    sudo apt update
    sudo apt install bc bison build-essential fakeroot flex git libelf-dev libssl-dev ncurses-dev rsync wget xz-utils
  5. Copy Kernel Archive & Install Modules (Mac -> VM):

    • On Mac Host (in a new terminal tab/window, ensure you are in the directory with linux-6.6.85-compiled.tar):
      # Copy the tarball (created earlier) TO the Pi VM
      scp -P 2222 linux-6.6.85-compiled.tar pi@localhost:~
    • Inside VM (via SSH session from step 8):
      # Extract the archive
      tar xf linux-6.6.85-compiled.tar
      # Navigate into the extracted directory
      cd linux-6.6.85
      # Install the modules
      sudo make modules_install
      cd ~ # Go back home
      # Shutdown VM cleanly before next boot
      sudo shutdown now

Phase 5: Boot with GPU (on Mac Host)

  1. Boot with GPU: (Using the Image file on your Mac)
    qemu-system-aarch64 -machine virt,accel=hvf -cpu cortex-a72 -smp 10 -m 4G \
        -kernel Image \
        -append "root=/dev/vda2 rootfstype=ext4 rw panic=0 console=ttyAMA0" \
        -drive format=raw,file=2024-11-19-raspios-bookworm-arm64-lite.img,if=none,id=hd0,cache=writeback \
        -device virtio-blk,drive=hd0,bootindex=0 \
        -netdev user,id=mynet,hostfwd=tcp::2222-:22 \
        -device virtio-net-pci,netdev=mynet \
        -device virtio-gpu-pci \
        -device usb-ehci,id=ehci \
        -device usb-kbd \
        -device usb-mouse \
        -monitor telnet:127.0.0.1:5555,server,nowait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment