]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/API2/LXC.pm
close #1940: pct console: added ability to specify escape sequence
[pve-container.git] / src / PVE / API2 / LXC.pm
index 47dcb08acee40debd69403208bc841a905944a6b..6b3395bea1e2d0bbf149fadd5576869b7b97197b 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param run_command);
-use PVE::Exception qw(raise raise_param_exc);
+use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
 use PVE::INotify;
 use PVE::Cluster qw(cfs_read_file);
 use PVE::AccessControl;
@@ -13,9 +13,11 @@ use PVE::Firewall;
 use PVE::Storage;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
+use PVE::ReplicationConfig;
 use PVE::LXC;
 use PVE::LXC::Create;
 use PVE::LXC::Migrate;
+use PVE::GuestHelpers;
 use PVE::API2::LXC::Config;
 use PVE::API2::LXC::Status;
 use PVE::API2::LXC::Snapshot;
@@ -72,7 +74,7 @@ __PACKAGE__->register_method({
        type => 'array',
        items => {
            type => "object",
-           properties => {},
+           properties => $PVE::LXC::vmstatus_return_properties,
        },
        links => [ { rel => 'child', href => "{vmid}" } ],
     },
@@ -89,7 +91,6 @@ __PACKAGE__->register_method({
            next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
 
            my $data = $vmstatus->{$vmid};
-           $data->{vmid} = $vmid;
            push @$res, $data;
        }
 
@@ -159,6 +160,18 @@ __PACKAGE__->register_method({
                description => "Setup public SSH keys (one key per line, " .
                                "OpenSSH format).",
            },
+           bwlimit => {
+               description => "Override i/o bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'number',
+               minimum => '0',
+           },
+           start => {
+               optional => 1,
+               type => 'boolean',
+               default => 0,
+               description => "Start the CT after its creation finished successfully.",
+           },
        }),
     },
     returns => {
@@ -177,6 +190,10 @@ __PACKAGE__->register_method({
 
        my $ignore_unpack_errors = extract_param($param, 'ignore-unpack-errors');
 
+       my $bwlimit = extract_param($param, 'bwlimit');
+
+       my $start_after_create = extract_param($param, 'start');
+
        my $basecfg_fn = PVE::LXC::Config->config_file($vmid);
 
        my $same_container_exists = -f $basecfg_fn;
@@ -245,6 +262,7 @@ __PACKAGE__->register_method({
            $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
        }
 
+       my %used_storages;
        my $check_and_activate_storage = sub {
            my ($sid) = @_;
 
@@ -256,6 +274,8 @@ __PACKAGE__->register_method({
            $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
 
            PVE::Storage::activate_storage($storage_cfg, $sid);
+
+           $used_storages{$sid} = 1;
        };
 
        my $conf = {};
@@ -385,7 +405,8 @@ __PACKAGE__->register_method({
 
                eval {
                    my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
-                   PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors);
+                   $bwlimit = PVE::Storage::get_bandwidth_limit('restore', [keys %used_storages], $bwlimit);
+                   PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit);
 
                    if ($restore) {
                        PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, $authuser ne 'root@pam');
@@ -414,6 +435,9 @@ __PACKAGE__->register_method({
                die $err;
            }
            PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
+
+           PVE::API2::LXC::Status->vm_start({ vmid => $vmid, node => $node })
+               if $start_after_create;
        };
 
        my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
@@ -461,6 +485,7 @@ __PACKAGE__->register_method({
            { subdir => 'config' },
            { subdir => 'status' },
            { subdir => 'vncproxy' },
+           { subdir => 'termproxy' },
            { subdir => 'vncwebsocket' },
            { subdir => 'spiceproxy' },
            { subdir => 'migrate' },
@@ -603,6 +628,10 @@ __PACKAGE__->register_method({
        die "unable to remove CT $vmid - used in HA resources\n"
            if PVE::HA::Config::vm_is_ha_managed($vmid);
 
+       # do not allow destroy if there are replication jobs
+       my $repl_conf = PVE::ReplicationConfig->new();
+       $repl_conf->check_for_existing_jobs($vmid);
+
        my $running_error_msg = "unable to destroy CT $vmid - container is running\n";
 
        die $running_error_msg if PVE::LXC::check_running($vmid); # check early
@@ -645,6 +674,20 @@ __PACKAGE__->register_method ({
                type => 'boolean',
                description => "use websocket instead of standard VNC.",
            },
+           width => {
+               optional => 1,
+               description => "sets the width of the console in pixels.",
+               type => 'integer',
+               minimum => 16,
+               maximum => 4096,
+           },
+           height => {
+               optional => 1,
+               description => "sets the height of the console in pixels.",
+               type => 'integer',
+               minimum => 16,
+               maximum => 2160,
+           },
        },
     },
     returns => {
@@ -687,10 +730,10 @@ __PACKAGE__->register_method ({
        # NOTE: vncterm VNC traffic is already TLS encrypted,
        # so we select the fastest chipher here (or 'none'?)
        my $remcmd = $remip ?
-           ['/usr/bin/ssh', '-t', $remip] : [];
+           ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : [];
 
        my $conf = PVE::LXC::Config->load_config($vmid, $node);
-       my $concmd = PVE::LXC::get_console_command($vmid, $conf);
+       my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1);
 
        my $shcmd = [ '/usr/bin/dtach', '-A',
                      "/var/run/dtach/vzctlconsole$vmid",
@@ -707,6 +750,14 @@ __PACKAGE__->register_method ({
                       '-timeout', $timeout, '-authpath', $authpath,
                       '-perm', 'VM.Console'];
 
+           if ($param->{width}) {
+               push @$cmd, '-width', $param->{width};
+           }
+
+           if ($param->{height}) {
+               push @$cmd, '-height', $param->{height};
+           }
+
            if ($param->{websocket}) {
                $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
                push @$cmd, '-notls', '-listen', 'localhost';
@@ -732,6 +783,89 @@ __PACKAGE__->register_method ({
        };
     }});
 
+__PACKAGE__->register_method ({
+    name => 'termproxy',
+    path => '{vmid}/termproxy',
+    method => 'POST',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
+    },
+    description => "Creates a TCP proxy connection.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => {
+       additionalProperties => 0,
+       properties => {
+           user => { type => 'string' },
+           ticket => { type => 'string' },
+           port => { type => 'integer' },
+           upid => { type => 'string' },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $vmid = $param->{vmid};
+       my $node = $param->{node};
+
+       my $authpath = "/vms/$vmid";
+
+       my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
+
+       my ($remip, $family);
+
+       if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
+           ($remip, $family) = PVE::Cluster::remote_node_ip($node);
+       } else {
+           $family = PVE::Tools::get_host_address_family($node);
+       }
+
+       my $port = PVE::Tools::next_vnc_port($family);
+
+       my $remcmd = $remip ?
+           ['/usr/bin/ssh', '-e', 'none', '-t', $remip, '--'] : [];
+
+       my $conf = PVE::LXC::Config->load_config($vmid, $node);
+       my $concmd = PVE::LXC::get_console_command($vmid, $conf, -1);
+
+       my $shcmd = [ '/usr/bin/dtach', '-A',
+                     "/var/run/dtach/vzctlconsole$vmid",
+                     '-r', 'winch', '-z', @$concmd];
+
+       my $realcmd = sub {
+           my $upid = shift;
+
+           syslog ('info', "starting lxc termproxy $upid\n");
+
+           my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
+                      '--perm', 'VM.Console', '--'];
+           push @$cmd, @$remcmd, @$shcmd;
+
+           PVE::Tools::run_command($cmd);
+       };
+
+       my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
+
+       PVE::Tools::wait_for_vnc_port($port);
+
+       return {
+           user => $authuser,
+           ticket => $ticket,
+           port => $port,
+           upid => $upid,
+       };
+    }});
+
 __PACKAGE__->register_method({
     name => 'vncwebsocket',
     path => '{vmid}/vncwebsocket',
@@ -911,7 +1045,7 @@ __PACKAGE__->register_method({
 
                my $cmd = ['ha-manager', 'migrate', $service, $target];
 
-               print "Executing HA migrate for CT $vmid to node $target\n";
+               print "Requesting HA migration for CT $vmid to node $target\n";
 
                PVE::Tools::run_command($cmd);
 
@@ -923,14 +1057,14 @@ __PACKAGE__->register_method({
        } else {
 
            my $realcmd = sub {
-               my $upid = shift;
-
                PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param);
+           };
 
-               return;
+           my $worker = sub {
+               return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
            };
 
-           return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
+           return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $worker);
        }
     }});
 
@@ -952,7 +1086,7 @@ __PACKAGE__->register_method({
             feature => {
                 description => "Feature to check.",
                 type => 'string',
-                enum => [ 'snapshot' ],
+                enum => [ 'snapshot', 'clone', 'copy' ],
             },
             snapname => get_standard_option('pve-lxc-snapshot-name', {
                 optional => 1,
@@ -1014,12 +1148,6 @@ __PACKAGE__->register_method({
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
-           experimental => {
-               type => 'boolean',
-               description => "The template feature is experimental, set this " .
-                   "flag if you know what you are doing.",
-               default => 0,
-           },
        },
     },
     returns => { type => 'null'},
@@ -1048,15 +1176,24 @@ __PACKAGE__->register_method({
            die "you can't convert a CT to template if the CT is running\n"
                if PVE::LXC::check_running($vmid);
 
+           my $scfg = PVE::Storage::config();
+           PVE::LXC::Config->foreach_mountpoint($conf, sub {
+               my ($ms, $mp) = @_;
+
+               my ($sid) =PVE::Storage::parse_volume_id($mp->{volume}, 0);
+               die "Directory storage '$sid' does not support container templates!\n"
+                   if $scfg->{ids}->{$sid}->{path};
+           });
+
            my $realcmd = sub {
                PVE::LXC::template_create($vmid, $conf);
-           };
 
-           $conf->{template} = 1;
+               $conf->{template} = 1;
 
-           PVE::LXC::Config->write_config($vmid, $conf);
-           # and remove lxc config
-           PVE::LXC::update_lxc_config($vmid, $conf);
+               PVE::LXC::Config->write_config($vmid, $conf);
+               # and remove lxc config
+               PVE::LXC::update_lxc_config($vmid, $conf);
+           };
 
            return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
        };
@@ -1115,26 +1252,18 @@ __PACKAGE__->register_method({
             }),
            storage => get_standard_option('pve-storage-id', {
                description => "Target storage for full clone.",
-               requires => 'full',
                optional => 1,
            }),
            full => {
                optional => 1,
                type => 'boolean',
-               description => "Create a full copy of all disk. This is always done when " .
+               description => "Create a full copy of all disks. This is always done when " .
                    "you clone a normal CT. For CT templates, we try to create a linked clone by default.",
-               default => 0,
            },
-           experimental => {
-               type => 'boolean',
-               description => "The clone feature is experimental, set this " .
-                   "flag if you know what you are doing.",
-               default => 0,
-           },
-#          target => get_standard_option('pve-node', {
-#              description => "Target node. Only allowed if the original VM is on shared storage.",
-#              optional => 1,
-#          }),
+           target => get_standard_option('pve-node', {
+               description => "Target node. Only allowed if the original VM is on shared storage.",
+               optional => 1,
+           }),
         },
     },
     returns => {
@@ -1163,150 +1292,220 @@ __PACKAGE__->register_method({
 
        my $storage = extract_param($param, 'storage');
 
+       my $target = extract_param($param, 'target');
+
         my $localnode = PVE::INotify::nodename();
 
+        undef $target if $target && ($target eq $localnode || $target eq 'localhost');
+
+       PVE::Cluster::check_node_exists($target) if $target;
+
        my $storecfg = PVE::Storage::config();
 
        if ($storage) {
            # check if storage is enabled on local node
            PVE::Storage::storage_check_enabled($storecfg, $storage);
+           if ($target) {
+               # check if storage is available on target node
+               PVE::Storage::storage_check_node($storecfg, $storage, $target);
+               # clone only works if target storage is shared
+               my $scfg = PVE::Storage::storage_config($storecfg, $storage);
+               die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared};
+           }
        }
 
        PVE::Cluster::check_cfs_quorum();
 
-       my $running = PVE::LXC::check_running($vmid) || 0;
+       my $conffile;
+       my $newconf = {};
+       my $mountpoints = {};
+       my $fullclone = {};
+       my $vollist = [];
+       my $running;
 
-       my $clonefn = sub {
+       PVE::LXC::Config->lock_config($vmid, sub {
+           my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk');
 
-           # do all tests after lock
-           # we also try to do all tests before we fork the worker
-           my $conf = PVE::LXC::Config->load_config($vmid);
+           $running = PVE::LXC::check_running($vmid) || 0;
 
-           PVE::LXC::Config->check_lock($conf);
+           my $full = extract_param($param, 'full');
+           if (!defined($full)) {
+               $full = !PVE::LXC::Config->is_template($src_conf);
+           }
+           die "parameter 'storage' not allowed for linked clones\n" if defined($storage) && !$full;
 
-           my $verify_running = PVE::LXC::check_running($vmid) || 0;
+           eval {
+               die "snapshot '$snapname' does not exist\n"
+                   if $snapname && !defined($src_conf->{snapshots}->{$snapname});
 
-           die "unexpected state change\n" if $verify_running != $running;
 
-           die "snapshot '$snapname' does not exist\n"
-               if $snapname && !defined( $conf->{snapshots}->{$snapname});
+               my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf;
 
-           my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
+               $conffile = PVE::LXC::Config->config_file($newid);
+               die "unable to create CT $newid: config file already exists\n"
+                   if -f $conffile;
 
-           my $conffile = PVE::LXC::Config->config_file($newid);
-           die "unable to create CT $newid: config file already exists\n"
-               if -f $conffile;
+               my $sharedvm = 1;
+               foreach my $opt (keys %$src_conf) {
+                   next if $opt =~ m/^unused\d+$/;
 
-           my $newconf = { lock => 'clone' };
-           my $mountpoints = {};
-           my $fullclone = {};
-           my $vollist = [];
+                   my $value = $src_conf->{$opt};
 
-           foreach my $opt (keys %$oldconf) {
-               my $value = $oldconf->{$opt};
+                   if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) {
+                       my $mp = $opt eq 'rootfs' ?
+                           PVE::LXC::Config->parse_ct_rootfs($value) :
+                           PVE::LXC::Config->parse_ct_mountpoint($value);
 
-               # no need to copy unused images, because VMID(owner) changes anyways
-               next if $opt =~ m/^unused\d+$/;
+                       if ($mp->{type} eq 'volume') {
+                           my $volid = $mp->{volume};
 
-               if (($opt eq 'rootfs') || ($opt =~ m/^mp\d+$/)) {
-                   my $mp = $opt eq 'rootfs' ?
-                       PVE::LXC::Config->parse_ct_rootfs($value) :
-                       PVE::LXC::Config->parse_ct_mountpoint($value);
+                           my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
+                           $sid = $storage if defined($storage);
+                           my $scfg = PVE::Storage::storage_config($storecfg, $sid);
+                           if (!$scfg->{shared}) {
+                               $sharedvm = 0;
+                               warn "found non-shared volume: $volid\n" if $target;
+                           }
 
-                   if ($mp->{type} eq 'volume') {
-                       my $volid = $mp->{volume};
-                       if ($param->{full}) {
-                           die "fixme: full clone not implemented";
+                           $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
 
-                           die "Full clone feature for '$volid' is not available\n"
-                               if !PVE::Storage::volume_has_feature($storecfg, 'copy', $volid, $snapname, $running);
-                           $fullclone->{$opt} = 1;
-                       } else {
-                           # not full means clone instead of copy
-                           die "Linked clone feature for '$volid' is not available\n"
-                               if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running);
-                       }
+                           if ($full) {
+                               die "Cannot do full clones on a running container without snapshots\n"
+                                   if $running && !defined($snapname);
+                               $fullclone->{$opt} = 1;
+                           } else {
+                               # not full means clone instead of copy
+                               die "Linked clone feature for '$volid' is not available\n"
+                                   if !PVE::Storage::volume_has_feature($storecfg, 'clone', $volid, $snapname, $running);
+                           }
 
-                       $mountpoints->{$opt} = $mp;
-                       push @$vollist, $volid;
+                           $mountpoints->{$opt} = $mp;
+                           push @$vollist, $volid;
 
+                       } else {
+                           # TODO: allow bind mounts?
+                           die "unable to clone mountpint '$opt' (type $mp->{type})\n";
+                       }
+                   } elsif ($opt =~ m/^net(\d+)$/) {
+                       # always change MAC! address
+                       my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
+                       my $net = PVE::LXC::Config->parse_lxc_network($value);
+                       $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
+                       $newconf->{$opt} = PVE::LXC::Config->print_lxc_network($net);
                    } else {
-                       # TODO: allow bind mounts?
-                       die "unable to clone mountpint '$opt' (type $mp->{type})\n";
+                       # copy everything else
+                       $newconf->{$opt} = $value;
                    }
-
-               } else {
-                   # copy everything else
-                   $newconf->{$opt} = $value;
                }
-           }
-
-           delete $newconf->{template};
-           if ($param->{hostname}) {
-               $newconf->{hostname} = $param->{hostname};
-           }
+               die "can't clone CT to node '$target' (CT uses local storage)\n"
+                   if $target && !$sharedvm;
 
-           if ($param->{description}) {
-               $newconf->{description} = $param->{description};
-           }
+               # Replace the 'disk' lock with a 'create' lock.
+               $newconf->{lock} = 'create';
 
-           # create empty/temp config - this fails if CT already exists on other node
-           PVE::Tools::file_set_contents($conffile, "# ctclone temporary file\nlock: clone\n");
+               delete $newconf->{template};
+               if ($param->{hostname}) {
+                   $newconf->{hostname} = $param->{hostname};
+               }
 
-           my $realcmd = sub {
-               my $upid = shift;
+               if ($param->{description}) {
+                   $newconf->{description} = $param->{description};
+               }
 
-               my $newvollist = [];
+               # create empty/temp config - this fails if CT already exists on other node
+               PVE::LXC::Config->write_config($newid, $newconf);
+           };
+           if (my $err = $@) {
+               eval { PVE::LXC::Config->remove_lock($vmid, 'disk') };
+               warn $@ if $@;
+               die $err;
+           }
+       });
 
-               eval {
-                   local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
+       my $update_conf = sub {
+           my ($key, $value) = @_;
+           return PVE::LXC::Config->lock_config($newid, sub {
+               my $conf = PVE::LXC::Config->load_config($newid);
+               die "Lost 'create' config lock, aborting.\n"
+                   if !PVE::LXC::Config->has_lock($conf, 'create');
+               $conf->{$key} = $value;
+               PVE::LXC::Config->write_config($newid, $conf);
+           });
+       };
 
-                   PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
+       my $realcmd = sub {
+           my ($upid) = @_;
 
-                   foreach my $opt (keys %$mountpoints) {
-                       my $mp = $mountpoints->{$opt};
-                       my $volid = $mp->{volume};
+           my $newvollist = [];
 
-                       if ($fullclone->{$opt}) {
-                           die "fixme: full clone not implemented\n";
-                       } else {
-                           print "create linked clone of mount point $opt ($volid)\n";
-                           my $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname);
-                           push @$newvollist, $newvolid;
-                           $mp->{volume} = $newvolid;
+           my $verify_running = PVE::LXC::check_running($vmid) || 0;
+           die "unexpected state change\n" if $verify_running != $running;
 
-                           $newconf->{$opt} = PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs');
-                           PVE::LXC::Config->write_config($newid, $newconf);
-                       }
+           eval {
+               local $SIG{INT} =
+                   local $SIG{TERM} =
+                   local $SIG{QUIT} =
+                   local $SIG{HUP} = sub { die "interrupted by signal\n"; };
+
+               PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
+
+               foreach my $opt (keys %$mountpoints) {
+                   my $mp = $mountpoints->{$opt};
+                   my $volid = $mp->{volume};
+
+                   my $newvolid;
+                   if ($fullclone->{$opt}) {
+                       print "create full clone of mountpoint $opt ($volid)\n";
+                       my $target_storage = $storage // PVE::Storage::parse_volume_id($volid);
+                       $newvolid = PVE::LXC::copy_volume($mp, $newid, $target_storage, $storecfg, $newconf, $snapname);
+                   } else {
+                       print "create linked clone of mount point $opt ($volid)\n";
+                       $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname);
                    }
 
-                   delete $newconf->{lock};
-                   PVE::LXC::Config->write_config($newid, $newconf);
+                   push @$newvollist, $newvolid;
+                   $mp->{volume} = $newvolid;
 
-                   PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
-               };
-               if (my $err = $@) {
-                   unlink $conffile;
+                   $update_conf->($opt, PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs'));
+               }
 
-                   sleep 1; # some storage like rbd need to wait before release volume - really?
+               PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
+               PVE::LXC::Config->remove_lock($newid, 'create');
 
-                   foreach my $volid (@$newvollist) {
-                       eval { PVE::Storage::vdisk_free($storecfg, $volid); };
-                       warn $@ if $@;
-                   }
-                   die "clone failed: $err";
-               }
+               if ($target) {
+                   # always deactivate volumes - avoid lvm LVs to be active on several nodes
+                   PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running;
+                   PVE::Storage::deactivate_volumes($storecfg, $newvollist);
 
-               return;
+                   my $newconffile = PVE::LXC::Config->config_file($newid, $target);
+                   die "Failed to move config to node '$target' - rename failed: $!\n"
+                       if !rename($conffile, $newconffile);
+               }
            };
+           my $err = $@;
 
-           PVE::Firewall::clone_vmfw_conf($vmid, $newid);
+           # Unlock the source config in any case:
+           eval { PVE::LXC::Config->remove_lock($vmid, 'disk') };
+           warn $@ if $@;
 
-           return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd);
+           if ($err) {
+               # Now cleanup the config & disks:
+               unlink $conffile;
 
+               sleep 1; # some storages like rbd need to wait before release volume - really?
+
+               foreach my $volid (@$newvollist) {
+                   eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+                   warn $@ if $@;
+               }
+               die "clone failed: $err";
+           }
+
+           return;
        };
 
-       return PVE::LXC::Config->lock_config($vmid, $clonefn);
+       PVE::Firewall::clone_vmfw_conf($vmid, $newid);
+       return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd);
     }});
 
 
@@ -1459,4 +1658,156 @@ __PACKAGE__->register_method({
        return PVE::LXC::Config->lock_config($vmid, $code);;
     }});
 
+__PACKAGE__->register_method({
+    name => 'move_volume',
+    path => '{vmid}/move_volume',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Move a rootfs-/mp-volume to a 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', { completion => \&PVE::LXC::complete_ctid }),
+           volume => {
+               type => 'string',
+               enum => [ PVE::LXC::Config->mountpoint_names() ],
+               description => "Volume which will be moved.",
+           },
+           storage => get_standard_option('pve-storage-id', {
+               description => "Target Storage.",
+               completion => \&PVE::Storage::complete_storage_enabled,
+           }),
+           delete => {
+               type => 'boolean',
+               description => "Delete the original volume after successful copy. By default the original is kept as an unused volume entry.",
+               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',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $storage = extract_param($param, 'storage');
+
+       my $mpkey = extract_param($param, 'volume');
+
+       my $lockname = 'disk';
+
+       my ($mpdata, $old_volid);
+
+       PVE::LXC::Config->lock_config($vmid, sub {
+           my $conf = PVE::LXC::Config->load_config($vmid);
+           PVE::LXC::Config->check_lock($conf);
+
+           die "cannot move volumes of a running container\n" if PVE::LXC::check_running($vmid);
+
+           if ($mpkey eq 'rootfs') {
+               $mpdata = PVE::LXC::Config->parse_ct_rootfs($conf->{$mpkey});
+           } elsif ($mpkey =~ m/mp\d+/) {
+               $mpdata = PVE::LXC::Config->parse_ct_mountpoint($conf->{$mpkey});
+           } else {
+               die "Can't parse $mpkey\n";
+           }
+           $old_volid = $mpdata->{volume};
+
+           die "you can't move a volume with snapshots and delete the source\n"
+               if $param->{delete} && PVE::LXC::Config->is_volume_in_use_by_snapshots($conf, $old_volid);
+
+           PVE::Tools::assert_if_modified($param->{digest}, $conf->{digest});
+
+           PVE::LXC::Config->set_lock($vmid, $lockname);
+       });
+
+       my $realcmd = sub {
+           eval {
+               PVE::Cluster::log_msg('info', $authuser, "move volume CT $vmid: move --volume $mpkey --storage $storage");
+
+               my $conf = PVE::LXC::Config->load_config($vmid);
+               my $storage_cfg = PVE::Storage::config();
+
+               my $new_volid;
+
+               eval {
+                   PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]);
+                   $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf);
+                   $mpdata->{volume} = $new_volid;
+
+                   PVE::LXC::Config->lock_config($vmid, sub {
+                       my $digest = $conf->{digest};
+                       $conf = PVE::LXC::Config->load_config($vmid);
+                       PVE::Tools::assert_if_modified($digest, $conf->{digest});
+
+                       $conf->{$mpkey} = PVE::LXC::Config->print_ct_mountpoint($mpdata, $mpkey eq 'rootfs');
+
+                       PVE::LXC::Config->add_unused_volume($conf, $old_volid) if !$param->{delete};
+
+                       PVE::LXC::Config->write_config($vmid, $conf);
+                   });
+
+                   eval {
+                       # try to deactivate volumes - avoid lvm LVs to be active on several nodes
+                       PVE::Storage::deactivate_volumes($storage_cfg, [ $new_volid ])
+                   };
+                   warn $@ if $@;
+               };
+               if (my $err = $@) {
+                   eval {
+                       PVE::Storage::vdisk_free($storage_cfg, $new_volid)
+                           if defined($new_volid);
+                   };
+                   warn $@ if $@;
+                   die $err;
+               }
+
+               if ($param->{delete}) {
+                   eval {
+                       PVE::Storage::deactivate_volumes($storage_cfg, [ $old_volid ]);
+                       PVE::Storage::vdisk_free($storage_cfg, $old_volid);
+                   };
+                   warn $@ if $@;
+               }
+           };
+           my $err = $@;
+           eval { PVE::LXC::Config->remove_lock($vmid, $lockname) };
+           warn $@ if $@;
+           die $err if $err;
+       };
+       my $task = eval {
+           $rpcenv->fork_worker('move_volume', $vmid, $authuser, $realcmd);
+       };
+       if (my $err = $@) {
+           eval { PVE::LXC::Config->remove_lock($vmid, $lockname) };
+           warn $@ if $@;
+           die $err;
+       }
+       return $task;
+  }});
+
 1;