create qemu_deviceadd, qemu_devicedel, qemu_driveadd, qemu_drivedel, qemu_deviceaddverify, qemu_devicedelverify

and replace code in vm_deviceplug/vm_deviceunplug

Signed-off-by: Derumier Alexandre <aderumier@odiso.com>
This commit is contained in:
Derumier Alexandre 2012-01-20 11:42:05 +01:00 committed by Dietmar Maurer
parent 7ebe888a03
commit 5e5dcb7362

View File

@ -2284,66 +2284,109 @@ sub vm_devices_list {
sub vm_deviceplug { sub vm_deviceplug {
my ($storecfg, $conf, $vmid, $deviceid, $device) = @_; my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
return if !check_running($vmid) || !$conf->{hotplug} || $conf->{$deviceid}; return 1 if !check_running($vmid) || !$conf->{hotplug} || $conf->{$deviceid};
if($deviceid =~ m/^(virtio)(\d+)$/) {
my $drive = print_drive_full($storecfg, $vmid, $device);
my $ret = vm_monitor_command($vmid, "drive_add auto $drive");
# If the command succeeds qemu prints: "OK"
if ($ret !~ m/OK/s) {
die "adding drive failed: $ret";
}
if ($deviceid =~ m/^(virtio)(\d+)$/) {
return undef if !qemu_driveadd($storecfg, $vmid, $device);
my $devicefull = print_drivedevice_full($storecfg, $vmid, $device); my $devicefull = print_drivedevice_full($storecfg, $vmid, $device);
$ret = vm_monitor_command($vmid, "device_add $devicefull"); qemu_deviceadd($vmid, $devicefull);
$ret =~ s/^\s+//; if(!qemu_deviceaddverify($vmid, $deviceid)) {
# Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error qemu_drivedel($vmid, $deviceid);
die 'error on hotplug device : $ret' if $ret ne ""; return undef;
}
} }
return 1;
for (my $i = 0; $i <= 5; $i++) {
my $devices_list = vm_devices_list($vmid);
return if defined($devices_list->{$deviceid});
sleep 1;
}
die "error on hotplug device $deviceid";
} }
sub vm_deviceunplug { sub vm_deviceunplug {
my ($vmid, $conf, $deviceid) = @_; my ($vmid, $conf, $deviceid) = @_;
return if !check_running ($vmid) || !$conf->{hotplug}; return 1 if !check_running ($vmid) || !$conf->{hotplug};
die "can't unplug bootdisk" if $conf->{bootdisk} eq $deviceid; die "can't unplug bootdisk" if $conf->{bootdisk} eq $deviceid;
if($deviceid =~ m/^(virtio)(\d+)$/){ if ($deviceid =~ m/^(virtio)(\d+)$/) {
return undef if !qemu_drivedel($vmid, $deviceid);
my $ret = vm_monitor_command($vmid, "drive_del drive-$deviceid"); qemu_devicedel($vmid, $deviceid);
$ret =~ s/^\s+//; return undef if !qemu_devicedelverify($vmid, $deviceid);
if ($ret =~ m/Device \'.*?\' not found/s) {
# NB: device not found errors mean the drive was auto-deleted and we ignore the error
}
elsif ($ret ne "") {
die "deleting drive $deviceid failed : $ret";
}
$ret = vm_monitor_command($vmid, "device_del $deviceid");
$ret =~ s/^\s+//;
die 'detaching device $deviceid failed : $ret' if $ret ne "";
} }
return 1;
}
sub qemu_deviceadd {
my ($vmid, $devicefull) = @_;
my $ret = vm_monitor_command($vmid, "device_add $devicefull");
$ret =~ s/^\s+//;
# Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error
return 1 if $ret eq "";
syslog("err", "error on hotplug device : $ret");
return undef;
}
sub qemu_devicedel {
my($vmid, $deviceid) = @_;
my $ret = vm_monitor_command($vmid, "device_del $deviceid");
$ret =~ s/^\s+//;
return 1 if $ret eq "";
syslog("err", "detaching device $deviceid failed : $ret");
return undef;
}
sub qemu_driveadd {
my($storecfg, $vmid, $device) = @_;
my $drive = print_drive_full($storecfg, $vmid, $device);
my $ret = vm_monitor_command($vmid, "drive_add auto $drive");
# If the command succeeds qemu prints: "OK"
if ($ret !~ m/OK/s) {
syslog("err", "adding drive failed: $ret");
return undef;
}
return 1;
}
sub qemu_drivedel {
my($vmid, $deviceid) = @_;
my $ret = vm_monitor_command($vmid, "drive_del drive-$deviceid");
$ret =~ s/^\s+//;
if ($ret =~ m/Device \'.*?\' not found/s) {
# NB: device not found errors mean the drive was auto-deleted and we ignore the error
}
elsif ($ret ne "") {
syslog("err", "deleting drive $deviceid failed : $ret");
return undef;
}
return 1;
}
sub qemu_deviceaddverify {
my ($vmid,$deviceid) = @_;
for (my $i = 0; $i <= 5; $i++) {
my $devices_list = vm_devices_list($vmid);
return 1 if defined($devices_list->{$deviceid});
sleep 1;
}
syslog("err", "error on hotplug device $deviceid");
return undef;
}
sub qemu_devicedelverify {
my ($vmid,$deviceid) = @_;
#need to verify the device is correctly remove as device_del is async and empty return is not reliable #need to verify the device is correctly remove as device_del is async and empty return is not reliable
for (my $i = 0; $i <= 5; $i++) { for (my $i = 0; $i <= 5; $i++) {
my $devices_list = vm_devices_list($vmid); my $devices_list = vm_devices_list($vmid);
return if !defined($devices_list->{$deviceid}); return 1 if !defined($devices_list->{$deviceid});
sleep 1; sleep 1;
} }
die "error on hot-plugging device $deviceid"; syslog("err", "error on hot-unplugging device $deviceid");
return undef;
} }
sub vm_start { sub vm_start {