Re: [systemd-devel] VirtualBox VM as a unit failures

2022-09-01 Thread Silvio Knizek
Am Donnerstag, dem 01.09.2022 um 19:17 -0300 schrieb Sergio Belkin:
> 
> 
> 
> El jue, 1 sept 2022 a las 16:22, Silvio Knizek
> () escribió:
> > Am Donnerstag, dem 01.09.2022 um 14:59 -0300 schrieb Sergio Belkin:
> > > 
> > > This is the unit file:
> > > [Unit]
> > > Description=VirtualBox VM %i
> > > After=network.target vboxdrv.service
> > > Before=runlevel2.target shutdown.target
> > > 
> > > [Service]
> > > Type=forking
> > > Restart=no
> > > TimeoutSec=5min
> > > IgnoreSIGPIPE=no
> > > KillMode=process
> > > GuessMainPID=no
> > > RemainAfterExit=no
> > > 
> > > #ExecStart=/usr/lib/virtualbox/VBoxHeadless --comment RHEL7 --
> > > startvm
> > > f02a9f08-2ff2-4a92-b3cd-a8dfb17513c6 --vrde config 
> > > ExecStart=/usr/bin/VBoxManage startvm %i --type headless
> > > ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton
> > > 
> > > [Install]
> > > WantedBy=default.target
> > > 
> > > (End of file)
> > > 
> > > What is the proper way to configure this kind of unit?
> > > 
> > > Thanks in advance
> > > 
> > Is this really a forking process? Or do you just instruct some
> > daemon
> > via a RPC call to start your VM? In this case no actuall process
> > would
> > be there.
> > If there is a forking process, can you instruct VBoxManage to run
> > in
> > the foreground?
> > Or do you need to start some daemon process first and if not
> > already
> > running, than VBoxManage does so? This would explain the lingering
> > processes.
> > In generall your KillMode=process is what keeps the other processes
> > running in the cgroup. Just remove it.
> > 
> > Also, your Before= line is bogus. And if your vboxdrv.service just
> > contains the lines to load the vbox modules, you would be far
> > better of
> > with some snippet in /etc/modules-load.d
> > 
> > Oh, and as you run in user mode, _all_ your Before= and After=
> > entries
> > are useless, as user units can't see and reference system units.
> > 
> > So yeah, your goal would be to see how you can actually start the
> > VM
> > process via CLI and nothing else.
> > 
> > BR
> > Silvio
> 
> Hi Silvio,
> Nice answer.
> How to tell if it is a forking process?
> The command  "/usr/bin/VBoxManage startvm RHEL7  --type headless"
> exits  and then it runs "/usr/bin/VBoxManage startvm RHEL7  --type
> headles"
> 
> Thanks again!

Hi,

please keep answers on-list.
For your message, I don't understand it. The command forks/exists and
runs itself? Or did you copy it wrong the second time? Has VBoxManage
other command line parameters you can use for _not_ forking?


[systemd-devel] SRe: VirtualBox VM as a unit failures

2022-09-01 Thread Colin Guthrie

Hi,

When you have an ExecStop, it's meant to synchronously stop all 
processes. Anything left once it exits, systemd considers fair game for 
killing!


So chances are, the ExecStop runs and tells the machine the power button 
was pressed. This command exits almost immediately and before the 
machine gets much of a chance to process it, systemd goes on a killing 
spree to tidy up what's left.


So you may need some kind of synchronous version of the stop command 
which waits a bit with it's own timeout. You can likely knock up a 
script in bash pretty easily. Here's a snippet you can adapt from a 
script (non-systemd) I use for managing VMs which would allow you to 
implement a controlled shutdown with a timeout.


There may be other issues at play but this is definitely one of them!!

(other problems may include some processes being are user wide if you 
and used by multiple VMs but will get lumped in the cgroup with the 
first one you start and thus may be killed when it is powered off even 
if other VMs are still running!)


HTHs

Col


VM_NAME="RHEL7"

