VM Hookscripts

yaro014

Active Member
Dec 27, 2012
32
15
28
Is there a way for VM pre-start hookscripts to dynamically append to "args:" section of VM config file ?

What I'm trying to accomplish is to append to original config "args:" without altering config.

I found something that would allow me to edit various config elements and write back to config however, if I have my script appending args it would end up appending it each and every time VM starts.

Perl:
my $conf = PVE::QemuConfig->load_config($vmid);

# 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";
    print "$conf->{memory}";
    $conf->{memory} = 32760;
    print "$conf->{memory}";
    PVE::QemuConfig->write_config($vmid, $conf)
;
 
Last edited:

Stefan_R

Proxmox Retired Staff
Retired Staff
Jun 4, 2019
1,300
280
88
Vienna
What is your end-goal with this? Maybe there is an easier way to get the result you want.

I don't really see the issue in your script - if you set $conf->{args} it will not "append", it will either set it new or overwrite the existing value. If you mean append to the args line itself, you can just check for your argument being there before appending, e.g. OTOH:

Perl:
$conf->{args} .= " -device foobar" if !$conf->{args} || $conf->{args} !~ m/-device foobar/;
 

yaro014

Active Member
Dec 27, 2012
32
15
28
Well what I'm trying to achieve is to script multiple VirtioFS shares into my VM using hookscript. To add single share there is a long line to be added into ARGS section so I'm looking for a method to do that. I know I can keep adding and removing them but I would rather have that being applied at runtime rather than storing in config.

As you would imaging adding 4 or 5 virtiofs shares would make the ARGS line on config quite large and ugly. Not even mentioning the complexity of removing those lines when VM shuts down etc.

I managed to create number of systemd services to start my virtiofs share for each vm on start and stop it when vm exits or being stopped:

Code:
[Unit]
Description=virtiofsd filesystem share /media for VMID %I
BindsTo=%I.scope

[Service]
Type=exec
PIDFile=/run/virtiofsd-media-%I.pid
ExecStart=/usr/bin/virtiofsd -f -o clone_fd -o vhost_user_socket=/run/virtiofsd-media-%I.sock -o source=/media -o cache=always

Then I would use this hookscript to start and stop it

Perl:
#!/usr/bin/perl

use PVE::QemuServer;

use strict;
use warnings;

print "GUEST HOOK: ".join(' ', @ARGV).
"\n";

my $vmid = shift;
my $conf = PVE::QemuConfig->load_config($vmid);

my $phase = shift;

if ($phase eq 'pre-start') {
  print "$vmid is starting, doing preparations.\n";
  my $vmmem = $conf->{memory};
  $conf->{args} = "-chardev socket,id=char0,path=/run/virtiofsd-tmp-shared-$vmid.sock -device vhost-user-fs-pci,chardev=char0,tag=tmp_shared -object memory-backend-memfd,id=mem,size=${vmmem}M,share=on -numa node,memdev=mem";
  PVE::QemuConfig->write_config($vmid, $conf);
  system("/usr/bin/systemctl start virtiofsd-tmp-shared\@$vmid.service");
}
elsif($phase eq 'post-start') {
  print "$vmid started successfully.\n";
}
elsif($phase eq 'pre-stop') {
  print "$vmid will be stopped.\n";
}
elsif($phase eq 'post-stop') {
  print "$vmid stopped. Doing cleanup.\n";
} else {
  die "got unknown phase '$phase'\n";
}

exit(0);


This seems to work ok when I have no ARGS preconfigured in main config already as it overwrites ARGS completely, the issues start when I have multiple of virtiofs shares and some other args which already existed.


So for example if I have ARGS configured for completely different purpose there already I would have to grab its initial value > append my virtiofs lines > put it back into config.

On next run it will keep adding duplicate lines.


So AFAIK ideal solution would be having ability to append ARGS for runtime only and not store it back into .conf of VM.
This would allow to keep original ARGS section intact on each boot and only hookscript would append to existing ARGS on runtime only.
 
Last edited:
  • Like
Reactions: Blinkiz

Stefan_R

Proxmox Retired Staff
Retired Staff
Jun 4, 2019
1,300
280
88
Vienna
So AFAIK ideal solution would be having ability to append ARGS for runtime only and not store it back into .conf of VM.
This would allow to keep original ARGS section intact on each boot and only hookscript would append to existing ARGS on runtime only.
You can open an issue on our bugtracker if you want, but this would break some assumptions about how our config works, so I'm not sure OTOH how feasible this would be...

It's certainly possible to script this though, just takes a bit more effort. You'd need to parse $conf->{args} somehow and remove all existing virtiofs-related entries, then add any new ones. This would be permanent still, but since it'd get cleaned on any boot it shouldn't really matter. Just as a starting point.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get your own in 60 seconds.

Buy now!