In my previous post, I’ve shown how I’m sandboxing various desktop applications in containers and VMs using Incus. When it comes to desktop VMs, I’ve created freedesktop.org desktop entries on my host machine, so that I can start and connect to individual VMs easily and switch between them quickly.
I have an individual desktop icon for each desktop VM, and I’ve added most of these to the Ubuntu dock of my Gnome desktop on the host. Here’s a my custom desktop entry for one of these VMs, which I keep in /usr/local/share/applications/dev.desktop:
[Desktop Entry]
Version=1.0
Type=Application
Name=Developer Desktop
Categories=Development;IDE;
Keywords=vscode;eclipse;
Terminal=false
Exec=/usr/local/bin/mq-show-incus-desktop-vm vm-dev
Icon=/usr/local/share/icons/dev.png
(Here, mq-show-incus-desktop-vm is a small wrapper script that ultimately runs incus console --type vga vm-dev. Ignore it for now.)
When I click the resulting dock icon, I get a new window that shows the desktop of this particular VM (e.g. vm-dev). So far, so good.
The Problem
However, there is one annoying glitch: a VM’s dock icon does not get a “running indicator”. That is, one of these small dots that indicate that an application is running and how many open windows it has. Instead, a new “mystery icon” appears in my dock and gets the indicator. When connecting another VM, from a different dock icon, that mystery icon gets another running indicator. I can access all VM windows through the mystery icon, but I’d rather use an individual dock icon for each VM:

