Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:guide-user:virtualization:qemu_host [2018/06/03 13:11] – ↷ Page moved and renamed from inbox:qemu_guests to docs:guide-user:virtualization:qemu_host bobafetthotmail | docs:guide-user:virtualization:qemu_host [2022/05/30 09:04] (current) – [Download installer files] use https sebastian | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== OpenWrt |
| ===== Introduction ===== | ===== Introduction ===== | ||
| - | It's possible to use OpenWrt as a QEMU host and run guests on it. If you want to run OpenWrt as a QEMU guest, see [[docs: | + | It's possible to use OpenWrt as a QEMU host and run guests on it. If you want to run OpenWrt as a QEMU guest itself, see [[docs: |
| - | + | ||
| - | OpenWrt provides QEMU packages for ARM and x86 platforms. This article focuses on the x86 target, the networking is done via '' | + | |
| + | OpenWrt provides QEMU packages for ARM and x86 platforms. This article focuses on the x86 target, the networking is done via [[https:// | ||
| ===== Installing QEMU ===== | ===== Installing QEMU ===== | ||
| - | You need the following packages on your device: | + | You need the following packages on your device: |
| - | Example for an Intel system: | + | Example for an Intel system |
| < | < | ||
| opkg install kmod-tun qemu-bridge-helper qemu-x86_64-softmmu kmod-kvm-intel | opkg install kmod-tun qemu-bridge-helper qemu-x86_64-softmmu kmod-kvm-intel | ||
| Line 16: | Line 15: | ||
| ===== Running a guest ===== | ===== Running a guest ===== | ||
| - | For the guest operation system, use a distribution that comes with virtio drivers by default (Debian or Fedora for example). | + | For the guest OS, use a distribution that comes with virtio drivers by default (Debian or Fedora for example). |
| - | The following QEMU command uses a physical disk, but you can use a qcow2 image as well. Below are explanations of the networking options. | + | The following QEMU command uses a physical disk, but you can use a disk image as well. Below are explanations of the networking options. |
| <code bash> | <code bash> | ||
| qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ | qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ | ||
| Line 26: | Line 25: | ||
| </ | </ | ||
| - | **Line 3** creates a virtio net device with a fixed MAC address | + | **Line 3** creates a virtio net device with a fixed MAC address |
| If your guest uses DHCP it should now receive an address by the server running in '' | If your guest uses DHCP it should now receive an address by the server running in '' | ||
| - | You can choose any other bridge interface available on your OpenWrt host as well. | + | You can choose any other bridge interface available on your OpenWrt host as well. See [[docs: |
| + | If you want to change which networks QEMU is allowed to connect to, you can do so via ''/ | ||
| - | ===== Init script | + | ===== Installing Debian as guest OS ===== |
| + | If you don't have a prepared disk image, you can install a guest OS directly on your OpenWrt device. There are several guides available on how to install a Linux distribution on a QEMU image: [[https:// | ||
| + | A quick way is to install Debian using the kernel and initrd of a Debian netboot installer. | ||
| + | ==== Create a disk image ==== | ||
| + | <code bash> | ||
| + | qemu-img create -f qcow2 debian.img 4G | ||
| + | </ | ||
| + | More details on disk images: [[https:// | ||
| - | To automatically start the VM at boot and shut it down cleanly using [[https:// | + | ==== Download installer files ==== |
| + | <code bash> | ||
| + | wget https:// | ||
| + | wget https:// | ||
| + | </ | ||
| + | Both files can be savely removed after finishing the installation. | ||
| + | |||
| + | ==== Run the installer ==== | ||
| + | The following command will launch the Debian installer inside QEMU, '' | ||
| + | <code bash> | ||
| + | qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ | ||
| + | -initrd initrd.gz \ | ||
| + | -kernel linux \ | ||
| + | -append " | ||
| + | -drive file=debian.img, | ||
| + | -device virtio-net-pci, | ||
| + | -netdev bridge, | ||
| + | -nographic | ||
| + | </ | ||
| + | Follow the installation instructions and install GRUB on ''/ | ||
| + | |||
| + | ==== Run the new guest ==== | ||
| + | After finishing the installation, | ||
| + | <code bash> | ||
| + | qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ | ||
| + | -drive file=debian.img, | ||
| + | -device virtio-net-pci, | ||
| + | -netdev bridge, | ||
| + | -nographic | ||
| + | </ | ||
| + | You should now be able to reach the VM via SSH from within '' | ||
| + | If you want to control the VM using the command line, you have to enable a serial console. To do this, edit the GRUB entry during boot and add '' | ||
| + | |||
| + | ===== Init script ===== | ||
| + | To automatically start the VM at boot and shut it down cleanly using [[https:// | ||
| + | <WRAP center round important 60%> | ||
| + | Be careful with copying the heredoc part. This code block is left unindented on purpose to avoid problems with whitespaces. If you want, you can indent the lines with tabs and use '' | ||
| + | </ | ||
| <code bash / | <code bash / | ||
| Line 40: | Line 84: | ||
| START=99 | START=99 | ||
| STOP=1 | STOP=1 | ||
| + | |||
| + | qemu_pidfile="/ | ||
| start() { | start() { | ||
| qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ | qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ | ||
| - | | + | -drive file=/ |
| - | -device virtio-net-pci, | + | -device virtio-net-pci, |
| - | -netdev bridge, | + | -netdev bridge, |
| - | -qmp tcp: | + | -qmp tcp: |
| - | -daemonize &> / | + | -daemonize &> / |
| + | |||
| + | / | ||
| + | echo "QEMU: Started VM with PID $(cat $qemu_pidfile)." | ||
| } | } | ||
| stop() { | stop() { | ||
| + | echo "QEMU: Sending ' | ||
| nc localhost 4444 << | nc localhost 4444 << | ||
| - | { " | + | { " |
| - | { " | + | { " |
| QMP | QMP | ||
| - | sleep 10s | + | if [ -e $qemu_pidfile ]; then |
| + | if [ -e /proc/$(cat $qemu_pidfile) ]; then | ||
| + | echo "QEMU: Waiting for VM shutdown." | ||
| + | while [ -e /proc/$(cat $qemu_pidfile) ]; do sleep 1s; done | ||
| + | echo "QEMU: VM Process $(cat $qemu_pidfile) finished." | ||
| + | else | ||
| + | echo "QEMU: Error: No VM with PID $(cat $qemu_pidfile) running." | ||
| + | fi | ||
| + | |||
| + | rm -f $qemu_pidfile | ||
| + | else | ||
| + | echo "QEMU: Error: $qemu_pidfile doesn' | ||
| + | fi | ||
| } | } | ||
| </ | </ | ||
| - | FIXME This script currently uses a fixed time of 10 seconds to wait for the VM shutdown. Provide a better way to wait for the qemu process to finish | ||
| Test the the script by running ''/ | Test the the script by running ''/ | ||
| If the script works as desired, enable it for every boot: ''/ | If the script works as desired, enable it for every boot: ''/ | ||
| + | |||
| + | FIXME This script needs to be ported to [[: | ||