User Tools

Site Tools


kvm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
kvm [2023/05/30 11:15] vissiekvm [2023/06/01 00:52] (current) – [Debian] vissie
Line 1: Line 1:
-====== Allow ping in host ======+====== Install ====== 
 +===== Debian =====
  
 +<color red>From: </color> [[https://linuxhint.com/install_kvm_debian_10/|https://linuxhint.com/install_kvm_debian_10/]]
 +
 +Some network advanced stuff: [[https://www.linuxtechi.com/install-configure-kvm-debian-10-buster/|https://www.linuxtechi.com/install-configure-kvm-debian-10-buster/]]
 <code> <code>
-sudo groupadd unpriv_ping + 
-sudo usermod --append --groups unpriv_ping vissie +sudo apt install --no-install-recommends qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst dnsmasq qemu-utils ovmf 
-getent group unpriv_ping | cut -f 3 -:+#  Some apps does not get installed with --no-install-recommends and seemds to be addedqemu-utils dnsmasq 
 +# ovmf is for uf 
 +sudo systemctl status libvirtd.service 
 +sudo virsh net-list --all 
 +sudo modprobe vhost_net
  
 </code> </code>
 +
 +As we can see in above output, default network is inactive so to make it active and auto-restart across the reboot by running the following commands,
 +
 <code> <code>
- +sudo virsh net-start default 
-sudo vim /etc/sysctl.conf+sudo virsh net-autostart default
  
 </code> </code>
  
-<sxh bash; gutter: false+=====Stop all iptables rules===== 
 +I had issues where these rules were interfering with my iptables manual rules. This worked 
 +  So edit your network (virsh net-edit) to <forward mode='open'/>.
  
-net.ipv4.ping_group_range = 1003 1003 
  
-… </sxh>+ 
 +If you want to offload the mechanism of “virtio-net” and want to improve the performance of KVM VMs then add “vhost_net” kernel module on your system using the beneath command,
  
 <code> <code>
-sysctl -p+echo "vhost_net" | sudo  tee -a /etc/modules 
 +lsmod | grep vhost
  
 </code> </code>
 +<code>
  
-<color red>or </color> +host_net               24576  0 
- +vhost                  49152  1 vhost_net 
-<code> +tap                    28672  1 vhost_net 
-sudo sysctl -w net.ipv4.ping_group_range='0 2147483647'+tun                    49152  2 vhost_net
  
 </code> </code>
  
-====== Create HDDs ======+Note: If you want a normal user to use virsh commands then add that user to libvirt and libvirt-qemu group using the following commands
  
-<sxh bash; gutter: false# create a file "disk_image" with format qcow2 and 40GB of max space qemu-img create -f qcow2 disk_image.img 40G+<code> 
 +sudo adduser pkumar libvirt 
 +sudo adduser pkumar libvirt-qemu
  
-</sxh>+</code>
  
-====== Running a VM ======+To refresh or reload group membership run the followings,
  
 <code> <code>
-sudo qemu-system-x86_64 -vga qxl -enable-kvm -m 2048 -smp 2 -cpu host -soundhw es1370 -device virtio-mouse-pci -device virtio-keyboard-pci -serial mon:stdio -boot menu=on -net nic -net user,hostfwd=tcp::5555-:22 -hda ./kvmdeb.img+newgrp libvirt 
 +newgrp libvirt-qemu
  
 </code> </code>
  
-====== Port Forward ======+======Setup Bridges Network====== 
 +  sudo vim /etc/network/interfaces 
 +<sxh bash; gutter: false>   
 +# This file describes the network interfaces available on your system
  
-QEMU can forward ports from the host to the guest to enable e.g. connecting from the host to an SSH server running on the guest. For example, to bind port 10022 on the host with port 22 (SSH) on the guest, start QEMU with a command like:+source /etc/network/interfaces.d/*
  
-<code> +# The loopback network interface 
-qemu-system-x86_64 disk_image -nic user,hostfwd=tcp::10022-:22+auto lo 
 +iface lo inet loopback
  
 +# The primary network interface
 +# allow-hotplug enp0s31f6
 +#iface enp0s31f6 inet dhcp
 +#auto enp0s31f6
 +
 +auto br0
 +iface br0 inet dhcp
 +    bridge_ports enp0s31f6
 +    bridge_stp off
 +    bridge_fd 0
 +    bridge_maxwait 0   
 +</sxh>
 +
 +====== Allow ping in host======
 +<code>
 +sudo groupadd unpriv_ping
 +sudo usermod --append --groups unpriv_ping vissie
 +getent group unpriv_ping | cut -f 3 -d :
 </code> </code>
 +<code>
 +sudo vim /etc/sysctl.conf
 +</code>
 +<sxh bash; gutter: false>
 +...
  
-====== SPICE ======+net.ipv4.ping_group_range 1003 1003
  
-<color red>From: </color>[[https://www.linux-kvm.org/page/SPICE</color|https://www.linux-kvm.org/page/SPICE</color]]>+... 
 +</sxh>
 <code> <code>
 +sysctl -p
 +</code>
  
-sudo apt install spice-client-gtk+<color red>or </color>
  
 +<code>sudo sysctl -w net.ipv4.ping_group_range='0 2147483647'</code>
 +
 +====== Create HDDs ======
 +<sxh bash; gutter: false>
 +# create a file "disk_image" with format qcow2 and 40GB of max space
 +qemu-img create -f qcow2 disk_image.img 40G
 +
 +</sxh>
 +
 +====== Running a VM ======
 +<code>
 +sudo qemu-system-x86_64 -vga qxl -enable-kvm -m 2048 -smp 2 -cpu host -soundhw es1370 -device virtio-mouse-pci -device virtio-keyboard-pci -serial mon:stdio -boot menu=on -net nic -net user,hostfwd=tcp::5555-:22 -hda ./kvmdeb.img
 </code> </code>
  
-You wanted copy and paste between host and guest right? ;) We need to add a virtio-serial device to the guest, and open a port for the spice vdagentWe also need to install the spice vdagent in guest. Be sure the agent is running (and for future, started automatically). First the guest sidesince the guest is running.+====== Port Forward ====== 
 +QEMU can forward ports from the host to the guest to enable e.g. connecting from the host to an SSH server running on the guest. 
 +For example, to bind port 10022 on the host with port 22 (SSHon the guest, start QEMU with a command like: 
 +<code>qemu-system-x86_64 disk_image -nic user,hostfwd=tcp::10022-:22</code>
  
 +====== SPICE ======
 +<color red>From: </color>https://www.linux-kvm.org/page/SPICE</color>
 +<code>sudo apt install spice-client-gtk</code>
 +
 +You wanted copy and paste between host and guest right? ;)
 +We need to add a virtio-serial device to the guest, and open a port for the spice vdagent. We also need to install the spice vdagent in guest. Be sure the agent is running (and for future, started automatically).
 +First the guest side, since the guest is running.
 <code> <code>
 sudo apt install xserver-xorg-video-qxl spice-vdagent qemu-guest-agent sudo apt install xserver-xorg-video-qxl spice-vdagent qemu-guest-agent
 sudo systemctl start spice-vdagent sudo systemctl start spice-vdagent
 sudo systemctl enable spice-vdagent sudo systemctl enable spice-vdagent
- 
 </code> </code>
  
Line 74: Line 143:
  
 ====== VNC ====== ====== VNC ======
- 
 <code> <code>
 sudo apt install tigervnc-viewer sudo apt install tigervnc-viewer
 sudo qemu-system-x86_64 -vga qxl -enable-kvm -m 2048 -smp 2 -cpu host -soundhw es1370 -device virtio-mouse-pci -device virtio-keyboard-pci -serial mon:stdio -boot menu=on -net nic -net user,hostfwd=tcp::5555-:22 -hda ./kvmdeb.img -vnc 127.0.0.1:0 sudo qemu-system-x86_64 -vga qxl -enable-kvm -m 2048 -smp 2 -cpu host -soundhw es1370 -device virtio-mouse-pci -device virtio-keyboard-pci -serial mon:stdio -boot menu=on -net nic -net user,hostfwd=tcp::5555-:22 -hda ./kvmdeb.img -vnc 127.0.0.1:0
 sudo netstat -npl | grep qemu sudo netstat -npl | grep qemu
- 
 </code> </code>
- 
 <cli>vissie@mycomputer$ vncviewer 127.0.0.1:0</cli> <cli>vissie@mycomputer$ vncviewer 127.0.0.1:0</cli>
  
Line 88: Line 154:
 ===== Working with domains ===== ===== Working with domains =====
 <code> <code>
- 
 virsh list --all virsh list --all
 virsh start server01 virsh start server01
Line 96: Line 161:
 sudo virsh edit debiantesting sudo virsh edit debiantesting
 sudo virsh undefine paulawin10 ## Kill or destroy a domain sudo virsh undefine paulawin10 ## Kill or destroy a domain
- 
 </code> </code>
  
 ===== Upgrading ===== ===== Upgrading =====
- 
 To increase the number of CPUs: To increase the number of CPUs:
- +  virsh setvcpus <vm_name> <vcpu_count> --config
-<code> +
-virsh setvcpus <vm_name> <vcpu_count> --config +
- +
-</code> +
 If you get an error that you exceeded the maximum number, first do: If you get an error that you exceeded the maximum number, first do:
- +  virsh setvcpus <vm_name> <vcpu_count> --config --maximum
-<code> +
-virsh setvcpus <vm_name> <vcpu_count> --config --maximum +
- +
-</code> +
 Then repeat the above: Then repeat the above:
- +  virsh setvcpus <vm_name> <vcpu_count> --config
-<code> +
-virsh setvcpus <vm_name> <vcpu_count> --config +
- +
-</code> +
 To increase the memory size: To increase the memory size:
- +  virsh setmaxmem <vm_name> <memsize> --config 
-<code> +  virsh setmem <vm_name> <memsize> --config
-virsh setmaxmem <vm_name> <memsize> --config +
-virsh setmem <vm_name> <memsize> --config +
- +
-</code>+
  
 I'll test this at some point too: I'll test this at some point too:
- +  sudo virsh setvcpus --domain test --maximum 2 --config 
-<code> +  sudo virsh setvcpus --domain test --count 2 --config 
-sudo virsh setvcpus --domain test --maximum 2 --config +  sudo virsh reboot test
-sudo virsh setvcpus --domain test --count 2 --config +
-sudo virsh reboot test +
- +
-</code>+
  
 Please remember to SWITCH OFF the VM, and back on for the changes to take affect. Please remember to SWITCH OFF the VM, and back on for the changes to take affect.
- +====error: Requested operation is not valid: cannot undefine domain with nvram==== 
-==== error: Requested operation is not valid: cannot undefine domain with nvram ==== +<code>sudo virsh undefine --nvram nameofvm</code>
- +
-<code> +
-sudo virsh undefine --nvram nameofvm +
- +
-</code> +
 ===== List all supported os-variants ===== ===== List all supported os-variants =====
- 
 <code> <code>
 apt-get install libosinfo-bin apt-get install libosinfo-bin
 osinfo-query os osinfo-query os
- 
 </code> </code>
  
 ===== Start a install ===== ===== Start a install =====
- 
 <code> <code>
 sudo virt-install --name kvmdeb \ sudo virt-install --name kvmdeb \
Line 169: Line 200:
    --cdrom /kvm/iso/debian-10.5.0-amd64-netinst.iso \    --cdrom /kvm/iso/debian-10.5.0-amd64-netinst.iso \
    --boot cdrom,hd    --boot cdrom,hd
- 
 </code> </code>
  
-===== Stop a running domain ===== 
  
 +<code>
 +qemu-system-x86_64 \
 +  -boot d \
 +  -cdrom "Bliss-v14.10-x86_64-OFFICIAL-opengapps-20221027.iso" \
 +  -bios /usr/share/ovmf/x64/OVMF.fd \
 +  -nodefaults \
 +  -enable-kvm \
 +  -smp 8 \
 +  -device intel-hda \
 +  -device hda-duplex \
 +  -device virtio-vga-gl \
 +  -net nic \
 +  -net user,hostfwd=tcp::5555-:5555 \
 +  -cpu host \
 +  -m 4096 \
 +  -display sdl,gl=on \
 +  -hda Android.img
 +</code>
 +===== Stop a running domain =====
 <code> <code>
 virsh managedsave kvmdeb --verbose virsh managedsave kvmdeb --verbose
- 
 </code> </code>
 +
  
 ===== Change network settings ===== ===== Change network settings =====
Line 235: Line 283:
 to restart the network (best done with no VMs running). to restart the network (best done with no VMs running).
  
-Or..+Or.....
  
-<code> 
-sudo virsh net-update default add-last ip-dhcp-host "<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip='192.168.122.10'/>" --live --config 
  
-</code>+<code>sudo virsh net-update default add-last ip-dhcp-host "<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip='192.168.122.10'/>" --live --config</code>
  
-===== Convert ===== 
  
-<code> +=====Convert===== 
-qemu-img convert -f vdi -O qcow2 ./Lineage.vdi ./Lineage.qcow2+  qemu-img convert -f vdi -O qcow2 ./Lineage.vdi ./Lineage.qcow2
  
-</code> 
  
 ====== GUI ====== ====== GUI ======
  
 ===== virt-manager ===== ===== virt-manager =====
- 
 <code> <code>
 virt-manager virt-manager
- 
 </code> </code>
  
 ===== Spice ===== ===== Spice =====
- 
 <code> <code>
 sudo apt install virt-viewer sudo apt install virt-viewer
- 
 </code> </code>
  
  
kvm.1685470534.txt.gz · Last modified: 2023/05/30 11:15 by vissie