]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/API2/LXC.pm
add fstrim lock to enum
[pve-container.git] / src / PVE / API2 / LXC.pm
index b1704280d9b4136f2c6120ed3f9752ac6e67fe9b..5a8a9c9686389f4c018af761f87e2f74106565a9 100644 (file)
@@ -144,6 +144,12 @@ __PACKAGE__->register_method({
                type => 'boolean',
                description => "Mark this as restore task.",
            },
+           unique => {
+               optional => 1,
+               type => 'boolean',
+               description => "Assign a unique random ethernet address.",
+               requires => 'restore',
+           },
            pool => {
                optional => 1,
                type => 'string', format => 'pve-poolid',
@@ -161,10 +167,11 @@ __PACKAGE__->register_method({
                                "OpenSSH format).",
            },
            bwlimit => {
-               description => "Override i/o bandwidth limit (in KiB/s).",
+               description => "Override I/O bandwidth limit (in KiB/s).",
                optional => 1,
                type => 'number',
                minimum => '0',
+               default => 'restore limit from datacenter or storage config',
            },
            start => {
                optional => 1,
@@ -180,32 +187,27 @@ __PACKAGE__->register_method({
     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');
+       my $unique = extract_param($param, 'unique');
 
        if ($restore) {
            # fixme: limit allowed parameters
-
        }
        
        my $force = extract_param($param, 'force');
@@ -213,17 +215,16 @@ __PACKAGE__->register_method({
        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");
@@ -247,9 +248,7 @@ __PACKAGE__->register_method({
 
        my $storage_cfg = cfs_read_file("storage.cfg");
 
-
        my $archive;
-
        if ($ostemplate eq '-') {
            die "pipe requires cli environment\n" 
                if $rpcenv->{type} ne 'cli'; 
@@ -274,7 +273,6 @@ __PACKAGE__->register_method({
            $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
 
            PVE::Storage::activate_storage($storage_cfg, $sid);
-
            $used_storages{$sid} = 1;
        };
 
@@ -330,42 +328,34 @@ __PACKAGE__->register_method({
 
        $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_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
-               if ($is_root) {
-                   $conf->{lxc} = [grep { $_->[0] eq 'lxc.idmap' } @{$orig_conf->{lxc}}]; # do not remove lxc.idmap entries
+               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) {
+                       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});
@@ -403,9 +393,10 @@ __PACKAGE__->register_method({
 
                $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 {
@@ -414,7 +405,7 @@ __PACKAGE__->register_method({
                    PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit);
 
                    if ($restore) {
-                       PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root);
+                       PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root, $unique);
                    } else {
                        my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
                        PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
@@ -436,8 +427,9 @@ __PACKAGE__->register_method({
            };
            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;
 
@@ -445,13 +437,10 @@ __PACKAGE__->register_method({
                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({
@@ -1006,6 +995,13 @@ __PACKAGE__->register_method({
                    " mounts. NOTE: deprecated, use 'shared' property of mount point instead.",
                optional => 1,
            },
+           bwlimit => {
+               description => "Override I/O bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'number',
+               minimum => '0',
+               default => 'migrate limit from datacenter or storage config',
+           },
        },
     },
     returns => {
@@ -1093,7 +1089,7 @@ __PACKAGE__->register_method({
                 type => 'string',
                 enum => [ 'snapshot', 'clone', 'copy' ],
             },
-            snapname => get_standard_option('pve-lxc-snapshot-name', {
+            snapname => get_standard_option('pve-snapshot-name', {
                 optional => 1,
             }),
        },
@@ -1252,7 +1248,7 @@ __PACKAGE__->register_method({
                type => 'string', format => 'pve-poolid',
                description => "Add the new CT to the specified pool.",
            },
-           snapname => get_standard_option('pve-lxc-snapshot-name', {
+           snapname => get_standard_option('pve-snapshot-name', {
                optional => 1,
             }),
            storage => get_standard_option('pve-storage-id', {
@@ -1269,6 +1265,13 @@ __PACKAGE__->register_method({
                description => "Target node. Only allowed if the original VM is on shared storage.",
                optional => 1,
            }),
+           bwlimit => {
+               description => "Override I/O bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'number',
+               minimum => '0',
+               default => 'clone limit from datacenter or storage config',
+           },
         },
     },
     returns => {
@@ -1453,6 +1456,7 @@ __PACKAGE__->register_method({
                    local $SIG{HUP} = sub { die "interrupted by signal\n"; };
 
                PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
+               my $bwlimit = extract_param($param, 'bwlimit');
 
                foreach my $opt (keys %$mountpoints) {
                    my $mp = $mountpoints->{$opt};
@@ -1461,8 +1465,10 @@ __PACKAGE__->register_method({
                    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);
+                       my $source_storage = PVE::Storage::parse_volume_id($volid);
+                       my $target_storage = $storage // $source_storage;
+                       my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', [$source_storage, $target_storage], $bwlimit);
+                       $newvolid = PVE::LXC::copy_volume($mp, $newid, $target_storage, $storecfg, $newconf, $snapname, $clonelimit);
                    } else {
                        print "create linked clone of mount point $opt ($volid)\n";
                        $newvolid = PVE::Storage::vdisk_clone($storecfg, $volid, $newid, $snapname);
@@ -1625,7 +1631,9 @@ __PACKAGE__->register_method({
                PVE::LXC::Config->write_config($vmid, $conf);
 
                if ($format eq 'raw') {
-                   my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+                   # we need to ensure that the volume is mapped, if not needed this is a NOP
+                   my $path = PVE::Storage::map_volume($storage_cfg, $volid);
+                   $path = PVE::Storage::path($storage_cfg, $volid) if !defined($path);
                    if ($running) {
 
                        $mp->{mp} = '/';
@@ -1653,6 +1661,9 @@ __PACKAGE__->register_method({
                            PVE::Tools::run_command(['resize2fs', $path]);
                        };
                        warn "Failed to update the container's filesystem: $@\n" if $@;
+
+                       # always un-map if not running, this is a NOP if not needed
+                       PVE::Storage::unmap_volume($storage_cfg, $volid);
                    }
                }
            };
@@ -1704,7 +1715,14 @@ __PACKAGE__->register_method({
                description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
                maxLength => 40,
                optional => 1,
-           }
+           },
+           bwlimit => {
+               description => "Override I/O bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'number',
+               minimum => '0',
+               default => 'clone limit from datacenter or storage config',
+           },
        },
     },
     returns => {
@@ -1761,7 +1779,10 @@ __PACKAGE__->register_method({
 
                eval {
                    PVE::Storage::activate_volumes($storage_cfg, [ $old_volid ]);
-                   $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf);
+                   my $bwlimit = extract_param($param, 'bwlimit');
+                   my $source_storage = PVE::Storage::parse_volume_id($old_volid);
+                   my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$source_storage, $storage], $bwlimit);
+                   $new_volid = PVE::LXC::copy_volume($mpdata, $vmid, $storage, $storage_cfg, $conf, undef, $movelimit);
                    $mpdata->{volume} = $new_volid;
 
                    PVE::LXC::Config->lock_config($vmid, sub {