Further digging has revealed that this mystery icon belongs to the Remote Viewer application, which is part of the Virt Viewer project. This is a SPICE client, and Incus starts it when connecting to a VM’s graphics console. Since it’s always the same Remote Viewer application running on the host, it always gets associated with the same desktop icon: the Remote Viewer icon.
This seems to be a common enough problem, but I couldn’t find much about it on the web. Best thing I could find was this “StartupWMClass problem” post in the Gnome forums. Looks like this describes pretty much the same problem that I’m having. There’s a proposed solution, too, but it looks rather convoluted. And it may only work under certain circumstances.
So I’ve used an LLM to workshop other solutions. In the end, I’ve found a fairly elegant one. And since the above topic in the Gnome forum has been closed, let me lay it out here…
The Plumbing in the Background
But first, let’s take apart what exactly is happening. I have to say, I’ve learned something new here. I had always assumed that the binding between a desktop entry and a running application is simply based on the executable file name.
Turns out, there can be a little more indirection. And part of it is application specific, and it differs slightly depending on the underlying window manager (e.g. X11 vs. Wayland). When an application creates a window, it can assign a custom window property: VM_CLASS on X11 or app_id on Wayland. (I’m going to use the term app_id for the rest of this post, since my host desktop runs on Wayland.)
A desktop entry can bind to a specific window using the StartupWMClass field, which maps to the window’s app_id. This allows the desktop to add running-indicators to desktop icons, when a matching windows exists. And to bring up the right window, when a user clicks on a desktop icon of an application.
Most desktops do implement fallback behavior for desktop entries that lack the optional StartupWMClass field. They may employ various heuristics, one of which is using the application’s executable name (from the Exec field) as StartupWMClass. (So, sometimes it is as simple as I had thought.)
The Fix for Remote Viewer
Some applications may allow users to tweak their app_id, e.g. through command args, env vars, or config files. Unfortunately, Remote Viewer does not support this. Luckily, Remote Viewer does not use a fully hard-coded app_id either. Instead, it derives the app_id from the basename of it’s own executable file (which seems to be a default behavior for GTK applications).
As you may know, Unix-like systems pass the executable filename as zeroth argument to a new process (i.e. argv[0] or $0). And there are common ways to tinker with this argument without changing the executable file itself. E.g. by creating a symlink like this:
$ sudo ln -s /usr/bin/remote-viewer /usr/local/bin/remote-viewer-vm-dev
Now you can run Remote Viewer from /usr/local/bin/remote-viewer-vm-dev and it will use remote-viewer-vm-dev as app_id.
So, to fix your desktop icon, simply adjust the Exec field in your desktop entry to use the above symlink! Also pass in any arguments that you’d otherwise pass to the remote-viewer command. And finally add a matching StartupWMClass field and a custom Icon:
[Desktop Entry]
Version=1.0
Type=Application
Name=Developer Desktop
Categories=Development;IDE;
Keywords=vscode;eclipse;
Terminal=false
Exec=/usr/local/bin/remote-viewer-vm-dev spice+unix:///path/to/spice/socket.spice
StartupWMClass=remote-viewer-vm-dev
Icon=/usr/local/share/icons/dev.png
This is all it takes to make Remote Viewer work nicely with a custom desktop icon! When you have multiple VMs, simply create another symlink to the remote-viewer executable and create a new desktop entry with matching Exec, StartupWMClass, and Icon fields.
This works for any desktop entry that executes remote-viewer (or virt-viewer) explicitly. Unfortunately, this may not be the case for all desktop icons…
The Fix for Incus
When using Incus, you typically do not execute remote-viewer yourself. Instead, you’d use the incus console command with the --type vga option, and Incus will run Remote Viewer for you.
You might try running Remote Viewer yourself instead and pointing it to the right spice socket. But there is a catch here: Incus does not create these spices sockets when it creates or starts a VM instance. It only creates a spice socket on demand, when you run incus console --type vga for a specific VM. Incus also tears down this socket once this command terminates.
So, can we convince Incus to use a different remote-viewer command and thus enforce a custom app_id? Yes, we can! But it’s a little convoluted, so bear with me…
As seen in the desktop entry example at the beginning of this post, my Incus VM desktop icons are using common wrapper script, named mq-show-incus-desktop-vm. This wrapper script does a few other things (e.g. starting the target VM instance, if it’s not running yet), but ultimately it just runs incus console. Here’s the corresponding script line, where ${instance} resolves to the name of the Incus VM instance, as passed to the wrapper script:
incus console --type vga "${instance}"
In order to fix my desktop icons problem, I have replaced this script line with this one:
mq_run_with_shadowed_command \
remote-viewer \
"mq-incus-console-remote-viewer-${instance}" \
incus console --type vga "${instance}"
This does not explain a lot, because all the magic happens in this shell function named mq_run_with_shadowed_command. You can find its code further below, but first let me explain what it does:
The mq_run_with_shadowed_command function can run an arbitrary actual command, while ensuring that whenever this actual command looks up a specific helper command on the PATH and runs it, it will run that helper command with a shadowed name instead.
Here’s a break down the above function invocation argument by argument:
- The first argument represents the name of the helper command.
In the wrapper script, we passremote-vieweras first argument, because this is the helper command that Incus will eventually run. - The second argument represents the shadowed name for the helper command.
In the wrapper script, we pass"mq-incus-console-remote-viewer-${instance}"as second argument, because we want the helper commandremote-viewerto run under the shadowed namewhenever Incus tries to invoke it."mq-incus-console-remote-viewer-${instance}" - The remaining arguments represent the actual command and its arguments.
In the wrapper script, we passincus console --type vga "${instance}", because this is the actual command that we want to run.
In summary, the wrapper script uses the mq_run_with_shadowed_command function to run Incus, so that when Incus runs remote-viewer, it will run the standard Remote Viewer executable under a custom name like . The Remote Viewer will in turn use the shadowed name mq-incus-console-remote-viewer-vm-devincus-console-vm-dev as the app_id of its main window.
This allows that the desktop entry for each Incus VM can use a custom app_id. The above example uses as mq-incus-console-remote-viewer-vm-devapp_id, because it belongs to an Incus VM that I’ve named vm-dev. For a different VM, I can easily use a different app_id. E.g., I’m using as vm-androidmq-incus-console-remote-viewer-app_id for the Incus VM that I’ve named vm-android, and so on…
Now that we can run Remote Viewer with a custom app_id per VM, all that’s left to do is adding a matching StartupWMClass field. This changes my initial desktop entry for vm-dev to this:
[Desktop Entry]
Version=1.0
Type=Application
Name=Developer Desktop
Categories=Development;IDE;
Keywords=vscode;eclipse;
Terminal=false
Exec=/usr/local/bin/mq-show-incus-desktop-vm vm-dev
StartupWMClass=mq-incus-console-remote-viewer-vm-dev
Icon=/usr/local/share/icons/dev.png
Note that the app_id mq-incus-console-remote-viewer-vm-dev does not appear in the Exec field explicitly. My mq-show-incus-desktop-vm wrapper script constructs the shadowed name based on its argument, which is vm-dev. And the result matches the StartupWMClass field.
This last bit is not a necessity. It’s just how I’ve fit these desktop entries into my existing infrastructure. (Ultimately Ansible manages all my desktop entry files and generates the Exec and StartupWMClass fields based on the VM name.)
But how?
Not confused enough yet? Then let’s have a look at how the mq_run_with_shadowed_command function achieves the functionality that I’ve described above…
Here’s the implementation of this shell function, with some error-handling and cleanup code stripped out for clarity:
mq_run_with_shadowed_command() {
local helper_command="$1"
local shadowed_name="$2"
local -a actual_command_with_args=( "${@:3}" )
local helper_command_path
helper_command_path="$( type -P "${helper_command}" )"
local temp_path_dir
temp_path_dir="$( mktemp --directory --tmpdir "mq-shadowed-${helper_command}.XXXXXXXX" )"
cat > "${temp_path_dir}/${helper_command}" <<EOF
#!/bin/bash
exec -a ${shadowed_name@Q} ${helper_command_path@Q} "\$@"
EOF
chmod a+x "${temp_path_dir}/${helper_command}"
PATH="${temp_path_dir}:${PATH}" "${actual_command_with_args[@]}"
}
This function does its magic by modifying the PATH env-var when running the actual command (e.g. the Incus command seen in the previous section). Here is, how it works step by step:
- Assign the first two arguments to variables
helper_commandandshadowed_namerespectively. Also assign the remaining arguments to array variableactual_command_with_args. - Determine the actual filesystem path of the
helper_commandand assign it tohelper_command_path. - Create a temporary directory just for the lifetime of this function invocation and assign it to
temp_path_dir. - Inside this temporary directory, create a short temporary script with the same name as the
.helper_command
This bash script uses theexecshell built-in with the-aoption, which passes theshadowed_nameas zeroth argument to the command that it executes. It passes through the remaining arguments to thehelper_command_paththat it executes.
Note that the shell resolves variable references${shadowed_name@Q}and${helper_command_path@Q}when writing the temporary script. I.e., the script will contain hard-coded values of these variables. The@Qjust ensures proper shell quoting. In contrast,"\$@"is escaped and will appear as literal"$@"inside the temporary script. - Use
chmodto make the temporary script executable. - Prepend the temporary directory
temp_path_dirto thePATHvariable while running theactual_command_with_args.
When the actual command tries looking up the helper command on thePATH, it will find the temporary script in the temporary directory first and run it.
As explained in step 4, the script willexecthe exact helper command that the actual command wants to run. The temporary script will pass through all the command arguments, too. The only difference is that the zeroth argument to the helper command will be the shadowed name instead.
I’ve written this mq_run_with_shadowed_command function just to resolve my desktop icons problem, but I guess it might have other applications. Maybe there is already a similar implementation somewhere out there?
It’s a Hack!
I think, I’ve found a good solution to my problem, but it’s just a hack nevertheless. Things that might break in the future:
- Remote Viewer might change its
app_idhandling. E.g., it might use a hard-coded value instead of deriving it from the zeroth argument. - Incus might not look up the
remote-viewerbinary on thePATH. E.g., it might use a hard-coded path or some other lookup mechanism.
For now, my hack seems pretty robust though. While I’ve tested on Wayland only, it should work on X11, too. Also, my approach does not rely on asynchronous behavior, like tweaking the app_id of an existing window in the background.
While writing this post, I found out that there might be alternative solutions for the Incus part: Incus’ CLI config file does have a console-spice-command option. This should make it easy to hook in a wrapper script that calls Remote Viewer with a custom zeroth argument. Newer version of Incus also support a SOCKET placeholder in the console-spice-command option, which could be used to deal with Incus’ on-demand SPICE socket creation behavior.
As long as nothing breaks, I’ll stick with my hack though.