use PVE::INotify;
use PVE::Cluster qw(cfs_read_file);
use PVE::AccessControl;
+use PVE::Firewall;
use PVE::Storage;
use PVE::RESTHandler;
use PVE::RPCEnvironment;
use PVE::LXC;
-use PVE::LXCCreate;
+use PVE::LXC::Create;
+use PVE::LXC::Migrate;
+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
-my $check_ct_modify_config_perm = sub {
- my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
-
- return 1 if $authuser ne 'root@pam';
-
- foreach my $opt (@$key_list) {
-
- if ($opt eq 'cpus' || $opt eq 'cpuunits' || $opt eq 'cpulimit') {
- $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
- } elsif ($opt eq 'disk') {
- $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
- } elsif ($opt eq 'memory' || $opt eq 'swap') {
- $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
- } elsif ($opt =~ m/^net\d+$/ || $opt eq 'nameserver' ||
- $opt eq 'searchdomain' || $opt eq 'hostname') {
- $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
- } else {
- $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
- }
- }
-
- return 1;
-};
-
-my $alloc_rootfs = sub {
- my ($storage_conf, $storage, $disk_size_gb, $vmid) = @_;
-
- my $volid;
-
- my $size = 4*1024*1024; # defaults to 4G
-
- $size = int($disk_size_gb*1024) * 1024 if defined($disk_size_gb);
-
- eval {
- my $scfg = PVE::Storage::storage_config($storage_conf, $storage);
- # fixme: use better naming ct-$vmid-disk-X.raw?
-
- if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs') {
- if ($size > 0) {
- $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'raw',
- undef, $size);
- } else {
- $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'subvol',
- undef, 0);
- }
- } elsif ($scfg->{type} eq 'zfspool') {
-
- $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'subvol',
- undef, $size);
- } elsif ($scfg->{type} eq 'drbd') {
-
- $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'raw', undef, $size);
+__PACKAGE__->register_method ({
+ subclass => "PVE::API2::LXC::Config",
+ path => '{vmid}/config',
+});
- } elsif ($scfg->{type} eq 'rbd') {
+__PACKAGE__->register_method ({
+ subclass => "PVE::API2::LXC::Status",
+ path => '{vmid}/status',
+});
- die "krbd option must be enabled on storage type '$scfg->{type}'\n" if !$scfg->{krbd};
- $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'raw', undef, $size);
+__PACKAGE__->register_method ({
+ subclass => "PVE::API2::LXC::Snapshot",
+ path => '{vmid}/snapshot',
+});
- } else {
- die "unable to create containers on storage type '$scfg->{type}'\n";
- }
- };
- if (my $err = $@) {
- # cleanup
- eval { PVE::Storage::vdisk_free($storage_conf, $volid) if $volid; };
- warn $@ if $@;
- die $err;
- }
-
- return $volid;
-};
-
-PVE::JSONSchema::register_standard_option('pve-lxc-snapshot-name', {
- description => "The name of the snapshot.",
- type => 'string', format => 'pve-configid',
- maxLength => 40,
+__PACKAGE__->register_method ({
+ subclass => "PVE::API2::Firewall::CT",
+ path => '{vmid}/firewall',
});
__PACKAGE__->register_method({
proxyto => 'node',
parameters => {
additionalProperties => 0,
- properties => PVE::LXC::json_config_properties_no_rootfs({
+ properties => PVE::LXC::Config->json_config_properties({
node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
ostemplate => {
description => "The OS template or backup file.",
type => 'string',
maxLength => 255,
+ completion => \&PVE::LXC::complete_os_templates,
},
password => {
optional => 1,
minLength => 5,
},
storage => get_standard_option('pve-storage-id', {
- description => "Target storage.",
+ description => "Default Storage.",
default => 'local',
optional => 1,
+ completion => \&PVE::Storage::complete_storage_enabled,
}),
- size => {
- optional => 1,
- type => 'number',
- description => "Amount of disk space for the VM in GB. A zero indicates no limits.",
- minimum => 0,
- default => 4,
- },
force => {
optional => 1,
type => 'boolean',
type => 'string', format => 'pve-poolid',
description => "Add the VM to the specified pool.",
},
+ 'ignore-unpack-errors' => {
+ optional => 1,
+ 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 $vmid = extract_param($param, 'vmid');
- my $basecfg_fn = PVE::LXC::config_file($vmid);
+ my $ignore_unpack_errors = extract_param($param, 'ignore-unpack-errors');
+
+ 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) {
if (!($same_container_exists && $restore && $force)) {
PVE::Cluster::check_vmid_unused($vmid);
+ } else {
+ 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 $disksize = extract_param($param, 'size');
-
- my $storage = extract_param($param, 'storage') // 'local';
-
- my $storage_cfg = cfs_read_file("storage.cfg");
-
- my $scfg = PVE::Storage::storage_check_node($storage_cfg, $storage, $node);
-
- raise_param_exc({ storage => "storage '$storage' does not support container root directories"})
- if !($scfg->{content}->{images} || $scfg->{content}->{rootdir});
+ 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');
$rpcenv->check_perm_modify($authuser, "/pool/$pool");
}
- $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']);
-
if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
# OK
} elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
raise_perm_exc();
}
- &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
+ PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param, []);
+
+ my $storage = extract_param($param, 'storage') // 'local';
- PVE::Storage::activate_storage($storage_cfg, $storage);
+ my $storage_cfg = cfs_read_file("storage.cfg");
my $ostemplate = extract_param($param, 'ostemplate');
if $rpcenv->{type} ne 'cli';
die "pipe can only be used with restore tasks\n"
if !$restore;
- die "pipe requires --size parameter\n"
- if !defined($disksize);
$archive = '-';
+ die "restore from pipe requires rootfs parameter\n" if !defined($param->{rootfs});
} else {
$rpcenv->check_volume_access($authuser, $storage_cfg, $vmid, $ostemplate);
$archive = PVE::Storage::abs_filesystem_path($storage_cfg, $ostemplate);
}
+ my $check_and_activate_storage = sub {
+ my ($sid) = @_;
+
+ my $scfg = PVE::Storage::storage_check_node($storage_cfg, $sid, $node);
+
+ raise_param_exc({ storage => "storage '$sid' does not support container directories"})
+ if !$scfg->{content}->{rootdir};
+
+ $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
+
+ PVE::Storage::activate_storage($storage_cfg, $sid);
+ };
+
my $conf = {};
- PVE::LXC::update_pct_config($vmid, $conf, 0, $param);
+ 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)
+ 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 "mountpoints configured, but 'rootfs' not set - aborting\n"
+ if !$storage_only_mode && !defined($mp_param->{rootfs});
+
+ # check storage access, activate storage
+ my $delayed_mp_param = {};
+ PVE::LXC::Config->foreach_mountpoint($mp_param, sub {
+ my ($ms, $mountpoint) = @_;
+
+ my $volid = $mountpoint->{volume};
+ my $mp = $mountpoint->{mp};
+
+ if ($mountpoint->{type} ne 'volume') { # bind or device
+ die "Only root can pass arbitrary filesystem paths.\n"
+ if $authuser ne 'root@pam';
+ } else {
+ my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
+ &$check_and_activate_storage($sid);
+ }
+ });
+
+ # check/activate default storage
+ &$check_and_activate_storage($storage) if !defined($mp_param->{rootfs});
+
+ PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $no_disk_param);
+
+ $conf->{unprivileged} = 1 if $unprivileged;
my $check_vmid_usage = sub {
if ($force) {
- die "cant overwrite running container\n"
+ 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
-
- PVE::Cluster::check_cfs_quorum();
+ my $old_conf;
- my $volid;
+ 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($disksize)) {
+ if ($storage_only_mode) {
if ($restore) {
- (undef, $disksize) = PVE::LXCCreate::recover_config($archive);
- die "unable to detect disk size - please specify with --size\n"
- if !$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');
+
+ if ($mountpoint->{backup}) {
+ warn "WARNING - unsupported configuration!\n";
+ warn "backup was enabled for $type mountpoint $ms ('$mountpoint->{mp}')\n";
+ warn "mountpoint configuration will be restored after archive extraction!\n";
+ warn "contained files will be restored to wrong directory!\n";
+ }
+ $delayed_mp_param->{$ms} = PVE::LXC::Config->print_ct_mountpoint($mountpoint, $ms eq 'rootfs');
+ }
+ });
} else {
- $disksize = 4;
+ $mp_param->{rootfs} = "$storage:4"; # defaults to 4GB
}
}
- $volid = &$alloc_rootfs($storage_cfg, $storage, $disksize, $vmid);
- PVE::LXCCreate::create_rootfs($storage_cfg, $storage, $volid, $vmid, $conf,
- $archive, $password, $restore);
+ $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, {});
+ }
- $conf->{rootfs} = PVE::LXC::print_ct_mountpoint({volume => $volid, size => $disksize });
+ eval {
+ my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
+ PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors);
+ if ($restore) {
+ PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf);
+ } 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;
-
- PVE::LXC::create_config($vmid, $conf);
+ foreach my $mp (keys %$delayed_mp_param) {
+ $conf->{$mp} = $delayed_mp_param->{$mp};
+ }
+ PVE::LXC::Config->write_config($vmid, $conf);
};
if (my $err = $@) {
- eval { PVE::Storage::vdisk_free($storage_cfg, $volid) if $volid; };
- warn $@ if $@;
+ PVE::LXC::destroy_disks($storage_cfg, $vollist);
PVE::LXC::destroy_config($vmid);
die $err;
}
PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
};
- my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
+ my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
&$check_vmid_usage(); # first check before locking
}});
-my $vm_config_perm_list = [
- 'VM.Config.Disk',
- 'VM.Config.CPU',
- 'VM.Config.Memory',
- 'VM.Config.Network',
- 'VM.Config.Options',
- ];
-
-__PACKAGE__->register_method({
- name => 'update_vm',
- path => '{vmid}/config',
- method => 'PUT',
- protected => 1,
- proxyto => 'node',
- description => "Set container options.",
- permissions => {
- check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
- },
- parameters => {
- additionalProperties => 0,
- properties => PVE::LXC::json_config_properties(
- {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- delete => {
- type => 'string', format => 'pve-configid-list',
- description => "A list of settings you want to delete.",
- optional => 1,
- },
- 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 => 'null'},
- 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');
-
- die "no options specified\n" if !scalar(keys %$param);
-
- my $delete_str = extract_param($param, 'delete');
- my @delete = PVE::Tools::split_list($delete_str);
-
- &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
-
- foreach my $opt (@delete) {
- raise_param_exc({ delete => "you can't use '-$opt' and " .
- "-delete $opt' at the same time" })
- if defined($param->{$opt});
-
- if (!PVE::LXC::option_exists($opt)) {
- raise_param_exc({ delete => "unknown option '$opt'" });
- }
- }
-
- &$check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
-
- my $storage_cfg = cfs_read_file("storage.cfg");
-
- my $code = sub {
-
- my $conf = PVE::LXC::load_config($vmid);
- PVE::LXC::check_lock($conf);
-
- PVE::Tools::assert_if_modified($digest, $conf->{digest});
-
- my $running = PVE::LXC::check_running($vmid);
-
- PVE::LXC::update_pct_config($vmid, $conf, $running, $param, \@delete);
-
- PVE::LXC::write_config($vmid, $conf);
- PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
- };
-
- PVE::LXC::lock_container($vmid, undef, $code);
-
- return undef;
- }});
-
-__PACKAGE__->register_method ({
- subclass => "PVE::API2::Firewall::CT",
- path => '{vmid}/firewall',
-});
-
__PACKAGE__->register_method({
name => 'vmdiridx',
path => '{vmid}',
my ($param) = @_;
# test if VM exists
- my $conf = PVE::LXC::load_config($param->{vmid});
+ my $conf = PVE::LXC::Config->load_config($param->{vmid});
my $res = [
{ subdir => 'config' },
{ subdir => 'vncwebsocket' },
{ subdir => 'spiceproxy' },
{ subdir => 'migrate' },
+ { subdir => 'clone' },
# { subdir => 'initlog' },
{ subdir => 'rrd' },
{ subdir => 'rrddata' },
{ subdir => 'firewall' },
{ subdir => 'snapshot' },
+ { subdir => 'resize' },
];
return $res;
}});
+
__PACKAGE__->register_method({
name => 'rrd',
path => '{vmid}/rrd',
"pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
}});
-
-__PACKAGE__->register_method({
- name => 'vm_config',
- path => '{vmid}/config',
- method => 'GET',
- proxyto => 'node',
- description => "Get container configuration.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- },
- },
- returns => {
- type => "object",
- properties => {
- digest => {
- type => 'string',
- description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
- }
- },
- },
- code => sub {
- my ($param) = @_;
-
- my $conf = PVE::LXC::load_config($param->{vmid});
-
- delete $conf->{snapshots};
- delete $conf->{lxc};
-
- return $conf;
- }});
-
__PACKAGE__->register_method({
name => 'destroy_vm',
path => '{vmid}',
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
},
},
returns => {
my $vmid = $param->{vmid};
# test if container exists
- my $conf = PVE::LXC::load_config($vmid);
+ my $conf = PVE::LXC::Config->load_config($vmid);
my $storage_cfg = cfs_read_file("storage.cfg");
+ PVE::LXC::Config->check_protection($conf, "can't remove CT $vmid");
+
+ die "unable to remove CT $vmid - used in HA resources\n"
+ if PVE::HA::Config::vm_is_ha_managed($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
+
my $code = sub {
# reload config after lock
- $conf = PVE::LXC::load_config($vmid);
- PVE::LXC::check_lock($conf);
+ $conf = PVE::LXC::Config->load_config($vmid);
+ PVE::LXC::Config->check_lock($conf);
+
+ die $running_error_msg if PVE::LXC::check_running($vmid);
PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf);
PVE::AccessControl::remove_vm_access($vmid);
+ PVE::Firewall::remove_vmfw_conf($vmid);
};
- my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
+ my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
}});
my $remcmd = $remip ?
['/usr/bin/ssh', '-t', $remip] : [];
+ my $conf = PVE::LXC::Config->load_config($vmid, $node);
+ my $concmd = PVE::LXC::get_console_command($vmid, $conf);
+
my $shcmd = [ '/usr/bin/dtach', '-A',
"/var/run/dtach/vzctlconsole$vmid",
- '-r', 'winch', '-z',
- 'lxc-console', '-n', $vmid ];
+ '-r', 'winch', '-z', @$concmd];
my $realcmd = sub {
my $upid = shift;
my $authpath = "/vms/$vmid";
my $permissions = 'VM.Console';
+ my $conf = PVE::LXC::Config->load_config($vmid);
+
+ die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+
+ my $concmd = PVE::LXC::get_console_command($vmid, $conf);
+
my $shcmd = ['/usr/bin/dtach', '-A',
"/var/run/dtach/vzctlconsole$vmid",
- '-r', 'winch', '-z',
- 'lxc-console', '-n', $vmid];
+ '-r', 'winch', '-z', @$concmd];
my $title = "CT $vmid";
return PVE::API2Tools::run_spiceterm($authpath, $permissions, $vmid, $node, $proxy, $title, $shcmd);
}});
+
__PACKAGE__->register_method({
- name => 'vmcmdidx',
- path => '{vmid}/status',
- method => 'GET',
+ name => 'migrate_vm',
+ path => '{vmid}/migrate',
+ method => 'POST',
+ protected => 1,
proxyto => 'node',
- description => "Directory index",
+ description => "Migrate the container to another node. Creates a new migration task.",
permissions => {
- user => 'all',
+ check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
},
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+ target => get_standard_option('pve-node', {
+ description => "Target node.",
+ completion => \&PVE::Cluster::complete_migration_target,
+ }),
+ online => {
+ type => 'boolean',
+ description => "Use online/live migration.",
+ optional => 1,
+ },
+ force => {
+ type => 'boolean',
+ description => "Force migration despite local bind / device" .
+ " mounts. WARNING: identical bind / device mounts need to ".
+ " be available on the target node.",
+ optional => 1,
+ },
},
},
returns => {
- type => 'array',
- items => {
- type => "object",
- properties => {
- subdir => { type => 'string' },
- },
- },
- links => [ { rel => 'child', href => "{subdir}" } ],
+ type => 'string',
+ description => "the task ID.",
},
code => sub {
my ($param) = @_;
- # test if VM exists
- my $conf = PVE::LXC::load_config($param->{vmid});
+ my $rpcenv = PVE::RPCEnvironment::get();
- my $res = [
- { subdir => 'current' },
- { subdir => 'start' },
- { subdir => 'stop' },
- { subdir => 'shutdown' },
- { subdir => 'migrate' },
- ];
+ my $authuser = $rpcenv->get_user();
- return $res;
- }});
+ my $target = extract_param($param, 'target');
-__PACKAGE__->register_method({
- name => 'vm_status',
- path => '{vmid}/status/current',
- method => 'GET',
- proxyto => 'node',
- protected => 1, # openvz /proc entries are only readable by root
- description => "Get virtual machine status.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- },
- },
- returns => { type => 'object' },
- code => sub {
- my ($param) = @_;
+ my $localnode = PVE::INotify::nodename();
+ raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
+
+ PVE::Cluster::check_cfs_quorum();
+
+ PVE::Cluster::check_node_exists($target);
+
+ my $targetip = PVE::Cluster::remote_node_ip($target);
+
+ my $vmid = extract_param($param, 'vmid');
# test if VM exists
- my $conf = PVE::LXC::load_config($param->{vmid});
+ PVE::LXC::Config->load_config($vmid);
- my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
- my $status = $vmstatus->{$param->{vmid}};
+ # try to detect errors early
+ if (PVE::LXC::check_running($vmid)) {
+ die "can't migrate running container without --online\n"
+ if !$param->{online};
+ }
- $status->{ha} = PVE::HA::Config::vm_is_ha_managed($param->{vmid}) ? 1 : 0;
+ if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
- return $status;
- }});
-
-__PACKAGE__->register_method({
- name => 'vm_start',
- path => '{vmid}/status/start',
- method => 'POST',
- protected => 1,
- proxyto => 'node',
- description => "Start the container.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- },
- },
- returns => {
- type => 'string',
- },
- 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');
-
- die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
-
- if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
-
- my $hacmd = sub {
- my $upid = shift;
+ my $hacmd = sub {
+ my $upid = shift;
my $service = "ct:$vmid";
- my $cmd = ['ha-manager', 'enable', $service];
+ my $cmd = ['ha-manager', 'migrate', $service, $target];
- print "Executing HA start for CT $vmid\n";
+ print "Executing HA migrate for CT $vmid to node $target\n";
PVE::Tools::run_command($cmd);
return;
};
- return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
+ return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
} else {
my $realcmd = sub {
my $upid = shift;
- syslog('info', "starting CT $vmid: $upid\n");
-
- my $conf = PVE::LXC::load_config($vmid);
-
- die "you can't start a CT if it's a template\n"
- if PVE::LXC::is_template($conf);
-
- my $storage_cfg = cfs_read_file("storage.cfg");
-
- PVE::LXC::foreach_mountpoint($conf, sub {
- my ($ms, $mountpoint) = @_;
- PVE::Storage::activate_volumes($storage_cfg, [$mountpoint->{volume}]);
- });
-
- PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
-
- my $cmd = ['lxc-start', '-n', $vmid];
-
- run_command($cmd);
+ PVE::LXC::Migrate->migrate($target, $targetip, $vmid, $param);
return;
};
- return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
+ return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
}
}});
__PACKAGE__->register_method({
- name => 'vm_stop',
- path => '{vmid}/status/stop',
- method => 'POST',
- protected => 1,
+ name => 'vm_feature',
+ path => '{vmid}/feature',
+ method => 'GET',
proxyto => 'node',
- description => "Stop the container.",
+ protected => 1,
+ description => "Check if feature for virtual machine is available.",
permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+ check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
},
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
vmid => get_standard_option('pve-vmid'),
+ feature => {
+ description => "Feature to check.",
+ type => 'string',
+ enum => [ 'snapshot' ],
+ },
+ snapname => get_standard_option('pve-lxc-snapshot-name', {
+ optional => 1,
+ }),
},
},
returns => {
- type => 'string',
+ type => "object",
+ properties => {
+ hasFeature => { type => 'boolean' },
+ #nodes => {
+ #type => 'array',
+ #items => { type => 'string' },
+ #}
+ },
},
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');
- die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+ my $snapname = extract_param($param, 'snapname');
- if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
+ my $feature = extract_param($param, 'feature');
- my $hacmd = sub {
- my $upid = shift;
+ my $conf = PVE::LXC::Config->load_config($vmid);
- my $service = "ct:$vmid";
+ if($snapname){
+ my $snap = $conf->{snapshots}->{$snapname};
+ die "snapshot '$snapname' does not exist\n" if !defined($snap);
+ $conf = $snap;
+ }
+ my $storage_cfg = PVE::Storage::config();
+ #Maybe include later
+ #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg);
+ my $hasFeature = PVE::LXC::Config->has_feature($feature, $conf, $storage_cfg, $snapname);
- my $cmd = ['ha-manager', 'disable', $service];
+ return {
+ hasFeature => $hasFeature,
+ #nodes => [ keys %$nodelist ],
+ };
+ }});
+
+__PACKAGE__->register_method({
+ name => 'template',
+ path => '{vmid}/template',
+ method => 'POST',
+ protected => 1,
+ proxyto => 'node',
+ description => "Create a Template.",
+ permissions => {
+ description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
+ check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
+ },
+ parameters => {
+ additionalProperties => 0,
+ 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'},
+ code => sub {
+ my ($param) = @_;
- print "Executing HA stop for CT $vmid\n";
+ my $rpcenv = PVE::RPCEnvironment::get();
- PVE::Tools::run_command($cmd);
+ my $authuser = $rpcenv->get_user();
- return;
- };
+ my $node = extract_param($param, 'node');
- return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
+ my $vmid = extract_param($param, 'vmid');
- } else {
+ my $updatefn = sub {
- my $realcmd = sub {
- my $upid = shift;
+ my $conf = PVE::LXC::Config->load_config($vmid);
+ PVE::LXC::Config->check_lock($conf);
- syslog('info', "stoping CT $vmid: $upid\n");
+ die "unable to create template, because CT contains snapshots\n"
+ if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
- my $conf = PVE::LXC::load_config($vmid);
+ die "you can't convert a template to a template\n"
+ if PVE::LXC::Config->is_template($conf);
- my $storage_cfg = PVE::Storage::config();
+ die "you can't convert a CT to template if the CT is running\n"
+ if PVE::LXC::check_running($vmid);
- my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+ my $realcmd = sub {
+ PVE::LXC::template_create($vmid, $conf);
+ };
- run_command($cmd);
+ $conf->{template} = 1;
- PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
+ PVE::LXC::Config->write_config($vmid, $conf);
+ # and remove lxc config
+ PVE::LXC::update_lxc_config(undef, $vmid, $conf);
- return;
- };
+ return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
+ };
- return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
- }
+ PVE::LXC::Config->lock_config($vmid, $updatefn);
+
+ return undef;
}});
__PACKAGE__->register_method({
- name => 'vm_shutdown',
- path => '{vmid}/status/shutdown',
+ name => 'clone_vm',
+ path => '{vmid}/clone',
method => 'POST',
protected => 1,
proxyto => 'node',
- description => "Shutdown the container.",
+ description => "Create a container clone/copy",
permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+ description => "You need 'VM.Clone' permissions on /vms/{vmid}, " .
+ "and 'VM.Allocate' permissions " .
+ "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
+ "'Datastore.AllocateSpace' on any used storage.",
+ check =>
+ [ 'and',
+ ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
+ [ 'or',
+ [ 'perm', '/vms/{newid}', ['VM.Allocate']],
+ [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
+ ],
+ ]
},
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- timeout => {
- description => "Wait maximal timeout seconds.",
- type => 'integer',
- minimum => 0,
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+ newid => get_standard_option('pve-vmid', {
+ completion => \&PVE::Cluster::complete_next_vmid,
+ description => 'VMID for the clone.' }),
+ hostname => {
optional => 1,
- default => 60,
+ type => 'string', format => 'dns-name',
+ description => "Set a hostname for the new CT.",
},
- forceStop => {
- description => "Make sure the Container stops.",
- type => 'boolean',
+ description => {
+ optional => 1,
+ type => 'string',
+ description => "Description for the new CT.",
+ },
+ pool => {
+ optional => 1,
+ type => 'string', format => 'pve-poolid',
+ description => "Add the new CT to the specified pool.",
+ },
+ snapname => get_standard_option('pve-lxc-snapshot-name', {
+ optional => 1,
+ }),
+ 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 " .
+ "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,
+# }),
+ },
},
returns => {
type => 'string',
my $rpcenv = PVE::RPCEnvironment::get();
- my $authuser = $rpcenv->get_user();
+ my $authuser = $rpcenv->get_user();
my $node = extract_param($param, 'node');
my $vmid = extract_param($param, 'vmid');
- my $timeout = extract_param($param, 'timeout');
+ my $newid = extract_param($param, 'newid');
- die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+ my $pool = extract_param($param, 'pool');
- my $realcmd = sub {
- my $upid = shift;
+ if (defined($pool)) {
+ $rpcenv->check_pool_exist($pool);
+ }
- syslog('info', "shutdown CT $vmid: $upid\n");
+ my $snapname = extract_param($param, 'snapname');
- my $cmd = ['lxc-stop', '-n', $vmid];
+ my $storage = extract_param($param, 'storage');
- $timeout = 60 if !defined($timeout);
+ my $localnode = PVE::INotify::nodename();
- my $conf = PVE::LXC::load_config($vmid);
+ my $storecfg = PVE::Storage::config();
- my $storage_cfg = PVE::Storage::config();
+ if ($storage) {
+ # check if storage is enabled on local node
+ PVE::Storage::storage_check_enabled($storecfg, $storage);
+ }
- push @$cmd, '--timeout', $timeout;
+ PVE::Cluster::check_cfs_quorum();
- eval { run_command($cmd, timeout => $timeout+5); };
- my $err = $@;
- if ($err && $param->{forceStop}) {
- $err = undef;
- warn "shutdown failed - forcing stop now\n";
+ my $running = PVE::LXC::check_running($vmid) || 0;
- push @$cmd, '--kill';
- run_command($cmd);
-
- }
+ my $clonefn = sub {
- PVE::LXC::vm_stop_cleanup($storage_cfg, $vmid, $conf);
-
- die $err if !$err;
+ # 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);
- return;
- };
+ PVE::LXC::Config->check_lock($conf);
- my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
+ my $verify_running = PVE::LXC::check_running($vmid) || 0;
- return $upid;
- }});
+ die "unexpected state change\n" if $verify_running != $running;
-__PACKAGE__->register_method({
- name => 'vm_suspend',
- path => '{vmid}/status/suspend',
- method => 'POST',
- protected => 1,
- proxyto => 'node',
- description => "Suspend the container.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- },
- },
- returns => {
- type => 'string',
- },
- code => sub {
- my ($param) = @_;
+ die "snapshot '$snapname' does not exist\n"
+ if $snapname && !defined( $conf->{snapshots}->{$snapname});
- my $rpcenv = PVE::RPCEnvironment::get();
+ my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
- my $authuser = $rpcenv->get_user();
+ my $conffile = PVE::LXC::Config->config_file($newid);
+ die "unable to create CT $newid: config file already exists\n"
+ if -f $conffile;
- my $node = extract_param($param, 'node');
+ my $newconf = { lock => 'clone' };
+ my $mountpoints = {};
+ my $fullclone = {};
+ my $vollist = [];
- my $vmid = extract_param($param, 'vmid');
+ foreach my $opt (keys %$oldconf) {
+ my $value = $oldconf->{$opt};
- die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
+ # no need to copy unused images, because VMID(owner) changes anyways
+ next if $opt =~ m/^unused\d+$/;
- my $realcmd = sub {
- my $upid = shift;
+ 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);
- syslog('info', "suspend CT $vmid: $upid\n");
+ if ($mp->{type} eq 'volume') {
+ my $volid = $mp->{volume};
+ if ($param->{full}) {
+ die "fixme: full clone not implemented";
- my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
+ 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);
+ }
- run_command($cmd);
+ $mountpoints->{$opt} = $mp;
+ push @$vollist, $volid;
- return;
- };
+ } else {
+ # TODO: allow bind mounts?
+ die "unable to clone mountpint '$opt' (type $mp->{type})\n";
+ }
- my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
+ } else {
+ # copy everything else
+ $newconf->{$opt} = $value;
+ }
+ }
- return $upid;
- }});
+ delete $newconf->{template};
+ if ($param->{hostname}) {
+ $newconf->{hostname} = $param->{hostname};
+ }
-__PACKAGE__->register_method({
- name => 'vm_resume',
- path => '{vmid}/status/resume',
- method => 'POST',
- protected => 1,
- proxyto => 'node',
- description => "Resume the container.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- },
- },
- returns => {
- type => 'string',
- },
- code => sub {
- my ($param) = @_;
+ if ($param->{description}) {
+ $newconf->{description} = $param->{description};
+ }
- my $rpcenv = PVE::RPCEnvironment::get();
+ # 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");
- my $authuser = $rpcenv->get_user();
+ my $realcmd = sub {
+ my $upid = shift;
- my $node = extract_param($param, 'node');
+ my $newvollist = [];
- my $vmid = extract_param($param, 'vmid');
+ eval {
+ local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
- die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
+ PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
- my $realcmd = sub {
- my $upid = shift;
+ foreach my $opt (keys %$mountpoints) {
+ my $mp = $mountpoints->{$opt};
+ my $volid = $mp->{volume};
- syslog('info', "resume CT $vmid: $upid\n");
+ 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 $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
- '-D', '/var/liv/vz/dump'];
+ $newconf->{$opt} = PVE::LXC::Config->print_ct_mountpoint($mp, $opt eq 'rootfs');
+ PVE::LXC::Config->write_config($newid, $newconf);
+ }
+ }
- run_command($cmd);
+ 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;
+
+ 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";
+ }
+
+ return;
+ };
- return;
- };
+ PVE::Firewall::clone_vmfw_conf($vmid, $newid);
- my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
+ return $rpcenv->fork_worker('vzclone', $vmid, $authuser, $realcmd);
+
+ };
- return $upid;
+ return PVE::LXC::Config->lock_config($vmid, $clonefn);
}});
+
__PACKAGE__->register_method({
- name => 'migrate_vm',
- path => '{vmid}/migrate',
- method => 'POST',
+ name => 'resize_vm',
+ path => '{vmid}/resize',
+ method => 'PUT',
protected => 1,
proxyto => 'node',
- description => "Migrate the container to another node. Creates a new migration task.",
+ description => "Resize a container mountpoint.",
permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
+ check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1],
},
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- target => get_standard_option('pve-node', { description => "Target node." }),
- online => {
- type => 'boolean',
- description => "Use online/live migration.",
- optional => 1,
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+ disk => {
+ type => 'string',
+ description => "The disk you want to resize.",
+ enum => [PVE::LXC::Config->mountpoint_names()],
},
- },
- },
- returns => {
- type => 'string',
- description => "the task ID.",
- },
- code => sub {
- my ($param) = @_;
-
- my $rpcenv = PVE::RPCEnvironment::get();
-
- my $authuser = $rpcenv->get_user();
-
- my $target = extract_param($param, 'target');
-
- my $localnode = PVE::INotify::nodename();
- raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
-
- PVE::Cluster::check_cfs_quorum();
-
- PVE::Cluster::check_node_exists($target);
-
- my $targetip = PVE::Cluster::remote_node_ip($target);
-
- my $vmid = extract_param($param, 'vmid');
-
- # test if VM exists
- PVE::LXC::load_config($vmid);
-
- # try to detect errors early
- if (PVE::LXC::check_running($vmid)) {
- die "cant migrate running container without --online\n"
- if !$param->{online};
- }
-
- if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
-
- my $hacmd = sub {
- my $upid = shift;
-
- my $service = "ct:$vmid";
-
- my $cmd = ['ha-manager', 'migrate', $service, $target];
-
- print "Executing HA migrate for CT $vmid to node $target\n";
-
- PVE::Tools::run_command($cmd);
-
- return;
- };
-
- return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
-
- } else {
-
- my $realcmd = sub {
- my $upid = shift;
-
- # fixme: implement lxc container migration
- die "lxc container migration not implemented\n";
-
- return;
- };
-
- return $rpcenv->fork_worker('vzmigrate', $vmid, $authuser, $realcmd);
- }
- }});
-
-__PACKAGE__->register_method({
- name => 'snapshot',
- path => '{vmid}/snapshot',
- method => 'POST',
- protected => 1,
- proxyto => 'node',
- description => "Snapshot a container.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- snapname => get_standard_option('pve-lxc-snapshot-name'),
-# vmstate => {
-# optional => 1,
-# type => 'boolean',
-# description => "Save the vmstate",
-# },
- description => {
- optional => 1,
+ size => {
type => 'string',
- description => "A textual description or comment.",
+ pattern => '\+?\d+(\.\d+)?[KMGT]?',
+ description => "The new size. With the '+' sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.",
},
- },
- },
- 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 $snapname = extract_param($param, 'snapname');
-
- die "unable to use snapshot name 'current' (reserved name)\n"
- if $snapname eq 'current';
-
- my $realcmd = sub {
- PVE::Cluster::log_msg('info', $authuser, "snapshot container $vmid: $snapname");
- PVE::LXC::snapshot_create($vmid, $snapname, $param->{description});
- };
-
- return $rpcenv->fork_worker('pctsnapshot', $vmid, $authuser, $realcmd);
- }});
-
-__PACKAGE__->register_method({
- name => 'delsnapshot',
- path => '{vmid}/snapshot/{snapname}',
- method => 'DELETE',
- protected => 1,
- proxyto => 'node',
- description => "Delete a LXC snapshot.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- snapname => get_standard_option('pve-lxc-snapshot-name'),
- force => {
+ 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,
- type => 'boolean',
- description => "For removal from config file, even if removing disk snapshots fails.",
- },
- },
- },
- 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 $snapname = extract_param($param, 'snapname');
-
- my $realcmd = sub {
- PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
- PVE::LXC::snapshot_delete($vmid, $snapname, $param->{force});
- };
-
- return $rpcenv->fork_worker('lxcdelsnapshot', $vmid, $authuser, $realcmd);
- }});
-
-__PACKAGE__->register_method({
- name => 'rollback',
- path => '{vmid}/snapshot/{snapname}/rollback',
- method => 'POST',
- protected => 1,
- proxyto => 'node',
- description => "Rollback LXC state to specified snapshot.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- snapname => get_standard_option('pve-lxc-snapshot-name'),
+ }
},
},
returns => {
my $vmid = extract_param($param, 'vmid');
- my $snapname = extract_param($param, 'snapname');
-
- my $realcmd = sub {
- PVE::Cluster::log_msg('info', $authuser, "rollback snapshot LXC $vmid: $snapname");
- PVE::LXC::snapshot_rollback($vmid, $snapname);
- };
-
- return $rpcenv->fork_worker('lxcrollback', $vmid, $authuser, $realcmd);
- }});
-
-__PACKAGE__->register_method({
- name => 'snapshot_list',
- path => '{vmid}/snapshot',
- method => 'GET',
- description => "List all snapshots.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
- },
- proxyto => 'node',
- protected => 1, # lxc pid files are only readable by root
- parameters => {
- additionalProperties => 0,
- properties => {
- vmid => get_standard_option('pve-vmid'),
- node => get_standard_option('pve-node'),
- },
- },
- returns => {
- type => 'array',
- items => {
- type => "object",
- properties => {},
- },
- links => [ { rel => 'child', href => "{name}" } ],
- },
- code => sub {
- my ($param) = @_;
-
- my $vmid = $param->{vmid};
-
- my $conf = PVE::LXC::load_config($vmid);
- my $snaphash = $conf->{snapshots} || {};
-
- my $res = [];
-
- foreach my $name (keys %$snaphash) {
- my $d = $snaphash->{$name};
- my $item = {
- name => $name,
- snaptime => $d->{snaptime} || 0,
- description => $d->{description} || '',
- };
- $item->{parent} = $d->{parent} if defined($d->{parent});
- $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
- push @$res, $item;
- }
-
- my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
- my $current = { name => 'current', digest => $conf->{digest}, running => $running };
- $current->{parent} = $conf->{parent} if defined($conf->{parent});
-
- push @$res, $current;
-
- return $res;
- }});
-
-__PACKAGE__->register_method({
- name => 'snapshot_cmd_idx',
- path => '{vmid}/snapshot/{snapname}',
- description => '',
- method => 'GET',
- permissions => {
- user => 'all',
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- vmid => get_standard_option('pve-vmid'),
- node => get_standard_option('pve-node'),
- snapname => get_standard_option('pve-lxc-snapshot-name'),
- },
- },
- returns => {
- type => 'array',
- items => {
- type => "object",
- properties => {},
- },
- links => [ { rel => 'child', href => "{cmd}" } ],
- },
- code => sub {
- my ($param) = @_;
-
- my $res = [];
-
- push @$res, { cmd => 'rollback' };
- push @$res, { cmd => 'config' };
-
- return $res;
- }});
-
-__PACKAGE__->register_method({
- name => 'update_snapshot_config',
- path => '{vmid}/snapshot/{snapname}/config',
- method => 'PUT',
- protected => 1,
- proxyto => 'node',
- description => "Update snapshot metadata.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- snapname => get_standard_option('pve-lxc-snapshot-name'),
- description => {
- optional => 1,
- type => 'string',
- description => "A textual description or comment.",
- },
- },
- },
- returns => { type => 'null' },
- code => sub {
- my ($param) = @_;
-
- my $rpcenv = PVE::RPCEnvironment::get();
-
- my $authuser = $rpcenv->get_user();
-
- my $vmid = extract_param($param, 'vmid');
-
- my $snapname = extract_param($param, 'snapname');
-
- return undef if !defined($param->{description});
-
- my $updatefn = sub {
-
- my $conf = PVE::LXC::load_config($vmid);
- PVE::LXC::check_lock($conf);
-
- my $snap = $conf->{snapshots}->{$snapname};
-
- die "snapshot '$snapname' does not exist\n" if !defined($snap);
-
- $snap->{description} = $param->{description} if defined($param->{description});
-
- PVE::LXC::write_config($vmid, $conf, 1);
- };
-
- PVE::LXC::lock_container($vmid, 10, $updatefn);
-
- return undef;
- }});
-
-__PACKAGE__->register_method({
- name => 'get_snapshot_config',
- path => '{vmid}/snapshot/{snapname}/config',
- method => 'GET',
- proxyto => 'node',
- description => "Get snapshot configuration",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- snapname => get_standard_option('pve-lxc-snapshot-name'),
- },
- },
- returns => { type => "object" },
- code => sub {
- my ($param) = @_;
-
- my $rpcenv = PVE::RPCEnvironment::get();
-
- my $authuser = $rpcenv->get_user();
-
- my $vmid = extract_param($param, 'vmid');
-
- my $snapname = extract_param($param, 'snapname');
-
- my $conf = PVE::LXC::load_config($vmid);
-
- my $snap = $conf->{snapshots}->{$snapname};
-
- die "snapshot '$snapname' does not exist\n" if !defined($snap);
+ my $digest = extract_param($param, 'digest');
- delete $snap->{lxc};
-
- return $snap;
- }});
+ my $sizestr = extract_param($param, 'size');
+ my $ext = ($sizestr =~ s/^\+//);
+ my $newsize = PVE::JSONSchema::parse_size($sizestr);
+ die "invalid size string" if !defined($newsize);
-__PACKAGE__->register_method({
- name => 'vm_feature',
- path => '{vmid}/feature',
- method => 'GET',
- proxyto => 'node',
- protected => 1,
- description => "Check if feature for virtual machine is available.",
- permissions => {
- check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- feature => {
- description => "Feature to check.",
- type => 'string',
- enum => [ 'snapshot' ],
- },
- snapname => get_standard_option('pve-lxc-snapshot-name', {
- optional => 1,
- }),
- },
- },
- returns => {
- type => "object",
- properties => {
- hasFeature => { type => 'boolean' },
- #nodes => {
- #type => 'array',
- #items => { type => 'string' },
- #}
- },
- },
- code => sub {
- my ($param) = @_;
+ die "no options specified\n" if !scalar(keys %$param);
- my $node = extract_param($param, 'node');
+ PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, $param, []);
- my $vmid = extract_param($param, 'vmid');
+ my $storage_cfg = cfs_read_file("storage.cfg");
- my $snapname = extract_param($param, 'snapname');
+ my $code = sub {
- my $feature = extract_param($param, 'feature');
+ my $conf = PVE::LXC::Config->load_config($vmid);
+ PVE::LXC::Config->check_lock($conf);
- my $conf = PVE::LXC::load_config($vmid);
+ PVE::Tools::assert_if_modified($digest, $conf->{digest});
- if($snapname){
- my $snap = $conf->{snapshots}->{$snapname};
- die "snapshot '$snapname' does not exist\n" if !defined($snap);
- $conf = $snap;
- }
- my $storage_cfg = PVE::Storage::config();
- #Maybe include later
- #my $nodelist = PVE::LXC::shared_nodes($conf, $storage_cfg);
- my $hasFeature = PVE::LXC::has_feature($feature, $conf, $storage_cfg, $snapname);
+ my $running = PVE::LXC::check_running($vmid);
- return {
- hasFeature => $hasFeature,
- #nodes => [ keys %$nodelist ],
- };
- }});
+ my $disk = $param->{disk};
+ my $mp = $disk eq 'rootfs' ? PVE::LXC::Config->parse_ct_rootfs($conf->{$disk}) :
+ PVE::LXC::Config->parse_ct_mountpoint($conf->{$disk});
-__PACKAGE__->register_method({
- name => 'template',
- path => '{vmid}/template',
- method => 'POST',
- protected => 1,
- proxyto => 'node',
- description => "Create a Template.",
- permissions => {
- description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
- check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
- },
- parameters => {
- additionalProperties => 0,
- properties => {
- node => get_standard_option('pve-node'),
- vmid => get_standard_option('pve-vmid'),
- },
- },
- returns => { type => 'null'},
- code => sub {
- my ($param) = @_;
+ my $volid = $mp->{volume};
- my $rpcenv = PVE::RPCEnvironment::get();
+ my (undef, undef, $owner, undef, undef, undef, $format) =
+ PVE::Storage::parse_volname($storage_cfg, $volid);
- my $authuser = $rpcenv->get_user();
+ die "can't resize mountpoint owned by another container ($owner)"
+ if $vmid != $owner;
- my $node = extract_param($param, 'node');
+ die "can't resize volume: $disk if snapshot exists\n"
+ if %{$conf->{snapshots}} && $format eq 'qcow2';
- my $vmid = extract_param($param, 'vmid');
+ my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
- my $updatefn = sub {
+ $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
- my $conf = PVE::LXC::load_config($vmid);
- PVE::LXC::check_lock($conf);
+ PVE::Storage::activate_volumes($storage_cfg, [$volid]);
- die "unable to create template, because CT contains snapshots\n"
- if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
+ my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5);
+ $newsize += $size if $ext;
+ $newsize = int($newsize);
- die "you can't convert a template to a template\n"
- if PVE::LXC::is_template($conf);
+ die "unable to shrink disk size\n" if $newsize < $size;
- die "you can't convert a CT to template if the CT is running\n"
- if PVE::LXC::check_running($vmid);
+ return if $size == $newsize;
+ PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr");
my $realcmd = sub {
- PVE::LXC::template_create($vmid, $conf);
+ # Note: PVE::Storage::volume_resize doesn't do anything if $running=1, so
+ # we pass 0 here (parameter only makes sense for qemu)
+ PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
+
+ $mp->{size} = $newsize;
+ $conf->{$disk} = PVE::LXC::Config->print_ct_mountpoint($mp, $disk eq 'rootfs');
+
+ PVE::LXC::Config->write_config($vmid, $conf);
+
+ if ($format eq 'raw') {
+ my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+ if ($running) {
+
+ $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"
+ if !$path;
+ PVE::Tools::run_command(['losetup', '--set-capacity', $path]) if $use_loopdev;
+
+ # In order for resize2fs to know that we need online-resizing a mountpoint needs
+ # to be visible to it in its namespace.
+ # To not interfere with the rest of the system we unshare the current mount namespace,
+ # mount over /tmp and then run resize2fs.
+
+ # interestingly we don't need to e2fsck on mounted systems...
+ my $quoted = PVE::Tools::shellquote($path);
+ my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
+ eval {
+ PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
+ };
+ warn "Failed to update the container's filesystem: $@\n" if $@;
+ } else {
+ eval {
+ PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
+ PVE::Tools::run_command(['resize2fs', $path]);
+ };
+ warn "Failed to update the container's filesystem: $@\n" if $@;
+ }
+ }
};
- $conf->{template} = 1;
-
- PVE::LXC::write_config($vmid, $conf);
- # and remove lxc config
- PVE::LXC::update_lxc_config(undef, $vmid, $conf);
-
- return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
+ return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd);
};
- PVE::LXC::lock_container($vmid, undef, $updatefn);
-
- return undef;
+ return PVE::LXC::Config->lock_config($vmid, $code);;
}});
1;