QEMU (Application)

From campisano.org
Jump to navigation Jump to search

Using a debian as host

Install QEMU KVM version

su -
apt-get install qemu-kvm
exit


Install Debian on a Debian

Create a raw image

  • Still raw images does not allocate all the initial required disk space (50 GB in this example), the initial disk space will be near to 0 and will increment during the use
qemu-img create -f qcow2 debian11emu64bit.qcow2 50G

Install Debian

  • In this example, we will emulate the same x86_64 system, with the same cpu as the host but with 4 cores and 6 GB of ram

Note the use of usb emulation and pass-through as well as ssh port forwarding.

qemu-system-x86_64 -no-user-config -nodefaults -no-reboot\
 -accel kvm\
 -machine q35\
 -cpu host\
 -smp cores=4\
 -m size=6G\
 -rtc base=utc\
 -serial none\
 -parallel none\
 -display gtk,grab-on-hover=on\
 -monitor vc\
 -vga none\
 -device VGA,vgamem_mb=128\
 -device qemu-xhci,id=xhci_ctrl\
 -device usb-kbd,bus=xhci_ctrl.0,port=1\
 -device usb-tablet,bus=xhci_ctrl.0,port=2\
 -device usb-host,bus=xhci_ctrl.0,port=3,vendorid=0x20a0,productid=0x4108\
 -device ich9-intel-hda\
 -device hda-duplex\
 -device rtl8139,netdev=network0\
 -netdev user,id=network0,hostfwd=tcp::10022-:22\
 -device ide-hd,drive=disk0\
 -drive id=disk0,if=none,cache=writethrough,format=qcow2,media=disk,file=debian11emu64bit.qcow2\
 -cdrom firmware-11.2.0-amd64-DVD-1.iso

Install Windows 7 on a Debian

Create a raw image

  • Still raw images does not allocate all the initial required disk space (50 GB in this example), the initial disk space will be near to 0 and will increment during the use
qemu-img create -f qcow2 win7emu64bit.qcow2 50G

Install Windows

  • In this example, we will emulate the same x86_64 system, with the same cpu as the host but with 4 cores and 3 GB of ram

Note the use of samba share folder that can be mapped as //10.0.2.4/qemu

qemu-system-x86_64 -no-user-config -nodefaults -no-reboot\
 -enable-kvm -machine q35 -cpu host -smp cores=4 -m size=8G\
 -rtc base=localtime -serial none -parallel none\
 -display gtk,grab-on-hover=on -vga std\
 -usb -device usb-tablet\
 -device ich9-intel-hda -device hda-duplex\
 -device e1000,netdev=network0 -netdev user,id=network0,smb="/home/shared/network",hostfwd=tcp::18080-:8080\
 -device ide-hd,drive=disk0 -drive id=disk0,if=none,cache=writeback,format=qcow2,media=disk,file=win7emu64bit.qcow2\
 -cdrom windows_7.iso;

Using a Macos as host

Install QEMU

  • then, use brew to install qemu:
brew install qemu

Install Debian 10 on a MacOs

Intel-based Macs

Create a raw image

  • Still raw images does not allocate all the initial required disk space (50 GB in this example), the initial disk space will be near to 0 and will increment during the use
qemu-img create -f qcow2 debian.qcow2 50G

Install Debian

  • In this example, we will emulate the same x86_64 system, with the same cpu as the host but with 6 cores and 10 GB of ram
qemu-system-x86_64 -no-user-config -nodefaults -no-reboot\
 -cpu host -smp 6 -m 10240 -accel hvf\
 -display default,show-cursor=on -full-screen\
 -device virtio-vga,edid=true,xres=1440,yres=900\
 -device virtio-keyboard-pci\
 -device virtio-tablet-pci\
 -audiodev coreaudio,id=snd0\
 -device intel-hda -device hda-output,audiodev=snd0\
 -device virtio-net-pci,netdev=network0\
 -netdev user,id=network0\
 -device virtio-blk-pci,drive=disk0\
 -drive id=disk0,if=none,cache=writethrough,format=qcow2,media=disk,file=debian.qcow2\
 -cdrom debian.iso

Run after installation

  • Note, the follow example forwards guest port 22 to host port 10022
qemu-system-x86_64 -no-user-config -nodefaults -no-reboot\
 -cpu host -smp 6 -m 10240 -accel hvf\
 -display default,show-cursor=on -full-screen\
 -device virtio-vga,edid=true,xres=1440,yres=900\
 -device virtio-keyboard-pci\
 -device virtio-tablet-pci\
 -audiodev coreaudio,id=snd0\
 -device intel-hda -device hda-output,audiodev=snd0\
 -device virtio-net-pci,netdev=network0\
 -netdev user,id=network0,hostfwd=tcp::10022-:22\
 -device virtio-blk-pci,drive=disk0\
 -drive id=disk0,if=none,cache=writethrough,format=qcow2,media=disk,file=debian.qcow2

ARM 64 (aarch64) bases Macs

from https://stackoverflow.com/questions/66819049/qemu-system-aarch64-accel-hvf-invalid-accelerator-hvf

and https://mricher.fr/post/boot-from-an-efi-shell/

Create a raw image

  • Still raw images does not allocate all the initial required disk space (50 GB in this example), the initial disk space will be near to 0 and will increment during the use
qemu-img create -f qcow2 debian.qcow2 50G
  • Copy the efi firmware image

Note, this example has qemu 6.2.0 installed in /opt/homebrew/

cp -a /opt/homebrew/Cellar/qemu/6.2.0/share/qemu/edk2-aarch64-code.fd .
  • Create a efi_vars image to store the boot configs
qemu-img create -f qcow2 efi_vars.fd 67108864b

Install Debian

  • In this example, we will emulate the same arch64 system, with the same cpu as the host but with 8 cores and 16 GB of ram

Note the use of usb emulation and pass-through as well as ssh port forwarding.

echo "WARN: please note: sudo is needed only to access (pass through) the host usb device"
sleep 5s

sudo qemu-system-aarch64 -no-user-config -nodefaults -no-reboot\
 -accel hvf\
 -machine virt,highmem=on\
 -cpu host\
 -smp cpus=8,sockets=1,cores=8,threads=1\
 -m size=16G\
 -rtc base=utc\
 -serial none\
 -parallel none\
 -display default,show-cursor=on\
 -monitor vc\
 -vga none\
 -device virtio-gpu-pci\
 -device nec-usb-xhci,id=xhci_ctrl\
 -device usb-kbd,bus=xhci_ctrl.0,port=1\
 -device usb-tablet,bus=xhci_ctrl.0,port=2\
 -device usb-host,bus=xhci_ctrl.0,port=3,vendorid=0x20a0,productid=0x4108\
 -device ich9-intel-hda\
 -device hda-duplex\
 -device e1000,netdev=network0\
 -netdev user,id=network0,hostfwd=tcp::10022-:22\
 -drive if=pflash,format=raw,unit=0,file=edk2-aarch64-code.fd,readonly=on\
 -drive if=pflash,format=qcow2,unit=1,file=efi_vars.fd\
 -drive if=virtio,cache=writeback,format=qcow2,media=disk,file=debian.qcow2\
 -cdrom debian-11.2.0-arm64-DVD-1.iso

Convert a Virtualbox image

qemu-img convert -f vdi -O raw Win7Emu.vdi Win7Emu.img

References