function isrunning()
{
  VBoxManage list runningvms | grep -q "$VM_NAME" && echo yes || echo no
}
function stop()
{
  if [ "yes" != $(isrunning) ]; then
echo "Not running"
  else
VBoxManage controlvm "$VM_NAME" acpipowerbutton
TIMEOUT=60
echo -n "Waiting for shutdown"
while [ "yes" = $(isrunning) ]; do
  echo -n "."
  sleep 1
  if [ 0 -eq $TIMEOUT ]; then
echo
echo "Timeout waiting for shutdown :-(" >&2
echo "Continuing with forced poweroff"
VBoxManage controlvm "$VM_NAME" poweroff
TIMEOUT=10
  fi
  TIMEOUT=$(( $TIMEOUT - 1 ))
done
echo
  fi
}


Sergio Belkin wrote on 01/09/2022 18:59:
I,m triying to configure an user-level unit for a VirtualBox VM, but it 
does not work well, when I stop, it complains:


systemctl --user status  vbox_vm_start@RHEL7.service
○ vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7
      Loaded: loaded 
(/home/sergio/.config/systemd/user/vbox_vm_start@.service; enabled; 
vendor preset: disabled)

      Active: inactive (dead) since Thu 2022-09-01 14:21:57 -03; 5s ago
     Process: 378373 ExecStart=/usr/bin/VBoxManage startvm RHEL7 --type 
headless (code=exited, status=0/SUCCESS)
     Process: 378581 ExecStop=/usr/bin/VBoxManage controlvm RHEL7 
acpipowerbutton (code=exited, status=0/SUCCESS)

       Tasks: 40 (limit: 38236)
      Memory: 23.6M
         CPU: 8.114s
      CGroup: 
/user.slice/user-1000.slice/user@1000.service/app.slice/app-vbox_vm_start.slice/vbox_vm_start@RHEL7.service

              ├─ 378386 /usr/lib/virtualbox/VBoxXPCOMIPCD
              ├─ 378392 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
              └─ 378442 /usr/lib/virtualbox/VBoxHeadless --comment RHEL7 
--startvm f02a9f08-2ff2-4a92-b3cd-a8dfb17513c6 --vrde config


sep 01 14:21:51 munster.belkin.home systemd[3452]: Starting 
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7...
sep 01 14:21:51 munster.belkin.home VBoxManage[378373]: Waiting for VM 
"RHEL7" to power on...
sep 01 14:21:51 munster.belkin.home VBoxManage[378373]: VM "RHEL7" has 
been successfully started.
sep 01 14:21:51 munster.belkin.home systemd[3452]: Started 
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7.
sep 01 14:21:56 munster.belkin.home systemd[3452]: Stopping 
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7...
sep 01 14:21:57 munster.belkin.home systemd[3452]: 
vbox_vm_start@RHEL7.service: Unit process 378386 (VBoxXPCOMIPCD) remains 
running after unit stopped.
sep 01 14:21:57 munster.belkin.home systemd[3452]: 
vbox_vm_start@RHEL7.service: Unit process 378392 (VBoxSVC) remains 
running after unit stopped.
sep 01 14:21:57 munster.belkin.home systemd[3452]: 
vbox_vm_start@RHEL7.service: Unit process 378442 (VBoxHeadless) remains 
running after unit stopped.
sep 01 14:21:57 munster.belkin.home systemd[3452]: Stopped 
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7.
sep 01 14:21:57 munster.belkin.home systemd[3452]: 
vbox_vm_start@RHEL7.service: Consumed 3.386s CPU time.


If I try to start, these are the errors:

× vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7
      Loaded: loaded 
(/home/sergio/.config/systemd/user/vbox_vm_start@.service; enabled; 
vendor preset: disabled)
      Active: failed (Result: exit-code) since Thu 2022-09-01 14:22:06 
-03; 7s ago
     Process: 378730 ExecStart=/usr/bin/VBoxManage startvm RHEL7 --type 
