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;
-use PVE::HA::Env::PVE2;
-use PVE::HA::Config;
use PVE::JSONSchema qw(get_standard_option);
use base qw(PVE::RESTHandler);
-use Data::Dumper; # fixme: remove
+BEGIN {
+ if (!$ENV{PVE_GENERATING_DOCS}) {
+ require PVE::HA::Env::PVE2;
+ import PVE::HA::Env::PVE2;
+ require PVE::HA::Config;
+ import PVE::HA::Config;
+ }
+}
__PACKAGE__->register_method ({
subclass => "PVE::API2::LXC::Config",
type => 'boolean',
description => "Ignore errors when extracting the template.",
},
+ 'ssh-public-keys' => {
+ optional => 1,
+ type => 'string',
+ description => "Setup public SSH keys (one key per line, " .
+ "OpenSSH format).",
+ },
}),
},
returns => {
my $password = extract_param($param, 'password');
+ my $ssh_keys = extract_param($param, 'ssh-public-keys');
+ PVE::Tools::validate_ssh_public_keys($ssh_keys) if defined($ssh_keys);
+
my $pool = extract_param($param, 'pool');
if (defined($pool)) {
raise_perm_exc();
}
- PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []);
-
+ my $ostemplate = extract_param($param, 'ostemplate');
my $storage = extract_param($param, 'storage') // 'local';
+ PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []);
+
my $storage_cfg = cfs_read_file("storage.cfg");
- my $ostemplate = extract_param($param, 'ostemplate');
my $archive;
$archive = '-';
die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs});
} else {
- $rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
+ PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, $vmid, $ostemplate);
$archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
}
my $conf = {};
my $no_disk_param = {};
+ my $mp_param = {};
+ my $storage_only_mode = 1;
foreach my $opt (keys %$param) {
my $value = $param->{$opt};
if ($opt eq 'rootfs' || $opt =~ m/^mp\d+$/) {
# allow to use simple numbers (add default storage in that case)
- $param->{$opt} = "$storage:$value" if $value =~ m/^\d+(\.\d+)?$/;
+ if ($value =~ m/^\d+(\.\d+)?$/) {
+ $mp_param->{$opt} = "$storage:$value";
+ } else {
+ $mp_param->{$opt} = $value;
+ }
+ $storage_only_mode = 0;
+ } elsif ($opt =~ m/^unused\d+$/) {
+ warn "ignoring '$opt', cannot create/restore with unused volume\n";
+ delete $param->{$opt};
} else {
$no_disk_param->{$opt} = $value;
}
}
+ die "mount points configured, but 'rootfs' not set - aborting\n"
+ if !$storage_only_mode && !defined($mp_param->{rootfs});
+
# check storage access, activate storage
- PVE::LXC::Config->foreach_mountpoint($param, sub {
+ my $delayed_mp_param = {};
+ PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
my ($ms, $mountpoint) = @_;
my $volid = $mountpoint->{volume};
});
# check/activate default storage
- &$check_and_activate_storage($storage) if !defined($param->{rootfs});
+ &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs});
PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param);
my $code = sub {
&$check_vmid_usage(); # final check after locking
-
+ my $old_conf;
+
+ my $config_fn = PVE::LXC::Config->config_file($vmid);
+ if (-f $config_fn) {
+ die "container exists" if !$restore; # just to be sure
+ $old_conf = PVE::LXC::Config->load_config($vmid);
+ } else {
+ eval {
+ # try to create empty config on local node, we have an flock
+ PVE::LXC::Config->write_config($vmid, {});
+ };
+
+ # another node was faster, abort
+ die "Could not reserve ID $vmid, already taken\n" if $@;
+ }
+
PVE::Cluster::check_cfs_quorum();
my $vollist = [];
eval {
- if (!defined($param->{rootfs})) {
+ if ($storage_only_mode) {
if ($restore) {
- my (undef, $disksize) = PVE::LXC::Create::recover_config($archive);
- die "unable to detect disk size - please specify rootfs (size)\n"
- if !$disksize;
- $disksize /= 1024 * 1024 * 1024; # create_disks expects GB as unit size
- $param->{rootfs} = "$storage:$disksize";
+ (undef, $mp_param) = PVE::LXC::Create::recover_config($archive);
+ die "rootfs configuration could not be recovered, please check and specify manually!\n"
+ if !defined($mp_param->{rootfs});
+ PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
+ my ($ms, $mountpoint) = @_;
+ my $type = $mountpoint->{type};
+ if ($type eq 'volume') {
+ die "unable to detect disk size - please specify $ms (size)\n"
+ if !defined($mountpoint->{size});
+ my $disksize = $mountpoint->{size} / (1024 * 1024 * 1024); # create_disks expects GB as unit size
+ delete $mountpoint->{size};
+ $mountpoint->{volume} = "$storage:$disksize";
+ $mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
+ } else {
+ my $type = $mountpoint->{type};
+ die "restoring rootfs to $type mount is only possible by specifying -rootfs manually!\n"
+ if ($ms eq 'rootfs');
+ die "restoring '$ms' to $type mount is only possible for root\n"
+ if $authuser ne 'root@pam';
+
+ if ($mountpoint->{backup}) {
+ warn "WARNING - unsupported configuration!\n";
+ warn "backup was enabled for $type mount point $ms ('$mountpoint->{mp}')\n";
+ warn "mount point configuration will be restored after archive extraction!\n";
+ warn "contained files will be restored to wrong directory!\n";
+ }
+ delete $mp_param->{$ms}; # actually delay bind/dev mps
+ $delayed_mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
+ }
+ });
} else {
- $param->{rootfs} = "$storage:4"; # defaults to 4GB
+ $mp_param->{rootfs} = "$storage:4"; # defaults to 4GB
}
}
- $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $param, $conf);
+ $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
+
+ if (defined($old_conf)) {
+ # destroy old container volumes
+ PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, {});
+ }
+
+ eval {
+ my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
+ PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors);
- PVE::LXC::Create::create_rootfs($storage_cfg, $vmid, $conf, $archive, $password, $restore, $ignore_unpack_errors);
+ if ($restore) {
+ PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, $authuser ne 'root@pam');
+ } else {
+ my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
+ PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
+ $lxc_setup->post_create_hook($password, $ssh_keys);
+ }
+ };
+ my $err = $@;
+ PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
+ PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::Config->get_vm_volumes($conf));
+ die $err if $err;
# set some defaults
$conf->{hostname} ||= "CT$vmid";
$conf->{memory} ||= 512;
$conf->{swap} //= 512;
+ foreach my $mp (keys %$delayed_mp_param) {
+ $conf->{$mp} = $delayed_mp_param->{$mp};
+ }
PVE::LXC::Config->write_config($vmid, $conf);
};
if (my $err = $@) {
{ subdir => 'config' },
{ subdir => 'status' },
{ subdir => 'vncproxy' },
+ { subdir => 'termproxy' },
{ subdir => 'vncwebsocket' },
{ subdir => 'spiceproxy' },
{ subdir => 'migrate' },
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
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 => {
# 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",
'-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';
push @$cmd, '-c', @$remcmd, @$shcmd;
- run_command($cmd);
+ run_command($cmd, keeplocale => 1);
return;
};
};
}});
+__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',
description => "Use online/live migration.",
optional => 1,
},
+ restart => {
+ type => 'boolean',
+ description => "Use restart migration",
+ optional => 1,
+ },
+ timeout => {
+ type => 'integer',
+ description => "Timeout in seconds for shutdown for restart migration",
+ optional => 1,
+ default => 180,
+ },
force => {
type => 'boolean',
description => "Force migration despite local bind / device" .
- " mounts. WARNING: identical bind / device mounts need to ".
- " be available on the target node.",
+ " mounts. NOTE: deprecated, use 'shared' property of mount point instead.",
optional => 1,
},
},
# try to detect errors early
if (PVE::LXC::check_running($vmid)) {
- die "can't migrate running container without --online\n"
- if !$param->{online};
+ die "can't migrate running container without --online or --restart\n"
+ if !$param->{online} && !$param->{restart};
}
if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
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);
} 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);
}
}});
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'},
PVE::LXC::Config->write_config($vmid, $conf);
# and remove lxc config
- PVE::LXC::update_lxc_config(undef, $vmid, $conf);
+ PVE::LXC::update_lxc_config($vmid, $conf);
return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
};
"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,
PVE::Cluster::check_cfs_quorum();
- my $running = PVE::LXC::check_running($vmid) || 0;
-
- my $clonefn = sub {
-
- # 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);
-
- PVE::LXC::Config->check_lock($conf);
-
- my $verify_running = PVE::LXC::check_running($vmid) || 0;
+ my $conffile;
+ my $newconf = {};
+ my $mountpoints = {};
+ my $fullclone = {};
+ my $vollist = [];
- die "unexpected state change\n" if $verify_running != $running;
+ PVE::LXC::Config->lock_config($vmid, sub {
+ my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk');
+ eval {
+ die "snapshot '$snapname' does not exist\n"
+ if $snapname && !defined($src_conf->{snapshots}->{$snapname});
- die "snapshot '$snapname' does not exist\n"
- if $snapname && !defined( $conf->{snapshots}->{$snapname});
+ my $running = PVE::LXC::check_running($vmid) || 0;
- my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
+ my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf;
- my $conffile = PVE::LXC::Config->config_file($newid);
- die "unable to create CT $newid: config file already exists\n"
- if -f $conffile;
+ $conffile = PVE::LXC::Config->config_file($newid);
+ die "unable to create CT $newid: config file already exists\n"
+ if -f $conffile;
- my $newconf = { lock => 'clone' };
- my $mountpoints = {};
- my $fullclone = {};
- my $vollist = [];
+ foreach my $opt (keys %$src_conf) {
+ next if $opt =~ m/^unused\d+$/;
- foreach my $opt (keys %$oldconf) {
- my $value = $oldconf->{$opt};
+ my $value = $src_conf->{$opt};
- # no need to copy unused images, because VMID(owner) changes anyways
- next if $opt =~ m/^unused\d+$/;
+ 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);
- 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);
+ if ($mp->{type} eq 'volume') {
+ my $volid = $mp->{volume};
+ if ($param->{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);
+ }
- if ($mp->{type} eq 'volume') {
- my $volid = $mp->{volume};
- if ($param->{full}) {
- die "fixme: full clone not implemented";
+ $mountpoints->{$opt} = $mp;
+ push @$vollist, $volid;
- 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);
+ # TODO: allow bind mounts?
+ die "unable to clone mountpint '$opt' (type $mp->{type})\n";
}
-
- $mountpoints->{$opt} = $mp;
- push @$vollist, $volid;
-
} 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};
- }
-
- if ($param->{description}) {
- $newconf->{description} = $param->{description};
- }
- # 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");
+ # Replace the 'disk' lock with a 'create' lock.
+ $newconf->{lock} = 'create';
- my $realcmd = sub {
- my $upid = shift;
+ delete $newconf->{template};
+ if ($param->{hostname}) {
+ $newconf->{hostname} = $param->{hostname};
+ }
- my $newvollist = [];
+ if ($param->{description}) {
+ $newconf->{description} = $param->{description};
+ }
- eval {
- local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
+ # 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;
+ }
+ });
- PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
+ 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);
+ });
+ };
- foreach my $opt (keys %$mountpoints) {
- my $mp = $mountpoints->{$opt};
- my $volid = $mp->{volume};
+ my $realcmd = sub {
+ my ($upid) = @_;
- if ($fullclone->{$opt}) {
- die "fixme: full clone not implemented\n";
- } else {
- print "create linked clone of mountpoint $opt ($volid)\n";
- my $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname);
- push @$newvollist, $newvolid;
- $mp->{volume} = $newvolid;
+ my $newvollist = [];
- $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);
-
- PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
- };
- if (my $err = $@) {
- unlink $conffile;
+ push @$newvollist, $newvolid;
+ $mp->{volume} = $newvolid;
- sleep 1; # some storage 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";
+ $update_conf->($opt, PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs'));
}
- return;
+ PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
+ PVE::LXC::Config->remove_lock($newid, 'create');
};
+ my $err = $@;
+
+ # Unlock the source config in any case:
+ eval { PVE::LXC::Config->remove_lock($vmid, 'disk') };
+ warn $@ if $@;
- PVE::Firewall::clone_vmfw_conf($vmid, $newid);
+ if ($err) {
+ # Now cleanup the config & disks:
+ unlink $conffile;
- return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd);
+ 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);
}});
method => 'PUT',
protected => 1,
proxyto => 'node',
- description => "Resize a container mountpoint.",
+ description => "Resize a container mount point.",
permissions => {
check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1],
},
my (undef, undef, $owner, undef, undef, undef, $format) =
PVE::Storage::parse_volname($storage_cfg, $volid);
- die "can't resize mountpoint owned by another container ($owner)"
+ die "can't resize mount point owned by another container ($owner)"
if $vmid != $owner;
die "can't resize volume: $disk if snapshot exists\n"
$mp->{mp} = '/';
my $use_loopdev = (PVE::LXC::mountpoint_mount_path($mp, $storage_cfg))[1];
$path = PVE::LXC::query_loopdev($path) if $use_loopdev;
- die "internal error: CT running but mountpoint not attached to a loop device"
+ die "internal error: CT running but mount point not attached to a loop device"
if !$path;
PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev;
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;