(reference: https://wiki.archlinux.org/index.php/Libvirt)
yay -Syy --noconfirm qemu qemu-arch-extra qemu-block-gluster qemu-block-iscsi qemu-block-rbd libvirt gnu-netcat dnsmasq ebtables bridge-utils virt-manager virt-viewer gnome-boxes ovmf create_ap haveged
libvirt provides a CLI client called virsh
, which is useful to create and configure new VMs.sudo virsh
) they are at
/var/lib/libvirt/images
. On a user session (virsh
) they are kept at
$HOME/VirtualMachines
.Add your user to the libvirt
and kvm
groups at /etc/group
and reboot the machine.
$ sudo rmmod kvm kvm_intel virtio
$ sudo modprobe kvm kvm_intel virtio
$ sudo vim /etc/modules-load.d/custom.conf
kvm
kvm_intel
virtio
Then, reboot the machine, to make sure it worked: $ sudo lsmod | sort
$ sudo systemctl enable libvirtd virtlogd virtlockd
The command above will enable all related libvirt
services at boot. If you don’t want to restart the machine yet, you must start them separatedly:
$ sudo systemctl start libvirtd virtlogd virtlockd
$ sudo vim /etc/libvirt/qemu.conf
There, change group = "78"
to group = "kvm"
.
To test if libvirt is working properly on a system level:
$ virsh -c qemu:///system
This mode is the priviledged mode. It allows you to create bridges, e.g., which normal mode does not. To create vms or manage networks on this mode, use sudo virsh
. This document will from now on assume this mode.
To test if libvirt is working properly for a user-session:
$ virsh -c qemu:///session
This mode is the normal mode. It does NOT allow you to create bridges, e.g. To create vms or manage networks on this mode, use virsh
only.
For networking, create a bridge network with NAT, to allow a different range of IPs from the host network for the VMs: (reference: https://jmutai.com/2017/02/14/managing-kvm-network-interfaces-with-virsh-nmcli-and-brctl-in-linux/)
$ vim kvm-nat-network.xml
<network>
<name>br1</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='br1' stp='on' delay='0'/>
<ip address='10.0.5.1' netmask='255.255.255.0'>
<dhcp>
<range start='10.0.5.10' end='10.0.5.100'/>
</dhcp>
</ip>
</network>
ping 10.0.5.1
.$ sudo virsh domiflist pxe
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 bridge virbr0 virtio 52:54:00:e9:ad:17
vnet1 bridge br1 virtio 52:54:00:47:2f:eb
sudo virsh net-list --all
. You can also use sudo brctl show br1
to confirm it does not exist anymore. :)$ sudo virsh list –all
$ sudo virt-install \
--name arch-linux_testing \
--memory 1024 \
--vcpus=2,maxvcpus=4 \
--cpu host \
--cdrom $HOME/Downloads/arch-linux_install.iso \
--disk size=2,format=qcow2 \
--network user \
--virt-type kvm
##### From a previous created volume on a pool
$ sudo virt-install \
--name fedora-testing \
--memory 2048 \
--vcpus=2 \
--cpu=host \
--cdrom /tmp/fedora20_x84-64.iso \
--os-type=linux --os-variant=fedora20 \
--vol poolname/volname \
--network bridge=br0 \
--graphics=vnc \
--noautoconsole
--virt-type kvm
$ sudo virt-install
–name demo
–memory 512
–disk /home/user/VMs/mydisk.img
–import
$ sudo virsh start domain
$ sudo virsh shutdown domain $ sudo virsh destroy domain
$ sudo virsh suspend domain $ sudo virsh resume domain
$ sudo virsh autostart domain $ sudo virsh autostart domain –disable
Running domains can be automatically suspended/shutdown at host shutdown using the libvirt-guests.service
systemd service. This same service will resume/startup the suspended/shutdown domain automatically at host startup. Read /etc/conf.d/libvirt-guests for service options.
$ sudo virsh edit domain
$ sudo virsh –connect qemu+ssh://username@host/system $ sudo LIBVIRT_DEBUG=1 virsh –connect qemu+ssh://username@host/system
$ sudo virt-viewer –connect qemu+ssh://username@host/system domain $ sudo virt-manager –connect qemu+ssh://username@host/system domain
NOTES:
virt-clone
does not change anything inside the guest OS, it only duplicates disks and does host side changes.
So things like changing passwords, changing static IP address, ssh-keys, hostnames etc are outside the scope of this tool.
If you need those changes, use virt-sysprep
instead.
$ sudo virt-clone –original [Domain-Vm-Name-Here] –auto-clone
OR
$ sudo virt-clone –original [Domain-Vm-Name-Here] –name [New-Domain-Vm-Name-Here]
After that, you can resume or start the original domain (the one used to create
the clone).
We use virt-sysprep
instead of virt-clone
if we want to clone the VM and make/reset anything inside the guest OS.
$ sudo virsh domiflist centos07-test
$ sudo virsh net-dhcp-leases br1 # - you must find the record containg the MAC adress of the domain you want to chech the IP from
$ sudo virsh undefine centos07-test
Makefile
to ease the daily tasks on KVM. Here
is a quick link to it: https://github.com/tiagoprn/devops/tree/master/shellscripts/kvm$ sudo virsh pool-list –all
NOTE: a pool location can be a directory, a network filesystem, or a partition (LVM included).
$ sudo virsh pool-define-as name type [source-host] [source-path] [source-dev] [source-name] [] [–source-format format] e.g. (directory) $ sudo virsh pool-define-as my_first_storage_pool dir - - - - /storage/kvm/storage_pools/001
$ sudo virsh pool-list --all
Nome Estado Auto-iniciar
-------------------------------------------
my_first_storage_pool inativo não
$ sudo virsh pool-build my_first_storage_pool
$ sudo virsh pool-start my_first_storage_pool
$ sudo virsh pool-autostart my_first_storage_pool
$ sudo virsh pool-list --all
Nome Estado Auto-iniciar
-------------------------------------------
my_first_storage_pool ativo sim
$ sudo virsh pool-undefine my_first_storage_pool
After creating a pool, you can create volumes inside of it.
$ sudo virsh vol-create-as poolname volumename 10GiB --format aw|bochs|raw|qcow|qcow2|vmdk
$ sudo virsh vol-upload --pool poolname volumename volumepath
$ sudo virsh vol-list poolname
$ sudo virsh vol-resize --pool poolname volumename 12GiB
$ sudo virsh vol-delete --pool poolname volumename
$ sudo virsh vol-dumpxml --pool poolname volumename # for details.
$ stat -Lc%s ~/distros/images/CentOS-7-x86_64-GenericCloud.qcow2
854851584
$ sudo virsh vol-create-as my_first_storage_pool centos7 854851584 --format qcow2
O volume centos7 foi criado
$ sudo virsh vol-upload --pool my_first_storage_pool --vol centos7 ~/distros/images/CentOS-7-x86_64-GenericCloud.qcow2
$ sudo virsh vol-list my_first_storage_pool
Nome Caminho
------------------------------------------------------------------------------
centos7 /storage/kvm/storage_pools/001/centos7