Search code examples
linuxcluster-computingqemu

How to launch multiple instances of the same image in QEMU?


I am launching a guest with:

qemu-system-x86-64 \
    -nographic \
    -machine virt \
    -smp 2 \
    -m 4G \
    -bios images/fw_jump.elf \
    -kernel images/Image \
    -append "root=/dev/vda2 ro" \
    -device virtio-blk-device,drive=hd0 -drive file=Fedora-38-sda.qcow2,format=qcow2,id=hd0 \
    -netdev user,id=net0,hostfwd=tcp::10000-:22 -device virtio-net-device,netdev=net0 \
    -device virtio-net-device,netdev=e0 -netdev tap,id=e0 \
    -virtfs local,path=mount/development-toolchain-native,mount_tag=host0,security_model=passthrough,id=host0 \
    -virtfs local,path=mount/development-2023-07-13-1400,mount_tag=host1,security_model=passthrough,id=host1

I would like to launch simultaneously more instances like that in order to create a cluster. What should I duplicate in order to have the virtual machines running independently?


Solution

  • You'll need an independent disk for each virtual machine. The easiest solution is to create a new qcow2 image for each vm that uses the original disk as the backing store:

    for node in {1..10}; do
      drive=node${node}.qcow2
      qemu-img create -f qcow2 -F qcow2 -b Fedora-38-sda.qcow2 $drive 10G
      qemu-system-x86-64 \
          -nographic \
          -machine virt \
          -smp 2 \
          -m 4G \
          -bios images/fw_jump.elf \
          -kernel images/Image \
          -append "root=/dev/vda2 ro" \
          -device virtio-blk-device,drive=hd0 -drive file=$drive,format=qcow2,id=hd0 \
          -netdev user,id=net0,hostfwd=tcp::10000-:22 -device virtio-net-device,netdev=net0 \
          -device virtio-net-device,netdev=e0 -netdev tap,id=e0 \
          -virtfs local,path=mount/development-toolchain-native,mount_tag=host0,security_model=passthrough,id=host0 \
          -virtfs local,path=mount/development-2023-07-13-1400,mount_tag=host1,security_model=passthrough,id=host1
    done
    

    I can't exactly reproduce the command you're running (you haven't provided all the necessary files, and my qemu doesn't have virt machine type), but I'm using the following script successfully:

    #!/bin/bash
    
    BASEIMAGE=Fedora-Cloud-Base-39-1.5.x86_64.qcow2
    
    if ! tmux has-session -t machines; then
      tmux new-session -d -s machines
    fi
    
    for node in 1 2 3 4; do
      name=node${node}
      drive="${name}.qcow2}"
      qemu-img create -f qcow2 -F qcow2 -b "$BASEIMAGE" "$drive" 10G
      tmux new-window -n "$name" -t machines \
      qemu-system-x86_64 \
        -machine pc \
        -cpu 'Broadwell-v4' \
        -nographic \
        -smp 2 \
        -m 2G \
        -drive file=$drive,format=qcow2,id=virtio-disk0,if=virtio
    done
    

    This results in four running Fedora 38 virtual machines.

    Note that I'm using tmux to wrap the qemu execution, since otherwise you would get garbage as multiple nodes output to the same terminal at once.