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;
type => 'array',
items => {
type => "object",
- properties => {},
+ properties => $PVE::LXC::vmstatus_return_properties,
},
links => [ { rel => 'child', href => "{vmid}" } ],
},
next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
my $data = $vmstatus->{$vmid};
- $data->{vmid} = $vmid;
push @$res, $data;
}
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 => {
code => sub {
my ($param) = @_;
- my $rpcenv = PVE::RPCEnvironment::get();
+ PVE::Cluster::check_cfs_quorum();
+ my $rpcenv = PVE::RPCEnvironment::get();
my $authuser = $rpcenv->get_user();
my $node = extract_param($param, 'node');
-
my $vmid = extract_param($param, 'vmid');
-
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;
# 'unprivileged' is read-only, so we can't pass it to update_pct_config
my $unprivileged = extract_param($param, 'unprivileged');
-
my $restore = extract_param($param, 'restore');
if ($restore) {
# fixme: limit allowed parameters
-
}
my $force = extract_param($param, 'force');
if (!($same_container_exists && $restore && $force)) {
PVE::Cluster::check_vmid_unused($vmid);
} else {
+ die "can't overwrite running container\n" if PVE::LXC::check_running($vmid);
my $conf = PVE::LXC::Config->load_config($vmid);
PVE::LXC::Config->check_protection($conf, "unable to restore CT $vmid");
}
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)) {
$rpcenv->check_pool_exist($pool);
$rpcenv->check_perm_modify($authuser, "/pool/$pool");
my $storage_cfg = cfs_read_file("storage.cfg");
-
my $archive;
-
if ($ostemplate eq '-') {
die "pipe requires cli environment\n"
if $rpcenv->{type} ne 'cli';
$archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
}
+ my %used_storages;
my $check_and_activate_storage = sub {
my ($sid) = @_;
$rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
PVE::Storage::activate_storage($storage_cfg, $sid);
+ $used_storages{$sid} = 1;
};
my $conf = {};
+ my $is_root = $authuser eq 'root@pam';
+
my $no_disk_param = {};
my $mp_param = {};
my $storage_only_mode = 1;
if ($mountpoint->{type} ne 'volume') { # bind or device
die "Only root can pass arbitrary filesystem paths.\n"
- if $authuser ne 'root@pam';
+ if !$is_root;
} else {
my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
&$check_and_activate_storage($sid);
$conf->{unprivileged} = 1 if $unprivileged;
- my $check_vmid_usage = sub {
- if ($force) {
- die "can't overwrite running container\n"
- if PVE::LXC::check_running($vmid);
- } else {
- PVE::Cluster::check_vmid_unused($vmid);
- }
- };
-
- my $code = sub {
- &$check_vmid_usage(); # final check after locking
- my $old_conf;
+ my $emsg = $restore ? "unable to restore CT $vmid -" : "unable to create CT $vmid -";
- 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, {});
- };
+ eval { PVE::LXC::Config->create_and_lock_config($vmid, $force) };
+ die "$emsg $@" if $@;
- # another node was faster, abort
- die "Could not reserve ID $vmid, already taken\n" if $@;
- }
+ my $code = sub {
+ my $old_conf = PVE::LXC::Config->load_config($vmid);
- PVE::Cluster::check_cfs_quorum();
my $vollist = [];
-
eval {
+ my $orig_mp_param; # only used if $restore
+ if ($restore) {
+ die "can't overwrite running container\n" if PVE::LXC::check_running($vmid);
+ if ($is_root && $archive ne '-') {
+ my $orig_conf;
+ ($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
+ # When we're root call 'restore_configuration' with ristricted=0,
+ # causing it to restore the raw lxc entries, among which there may be
+ # 'lxc.idmap' entries. We need to make sure that the extracted contents
+ # of the container match up with the restored configuration afterwards:
+ $conf->{lxc} = [grep { $_->[0] eq 'lxc.idmap' } @{$orig_conf->{lxc}}];
+ }
+ }
if ($storage_only_mode) {
if ($restore) {
- (undef, $mp_param) = PVE::LXC::Create::recover_config($archive);
+ if (!defined($orig_mp_param)) {
+ (undef, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
+ }
+ $mp_param = $orig_mp_param;
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 {
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 !$is_root;
if ($mountpoint->{backup}) {
warn "WARNING - unsupported configuration!\n";
$vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
- if (defined($old_conf)) {
+ # we always have the 'create' lock so check for more than 1 entry
+ if (scalar(keys %$old_conf) > 1) {
# destroy old container volumes
- PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, {});
+ PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, { lock => 'create' });
}
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');
+ PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root);
} else {
my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
};
if (my $err = $@) {
PVE::LXC::destroy_disks($storage_cfg, $vollist);
- PVE::LXC::destroy_config($vmid);
- die $err;
+ eval { PVE::LXC::destroy_config($vmid) };
+ warn $@ if $@;
+ die "$emsg $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 $workername = $restore ? 'vzrestore' : 'vzcreate';
my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
- &$check_vmid_usage(); # first check before locking
-
- return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate',
- $vmid, $authuser, $realcmd);
-
+ return $rpcenv->fork_worker($workername, $vmid, $authuser, $realcmd);
}});
__PACKAGE__->register_method({
['/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 $concmd = PVE::LXC::get_console_command($vmid, $conf, -1);
my $shcmd = [ '/usr/bin/dtach', '-A',
"/var/run/dtach/vzctlconsole$vmid",
['/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 $concmd = PVE::LXC::get_console_command($vmid, $conf, -1);
my $shcmd = [ '/usr/bin/dtach', '-A',
"/var/run/dtach/vzctlconsole$vmid",
feature => {
description => "Feature to check.",
type => 'string',
- enum => [ 'snapshot' ],
+ enum => [ 'snapshot', 'clone', 'copy' ],
},
snapname => get_standard_option('pve-lxc-snapshot-name', {
optional => 1,
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'},
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);
};
}),
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 => {
my $storage = extract_param($param, 'storage');
- die "Full clone requires a target storage.\n"
- if $param->{full} && !$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 $mountpoints = {};
my $fullclone = {};
my $vollist = [];
+ my $running;
PVE::LXC::Config->lock_config($vmid, sub {
my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk');
+
+ $running = PVE::LXC::check_running($vmid) || 0;
+
+ 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;
+
eval {
die "snapshot '$snapname' does not exist\n"
if $snapname && !defined($src_conf->{snapshots}->{$snapname});
- my $running = PVE::LXC::check_running($vmid) || 0;
my $src_conf = $snapname ? $src_conf->{snapshots}->{$snapname} : $src_conf;
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+$/;
if ($mp->{type} eq 'volume') {
my $volid = $mp->{volume};
- if ($param->{full}) {
+
+ 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;
+ }
+
+ $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
+
+ if ($full) {
die "Cannot do full clones on a running container without snapshots\n"
if $running && !defined($snapname);
$fullclone->{$opt} = 1;
# 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 {
# copy everything else
$newconf->{$opt} = $value;
}
}
+ die "can't clone CT to node '$target' (CT uses local storage)\n"
+ if $target && !$sharedvm;
# Replace the 'disk' lock with a 'create' lock.
$newconf->{lock} = 'create';
my $newvollist = [];
+ my $verify_running = PVE::LXC::check_running($vmid) || 0;
+ die "unexpected state change\n" if $verify_running != $running;
+
eval {
local $SIG{INT} =
local $SIG{TERM} =
my $newvolid;
if ($fullclone->{$opt}) {
print "create full clone of mountpoint $opt ($volid)\n";
- $newvolid = PVE::LXC::copy_volume($mp, $newid, $storage, $storecfg, $newconf, $snapname);
+ 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);
PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
PVE::LXC::Config->remove_lock($newid, 'create');
+
+ 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);
+
+ 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::LXC::Config->write_config($vmid, $conf);
if ($format eq 'raw') {
- my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+ my $path = PVE::Storage::map_volume($storage_cfg, $volid) // PVE::Storage::path($storage_cfg, $volid);
if ($running) {
$mp->{mp} = '/';
PVE::Tools::run_command(['resize2fs', $path]);
};
warn "Failed to update the container's filesystem: $@\n" if $@;
+
+ PVE::Storage::unmap_volume($storage_cfg, $volid);
}
}
};
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;