mirror of
https://git.proxmox.com/git/qemu-server
synced 2025-06-15 09:49:10 +00:00
Fix/cleanup disk hotplug
- pass $conf to create_disks sub - add some checks on results of "drive_del","drive_add","device_del","device_add" - replace "sleep 2" by iterative sub vm_waitfordevicecleanup Signed-off-by: Derumier Alexandre <aderumier@odiso.com>
This commit is contained in:
parent
43779a7baa
commit
f19d1c4790
@ -458,7 +458,7 @@ __PACKAGE__->register_method({
|
|||||||
}
|
}
|
||||||
next if !defined($conf->{$opt});
|
next if !defined($conf->{$opt});
|
||||||
if (PVE::QemuServer::valid_drivename($opt)) {
|
if (PVE::QemuServer::valid_drivename($opt)) {
|
||||||
PVE::QemuServer::vm_devicedel($vmid,$opt);
|
PVE::QemuServer::vm_devicedel($vmid, $conf, $opt);
|
||||||
my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
|
||||||
if (PVE::QemuServer::drive_is_cdrom($drive)) {
|
if (PVE::QemuServer::drive_is_cdrom($drive)) {
|
||||||
$cdchange->{$opt} = undef;
|
$cdchange->{$opt} = undef;
|
||||||
@ -484,7 +484,7 @@ __PACKAGE__->register_method({
|
|||||||
$unset->{$opt} = 1;
|
$unset->{$opt} = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
PVE::QemuServer::create_disks($storecfg, $vmid, $param);
|
PVE::QemuServer::create_disks($storecfg, $vmid, $param, $conf);
|
||||||
|
|
||||||
PVE::QemuServer::change_config_nolock($vmid, $param, $unset, 1);
|
PVE::QemuServer::change_config_nolock($vmid, $param, $unset, 1);
|
||||||
|
|
||||||
|
@ -1265,7 +1265,7 @@ sub touch_config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub create_disks {
|
sub create_disks {
|
||||||
my ($storecfg, $vmid, $settings) = @_;
|
my ($storecfg, $vmid, $settings, $conf) = @_;
|
||||||
|
|
||||||
my $vollist = [];
|
my $vollist = [];
|
||||||
|
|
||||||
@ -1302,7 +1302,7 @@ sub create_disks {
|
|||||||
die "image '$path' does not exists\n";
|
die "image '$path' does not exists\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PVE::QemuServer::vm_deviceadd($storecfg, $vmid, $ds, $disk);
|
PVE::QemuServer::vm_deviceadd($storecfg, $conf, $vmid, $ds, $disk) if defined($conf);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -2273,46 +2273,67 @@ sub vm_devices_list {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub vm_deviceadd {
|
sub vm_deviceadd {
|
||||||
my ($storecfg, $vmid, $deviceid, $device) = @_;
|
my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
|
||||||
|
return if !check_running($vmid) || !$conf->{hotplug} || $conf->{$deviceid};
|
||||||
my $cfspath = cfs_config_path($vmid);
|
|
||||||
my $conf = PVE::Cluster::cfs_read_file($cfspath) || {};
|
|
||||||
|
|
||||||
return if !check_running($vmid) || !$conf->{hotplug};
|
|
||||||
|
|
||||||
if($deviceid =~ m/^(virtio)(\d+)$/) {
|
if($deviceid =~ m/^(virtio)(\d+)$/) {
|
||||||
|
|
||||||
my $drive = print_drive_full($storecfg, $vmid, $device);
|
my $drive = print_drive_full($storecfg, $vmid, $device);
|
||||||
vm_monitor_command($vmid, "drive_add auto $drive", 1);
|
my $ret = vm_monitor_command($vmid, "drive_add auto $drive", 1);
|
||||||
my $devicefull = print_drivedevice_full($storecfg, $vmid, $device);
|
# If the command succeeds qemu prints: "OK"
|
||||||
vm_monitor_command($vmid, "device_add $devicefull", 1);
|
if ($ret !~ m/OK/s) {
|
||||||
|
die "adding drive failed: $ret";
|
||||||
}
|
}
|
||||||
|
|
||||||
#verification
|
my $devicefull = print_drivedevice_full($storecfg, $vmid, $device);
|
||||||
sleep 2; #give a litlle time to os to add the device
|
$ret = vm_monitor_command($vmid, "device_add $devicefull", 1);
|
||||||
my $devices_list = vm_devices_list($vmid);
|
$ret =~ s/^\s+//;
|
||||||
die "error on hotplug device" if !defined($devices_list->{$deviceid});
|
# Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error
|
||||||
|
die 'error on hotplug device : $ret' if $ret ne "";
|
||||||
|
}
|
||||||
|
|
||||||
|
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_devicedel {
|
sub vm_devicedel {
|
||||||
my ($vmid,$deviceid) = @_;
|
my ($vmid, $conf, $deviceid) = @_;
|
||||||
|
|
||||||
my $cfspath = cfs_config_path($vmid);
|
|
||||||
my $conf = PVE::Cluster::cfs_read_file($cfspath) || {};
|
|
||||||
|
|
||||||
return if !check_running ($vmid) || !$conf->{hotplug};
|
return if !check_running ($vmid) || !$conf->{hotplug};
|
||||||
|
|
||||||
|
die "can't unplug bootdisk" if $conf->{bootdisk} eq $deviceid;
|
||||||
|
|
||||||
if($deviceid =~ m/^(virtio)(\d+)$/){
|
if($deviceid =~ m/^(virtio)(\d+)$/){
|
||||||
|
|
||||||
vm_monitor_command($vmid, "drive_del drive-$deviceid", 1);
|
my $ret = vm_monitor_command($vmid, "drive_del drive-$deviceid", 1);
|
||||||
vm_monitor_command($vmid, "device_del $deviceid", 1);
|
$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 "") {
|
||||||
|
die "deleting drive $deviceid failed : $ret";
|
||||||
|
}
|
||||||
|
|
||||||
|
$ret = vm_monitor_command($vmid, "device_del $deviceid", 1);
|
||||||
|
$ret =~ s/^\s+//;
|
||||||
|
die 'detaching device $deviceid failed : $ret' if $ret ne "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep 2;
|
#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++) {
|
||||||
my $devices_list = vm_devices_list($vmid);
|
my $devices_list = vm_devices_list($vmid);
|
||||||
die "error on hot-unplugging device " if defined($devices_list->{$deviceid});
|
return if !defined($devices_list->{$deviceid});
|
||||||
|
sleep 1;
|
||||||
|
}
|
||||||
|
die "error on hot-plugging device $deviceid";
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub vm_start {
|
sub vm_start {
|
||||||
|
Loading…
Reference in New Issue
Block a user