headless (code=exited, status=1/FAILURE)

       Tasks: 40 (limit: 38236)
      Memory: 25.7M
         CPU: 3.338s
      CGroup: 
/user.slice/user-1000.slice/user@1000.service/app.slice/app-vbox_vm_start.slice/vbox_vm_start@RHEL7.service

              ├─ 378386 /usr/lib/virtualbox/VBoxXPCOMIPCD
              ├─ 378392 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
              └─ 378442 /usr/lib/virtualbox/VBoxHeadless --comment RHEL7 
--startvm f02a9f08-2ff2-4a92-b3cd-a8dfb17513c6 --vrde config


sep 01 14:22:06 munster.belkin.home systemd[3452]: Starting 

Re: [systemd-devel] VirtualBox VM as a unit failures

2022-09-01 Thread Silvio Knizek
Am Donnerstag, dem 01.09.2022 um 14:59 -0300 schrieb Sergio Belkin:
> 
> This is the unit file:
> [Unit]
> Description=VirtualBox VM %i
> After=network.target vboxdrv.service
> Before=runlevel2.target shutdown.target
> 
> [Service]
> Type=forking
> Restart=no
> TimeoutSec=5min
> IgnoreSIGPIPE=no
> KillMode=process
> GuessMainPID=no
> RemainAfterExit=no
> 
> #ExecStart=/usr/lib/virtualbox/VBoxHeadless --comment RHEL7 --startvm
> f02a9f08-2ff2-4a92-b3cd-a8dfb17513c6 --vrde config 
> ExecStart=/usr/bin/VBoxManage startvm %i --type headless
> ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton
> 
> [Install]
> WantedBy=default.target
> 
> (End of file)
> 
> What is the proper way to configure this kind of unit?
> 
> Thanks in advance
> 
Is this really a forking process? Or do you just instruct some daemon
via a RPC call to start your VM? In this case no actuall process would
be there.
If there is a forking process, can you instruct VBoxManage to run in
the foreground?
Or do you need to start some daemon process first and if not already
running, than VBoxManage does so? This would explain the lingering
processes.
In generall your KillMode=process is what keeps the other processes
running in the cgroup. Just remove it.

Also, your Before= line is bogus. And if your vboxdrv.service just
contains the lines to load the vbox modules, you would be far better of
with some snippet in /etc/modules-load.d

Oh, and as you run in user mode, _all_ your Before= and After= entries
are useless, as user units can't see and reference system units.

So yeah, your goal would be to see how you can actually start the VM
process via CLI and nothing else.

BR
Silvio


[systemd-devel] VirtualBox VM as a unit failures

2022-09-01 Thread Sergio Belkin
Hi,
I,m triying to configure an user-level unit for a VirtualBox VM, but it
does not work well, when I stop, it complains:

systemctl --user status  vbox_vm_start@RHEL7.service
○ vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7
 Loaded: loaded (/home/sergio/.config/systemd/user/vbox_vm_start@.service;
enabled; vendor preset: disabled)
 Active: inactive (dead) since Thu 2022-09-01 14:21:57 -03; 5s ago
Process: 378373 ExecStart=/usr/bin/VBoxManage startvm RHEL7 --type
headless (code=exited, status=0/SUCCESS)
Process: 378581 ExecStop=/usr/bin/VBoxManage controlvm RHEL7
acpipowerbutton (code=exited, status=0/SUCCESS)
  Tasks: 40 (limit: 38236)
 Memory: 23.6M
CPU: 8.114s
 CGroup: /user.slice/user-1000.slice/user@1000.service
/app.slice/app-vbox_vm_start.slice/vbox_vm_start@RHEL7.service
 ├─ 378386 /usr/lib/virtualbox/VBoxXPCOMIPCD
 ├─ 378392 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
 └─ 378442 /usr/lib/virtualbox/VBoxHeadless --comment RHEL7
--startvm f02a9f08-2ff2-4a92-b3cd-a8dfb17513c6 --vrde config

sep 01 14:21:51 munster.belkin.home systemd[3452]: Starting
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7...
sep 01 14:21:51 munster.belkin.home VBoxManage[378373]: Waiting for VM
"RHEL7" to power on...
sep 01 14:21:51 munster.belkin.home VBoxManage[378373]: VM "RHEL7" has been
successfully started.
sep 01 14:21:51 munster.belkin.home systemd[3452]: Started
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7.
sep 01 14:21:56 munster.belkin.home systemd[3452]: Stopping
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7...
sep 01 14:21:57 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Unit process 378386 (VBoxXPCOMIPCD) remains
running after unit stopped.
sep 01 14:21:57 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Unit process 378392 (VBoxSVC) remains running
after unit stopped.
sep 01 14:21:57 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Unit process 378442 (VBoxHeadless) remains
running after unit stopped.
sep 01 14:21:57 munster.belkin.home systemd[3452]: Stopped
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7.
sep 01 14:21:57 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Consumed 3.386s CPU time.

If I try to start, these are the errors:

× vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7
 Loaded: loaded (/home/sergio/.config/systemd/user/vbox_vm_start@.service;
enabled; vendor preset: disabled)
 Active: failed (Result: exit-code) since Thu 2022-09-01 14:22:06 -03;
7s ago
Process: 378730 ExecStart=/usr/bin/VBoxManage startvm RHEL7 --type
headless (code=exited, status=1/FAILURE)
  Tasks: 40 (limit: 38236)
 Memory: 25.7M
CPU: 3.338s
 CGroup: /user.slice/user-1000.slice/user@1000.service
/app.slice/app-vbox_vm_start.slice/vbox_vm_start@RHEL7.service
 ├─ 378386 /usr/lib/virtualbox/VBoxXPCOMIPCD
 ├─ 378392 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
 └─ 378442 /usr/lib/virtualbox/VBoxHeadless --comment RHEL7
--startvm f02a9f08-2ff2-4a92-b3cd-a8dfb17513c6 --vrde config

sep 01 14:22:06 munster.belkin.home systemd[3452]: Starting
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7...
sep 01 14:22:06 munster.belkin.home VBoxManage[378730]: VBoxManage: error:
The machine 'RHEL7' is already locked by a session (or being locked or
unlocked)
sep 01 14:22:06 munster.belkin.home VBoxManage[378730]: VBoxManage: error:
Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component
MachineWrap, interface IMachine, callee n>
sep 01 14:22:06 munster.belkin.home VBoxManage[378730]: VBoxManage: error:
Context: "LaunchVMProcess(a->session, sessionType.raw(),
ComSafeArrayAsInParam(aBstrEnv), progress.asOutPar>
sep 01 14:22:06 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Control process exited, code=exited,
status=1/FAILURE
sep 01 14:22:06 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Failed with result 'exit-code'.
sep 01 14:22:06 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Unit process 378386 (VBoxXPCOMIPCD) remains
running after unit stopped.
sep 01 14:22:06 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Unit process 378392 (VBoxSVC) remains running
after unit stopped.
sep 01 14:22:06 munster.belkin.home systemd[3452]:
vbox_vm_start@RHEL7.service: Unit process 378442 (VBoxHeadless) remains
running after unit stopped.
sep 01 14:22:06 munster.belkin.home systemd[3452]: Failed to start
vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7.

This the unit file:
× vbox_vm_start@RHEL7.service - VirtualBox VM RHEL7
 Loaded: loaded (/home/sergio/.config/systemd/user/vbox_vm_start@.service;
enabled; vendor preset: disabled)
 Active: failed (Result: exit-code) since Thu 2022-09-01 14:22:06 -03;
7s ago
Process: 378730 ExecStart=/usr/bin/VBoxManage startvm RHEL7 --type
headless (code=exited, status=1/FAILURE)
  Tasks: 40 (limit: 38236)