You are not logged in.

#1 2021-08-25 10:21:14

damo
....moderator....
Registered: 2015-08-20
Posts: 6,701

Using virt-manager to run Virtual Machines

It is possible to run virtual machines without installing heavyweight applications, by using virt-manager

These are the notes and script I made for myself a while ago, to set it up and be able to run the virtual machines from the menu...
AHtjB3r.jpg

The main commands to use are

virsh list --all
virsh start <domain name>
virt-viewer --connect qemu:///session <domain name>
virt-viewer --attach <domain name>

The menu script

#!/bin/bash
##
## vm-pipemenu
##
## Bunsenlabs pipemenu script to manage virtual machines from main menu
## by @damo 2020
##
## To include in jgmenu, add 'Virtual Machines,^pipe(bl-vm-pipemenu)'
##
## To run as standalone menu: 'bl-vm-pipemenu | jgmenu --simple'
##
########################################################################
#   1) In '/etc/libvirt/libvirt.conf', set 'uri_default = "qemu:///system"'
#      Then copy 'libvirt.conf' to '~/.config/libvirt/'
#   2) In GUEST, to allow remote shutdown (should be able to do this with systemd?)
#           install 'acpi-support-base';
#           sudo systemctl enable acpid.service
#
#           sudo nano /etc/acpi/events/powerbtn
#           add:    event=button/power
#                   action=/sbin/poweroff
#
#           sudo systemctl restart acpid.service
#
#   3)  In host, to allow virsh without sudo:
#
#           Create 'libvirt' group, and add $USER to it;
#           Edit '/etc/libvirt/libvirtd.conf' and set:
#
#           unix_sock_group = "libvirt"
#           unix_sock_rw_perms = "0770"
#           auth_unix_rw = "polkit"     (necessary ?)

#           sudo systemctl restart libvirtd.service
#
#   4)  Shared Clipboard:
#           In HOST:    install 'spice-client-gtk'
#           In GUEST:   install 'spice-vdagent', then log out/in
#
########################################################################

BL_COMMON_LIBDIR="/usr/lib/bunsen/common"   # source jgmenu helper functions
declare -a DOMAINS
declare -a RUN

if ! . "$BL_COMMON_LIBDIR/bl-include.cfg" 2> /dev/null; then
    echo $"Error: Failed to locate bl-include.cfg in $BL_COMMON_LIBDIR" >&2
    exit 1
fi

list=$(virsh list --all)                # get all VM's

function write-item(){                  # populate submenu with VM's
    item=$(printf "%-5s %s" "$2" "$1")
    jgmenuSubmenu 'root' "$1" "${item}"
    jgmenuSubmenu "$1" "${item}"
    
    if [[ $2 = "ON" ]];then
        jgmenuItem "$1" "shutdown" "virsh shutdown $1 --mode acpi"
        jgmenuItem "$1" "reboot" "virsh shutdown $1 --mode acpi;virsh start $1;virt-viewer -w $1"
        jgmenuItem "$1" "save state and shutdown" "virsh save $1 $1_config.xml --running"
    else
        jgmenuItem "$1" "start" "virsh start $1;virt-viewer -w $1"
        if [[ -f "$1_config.xml" ]];then
            jgmenuItem "$1" "restore saved state" "virsh restore $1_config.xml --running;virt-viewer -w $1"
        fi
    fi
}

count=1
while read -r line;do       # get state of VM's from list, add to arrays
    if (( count > 2 ));then
        domain="$(awk '{print $2}' <<< "${line}")"
        run="$(awk '{print $3}' <<< "${line}")"
        [[ ${run} = "running" ]] && state="ON" || state="OFF"
        
        DOMAINS+=("${domain}")
        RUN+=("${state}")
    fi
    count=$((count+1))
done <<< "${list}"

### Write out text for jgmenu
echo "VM Manager GUI,virt-manager"
echo "^sep(Machines)"

i=0
for d in "${DOMAINS[@]}";do                 # submenu for each VM (domain)
    write-item "${DOMAINS[i]}" "${RUN[i]}"
    i=$((i+1))
done

jgmenuEnd
exit

It is possible to use virt-manager instead of virt-viewer, but I haven't investigated that:

virt-manager --show-domain-console <VM name>

Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#2 2021-08-29 06:37:10

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,281
Website

Re: Using virt-manager to run Virtual Machines

damo wrote:

It is possible to use virt-manager instead of virt-viewer, but I haven't investigated that:

virt-manager --show-domain-console <VM name>

My personal pipemenu is very similar to yours - get the list from virsh, etc, but uses virt-manager.
(Oh-oh but I use 'qemu:///system' instead of 'qemu:///session' I forget why that was, but there seem to be pros and cons both ways.)

connection='qemu:///system'

### much snipping

    virsh --connect "$connection" start "$domain"
    virt-manager --connect="$connection" --show-domain-console "$domain"
    exit

### etc

I thought I had seen a Debian wiki on virt-manager specifically, but there is this on KVM: https://wiki.debian.org/KVM
There also seems to be quite a lot out there by RedHat and others on virt-manager.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

Board footer

Powered by FluxBB