Skip to content

Commit

Permalink
Support UUID for lxc containers (#307)
Browse files Browse the repository at this point in the history
* Support UUID for lxc containers:
UUID defined as sha1 sum of string containing /etc/machine-id and /etc/hostname contents
  • Loading branch information
g-bougard authored Jun 1, 2017
1 parent b65179f commit 5a3dd3c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
41 changes: 37 additions & 4 deletions lib/FusionInventory/Agent/Task/Inventory/Virtualization/Lxc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use strict;
use warnings;

use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Virtualization;

sub isEnabled {
return canRun('lxc-ls');
Expand Down Expand Up @@ -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};
}

Expand Down Expand Up @@ -111,22 +112,54 @@ 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}
);

push @machines, {
NAME => $name,
VMTYPE => 'LXC',
VMTYPE => 'lxc',
STATUS => $status,
VCPU => $config->{VCPU},
MEMORY => $config->{MEMORY},
UUID => getVirtualUUID($machineid, $name)
};
}
close $handle;

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;
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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') {
Expand Down
36 changes: 36 additions & 0 deletions lib/FusionInventory/Agent/Tools/Virtualization.pm
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 5a3dd3c

Please sign in to comment.