[ovirt-users] Exporting a VM's disks

John Gardeniers jgardeniers at objectmastery.com
Thu Feb 19 22:33:48 UTC 2015


Hi All,

Having recently had a need to manually export a VM from our RHEV 
environment I searched for a way to identify the VM's disks with the 
files on the storage system. The search led me to 
http://rhevdup.blogspot.com.au/2013/05/manual-export-of-vm-from-rhev.html but 
unfortunately, the script in that post only lists the first drive for a 
VM, making it less than useful for exporting VMs with multiple drives.

Inspired by that post I wrote my own version in Perl (I detest Python). 
The script works on our RHEV 3.4 with a Gluster storage back end. I 
would greatly appreciate it if some of you could test it on other 
RHEV/Ovirt versions and on different storage systems. Thanks for any 
feedback.

regards,
John


#! /usr/bin/env perl

# Run this script on the RHEV/Ovirt engine (management) machine
#
# Find the files on the storage which belong to a VM's disk(s)
# Input parameter: A single VM name (case sensitive)

use DBI;

my $vm = shift or die "No VM specified\n";

my $conf = '/etc/ovirt-engine/engine.conf.d/10-setup-database.conf';
my $host = 'localhost';
my $db;
my $user;
my $pass;
my $port = 5432;

open my $fi, '<', $conf or die "Can't read $conf\n";

while(<$fi>) {
     my $line = $_;
     $host = $1 if($line =~ /ENGINE_DB_HOST="(.*)"/);
     $db = $1 if($line =~ /ENGINE_DB_DATABASE="(.*)"/);
     $user = $1 if($line =~ /ENGINE_DB_USER="(.*)"/);
     $pass = $1 if($line =~ /ENGINE_DB_PASSWORD="(.*)"/);
     $port = $1 if($line =~ /ENGINE_DB_PORT="(.*)"/);
}

die "Unable to get all database details\n" if(!$db || !$user || !$pass);
my $dbh = DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port", $user, 
$pass, {AutoCommit=>1,RaiseError=>1,PrintError=>0});
die "Error connecting to the database\n" if(!$dbh);

# Get the device ID
my $sql = "select device_id from vms_for_disk_view where array_vm_names 
= '{$vm}'";
my @row = $dbh->selectrow_array($sql);
my $vm_id = $row[0];

if($vm_id) {
     print "\nVM ID:      $vm_id\n";

     # Get the drive ID(s)
     $sql = "select image_guid, to_char(size/1024/1024/1024.0, 
'999G999D99'), to_char(actual_size/1024/1024/1024.0, '999G999D99'), 
storage_name from images_storage_domain_view where vm_names = '$vm' and 
entity_type = 'VM'";
     my $sth = $dbh->prepare($sql) || die "Error preparing the SQL 
string \"$sql\"\n$dbh::errstr";
     print "\nError executing $sql\n$DBI::errstr\n\n" unless 
$sth->execute();
     print "Disk ID(s): ";
     my $cnt = 0;

     while (@row = $sth->fetchrow_array) {
         print "            " if($cnt++);
         print "$row[0], $row[1]GB allocated, $row[2]GB actual, on 
$row[3]\n";
     }

     $sth->finish;
     print "\n";
} else {
     print "\nVM \"$vm\" not found. Be aware that the name is case 
sensitive.\n\n";
}

$dbh->disconnect;




More information about the Users mailing list