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
Script fstrim_via_guest-agent.pl
Updates at https://github.com/eurodomenii/proxmox_fstrim_qemu-guest-agent
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
Howewer, there's a print mode on screen:*/30 * * * * root /var/eurodomenii/scripts/github/proxmox_fstrim_qemu-guest-agent/fstrim_via_guest-agent.pl >/dev/null 2>&1
./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: