Run Raspbian with qemu and virt-manager

image

Run with QEMU

  • Install necessary tools

    # For Debian
    $ sudo apt-get install qemu-utils qemu-system-arm unzip
    
    # For Fedora
    $ sudo dnf install qemu-img qemu-system-arm unzip
    
  • Extract the image

    $ unzip <raspbian-image>.zip
    
  • Convert .img to .qcow2

    $ qemu-img convert -f raw -O qcow2 <raspbian-image>.img <raspbian-image>.qcow2
    

    Increase image size to the size you want

    $ qemu-img resize <raspbian-image>.qcow2 +10G
    
  • If you just want to boot it with QEMU:

    $ sudo qemu-system-arm -kernel kernel-qemu-4.14.79-stretch \
    -dtb versatile-pb.dtb \
    -m 256 -cpu arm1176 \
    -machine versatilepb \
    -hda <raspbian-image>.qcow2 \
    -append "root=/dev/sda2"
    
  • Login with username:password is pi:raspberry.

Run with virt-manager

  • Install virt-manager

    # For Debian
    $ sudo apt-get install virt-manager
    
    # For Fedora
    $ sudo dnf install virt-manager
    
  • Create a sample domain file /tmp/rpi.xml with content:

    <domain type='qemu'>
      <name>rpi</name>
      <uuid>b6dc9356-f772-4f63-bf78-0fe5f9f11923</uuid>
      <memory unit='KiB'>262144</memory>
      <currentMemory unit='KiB'>262144</currentMemory>
      <os>
        <type arch='armv7l' machine='versatilepb'>hvm</type>
        <kernel>/path/to/kernel-qemu-4.14.79-stretch</kernel> <!--update path here-->
        <cmdline>root=/dev/sda2</cmdline>
        <dtb>/path/to/versatile-pb.dtb</dtb>                  <!--update path here-->
        <boot dev='hd'/>
      </os>
      <cpu mode='custom' match='exact' check='none'>
        <model fallback='forbid'>arm1176</model>
      </cpu>
      <devices>
        <emulator>/usr/bin/qemu-system-arm</emulator>
        <disk type='file' device='disk'>
          <driver name='qemu' type='qcow2'/>
          <source file='/path/to/.qcow2'/>                     <!--update path here-->
          <backingStore/>
          <target dev='sda' bus='scsi'/>
          <address type='drive' controller='0' bus='0' target='0' unit='0'/>
        </disk>
        <controller type='pci' index='0' model='pci-root'/>
        <interface type='bridge'>
          <mac address='52:54:00:ed:eb:c7'/>
          <source bridge='virbr0'/>
          <model type='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
        </interface>
        <graphics type='spice' autoport='yes'>
          <listen type='address'/>
          <image compression='off'/>
          <gl enable='no' rendernode='/dev/dri/by-path/pci-0000:00:02.0-render'/>
        </graphics>
        <video>
          <model type='virtio' heads='1' primary='yes'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
        </video>
      </devices>
    </domain>
    

    Change the path of kernel-qemu-4.14.79-stretch, versatile-pb.dtb and .qcow2 file to the correct absolute path.

  • Create a new domain for raspbian

    $ sudo virsh define /tmp/rpi.xml
    $ sudo virsh start rpi
    

    Open Virtual Machine Manager (virt-manager), you will see a new machine name rpi is running.

  • In the virtual machine, login with username:password is pi:raspberry.

  • Extend partitions and file system size to the image size

    $ sudo /usr/lib/raspi-config/init_resize.sh
    # Waiting for reboot done
    $ sudo resize2fs /dev/sda2
    
  • Change keyboard layout from gb to us

    $ sudo vi /etc/default/keyboard
    ...
    XKBLAYOUT="us"
    ...
    

    then restart service console-setup.sh

    $ sudo service console-setup.sh restart
    

Done.

See also

comments powered by Disqus