diff --git a/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Lxc.pm b/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Lxc.pm index 666666eeac..e300335428 100644 --- a/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Lxc.pm +++ b/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Lxc.pm @@ -7,6 +7,7 @@ use strict; use warnings; use FusionInventory::Agent::Tools; +use FusionInventory::Agent::Tools::Virtualization; sub isEnabled { return canRun('lxc-ls'); @@ -45,9 +46,9 @@ sub _getVirtualMachineState { close $handle; return - $info{state} eq 'RUNNING' ? 'running' : - $info{state} eq 'FROZEN' ? 'paused' : - $info{state} eq 'STOPPED' ? 'off' : + $info{state} eq 'RUNNING' ? STATUS_RUNNING : + $info{state} eq 'FROZEN' ? STATUS_PAUSED : + $info{state} eq 'STOPPED' ? STATUS_OFF : $info{state}; } @@ -111,6 +112,18 @@ sub _getVirtualMachines { logger => $params{logger} ); + my $machineid = ( $status && $status eq STATUS_RUNNING ) ? + getFirstLine( + command => "/usr/bin/lxc-attach -n '$name' -- cat /etc/machine-id", + logger => $params{logger} + ) + : + _getVirtualMachineId( + command => "/usr/bin/lxc-info -n '$name' -c lxc.rootfs", + pattern => qr/^lxc.rootfs\s*=\s*(.+)$/, + logger => $params{logger} + ); + my $config = _getVirtualMachineConfig( file => "/var/lib/lxc/$name/config", logger => $params{logger} @@ -118,10 +131,11 @@ sub _getVirtualMachines { push @machines, { NAME => $name, - VMTYPE => 'LXC', + VMTYPE => 'lxc', STATUS => $status, VCPU => $config->{VCPU}, MEMORY => $config->{MEMORY}, + UUID => getVirtualUUID($machineid, $name) }; } close $handle; @@ -129,4 +143,23 @@ sub _getVirtualMachines { return @machines; } +sub _getVirtualMachineId { + my (%params) = @_; + + my $rootfs = getFirstMatch(%params); + return unless $rootfs; + + if ($rootfs =~ /^overlayfs:/) { + my @overlayfs = split(/:/,$rootfs); + $rootfs = $overlayfs[2]; + } + + return unless -e "$rootfs/etc/machine-id"; + + return getFirstLine( + file => "$rootfs/etc/machine-id", + logger => $params{logger} + ); +} + 1; diff --git a/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Vmsystem.pm b/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Vmsystem.pm index f0da6044e2..8aebe898a7 100644 --- a/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Vmsystem.pm +++ b/lib/FusionInventory/Agent/Task/Inventory/Virtualization/Vmsystem.pm @@ -5,6 +5,7 @@ use warnings; use FusionInventory::Agent::Tools; use FusionInventory::Agent::Tools::Solaris; +use FusionInventory::Agent::Tools::Virtualization; my @vmware_patterns = ( 'Hypervisor detected: VMware', @@ -94,6 +95,21 @@ sub doInventory { $inventory->setHardware({ UUID => $containerid || '' }); $inventory->setBios({ SSN => '' }); + + } elsif ($type ne 'Physical' && !$inventory->getHardware('UUID') && -e '/etc/machine-id') { + # Set UUID from /etc/machine-id & /etc/hostname for container like lxc + my $machineid = getFirstLine( + file => '/etc/machine-id', + logger => $params{logger} + ); + my $hostname = getFirstLine( + file => '/etc/hostname', + logger => $params{logger} + ); + + if ($machineid && $hostname) { + $inventory->setHardware({ UUID => getVirtualUUID($machineid, $hostname) }); + } } $inventory->setHardware({ @@ -216,12 +232,16 @@ sub _getType { } return $result if $result; - if (getFirstMatch( - file => '/proc/1/environ', - pattern => qr/container=lxc/ - )) { - return 'lxc'; - } + # systemd based container like lxc + + my $init_env = slurp('/proc/1/environ'); + $init_env =~ s/\0/\n/g; + my $container_type = getFirstMatch( + string => $init_env, + pattern => qr/^container=(\S+)/, + logger => $logger + ); + return $container_type if $container_type; # OpenVZ if (-f '/proc/self/status') { diff --git a/lib/FusionInventory/Agent/Tools/Virtualization.pm b/lib/FusionInventory/Agent/Tools/Virtualization.pm new file mode 100644 index 0000000000..5927c23d3c --- /dev/null +++ b/lib/FusionInventory/Agent/Tools/Virtualization.pm @@ -0,0 +1,36 @@ +package FusionInventory::Agent::Tools::Virtualization; + +use strict; +use warnings; +use base 'Exporter'; + +use UNIVERSAL::require; + +use constant STATUS_RUNNING => 'running'; +use constant STATUS_BLOCKED => 'blocked'; +use constant STATUS_IDLE => 'idle'; +use constant STATUS_PAUSED => 'paused'; +use constant STATUS_SHUTDOWN => 'shutdown'; +use constant STATUS_CRASHED => 'crashed'; +use constant STATUS_DYING => 'dying'; +use constant STATUS_OFF => 'off'; + +our @EXPORT = qw( + STATUS_RUNNING STATUS_BLOCKED STATUS_IDLE STATUS_PAUSED + STATUS_SHUTDOWN STATUS_CRASHED STATUS_DYING STATUS_OFF + getVirtualUUID +); + +sub getVirtualUUID { + my $machineid = shift + or return ''; + + my $name = shift + or return ''; + + return '' unless Digest::SHA->use('sha1_hex'); + + return sha1_hex($machineid . $name); +} + +1;