Skip to content

Commit

Permalink
feat: Use uts.name for LXC container name under Proxmox
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard committed Feb 4, 2022
1 parent 4aec548 commit dd04d55
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/GLPI/Agent/Task/Inventory/Virtualization/Lxc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ sub _getVirtualMachine {
)
};

# Proxmox environment sets name as number
my $proxmox = $name =~ /^\d+$/ ? 1 : 0;

my $command = "lxc-info -n '$name' -c lxc.cgroup.memory.limit_in_bytes -c lxc.cgroup.cpuset.cpus";
if ($params{version} < 2.1) {
# Before 2.1, we need to find MAC as lxc.network.hwaddr in config
$command .= "; grep lxc.network.hwaddr $params{config}";
# Look for lxc.utsname from config file in Proxmox environment
$command .= "; grep utsname $params{config}" if $proxmox;
} else {
$command .= " -c lxc.net.0.hwaddr";
# Look for lxc.uts.name in Proxmox environment
$command .= " -c lxc.uts.name" if $proxmox;
}

my $handle = getFileHandle(
Expand All @@ -96,6 +103,11 @@ sub _getVirtualMachine {
$container->{MEMORY} = $val;
}

# Update container name in Proxmox environment
if ($proxmox && ($key eq 'lxc.uts.name' || $key eq 'lxc.utsname')) {
$container->{NAME} = $val;
}

if ($key eq 'lxc.cgroup.cpuset.cpus') {
###eg: lxc.cgroup.cpuset.cpus = 0,3-5,7,2,1
foreach my $cpu ( split( /,/, $val ) ){
Expand Down
8 changes: 8 additions & 0 deletions resources/virtualization/lxc/200-proxmox
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# lxc-info -n 200 -s -c lxc.cgroup.memory.limit_in_bytes -c lxc.cgroup.cpuset.cpus -c lxc.net.0.hwaddr -c lxc.uts.name
State: RUNNING
lxc.cgroup.memory.limit_in_bytes = 2147483648

lxc.cgroup.cpuset.cpus = 0,2

lxc.net.0.hwaddr = FA:EE:26:EF:6B:1C
lxc.uts.name = glpi-10-rc1
13 changes: 12 additions & 1 deletion t/tasks/inventory/virtualization/lxc.t
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ my %container_tests = (
VCPU => 3
}
},
'200-proxmox' => {
version => 3.0,
result => {
NAME => 'glpi-10-rc1',
VMTYPE => 'lxc',
STATUS => STATUS_RUNNING,
MAC => 'fa:ee:26:ef:6b:1c',
MEMORY => 2147483648,
VCPU => 2
}
},
);

plan tests => keys(%result_lxc_info) + keys(%container_tests) + 1;
Expand All @@ -63,7 +74,7 @@ foreach my $name (keys(%container_tests)) {
my $file = "resources/virtualization/lxc/$name";
$file =~ s|/|\\|g if $OSNAME eq "MSWin32";
my $config = GLPI::Agent::Task::Inventory::Virtualization::Lxc::_getVirtualMachine(
name => $name,
name => $name =~ /(^.*)-proxmox$/ ? $1 : $name,
version => $container_tests{$name}->{version},
test_cmdstate => $OSNAME eq "MSWin32" ? "type $file" : "cat $file",
test_cmdinfo => $OSNAME eq "MSWin32" ? "type $file" : "cat $file",
Expand Down

0 comments on commit dd04d55

Please sign in to comment.