alpha754293
alpha754293

Reputation: 11

How I combine a bash hookscript with a Perl hookscript for use with Proxmox?

I am trying to use Promox VE has the hypervisor for running VMs.

In one of my VMs, I have a hookscript that is written for the bash shell:

#!/bin/bash

if [ $2 == "pre-start" ]
then
echo "gpu-hookscript: Resetting GPU for Virtual Machine $1"
echo 1 > /sys/bus/pci/devices/0000\:01\:00.0/remove
echo 1 > /sys/bus/pci/rescan
fi

which is to help with enabling GPU passthrough.

And then I have another hookscript that is written in Perl, which enables virtio-fs:

#!/usr/bin/perl
# Exmple hook script for PVE guests (hookscript config option)
# You can set this via pct/qm with
# pct set <vmid> -hookscript <volume-id>
# qm set <vmid> -hookscript <volume-id>
# where <volume-id> has to be an executable file in the snippets folder
# of any storage with directories e.g.:
# qm set 100 -hookscript local:snippets/hookscript.pl
use strict;
use warnings;
print "GUEST HOOK: " . join(' ', @ARGV). "\n";
# First argument is the vmid
my $vmid = shift;
# Second argument is the phase
my $phase = shift;
if ($phase eq 'pre-start') {
# First phase 'pre-start' will be executed before the guest
    # ist started. Exiting with a code != 0 will abort the start
print "$vmid is starting, doing preparations.\n";
system('/var/lib/vz/snippets/launch-virtio-daemon.sh');
# print "preparations failed, aborting."
    # exit(1);
} elsif ($phase eq 'post-start') {
# Second phase 'post-start' will be executed after the guest
    # successfully started.
print "$vmid started successfully.\n";
} elsif ($phase eq 'pre-stop') {
# Third phase 'pre-stop' will be executed before stopping the guest
    # via the API. Will not be executed if the guest is stopped from
    # within e.g., with a 'poweroff'
print "$vmid will be stopped.\n";
} elsif ($phase eq 'post-stop') {
# Last phase 'post-stop' will be executed after the guest stopped.
    # This should even be executed in case the guest crashes or stopped
    # unexpectedly.
print "$vmid stopped. Doing cleanup.\n";
} else {
    die "got unknown phase '$phase'\n";
}
exit(0);

What would be the best way for me to combine these two files into a single format, so that I can use it as a hookscript in Proxmox?

I tried reading the thread here about how to convert a bash shell script to Perl, and not being a programmer, admittedly, I didn't understand what I was reading.

I appreciate the teams help in educating a non-programmer.

Thank you.

Upvotes: 1

Views: 1014

Answers (2)

Henk
Henk

Reputation: 1

This solved my problem, thanks! I used this command:

system('/sys/bus/pci/devices/0000:01:00.0/remove')

Upvotes: -1

ZoltanB
ZoltanB

Reputation: 109

before

system('/var/lib/vz/snippets/launch-virtio-daemon.sh');

insert pls.

system('echo 1 > /sys/bus/pci/devices/0000\:01\:00.0/remove');
system('echo 1 > /sys/bus/pci/rescan');

Had your original code above evaluated return code of these perl calls (it is not the case):

echo 1 > /sys/bus/pci/devices/0000\:01\:00.0/remove
echo 1 > /sys/bus/pci/rescan

you could apply solutions from: Getting Perl to return the correct exit code

Upvotes: 0

Related Questions