Skip to content

Instantly share code, notes, and snippets.

@diffficult
Last active December 3, 2024 08:02
Show Gist options
  • Save diffficult/cb8c385e646466b2a3ff129ddb886185 to your computer and use it in GitHub Desktop.
Save diffficult/cb8c385e646466b2a3ff129ddb886185 to your computer and use it in GitHub Desktop.
Easy instructions to get virt-manager qemuv/kvm running on Arch

Easy instructions to get QEMU/KVM and virt-manager up and running on Arch

  1. Make sure your cpu support kvm with below command:

     grep -E "(vmx|svm)" --color=always /proc/cpuinfo
    
  2. Make sure BIOS have enable “Virtualization Technology”.

  3. User access to /dev/kvm so add your account into kvm(78) group:

     sudo gpasswd -a $(whoami) kvm
    
  4. Loading kernel modules kvm_intel or kvm_amd depend on your CPU, Add module name in /etc/modules-load.d/kvm.conf:

     kvm_intel
    
  • Load module:

    modprobe kvm_amd
    
  1. Install qemu, virt-manager, dnsmasq and iptables:

     sudo pacman -S --needed qemu virt-manager dnsmasq iptables-nft
    
  2. Run and enable boot up start libvirtd daemon:

     systemctl enable --now libvirtd
    
  3. Use PolicyKit authorization create /etc/polkit-1/rules.d/50-libvirt.rules (before /etc/polkit-1/rules.d/50-org.libvirt.unix.manage.rules) as below context:

/* Allow users in kvm group to manage the libvirt
daemon without authentication */
polkit.addRule(function(action, subject) {
    if (action.id == "org.libvirt.unix.manage" &&
        subject.isInGroup("kvm")) {
            return polkit.Result.YES;
    }
});
  1. You will need to create the libvirt group and add any users you want to have access to libvirt to that group:

     groupadd libvirt
    
     sudo gpasswd -a $(whoami) libvirt
    
  2. Check network interface status:

     sudo virsh net-list --all
    
  • If it is inactive start it using:

     sudo virsh net-start default
    
  1. Now you can use virt-manager manager your virtual machine.

  2. Things to do after installing a Windows VM.

  • Check and install drivers on your guest Windows VM, probably virtio-win is a nice place to start.

What to do if default network interface is not listed

  • If virsh net-list is not listing any network interface just reinitialize it with,

    sudo virsh net-define /usr/share/libvirt/networks/default.xml
    
  • Then just autostart it like so,

    sudo virsh net-autostart default 
    

What to do if you cannot access storage file, and get "Permission denied Error in KVM Libvirt"

  • Step 1: Edit /etc/libvirt/qemu.conf file:

    sudo nano /etc/libvirt/qemu.conf
    
  • Step 2: Find the user and group directives. By default, both are set to "root",

     [...] 
     Some examples of valid values are:
     #
     user = "qemu"   # A user named "qemu"
     user = "+0"     # Super user (uid=0)
     user = "100"    # A user named "100" or a user with uid=100
     #
     #user = "root"
     The group for QEMU processes run by the system instance. It can be
     specified in a similar way to user.
     #group = "root"
     [...]
    

    Uncomment both lines and replace root with your username and group with libvirt as shown below:

     [...] 
     Some examples of valid values are:
     #
     user = "qemu"   # A user named "qemu"
     user = "+0"     # Super user (uid=0)
     user = "100"    # A user named "100" or a user with uid=100
     #
     user = "sk"
     The group for QEMU processes run by the system instance. It can be
     specified in a similar way to user.
     group = "libvirt"
     [...]
    
  • Step 3: Restart libvirtd service:

     sudo systemctl restart libvirtd
    

How to extend / increase a Windows Partition on KVM QEMU VM

We have a Windows 7 VM running on Ubuntu KVM. I needed to give the Windows 7 machine more disk space. This turns out to be really easy (when you know how).

  1. Shutdown the VM

     virsh shutdown hostname
    
  2. Increase the qcow2 image

Find the qcow2 file of the VM and take a backup (just in case).

cp hostname.qcow2 hostname.qcow2.backup
qemu-img resize hostname.qcow2 +100GB
  1. Start the VM

     virsh start hostname
    
  2. Extend the partition in Window

Windows has a really good partition management utility built into it. Search for disk management

Reference:

Original guide - http://wood1978.dyndns.org/~wood/wordpress/2013/07/22/arch-linux-setup-kvm-with-virt-manager-gui/comment-page-1/

KVM @ Arch Wiki - https://wiki.archlinux.org/index.php/KVM

libvirt @ Arch Wiki - https://wiki.archlinux.org/index.php/Libvirt

QEMU @ Arch Wiki - https://wiki.archlinux.org/index.php/QEMU

Network Interface Status - http://ask.xmodulo.com/network-default-is-not-active.html

Network Interface Troubleshooting - https://blog.programster.org/kvm-missing-default-network

Networking libvirt wiki - https://wiki.libvirt.org/page/Networking#NAT_forwarding_.28aka_.22virtual_networks.22.29

