[SOLVED] Fstrim script via QEMU guest agent

EuroDomenii

Well-Known Member
Sep 30, 2016
139
25
48
Slatina
www.domenii.eu
For performance reasons, instead of mounting with discard option inside KVM, it's better running external fstrim command via qemu-guest-agent, on host cron event.

However, discard=on must be setup in KVM storage configuration at /etc/pve/qemu-server/VMid.conf

Default mode is sending mail to Linux root when QEMU guest agent is running, or on another execution error

cat /etc/crontab
*/30 * * * * root /var/eurodomenii/scripts/github/proxmox_fstrim_qemu-guest-agent/fstrim_via_guest-agent.pl >/dev/null 2>&1
Howewer, there's a print mode on screen:

./fstrim_via_guest-agent.pl print

Script fstrim_via_guest-agent.pl

Perl:
#!/usr/bin/perl
use strict;
use warnings;

my $mode = "mail";
if (@ARGV && ($ARGV[0] eq "print")) {
    $mode = "print";
}

my $node = `hostname`;
chomp $node;
my @vmids = `pvesh get /nodes/$node/qemu/ --output-format json-pretty | jq -r '.[] | .vmid'`;

my $fstrim_exec = "";
my $message = "";

foreach my $i (@vmids) {
    chomp $i;
    my $status = `qm status $i`;
    chomp $status;
    next if $status ne "status: running";
    if ($mode eq "print") {
        $fstrim_exec = `qm guest exec $i fstrim -- "-av"`;
        print "Fstrim results for $i VM \n $fstrim_exec \n";
    } else {
        $fstrim_exec = `qm guest exec $i fstrim -- "-av" | awk -F":" '/exited/{print \$2}'`;
        if ($fstrim_exec eq "") {
           #This error code means QEMU guest agent is not running
            #Test this by running in VM systemctl stop qemu-guest-agent.service
            $message .= "QEMU guest agent is not running for VM $i on node $node\n";
        } elsif (index($fstrim_exec, "0") != -1) {
            $message .= "Even QEMU guest agent is running, there's an execution error of fstrim command for VM $i on node $node\n";
        }
    }
}

if ($mode eq "mail" && $message ne "") {
    my $to = "root";
    my $fullhostname = `hostname -f`;
    $fullhostname =~ s/^\s+|\s+$//g ;
    my $from = "root@".$fullhostname;
    my $subject = "Fstrim discard report on Proxmox node $node";

    open(MAIL, "|/usr/sbin/sendmail -t");

    # Email Header
    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";
    # Email Body
    print MAIL $message;

    my $result = close(MAIL);
    if(!$result){
        #ToDo write to logs
        #print "There was a problem, Bro!\n";
    }
}

Updates at https://github.com/eurodomenii/proxmox_fstrim_qemu-guest-agent
 
Last edited:

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!