Difference between revisions of "KVM"

From Dogtag
Jump to: navigation, search
m (Managing Networks)
m (References)
Line 163: Line 163:
* [https://wiki.debian.org/KVM Debian KVM]
* [https://wiki.debian.org/KVM Debian KVM]
* [[LVM]]
* [[LVM]]
* [https://aboullaite.me/kvm-qemo-forward-ports-with-iptables/ KVM/libvirt: Forward Ports to guests with Iptables]
* [https://wiki.libvirt.org/page/Networking Networking]

Revision as of 05:07, 14 January 2020


$ dnf install @virtualization

Starting the Service

$ systemctl start libvirtd

Creating a Storage

See Storage Management.

Sparse Raw File

$ rm -f /var/lib/libvirt/images/guest.img
$ truncate --size=10240M /var/lib/libvirt/images/guest.img


$ qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 10240

Installing a Guest

Installing QCOW2 Image

To create a VM from a QCOW2 image:

$ virt-install --name <name> --memory 1024 --import --disk path=<image> --noautoconsole

Installing Fedora ISO

To create a Fedora 28 VM:

$ virt-install \
    --name fedora \
    --memory 1024 \
    --disk path=/var/lib/libvirt/images/guest.qcow2,size=10 \
    --network bridge=virbr0 \
    --graphics vnc,listen=,port=5901 \
    --cdrom /var/lib/libvirt/images/Fedora-Workstation-Live-x86_64-28-1.1.iso \

Installing Debian ISO

$ virt-install \
    --name debian \
    --memory 1024 \
    --disk size=10 \
    --cdrom /var/lib/libvirt/images/debian-9.5.0-amd64-netinst.iso

Connecting to VM

Connect with VNC to

$ dnf install spice-vdagent
$ systemctl start spice-vdagentd

Running KVM

Start the VM with the following command:

$ virt-manager

Managing Virtual Machines

To list running virtual machines:

$ virsh list

To show virtual machine configuration:

$ virsh dumpxml <machine>

To find the mac address:

$ virsh dumpxml <machine> | grep -i '<mac'

Managing Networks

To display available networks:

$ virsh net-list

To show network configuration:

$ virsh net-dumpxml <network>

To configure port forwarding:

$ virsh net-edit <network>

Add a <host> element as follows:

      <range start='192.168.124.<start>' end='192.168.124.<end>'/>
      <host mac='<mac address>' name='<machine>' ip='192.168.124.<n>'/>

Then restart the network:

$ virsh net-destroy default
$ virsh net-start default

If that doesn't work, restart

$ virsh shutdown <machine>
$ systemctl restart libvirtd
$ virsh start <machine>
$ ping 192.168.124.<n>

Configuring Port Forwarding

$ iptables -I FORWARD -o virbr0 -d 192.168.124.<n> -j ACCEPT
$ iptables -t nat -I PREROUTING -p tcp --dport 8080 -j DNAT --to 192.168.124.<n>:8080
$ iptables -I FORWARD -o virbr0 -d 192.168.124.<n> -j ACCEPT
$ iptables -t nat -A POSTROUTING -s -j MASQUERADE
$ iptables -A FORWARD -o virbr0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$ iptables -A FORWARD -i virbr0 -o eth0 -j ACCEPT
$ iptables -A FORWARD -i virbr0 -o lo -j ACCEPT