User Tools

Site Tools


linux:apps: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
linux:apps:kvm [2023/08/06 09:07] oscarlinux:apps:kvm [2025/10/21 20:15] (current) oscar
Line 1: Line 1:
 ====== KVM ====== ====== KVM ======
 +
 +----
  
 ===== Installation ===== ===== Installation =====
Line 51: Line 53:
 irqbypass              16384  1 kvm irqbypass              16384  1 kvm
 </code> </code>
 +===== Configuration ===== 
 +=== User priviliges ===
 In order to manage virtual machines as a regular user, that user needs to be added to the libvirt group: In order to manage virtual machines as a regular user, that user needs to be added to the libvirt group:
  
-  # adduser <youruser> libvirt+  # adduser <username> libvirt 
 +       or  (not sure if kvm group is needed) 
 +  # usermod -aG libvirt,kvm <username> 
 + 
 +=== User-specific and system-wide VMs === 
 +By default, if virsh is run as a normal user it will connect to libvirt using: **qemu:%%///%%session** URI string. This URI allows virsh to manage only the set of VMs belonging to this particular user.  
 + 
 +To manage the system set of VMs (i.e., VMs belonging to root) virsh should be run as root or with: **qemu:%%///%%system** URI: 
 + 
 +  $ virsh --connect qemu:///system list --all 
 + 
 +To avoid having to use the --connect flag on every command, the URI string can be set in the LIBVIRT_DEFAULT_URI environment variable: 
 + 
 +  $ export LIBVIRT_DEFAULT_URI='qemu:///system' 
 + 
 +===== Libvirt default network ===== 
 +If you use libvirt to manage your VMs, libvirt provides a NATed bridged network named "default/virbr0" that allows the host to communicate with the guests.  
 +<code> 
 +3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 
 +    link/ether 52:54:00:f5:c4:41 brd ff:ff:ff:ff:ff:ff 
 +    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 
 +       valid_lft forever preferred_lft forever 
 +</code> 
 +This network is available only for the system domains (that is VMs created by root or using the "qemu:system" connection URI). VMs using this network end up in 192.168.122.1/24 and DHCP is provided to them via dnsmasq. This network is not automatically started. To start it use: 
 +  virsh --connect=qemu:///system net-start default 
 + 
 +To make the default network start automatically use: 
 + 
 +  virsh --connect=qemu:///system net-autostart default 
 + 
 +===== Change XML configuration ===== 
 +Extract the xml from an existing qcow2 image by: 
 +  virsh dumpxml Windows-11 > Windows-11.xml 
 +Edit and modify the tags in the XML file. 
 +Undefine the XML config from the old vm to prevent an error because of a duplicate UUID: 
 +  virsh undefine --nvram Windows-11 
 +  or 
 +  virsh undefine Windows-11 
 +Import / define the new XML file: 
 +  virsh define Windows-11.xml 
 +Of course you will have to stop and start the vm for the changes to take effect: 
 +  virsh destroy name_of_vm 
 +  virsh start name_of_vm 
 + 
 + 
 +===== Error Access ISO files, etc. ===== 
 +Directory, that libvirt is using for storing/reading ISO and qcow2 files needs to be readable by libvirt-qemu user. In case of an error opening an ISO or image file, the "libvirt-qemu" user does not have access to the file or the directories holding the files. The "libvirt-qemu" user needs **"r+x"** permissions **all the way up the path**. You can check if unix permissions are correct by running a shell under user "libvirt-qemu" and see if you can ls along the path all the way up to the files: 
 + 
 +  $ sudo su -s /bin/bash libvirt-qemu 
 + 
 +If you can't ls with an account that does have access yields a result with a permission line that contains a dot such as drwxrwxr-x. meaning extended ACL permissions. 
 + 
 +=== Fix 1: === 
 +Put the iso or image into one of the pool directories of libvirt manager. E.g. /var/lib/libvirt/images 
 +Or any other paths that have full r others rights. 
 + 
 +===== Convert VirtualBox to KVM ===== 
 +  qemu-img convert -f vdi -O qcow2 virtualbox-name.vdi kvm-name.qcow2 
 +===== Shrink KVM  qcow2 image ===== 
 +  qemu-img convert -O qcow2 source.qcow2 shrunk.qcow2 
 +===== Make SATA disk available in Windows Client =====   
 +To make a SATA disk partition available in a Windows VM add the disk to the domain’s xml config file by hand. First find the id of the partition that you want to add. Rather than using /dev/sda you should use /dev/disk/by-id/ where you get from 
 + 
 +   ls -l /dev/disk/by-id 
 + 
 +Now edit the ///etc/libvirt/qemu/${YOUR_VM}.xml// file and add a <disk> section to the <devices> section: 
 +<code> 
 +# nano /etc/libvirt/qemu/${YOUR_VM}.xml 
 + 
 +or  
 + 
 +virsh edit ${YOUR_VM} 
 +---------------------------------------------- 
 +<disk type='block' device='disk'> 
 +    <driver name='qemu' type='raw'/> 
 +    <source dev='/dev/disk/by-id/{the-partition-id}'/> 
 +    <target dev='vdb' bus='virtio'/> 
 +</disk> 
 +</code> 
 +This will make the host’s partition available in the guest as /dev/vdb (D:). After changing a domain’s config by hand, you have to reload the config by hand. Log in to your host and issue this command: 
 + 
 +  # virsh define /etc/libvirt/qemu/${YOUR_VM}.xml 
 +   
 +  Domain YOUR_VM defined from /etc/libvirt/qemu/${YOUR_VM}.xml 
 +   
 +===== Links ===== 
 +  * [[https://wiki.debian.org/KVM#Installation]]
  
linux/apps/kvm.1691312827.txt.gz · Last modified: by oscar