Commit 998788ab authored by Francesc Guasch's avatar Francesc Guasch
Browse files

check md5 of ISO images

parent 73aed1cf
......@@ -129,6 +129,7 @@ sub _create_vm_kvm {
sub _refresh_vm_kvm {
my $self = shift;
sleep 1;
for my $n ( 0 .. $#{$self->vm}) {
my $vm = $self->vm->[$n];
next if ref $vm !~ /KVM/i;
......@@ -492,19 +493,23 @@ sub process_requests {
$self->check_vms();
my $sth = $CONNECTOR->dbh->prepare("SELECT id FROM requests "
." WHERE status='requested' OR status = 'retry'");
." WHERE status='requested' OR status like 'retry %'");
$sth->execute;
while (my ($id)= $sth->fetchrow) {
$self->_wait_pids_nohang();
my $req = Ravada::Request->open($id);
warn "executing request ".$req->id." ".$req->status()." ".$req->command
." ".Dumper($req->args) if $DEBUG || $debug;
my ($n_retry) = $req->status() =~ /retry (\d+)/;
$n_retry = 0 if !$n_retry;
eval { $self->_execute($req, $dont_fork) };
my $err = $@;
if ($err =~ /libvirt error code: 38/) {
if ( $req->status() ne 'retry') {
if ( $n_retry < 3) {
warn $req->id." ".$req->command." to retry" if $DEBUG;
$req->status('retry')
$req->status("retry ".++$n_retry)
}
$self->_refresh_vm_kvm();
} else {
......@@ -514,6 +519,8 @@ sub process_requests {
warn "req ".$req->id." , command: ".$req->command." , status: ".$req->status()
." , error: '".($req->error or 'NONE')."'"
if $DEBUG || $debug;
$self->_refresh_vm_kvm() if $req->command =~ /create|remove/i;
}
$sth->finish;
}
......@@ -593,7 +600,7 @@ sub _wait_pids_nohang {
return if !keys %{$self->{pids}};
my $kid = waitpid(-1 , WNOHANG);
return if !$kid;
return if !$kid || $kid == -1;
warn "Kid $kid finished" if $DEBUG;
delete $self->{pids}->{$kid};
......@@ -603,6 +610,8 @@ sub _wait_pids {
my $self = shift;
my $request = shift;
$request->status('waiting for other tasks') if $request;
for my $pid ( keys %{$self->{pids}}) {
$request->status("waiting for pid $pid") if $request;
......@@ -630,7 +639,6 @@ sub _cmd_create {
return $self->_do_cmd_create($request)
if $dont_fork;
$request->status('waiting for other tasks');
$self->_wait_pids($request);
......@@ -650,7 +658,7 @@ sub _cmd_create {
return;
}
sub _cmd_remove {
sub _do_cmd_remove {
my $self = shift;
my $request = shift;
......@@ -664,6 +672,32 @@ sub _cmd_remove {
}
sub _cmd_remove {
my $self = shift;
my $request = shift;
my $dont_fork = shift;
return $self->_do_cmd_remove($request)
if $dont_fork;
$self->_wait_pids($request);
$request->status('forking');
my $pid = fork();
if (!defined $pid) {
$request->status('done');
$request->error("I can't fork");
return;
}
if ($pid == 0 ) {
$self->_do_cmd_remove($request);
exit;
}
$self->_add_pid($pid);
return;
}
sub _cmd_start {
my $self = shift;
my $request = shift;
......
......@@ -2,6 +2,7 @@ package Ravada::VM::KVM;
use Carp qw(croak);
use Data::Dumper;
use Digest::MD5;
use Encode;
use Encode::Locale;
use Fcntl qw(:flock O_WRONLY O_EXCL O_CREAT);
......@@ -405,12 +406,35 @@ sub _iso_name {
my ($iso_name) = $iso->{url} =~ m{.*/(.*)};
my $device = "$DEFAULT_DIR_IMG/$iso_name";
confess "Missing MD5 field on table iso_images FOR $iso->{url}"
if !$iso->{md5};
if (! -e $device || ! -s $device) {
_download_file_external($iso->{url}, $device);
}
confess "Download failed, MD5 missmatched"
if (! _check_md5($device, $iso->{md5}));
return $device;
}
sub _check_md5 {
my ($file, $md5 ) =@_;
my $ctx = Digest::MD5->new;
open my $in,'<',$file or die "$! $file";
$ctx->addfile($in);
my $digest = $ctx->hexdigest;
return 1 if $digest eq $md5;
warn "$file MD5 fails\n"
." got : $digest\n"
."expecting: $md5\n"
;
return 0;
}
sub _download_file_lwp_progress {
my( $data, $response, $proto ) = @_;
print $DOWNLOAD_FH $data; # write data to file
......@@ -458,7 +482,7 @@ sub _download_file_lwp {
sub _download_file_external {
my ($url,$device) = @_;
my @cmd = ("/usr/bin/wget",$url,'-o',$device);
my @cmd = ("/usr/bin/wget",$url,'-O',$device);
my ($in,$out,$err) = @_;
warn join(" ",@cmd)."\n";
run3(\@cmd,\$in,\$out,\$err);
......
INSERT INTO iso_images
(id,name,description,arch,xml,xml_volume,url)
(id,name,description,arch,xml,xml_volume,url,md5)
VALUES(1,'Debian Jessie 32 bits netinst'
,'Debian 8.5.0 Jessie 32 bits (netsinst)'
,'i386'
,'jessie-i386.xml'
,'jessie-volume.xml'
,'http://cdimage.debian.org/cdimage/archive/8.5.0/i386/iso-cd/debian-8.5.0-i386-netinst.iso');
,'http://cdimage.debian.org/cdimage/archive/8.5.0/i386/iso-cd/debian-8.5.0-i386-netinst.iso'
,'b3cd3e2e6a74b93ab41059c47c2b43fd'
);
INSERT INTO iso_images
(id,name,description,arch,xml,xml_volume,url)
VALUES(2,'Ubuntu Trusty 32 bits','Ubuntu 14.04 LTS Trusty 32 bits'
......@@ -23,7 +27,9 @@ VALUES(3,'Ubuntu Xenial Xerus 32 bits','Ubuntu 16.04 LTS Xenial Xerus 32 bits'
,'http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-i386.iso');
INSERT INTO iso_images
(id,name,description,arch,url)
(id,name,description,arch,url,md5)
VALUES(4,'Ubuntu Xenial Xerus 64 bits','Ubuntu 16.04 LTS Xenial Xerus 64 bits'
,'amd64'
,'http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso');
,'http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso'
,'610c4a399df39a78866f9236b8c658da'
);
......@@ -6,6 +6,7 @@ CREATE TABLE `iso_images` (
`xml` varchar(64),
`xml_volume` varchar(64),
`url` varchar(255),
'md5' char(32),
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
);
......@@ -5,6 +5,7 @@ CREATE TABLE `iso_images` (
`arch` char(8),
`xml` varchar(64),
`xml_volume` varchar(64),
`url` varchar(255)
`url` varchar(255),
`md5` char(32)
);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment