- my $distro = shift;
- my $major_version = shift;
-
- # Based on the distro, take a guess at the package format
- # and package management.
- my ($package_format, $package_management);
- if (defined $distro) {
- if ($distro eq "debian") {
- $package_format = "deb";
- $package_management = "apt";
- }
- elsif ($distro eq "fedora") {
- $package_format = "rpm";
- $package_management = "yum";
- }
- elsif ($distro eq "pardus") {
- $package_format = "pisi";
- $package_management = "pisi";
- }
- elsif ($distro =~ /redhat/ || $distro =~ /rhel/) {
- if ($major_version >= 5) {
- $package_format = "rpm";
- $package_management = "yum";
- } else {
- $package_format = "rpm";
- $package_management = "up2date";
- }
- }
- # else unknown.
- }
-
- $xml->dataElement (package_format => $package_format)
- if defined $package_format;
- $xml->dataElement (package_management => $package_management)
- if defined $package_management;
-
- # Do we know how to get a list of applications?
- if (defined $package_format) {
- if ($package_format eq "rpm") {
- output_applications_rpm ($root);
- }
- elsif ($package_format eq "deb") {
- output_applications_deb ($root);
- }
- }
-}
-
-sub output_applications_rpm
-{
- local $_;
- my $root = shift;
-
- # Previous virt-inspector ran the 'rpm' program from the guest.
- # This is insecure, and unnecessary because we can get the same
- # information directly from the RPM database.
-
- my @applications;
-
- eval {
- my ($fh, $filename) = tempfile (UNLINK => 1);
- my $fddev = "/dev/fd/" . fileno ($fh);
- $g->download ("/var/lib/rpm/Name", $fddev);
- close $fh or die "close: $!";
-
- # Read the database with the Berkeley DB dump tool.
- my $cmd = "db_dump -p '$filename'";
- open PIPE, "$cmd |" or die "close: $!";
- while (<PIPE>) {
- chomp;
- last if /^HEADER=END$/;
- }
- while (<PIPE>) {
- chomp;
- last if /^DATA=END$/;
-
- # First character on each data line is a space.
- if (length $_ > 0 && substr ($_, 0, 1) eq ' ') {
- $_ = substr ($_, 1);
- }
- # Name should never contain non-printable chars.
- die "name contains non-printable chars" if /\\/;
- push @applications, $_;
-
- $_ = <PIPE>; # discard value
- }
- close PIPE or die "close: $!";
- };
- if (!$@ && @applications > 0) {
- @applications = sort @applications;
- $xml->startTag ("applications");
- foreach (@applications) {
- $xml->startTag ("application");
- $xml->dataElement (name => $_);
- $xml->endTag ("application");
- }
- $xml->endTag ("applications");
- }
-}