From 80d85b0e1dea96d001adaf9be82650066c114d81 Mon Sep 17 00:00:00 2001 From: Jo-NMS Date: Tue, 11 Mar 2025 15:35:52 +0100 Subject: [PATCH 1/3] Fix Proxmox memory conversion Lxc.pm Fix memory conversion for LXC containers on Proxmox by converting plain byte values to MB --- .../Agent/Task/Inventory/Virtualization/Lxc.pm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm b/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm index 2fae85cc7..18d64735a 100644 --- a/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm +++ b/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm @@ -102,10 +102,17 @@ sub _getVirtualMachine { if $val =~ $mac_address_pattern; } - if ($key eq 'lxc.cgroup.memory.limit_in_bytes' || $key eq 'lxc.cgroup2.memory.max') { - $val .= "b" if $val =~ /[KMGTP]$/i; - $container->{MEMORY} = getCanonicalSize($val, 1024); - } + if ($key eq 'lxc.cgroup.memory.limit_in_bytes' || $key eq 'lxc.cgroup2.memory.max') { + if ($val =~ /[KMGTP]$/i) { + $val .= "b"; + $container->{MEMORY} = getCanonicalSize($val, 1024); + } elsif ($val =~ /^\d+$/) { + # Plain numeric value in bytes, convert to MB + $container->{MEMORY} = int($val / (1024 * 1024)); + } else { + $container->{MEMORY} = getCanonicalSize($val, 1024); + } + } # Update container name in Proxmox environment if ($proxmox && ($key eq 'lxc.uts.name' || $key eq 'lxc.utsname')) { From 6bb1b571350027a57e6c3c4c83c66b9f20893bca Mon Sep 17 00:00:00 2001 From: Jo-NMS Date: Wed, 12 Mar 2025 09:32:57 +0100 Subject: [PATCH 2/3] Update lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm --- .../Agent/Task/Inventory/Virtualization/Lxc.pm | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm b/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm index 18d64735a..55ac6c269 100644 --- a/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm +++ b/lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm @@ -102,17 +102,10 @@ sub _getVirtualMachine { if $val =~ $mac_address_pattern; } - if ($key eq 'lxc.cgroup.memory.limit_in_bytes' || $key eq 'lxc.cgroup2.memory.max') { - if ($val =~ /[KMGTP]$/i) { - $val .= "b"; - $container->{MEMORY} = getCanonicalSize($val, 1024); - } elsif ($val =~ /^\d+$/) { - # Plain numeric value in bytes, convert to MB - $container->{MEMORY} = int($val / (1024 * 1024)); - } else { - $container->{MEMORY} = getCanonicalSize($val, 1024); - } - } + if ($key eq 'lxc.cgroup.memory.limit_in_bytes' || $key eq 'lxc.cgroup2.memory.max') { + $val .= $val =~ /[KMGTP]$/i ? "b" : $val =~ /^\d+$/ ? "bytes" : ""; + $container->{MEMORY} = getCanonicalSize($val, 1024); + } # Update container name in Proxmox environment if ($proxmox && ($key eq 'lxc.uts.name' || $key eq 'lxc.utsname')) { From 9244ba9ad5d6c638a4613890a50433d2a79adc01 Mon Sep 17 00:00:00 2001 From: Jo-NMS Date: Wed, 12 Mar 2025 10:21:13 +0100 Subject: [PATCH 3/3] Update t/tasks/inventory/virtualization/lxc.t Update expected memory values in t/tasks/inventory/virtualization/lxc.t to match the new conversion logic: - On line 26, change '2048000' to '1' (2048000 bytes round to 1 MB). - On line 58, change '2147483648' to '2048' for better consistency. --- t/tasks/inventory/virtualization/lxc.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/tasks/inventory/virtualization/lxc.t b/t/tasks/inventory/virtualization/lxc.t index 093d70ff1..bb17498bf 100755 --- a/t/tasks/inventory/virtualization/lxc.t +++ b/t/tasks/inventory/virtualization/lxc.t @@ -23,7 +23,7 @@ my %container_tests = ( NAME => 'config', VMTYPE => 'lxc', STATUS => STATUS_OFF, - MEMORY => '2048000', + MEMORY => '1', MAC => '01:23:45:67:89:0a', VCPU => 4 } @@ -55,7 +55,7 @@ my %container_tests = ( VMTYPE => 'lxc', STATUS => STATUS_RUNNING, MAC => 'fa:ee:26:ef:6b:1c', - MEMORY => 2147483648, + MEMORY => 2048, VCPU => 2 } },