Qemu hook for opening looking glass

Hi everyone,

does anyone know of a way to start looking glass via a qemu hook?
So when I turn on the VM the looking glass windows opens on my desktop?

I just found an unresolved Reddit thread that has not been much help.

I tried the tricks from the thread anyway but they really do not work.

Thanks in advance!

2 Likes

Honestly, at first glance I got good laugh from this idea.
However on second thought it will save at least two mouse clicks each VM run :wink:
Also, without any research, my first solution would be to run small script as service to check for VM, and spawn LG if VM exists.

I have this idea not because it is so hard to open a terminal and run the looking glass command, but because it would be a seamless experience!

Thank you but this is already what qemu hooks are for. You can put scripts into /etc/libvirt/hooks that will be executed once you start the VM, no need really to run a service. The problem is to get the looking glass window displayed and also that, like mentioned in the reddit thread, the fact that looking glass is not a command that is getting run and then finishes immediately so the hook is not working correctly.

I talk with 0 knowledge of the software, but what you are describing sound like a launcher forking it’s software as a daemon and closing itself afterward.
Usually software who do that also offer 2 thing, a known way to get the daemon PID, and a command-line argument that don’t spawn a daemon but keep everything in the launcher.

If there isn’t an argument like that, look for a file somewhere like /run that would contain the PID of the LG software.
Then you can create a script that lunch LG, get the pid from that file, and close when the process with that pid die

Once again i never used LG nor QEMU so i may be completely of course.

I made myself shortcut in xfce menu that just runs “looking-glass-client -a -F”. No need to start console before.

Yes I’m aware, but what I meant is that self made solution will be faster than getting a grip on huge project that has small bug.

I’m also forgetful :slight_smile: Just checked my machine and apparently I did try this mechanism for automatically mounting and dismounting samba after VM started or stopped. Timestamp shows me it was year ago.

I remember now that it was “kinda” working, I.E hook was running ok. But if mountpoint was still busy I’ve had problems with it. “Hard reset” problems. So I scrapped whole idea and forgot about it.

But It may help you to do what I did at the begining of my /etc/libvirt/hooks/qemu script:

#!/bin/bash
echo $0 $1 $2 $3 $4 >> /home/misiek/hooks.log

This allowed me to precisely check with “tail -f” when hook is run, and what commands it sends when.

BTW, I’m on Manjaro.

1 Like

Well, I tried to make qemu hook for it. But looking glass doesn’t like it.
When trying to run “looking-glass-client -a -F” on “vm started” signal it just does nothing.
So I tried to add delay, but hook doesn’t exit until delay is over, so no difference.
Then tried to fork function and separate script with &, (&), and setsid, but no joy.

Now I remember why I opted for menu shortcut instead hook…

Thank you for your afford. I am also still looking for a solution, I do not really understand until know why this approach is so much of an issue. There seems to be an important piece of knowledge missing in my head but until know I was unable to figure out what it is.
I know that some other software, like starting and stopping Synergy works with hooks.

@gnif: Sorry to bother you, but do you know if this has something to do with LG? I want to know if this is a problem specific to LG or if the problem is somewhere else in the software stack!

AFAIR LG doesn’t wait for IVSHMEM to start outputting data. It just exits. That’s why I tried to make delay until driver loads.
But even if it would wait, then hook doesn’t exit (and therefore VM doesn’t load) until all processes spawned inside are done. And I couldn’t find solution to that.

Edit: Oh, synergy probably works from systemd, didn’t thought about that. That could work…

Holy sh**, got it working from systemd :grinning:. Thanks for hint that synergy works from hook @anon89476829 :slight_smile:

First simple script to run lg “lghelper”. You may have to change DISPLAY variable. echo $DISPLAY to check what it is:

#!/bin/sh
export DISPLAY=:0.0 # change to your display port if different!
sleep 10  # can change delay depending on host
looking-glass-client -F -a

Then service file “looking-glass.service”:

[Unit]
Description=Looking Glass workaround
After=libvirt.service
Wants=libvirt.service

[Service]
Type=simple
# change to your username and group!
User=misiek 
Group=misiek
# change to your path!
ExecStart=/home/misiek/scripts/lghelper 

[Install]
WantedBy=default.target

To install service usually following is enough:

ln -s /path/to/looking-glass.sevice /etc/systemd/system/looking-glass.service
sudo systemd enable looking-glass

But if your distro has apparmor, it may need some extra steps.
Also after enabling service you can check if it working properly. Following should spawn looking glass while your VM is running:

sudo systemctl start looking-glass

And finally you can add hook, paste to /etc/libvirt/qemu and change VM1NAME:

#!/bin/sh
VM1NAME="my_vm_name"

if [[ $1 == $VM1NAME ]] && [[ $2 == "started" ]]; then
  systemctl start looking-glass
fi
if [[ $1 == $VM1NAME ]] && [[ $2 == "stopped" ]]; then
 systemctl stop looking-glass
fi

Above was tested on Manjaro 20.2

Edit: Scripts pobably could use some massage, like service should not start at boot or sth. Just grabbed some template for service for testing. Corrections encouraged :slight_smile:

2 Likes

Did you do something addtional to the service file? I get the following error message trying to enable the service the way you described:

Failed to enable unit: Unit file /<PATH>/looking-glass.service is masked.

No, I just symlinked it and systemctl enabled…
Maybe its apparmor issue, for example if you using Ubu.
Check “journalctl -f” when you enabling service, maybe it will tell you what the heck :wink:

Also there are few obvious things i didn’t mention:
Change username to yours.
Change path to lghelper if its in different location.

Look like either a permission issue (system, apparmor), or the link did not work correctly.

Another way to do it, this is what I use:
cp to /etc/systemd/system/ (hard link should also work no problem)
sudo systemctl daemon-reload
sudo systemctl start looking-glass

I did exactly that!

The output after running

sudo systemctl start looking-glass.service
journalctl -f

is

Nov 23 21:37:41 hero systemd[1]: Started Looking Glass workaround.
Nov 23 21:37:41 hero audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 subj==unconfined msg='unit=looking-glass comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
Nov 23 21:37:41 hero systemd[17316]: looking-glass.service: Failed to determine user credentials: No such process
Nov 23 21:37:41 hero systemd[17316]: looking-glass.service: Failed at step USER spawning /home/neuromancer/System/Scripts/looking-glass-helper.sh: No such process
Nov 23 21:37:41 hero systemd[1]: looking-glass.service: Main process exited, code=exited, status=217/USER
Nov 23 21:37:41 hero systemd[1]: looking-glass.service: Failed with result 'exit-code'.
Nov 23 21:37:41 hero sudo[17313]: pam_unix(sudo:session): session closed for user root
Nov 23 21:37:41 hero audit[17313]: USER_END pid=17313 uid=0 auid=1000 ses=4 subj==unconfined msg='op=PAM:session_close grantors=pam_limits,pam_unix,pam_permit acct="root" exe="/usr/bin/sudo" hostname=? addr=? terminal=/dev/pts/1 res=success'
Nov 23 21:37:41 hero audit[17313]: CRED_DISP pid=17313 uid=0 auid=1000 ses=4 subj==unconfined msg='op=PAM:setcred grantors=pam_u2f,pam_env,pam_fprintd acct="root" exe="/usr/bin/sudo" hostname=? addr=? terminal=/dev/pts/1 res=success'
Nov 23 21:37:41 hero audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 subj==unconfined msg='unit=looking-glass comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=failed'

Typo in user name?

With systemctl start that service should spawn looking glass if VM is running.
But normally it should be stopped, and hook should start and then stop it.

If its apparmor issue (stopping it will tell you that) then you have find how to add new service to apparmor rules.

sudo systemctl disable apparmor
sudo systemctl stop apparmor

I stopped using it while back, because its annoying, so cant help you with that.

Ah, also

sudo systemctl status looking-glass

Will tell you things…

2 Likes

Indeed that was it. I did not even see it when I checked the files. Now everything works and the LG windows opens when starting the VM. I needed to make some small modifications like for me it is:

Display=:1 

Thanks for your effort! I will mark your answer as the solution!

1 Like

Great :slight_smile:
I just wish I could still edit it, I would put some extra notes for future generations :slight_smile:

1 Like

@TheCakeIsNaOH: Can you help here?

@misiektw I think you should be able to edit it again.
We have a short edit window for lower trust levels due to the amount of spam we have been getting.

1 Like

Thanks, I was able to edit it again :slight_smile:

1 Like