[pve-devel] [PATCH 2/3] add targetstorage to vm_start

Alexandre Derumier aderumier at odiso.com
Tue Oct 11 16:45:20 CEST 2016


This will create a new drive for first local drive found,
and start the vm with this new drive.

a nbd server is started in qemu and expose volume to network port

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/API2/Qemu.pm  | 14 ++++++++++-
 PVE/QemuServer.pm | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index acb1412..ff49fff 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -1627,6 +1627,10 @@ __PACKAGE__->register_method({
 	    stateuri => get_standard_option('pve-qm-stateuri'),
 	    migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
 	    machine => get_standard_option('pve-qm-machine'),
+	    targetstorage => get_standard_option('pve-storage-id', {
+		description => "Target storage.",
+		optional => 1,
+	    }),
 	},
     },
     returns => {
@@ -1657,6 +1661,14 @@ __PACKAGE__->register_method({
 	raise_param_exc({ migratedfrom => "Only root may use this option." })
 	    if $migratedfrom && $authuser ne 'root at pam';
 
+	my $targetstorage = extract_param($param, 'targetstorage');
+	raise_param_exc({ migratedfrom => "Only root may use this option." })
+	    if $targetstorage && $authuser ne 'root at pam';
+
+
+	raise_param_exc({ targetstorage => "targetstorage can only by used with migratedfrom." })
+	    if $targetstorage && !$migratedfrom;
+	
 	# read spice ticket from STDIN
 	my $spice_ticket;
 	if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
@@ -1697,7 +1709,7 @@ __PACKAGE__->register_method({
 		syslog('info', "start VM $vmid: $upid\n");
 
 		PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef,
-					  $machine, $spice_ticket);
+					  $machine, $spice_ticket, $targetstorage);
 
 		return;
 	    };
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index f4bb4dd..b80f8f1 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4357,7 +4357,7 @@ sub vmconfig_update_disk {
 
 sub vm_start {
     my ($storecfg, $vmid, $statefile, $skiplock, $migratedfrom, $paused,
-	$forcemachine, $spice_ticket) = @_;
+	$forcemachine, $spice_ticket, $targetstorage) = @_;
 
     PVE::QemuConfig->lock_config($vmid, sub {
 	my $conf = PVE::QemuConfig->load_config($vmid, $migratedfrom);
@@ -4378,6 +4378,57 @@ sub vm_start {
 	# set environment variable useful inside network script
 	$ENV{PVE_MIGRATED_FROM} = $migratedfrom if $migratedfrom;
 
+	my $local_volumes = {};
+
+	if($targetstorage){
+
+	    my $i = 0;
+            foreach_drive($conf, sub {
+                my ($ds, $drive) = @_;
+
+                return if drive_is_cdrom($drive);
+
+                my $volid = $drive->{file};
+
+                return if !$volid;
+
+		my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
+
+		my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+		next if $scfg->{shared};
+		$local_volumes->{$ds} = $volid;
+		$i++;
+		die "Can't online storage migrate more than 1 disk currently" if $i > 1;
+            });
+
+	    my $format = undef;
+
+	    foreach my $opt (sort keys %$local_volumes) {
+
+		my $volid = $local_volumes->{$opt};
+		my $drive = parse_drive($opt, $conf->{$opt});
+
+		my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
+		$storeid = $targetstorage if $targetstorage;
+
+                my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
+                if (!$format) {
+		    $format = $drive->{format} || $defFormat;
+		}
+
+		# test if requested format is supported - else use default
+		my $supported = grep { $_ eq $format } @$validFormats;
+		$format = $defFormat if !$supported;
+		my $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, undef, ($drive->{size}/1024));
+		my $newdrive = $drive;
+		$newdrive->{format} = $format;
+		$newdrive->{file} = $newvolid;
+		$local_volumes->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive);
+		#pass drive to conf for command line
+		$conf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive);
+	    }
+	}
+
 	my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine);
 
 	my $migrate_port = 0;
@@ -4499,6 +4550,24 @@ sub vm_start {
 	    warn $@ if $@;
 	}
 
+	#start nbd server for storage migration
+	if ($targetstorage) {
+	    my $nodename = PVE::INotify::nodename();
+	    my $localip = PVE::Cluster::remote_node_ip($nodename, 1);
+	    $localip = "[$localip]" if Net::IP::ip_is_ipv6($localip);
+	    my $pfamily = PVE::Tools::get_host_address_family($nodename);
+	    $migrate_port = PVE::Tools::next_migrate_port($pfamily);
+	    #fixme
+#	    vm_mon_cmd_nocheck($vmid, "nbd-server-start", addr => { inet => { host => "${localip}", port => "${migrate_port}" } } );
+	    vm_human_monitor_command($vmid, "nbd_server_start ${localip}:${migrate_port}", 1);
+            foreach my $opt (sort keys %$local_volumes) {
+                my $volid = $local_volumes->{$opt};
+		vm_mon_cmd_nocheck($vmid, "nbd-server-add", device => "drive-$opt", writable => JSON::true );
+		my $migrate_storage_uri = "nbd:${localip}:${migrate_port}:exportname=drive-$opt";
+		print "storage migration listens on $migrate_storage_uri volume:$volid\n";
+	    }
+	}
+
 	if ($migratedfrom) {
 
 	    eval {
@@ -4585,7 +4654,7 @@ sub vm_qmp_command {
 }
 
 sub vm_human_monitor_command {
-    my ($vmid, $cmdline) = @_;
+    my ($vmid, $cmdline, $nocheck) = @_;
 
     my $res;
 
@@ -4594,7 +4663,7 @@ sub vm_human_monitor_command {
 	arguments => { 'command-line' => $cmdline},
     };
 
-    return vm_qmp_command($vmid, $cmd);
+    return vm_qmp_command($vmid, $cmd, $nocheck);
 }
 
 sub vm_commandline {
-- 
2.1.4




More information about the pve-devel mailing list