No Auto Resize with SPICE and virt-manager

17,536

Solution 1

The gnome desktop, mutter, has some hotplug code that deals with the resize.

If you have any other desktop like xfce4 or KDE or a bare .xinitrc file running compiz like I do, you are pretty stuck, but I think I finally found an elegant and easy solution.

xrandr --output Virtual-0 --auto will pick up the size and apply it from vdagent. All that's need is a trigger.

Here is the trigger and how to handle it.

udev exposes the resize event as a drm device change that looks like this:
UDEV [10758.537471] change /devices/pci0000:00/0000:00:02.0 `drm/card0 (drm)

To have your desktop react to it create a udev rule and refer to a script to resize:
Rule in /etc/udev/rules.d/50-x-resize.rules:

ACTION=="change",KERNEL=="card0", SUBSYSTEM=="drm", RUN+="/usr/local/bin/x-resize" 

Script in /usr/local/bin/x-resize:

#! /bin/sh
PATH=/usr/bin
export DISPLAY=:0.0
xrandr --output "$(xrandr | awk '/ connected/{print $1; exit; }')" --auto

You may need to customize that shell script for your situation.

I found that on debian 10 and Ubuntu 20.04 I didn't even have to restart anything; it picked it up immediately.

Update: 2021-03-17

I recently found that I had to get permissions to allow that script to work. I changed it to this:

#! /bin/sh 
PATH=/usr/bin
desktopuser=$(/bin/ps -ef  | /bin/grep -oP '^\w+ (?=.*vdagent( |$))') || exit 0
export DISPLAY=:0
export XAUTHORITY=$(eval echo "~$desktopuser")/.Xauthority
xrandr --output $(xrandr | awk '/ connected/{print $1; exit; }') --auto

Solution 2

I'm using Debian 10 and icewm as guest VM. When spice-vdagent is running I can manually adjust the screen size with xrandr --output Virtual-1 --auto

A change in the display size can also be observed with xev:

joe@l1:~$ xev -root -event randr 

RRScreenChangeNotify event, serial 18, synthetic NO, window 0x3af,
    root 0x3af, timestamp 39153, config_timestamp 82137
    size_index 65535, subpixel_order SubPixelUnknown
    rotation RR_Rotate_0
    width 1320, height 949, mwidth 348, mheight 250

RRNotify event, serial 18, synthetic NO, window 0x3af,
    subtype XRROutputChangeNotifyEvent
    output Virtual-1, crtc 63, mode 1320x949 (1320x949)
    rotation RR_Rotate_0
    connection RR_Connected, subpixel_order SubPixelUnknown

Using that as a trigger seems less intrusive than working with udev, in particular when using different desktop environments or window managers in the same VM.

joe@l1:~$ cat /home/joe/.xsession
#!/bin/sh

if [ -x /usr/bin/spice-vdagent ] ; then
    /usr/bin/spice-vdagent
    /home/joe/.icewm/xrandr-loop &
fi
exec /usr/bin/icewm-session

joe@l1:~$ cat /home/joe/.icewm/xrandr-loop
#!/bin/sh

sleep 2

xrandr --output "$(xrandr | awk '/ connected/{print $1; exit; }')" --auto

xev -root -event randr | \
grep --line-buffered 'subtype XRROutputChangeNotifyEvent' | \
while read foo ; do \
    xrandr --output "$(xrandr | awk '/ connected/{print $1; exit; }')" --auto
done

joe@l1:~$ 

Solution 3

go to the hardware section of your VM

Remove existing channel device if there is one and add the com.redhat.spice.0 spicevmc device

Share:
17,536

Related videos on Youtube

howdoieven
Author by

howdoieven

Updated on September 18, 2022

Comments

  • howdoieven
    howdoieven almost 2 years

    I've setup a VM that works great except for the auto resize functionality which seems to be inconsistent. I've got it randomly working a few times but it also randomly stops working. It's currently not working and I'm not sure what I need to do to get it to work reliably (like VirtualBox autoresizing). Please see below for my configuration.

    setup

    Host: Ubuntu 16.10
    Guest: Ubuntu 16.04.2, et. al (I've tried with other Linux distros with similar results)
    Guest: created using virt-manager
    Guest: installed spice-vdagent
    Guest: virt-manager > View > Scale Display > Auto resize VM with window (checked)
    

    spice

    user@ubuntu:~$ ps aux | grep spice
    root       805  0.0  0.0  30568   260 ?        Ss   08:59   0:00 /usr/sbin/spice-vdagentd
    user      1365  0.0  0.0  35124  2360 ?        Ss   08:59   0:00 /usr/bin/spice-vdagent
    user      1933  0.0  0.0  21292  1020 pts/17   S+   09:00   0:00 grep --color=auto spice
    

    qxl

    user@ubuntu:~$ sudo lshw -c Video
      *-display               
           description: VGA compatible controller
           product: QXL paravirtual graphic card
           vendor: Red Hat, Inc.
           physical id: 2
           bus info: pci@0000:00:02.0
           version: 04
           width: 32 bits
           clock: 33MHz
           capabilities: vga_controller rom
           configuration: driver=qxl latency=0
           resources: irq:10 memory:f4000000-f7ffffff memory:f8000000-fbffffff memory:fc058000-fc059fff ioport:c040(size=32) memory:c0000-dffff
    

    What am I missing to correctly enable auto resize with qemu/kvm?

  • Jon Bentley
    Jon Bentley about 4 years
    It's a known bug for some DEs.
  • keen
    keen over 3 years
    This is great, but be carfeul, it made my XFCE VM fail to boot with a blinking cursor. The answer by Joe H. below works without such problems
  • keen
    keen over 3 years
    Perfect, thanks