+__PACKAGE__->register_method({
+ name => 'move_vm_disk',
+ path => '{vmid}/move_disk',
+ method => 'POST',
+ protected => 1,
+ proxyto => 'node',
+ description => "Move volume to different storage.",
+ permissions => {
+ description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " .
+ "and 'Datastore.AllocateSpace' permissions on the storage.",
+ check =>
+ [ 'and',
+ ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
+ ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]],
+ ],
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ vmid => get_standard_option('pve-vmid'),
+ disk => {
+ type => 'string',
+ description => "The disk you want to move.",
+ enum => [ PVE::QemuServer::disknames() ],
+ },
+ storage => get_standard_option('pve-storage-id', { description => "Target Storage." }),
+ 'format' => {
+ type => 'string',
+ description => "Target Format.",
+ enum => [ 'raw', 'qcow2', 'vmdk' ],
+ optional => 1,
+ },
+ delete => {
+ type => 'boolean',
+ description => "Delete the original disk after successful copy. By default the original disk is kept as unused disk.",
+ optional => 1,
+ default => 0,
+ },
+ digest => {
+ type => 'string',
+ description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
+ maxLength => 40,
+ optional => 1,
+ },
+ },
+ },
+ returns => {
+ type => 'string',
+ description => "the task ID.",
+ },
+ code => sub {
+ my ($param) = @_;
+
+ my $rpcenv = PVE::RPCEnvironment::get();
+
+ my $authuser = $rpcenv->get_user();
+
+ my $node = extract_param($param, 'node');
+
+ my $vmid = extract_param($param, 'vmid');
+
+ my $digest = extract_param($param, 'digest');
+
+ my $disk = extract_param($param, 'disk');
+
+ my $storeid = extract_param($param, 'storage');
+
+ my $format = extract_param($param, 'format');
+
+ my $storecfg = PVE::Storage::config();
+
+ my $updatefn = sub {
+
+ my $conf = PVE::QemuServer::load_config($vmid);
+
+ die "checksum missmatch (file change by other user?)\n"
+ if $digest && $digest ne $conf->{digest};
+
+ die "disk '$disk' does not exist\n" if !$conf->{$disk};
+
+ my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
+
+ my $old_volid = $drive->{file} || die "disk '$disk' has no associated volume\n";
+
+ die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
+
+ my $oldfmt;
+ my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid);
+ if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){
+ $oldfmt = $1;
+ }
+
+ die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid &&
+ (!$format || !$oldfmt || $oldfmt eq $format);
+
+ PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid");
+
+ my $running = PVE::QemuServer::check_running($vmid);
+
+ PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]);
+
+ my $realcmd = sub {
+
+ my $newvollist = [];
+
+ eval {
+ local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
+
+ my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef,
+ $vmid, $storeid, $format, 1, $newvollist);
+
+ $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive);
+
+ PVE::QemuServer::add_unused_volume($conf, $old_volid) if !$param->{delete};
+
+ PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+ };
+ if (my $err = $@) {
+
+ foreach my $volid (@$newvollist) {
+ eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+ warn $@ if $@;
+ }
+ die "storage migration failed: $err";
+ }
+
+ if ($param->{delete}) {
+ eval { PVE::Storage::vdisk_free($storecfg, $old_volid); };
+ warn $@ if $@;
+ }
+ };
+
+ return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd);
+ };
+
+ return PVE::QemuServer::lock_config($vmid, $updatefn);
+ }});
+