Fedora Wiki - Windows Virtio Drivers - https://stg.fedoraproject.org/wiki/Windows_Virtio_Drivers

Extend disk size Windows partition KVM QEMU VM - https://www.randomhacks.co.uk/how-to-extend-increase-a-windows-partition-on-kvm-qemu-vm/

Increasing a KVM machine disk space - https://serverfault.com/questions/324281/how-do-you-increase-a-kvm-guests-disk-space

Fixing Cannot access storage file, Permission denied Error in KVM Libvirt - https://ostechnix.com/solved-cannot-access-storage-file-permission-denied-error-in-kvm-libvirt/

@DraconicVision
Copy link

Would you mind adding more verbosity to Step 7? I'm not at all familiar with PolicyKit or how to add new rules.

@diffficult
Copy link
Author

@DraconicVision as the step says you just need to create a new file in that location stating a rule for libvirt to let it have full management access (RW daemon socket) without further user intervention (having to input your user password each time it needs to do anything).

You can find the additional info on the source links at the end of the gist, on https://wiki.archlinux.org/title/Libvirt#Using_polkit

@DraconicVision
Copy link

Ah, I see. Admittedly I'm pretty new so I didn't really understand. My suggestion is to change the wording to the below.

Create this file: /etc/polkit-1/rules.d/50-libvirt.rules (before /etc/polkit-1/rules.d/50-org.libvirt.unix.manage.rules) and paste the below into it:

/* Allow users in kvm group to manage the libvirt
daemon without authentication */
polkit.addRule(function(action, subject) {
    if (action.id == "org.libvirt.unix.manage" &&
        subject.isInGroup("kvm")) {
            return polkit.Result.YES;
    }
});

This makes use of PolKit (formerly known as PolicyKit) to grant authorization.

If I've misunderstood please let me know, I just think some verbosity would be useful for those who are newbies! Thanks for making this guide by the way, I found it really useful. :)

@guigirl42
Copy link

guigirl42 commented Jan 8, 2022

Thanks very much for this nice guide, which has been very helpful to me. Using it i am able to run Virt-Manager, & thus run my VMs converted from VirtualBox, & also create new VMs. However, i have two big problems [in converted & new VMs], that days of research still have not resolved:

  1. No Guest has internet access at all.
  2. No Guest has a Shared Clipboard with the Host.

Is your procedure intended to also provide those functionalities, or are there additional procedures i was supposed to have performed? I'd appreciate any advice you could offer please, because unless i can solve both problems i shall need to abandon kvm/libvirt/qemu & just stay with VirtualBox.



UPDATE 13/1/22:
I seem to have stumbled my way to a solution. After installing extra packages bridge-utils and openbsd-netcat in Host, then rebooting Host, i found that Guest internet access began working fine.

UPDATE 24/1/22:

  1. After installing package spice-vdagent into each Guest [& rebooting them for good measure, though idk maybe that's redundant?], Shared Clipboard now works correctly.
  2. By following this procedure, Shared Folders now work well.

@jarrard
Copy link

jarrard commented Aug 3, 2022

EDIT: Seems I was missing some modules after all. nvm.

@diffficult
Copy link
Author

@DraconicVision sorry for the late response, I will add your edit, thanks for the suggestion.

@guigirl42 sorry for the late response, I see the issue I will add your update to the guide.

@jarrard glad to see you found the solution.

@guigirl42
Copy link

Thanks for these edits, @diffficult, now your excellent guide is even more comprehensive.

FYI: Unfortunately, for a few months now, Shared Clipboard [SC] has been broken in ALL my Arch/Arch-based VMs, whereas it continues to work just fine in all my non-Arch VMs [eg, my Fedoras, Mint, PopOS, ZorinLinux VMs all continue happily with good SC]. None of my several attempts to troubleshoot & solve this has been successful. For instance:

In https://wiki.archlinux.org/title/QEMU#Enabling_SPICE_support_on_the_guest [edited on 25 June 2022, at 17:58], it says:

"For Arch Linux guests, for improved support for multiple monitors or _**clipboard sharing**_, the following packages should be installed:

spice-vdagent: Spice agent xorg client that enables copy and paste between client and X-session and more.
xf86-video-qxl: Xorg X11 qxl video driver"

Obviously i've had spice-vdagent installed since 24/1/22, but the other one is new info for me, & was not in either my Arch or Arch-based VMs. I therefore installed it in each relevant Guest, rebooted each Guest, &... it made NO difference, so i uninstalled it again, & have given up.

I've posted this info here for the benefit of other readers -- the original method in this guide [including my 24/1/22 post's update, above] used to work great for ALL my Guests, irrespective of specific distro. A few months ago, implicitly, some Arch package update seems to have broken SC for Arch Guests, but i do not yet know which is the "bad" package.

@skeezmoe
Copy link

skeezmoe commented Nov 4, 2022

I just found this and it is great to confirm that Copy / Paste is definitely broken in Virt-Manager. I have been trying to fix this and tried all the solutions for Ubuntu and Arch and none work.

@slash071
Copy link

Update required. There is no qemu package anymore, it's divided into three packages: qemu-base, qemu-desktop and qemu-full

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment