FusionInventory-Agent
view release on metacpan or search on metacpan
lib/FusionInventory/Agent/Task/Inventory/Virtualization/Lxc.pm view on Meta::CPAN
return $container;
}
sub _getVirtualMachines {
my (%params) = @_;
my $version = getFirstMatch(
command => "lxc-ls --version",
pattern => qr/^(\d+\.\d+)/,
%params
);
my $lxcpath = getFirstLine(
command => "lxc-config lxc.lxcpath",
logger => $params{logger}
) || "/var/lib/lxc";
my $handle = getFileHandle(
command => 'lxc-ls -1',
logger => $params{logger}
);
return unless $handle;
my $rootfs_conf = $version < 2.1 ? "lxc.rootfs" : "lxc.rootfs.path";
my $max_cpus = 0;
my @machines;
while(my $name = <$handle>) {
# lxc-ls -1 shows one entry by line
chomp $name;
$name =~ s/\s+$//; # trim trailing whitespace
next unless length($name); # skip if empty as name can contain space
my $container = _getVirtualMachine(
name => $name,
version => $version,
config => "$lxcpath/$name/config",
logger => $params{logger}
);
# Set VCPU to max host cpus count if not set in conf
if (!$container->{VCPU}) {
$max_cpus = getCPUsFromProc(logger => $params{logger})
unless $max_cpus;
$container->{VCPU} = $max_cpus;
}
my ($machineid, $hostname);
if ( $container->{STATUS} && $container->{STATUS} eq STATUS_RUNNING ) {
$machineid = getFirstLine(
command => "lxc-attach -n '$name' -- /bin/cat /etc/machine-id",
logger => $params{logger}
);
$hostname = getFirstLine(
command => "lxc-attach -n '$name' -- /bin/cat /etc/hostname",
logger => $params{logger}
);
} else {
# Try to directly access container filesystem for not powered container
# Works for standard fs or overlay rootfs
my $rootfs = getFirstMatch(
command => "/usr/bin/lxc-info -n '$name' -c $rootfs_conf",
pattern => qr/^lxc\.rootfs.*\s*=\s*(.*)$/,
logger => $params{logger}
);
$rootfs =~ s/.*:// if $rootfs =~ /:/;
if (-e "$rootfs/etc/machine-id" && -e "$rootfs/etc/hostname") {
$machineid = getFirstLine(
file => "$rootfs/etc/machine-id",
logger => $params{logger}
);
$hostname = getFirstLine(
file => "$rootfs/etc/hostname",
logger => $params{logger}
);
}
}
my $uuid = getVirtualUUID($machineid, $hostname);
$container->{UUID} = $uuid if $uuid;
push @machines, $container;
}
close $handle;
return @machines;
}
1;
( run in 0.835 second using v1.01-cache-2.11-cpan-99c4e6809bf )