]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
fix typo, whitespace cleanups
[qemu-server.git] / PVE / API2 / Qemu.pm
index 6d02ded146f6ab753d89e870ef997ad1ba4e1d4c..883a26c1927a462b96da4bb92f741978886bcb48 100644 (file)
@@ -7,7 +7,7 @@ use Cwd 'abs_path';
 use PVE::Cluster qw (cfs_read_file cfs_write_file);;
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
-use PVE::Exception qw(raise raise_param_exc);
+use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
 use PVE::Storage;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RESTHandler;
@@ -59,7 +59,7 @@ my $check_storage_access = sub {
     });
 };
 
-my $check_storage_access_copy = sub {
+my $check_storage_access_clone = sub {
    my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_;
 
    my $sharedvm = 1;
@@ -254,11 +254,10 @@ __PACKAGE__->register_method({
     method => 'POST',
     description => "Create or restore a virtual machine.",
     permissions => {
-       description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
-       check => [ 'or',
-                  [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
-                  [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
-           ],
+       description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
+           "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
+           "If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
+        user => 'all', # check inside
     },
     protected => 1,
     proxyto => 'node',
@@ -334,6 +333,17 @@ __PACKAGE__->register_method({
        $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
            if defined($storage);
 
+       if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
+           # OK
+       } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
+           # OK
+       } elsif ($archive && $force && (-f $filename) &&
+                $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
+           # OK: user has VM.Backup permissions, and want to restore an existing VM
+       } else {
+           raise_perm_exc();
+       }
+
        if (!$archive) {
            &$resolve_cdrom_alias($param);
 
@@ -1784,19 +1794,19 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'copy_vm',
-    path => '{vmid}/copy',
+    name => 'clone_vm',
+    path => '{vmid}/clone',
     method => 'POST',
     protected => 1,
     proxyto => 'node',
     description => "Create a copy of virtual machine/template.",
     permissions => {
-       description => "You need 'VM.Copy' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " .
+       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.Copy' ]],
+         ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
          [ 'or',
            [ 'perm', '/vms/{newid}', ['VM.Allocate']],
            [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
@@ -1808,7 +1818,7 @@ __PACKAGE__->register_method({
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid'),
-           newid => get_standard_option('pve-vmid', { description => 'VMID for the copy.' }),
+           newid => get_standard_option('pve-vmid', { description => 'VMID for the clone.' }),
            name => {
                optional => 1,
                type => 'string', format => 'dns-name',
@@ -1829,7 +1839,7 @@ __PACKAGE__->register_method({
                optional => 1,
             }),
            storage => get_standard_option('pve-storage-id', {
-               description => "Target storage for full copy.",
+               description => "Target storage for full clone.",
                requires => 'full',
                optional => 1,
            }),
@@ -1844,7 +1854,7 @@ __PACKAGE__->register_method({
                optional => 1,
                type => 'boolean',
                description => "Create a full copy of all disk. This is always done when " .
-                   "you copy a normal VM. For VM templates, we try to create a linked copy by default.",
+                   "you clone a normal VM. For VM templates, we try to create a linked clone by default.",
                default => 0,
            },
            target => get_standard_option('pve-node', {
@@ -1896,18 +1906,14 @@ __PACKAGE__->register_method({
 
        my $running = PVE::QemuServer::check_running($vmid) || 0;
 
-       die "Copy running VM $vmid not implemented\n" if $running; # fixme: implement this
-
        # exclusive lock if VM is running - else shared lock is enough;
        my $shared_lock = $running ? 0 : 1;
 
-       # fixme: do early checks - re-check after lock
+       my $clonefn = sub {
 
-       # fixme: impl. target node parameter (mv VM config if all storages are shared)
+           # do all tests after lock
+           # we also try to do all tests before we fork the worker
 
-       my $copyfn = sub {
-
-           # all tests after lock
            my $conf = PVE::QemuServer::load_config($vmid);
 
            PVE::QemuServer::check_lock($conf);
@@ -1921,17 +1927,63 @@ __PACKAGE__->register_method({
 
            my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
 
-           my $sharedvm = &$check_storage_access_copy($rpcenv, $authuser, $storecfg, $oldconf, $storage);
+           my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage);
 
-           die "can't copy VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm;
+           die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm;
 
            my $conffile = PVE::QemuServer::config_file($newid);
 
            die "unable to create VM $newid: config file already exists\n"
                if -f $conffile;
 
+           my $newconf = { lock => 'clone' };
+           my $drives = {};
+           my $vollist = [];
+
+           foreach my $opt (keys %$oldconf) {
+               my $value = $oldconf->{$opt};
+
+               # do not copy snapshot related info
+               next if $opt eq 'snapshots' ||  $opt eq 'parent' || $opt eq 'snaptime' ||
+                   $opt eq 'vmstate' || $opt eq 'snapstate';
+
+               # always change MAC! address
+               if ($opt =~ m/^net(\d+)$/) {
+                   my $net = PVE::QemuServer::parse_net($value);
+                   $net->{macaddr} =  PVE::Tools::random_ether_addr();
+                   $newconf->{$opt} = PVE::QemuServer::print_net($net);
+               } elsif (my $drive = PVE::QemuServer::parse_drive($opt, $value)) {
+                   if (PVE::QemuServer::drive_is_cdrom($drive)) {
+                       $newconf->{$opt} = $value; # simply copy configuration
+                   } else {
+                       if ($param->{full} || !PVE::Storage::volume_is_base($storecfg,  $drive->{file})) {
+                           die "Full clone feature is not available"
+                               if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running);
+                           $drive->{full} = 1;
+                       }
+                       $drives->{$opt} = $drive;
+                       push @$vollist, $drive->{file};
+                   }
+               } else {
+                   # copy everything else
+                   $newconf->{$opt} = $value;
+               }
+           }
+
+           delete $newconf->{template};
+
+           if ($param->{name}) {
+               $newconf->{name} = $param->{name};
+           } else {
+               $newconf->{name} = "Copy-of-$oldconf->{name}";
+           }
+
+           if ($param->{description}) {
+               $newconf->{description} = $param->{description};
+           }
+
            # create empty/temp config - this fails if VM already exists on other node
-           PVE::Tools::file_set_contents($conffile, "# qmcopy temporary file\nlock: copy\n");
+           PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n");
 
            my $realcmd = sub {
                my $upid = shift;
@@ -1939,90 +1991,51 @@ __PACKAGE__->register_method({
                my $newvollist = [];
 
                eval {
-                   my $newconf = { lock => 'copy' };
-                   my $drives = {};
-                   my $vollist = [];
-
-                   foreach my $opt (keys %$oldconf) {
-                       my $value = $oldconf->{$opt};
-
-                       # do not copy snapshot related info
-                       next if $opt eq 'snapshots' ||  $opt eq 'parent' || $opt eq 'snaptime' ||
-                           $opt eq 'vmstate' || $opt eq 'snapstate';
-
-                       # always change MAC! address
-                       if ($opt =~ m/^net(\d+)$/) {
-                           my $net = PVE::QemuServer::parse_net($value);
-                           $net->{macaddr} =  PVE::Tools::random_ether_addr();
-                           $newconf->{$opt} = PVE::QemuServer::print_net($net);
-                       } elsif (my $drive = PVE::QemuServer::parse_drive($opt, $value)) {
-                           if (PVE::QemuServer::drive_is_cdrom($drive)) {
-                               $newconf->{$opt} = $value; # simply copy configuration
-                           } else {
-                               $drives->{$opt} = $drive;
-                               push @$vollist, $drive->{file};
-                           }
-                       } else {
-                           # copy everything else
-                           $newconf->{$opt} = $value;
-                       }
-                   }
-
-                   delete $newconf->{template};
-
-                   if ($param->{name}) {
-                       $newconf->{name} = $param->{name};
-                   } else {
-                       $newconf->{name} = "Copy-of-$oldconf->{name}";
-                   }
-
-                   if ($param->{description}) {
-                       $newconf->{description} = $param->{description};
-                   }
+                   local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
 
                    PVE::Storage::activate_volumes($storecfg, $vollist);
 
-                   eval {
-                       local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
-
-                       foreach my $opt (keys %$drives) {
-                           my $drive = $drives->{$opt};
+                   foreach my $opt (keys %$drives) {
+                       my $drive = $drives->{$opt};
 
-                           my $newvolid;
-                           if (!$param->{full} && PVE::Storage::volume_is_base($storecfg,  $drive->{file})) {
-                               print "clone drive $opt ($drive->{file})\n";
-                               $newvolid = PVE::Storage::vdisk_clone($storecfg,  $drive->{file}, $newid);
-                               push @$newvollist, $newvolid;
+                       my $newvolid;
+                       if (!$drive->{full}) {
+                           print "create linked clone of drive $opt ($drive->{file})\n";
+                           $newvolid = PVE::Storage::vdisk_clone($storecfg,  $drive->{file}, $newid);
+                           push @$newvollist, $newvolid;
 
-                           } else {
-                               my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
-                               $storeid = $storage if $storage;
-
-                               my $fmt = undef;
-                               if($format){
-                                   $fmt = $format;
-                               }else{
-                                   my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
-                                   $fmt = $drive->{format} || $defformat;
-                               }
+                       } else {
+                           my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
+                           $storeid = $storage if $storage;
+
+                           my $fmt = undef;
+                           if($format){
+                               $fmt = $format;
+                           }else{
+                               my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
+                               $fmt = $drive->{format} || $defformat;
+                           }
 
-                               my ($size) = PVE::Storage::volume_size_info($storecfg, $drive->{file}, 3);
+                           my ($size) = PVE::Storage::volume_size_info($storecfg, $drive->{file}, 3);
 
-                               print "copy drive $opt ($drive->{file})\n";
-                               $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newid, $fmt, undef, ($size/1024));
-                               push @$newvollist, $newvolid;
+                           print "create full clone of drive $opt ($drive->{file})\n";
+                           $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newid, $fmt, undef, ($size/1024));
+                           push @$newvollist, $newvolid;
 
+                           if(!$running || $snapname){
                                PVE::QemuServer::qemu_img_convert($drive->{file}, $newvolid, $size, $snapname);
+                           }else{
+                               PVE::QemuServer::qemu_drive_mirror($vmid, $opt, $newvolid, $newid);
                            }
 
-                           my ($size) = PVE::Storage::volume_size_info($storecfg, $newvolid, 3);
-                           my $disk = { file => $newvolid, size => $size };
-                           $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $disk);
-
-                           PVE::QemuServer::update_config_nolock($newid, $newconf, 1);
                        }
-                   };
-                   die $@ if $@;
+
+                       my ($size) = PVE::Storage::volume_size_info($storecfg, $newvolid, 3);
+                       my $disk = { file => $newvolid, size => $size };
+                       $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $disk);
+
+                       PVE::QemuServer::update_config_nolock($newid, $newconf, 1);
+                   }
 
                    delete $newconf->{lock};
                    PVE::QemuServer::update_config_nolock($newid, $newconf, 1);
@@ -2042,18 +2055,18 @@ __PACKAGE__->register_method({
                        eval { PVE::Storage::vdisk_free($storecfg, $volid); };
                        warn $@ if $@;
                    }
-                   die "copy failed: $err";
+                   die "clone failed: $err";
                }
 
                return;
            };
 
-           return $rpcenv->fork_worker('qmcopy', $vmid, $authuser, $realcmd);
+           return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd);
        };
 
        return PVE::QemuServer::lock_config_mode($vmid, 1, $shared_lock, sub {
            # Aquire exclusive lock lock for $newid
-           return PVE::QemuServer::lock_config_full($newid, 1, $copyfn);
+           return PVE::QemuServer::lock_config_full($newid, 1, $clonefn);
        });
 
     }});