]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
do not allow template creation if there are snapshots.
[qemu-server.git] / PVE / API2 / Qemu.pm
index 487fde2893fc1307673f9fee5f0c1f44b06ad9e2..3cc35a223d34f626dae0aeb9b89eb7151ca8b5d3 100644 (file)
@@ -16,6 +16,7 @@ use PVE::QemuMigrate;
 use PVE::RPCEnvironment;
 use PVE::AccessControl;
 use PVE::INotify;
+use PVE::Network;
 
 use Data::Dumper; # fixme: remove
 
@@ -646,8 +647,21 @@ my $vmconfig_delete_option = sub {
            $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
        }
     }
-               
-    die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
+
+    my $unplugwarning = "";
+    if($conf->{ostype} && $conf->{ostype} eq 'l26'){
+       $unplugwarning = "<br>verify that you have acpiphp && pci_hotplug modules loaded in your guest VM";
+    }elsif($conf->{ostype} && $conf->{ostype} eq 'l24'){
+       $unplugwarning = "<br>kernel 2.4 don't support hotplug, please disable hotplug in options";
+    }elsif(!$conf->{ostype} || ($conf->{ostype} && $conf->{ostype} eq 'other')){
+       $unplugwarning = "<br>verify that your guest support acpi hotplug";
+    }
+
+    if($opt eq 'tablet'){
+       PVE::QemuServer::vm_deviceplug(undef, $conf, $vmid, $opt);
+    }else{
+        die "error hot-unplug $opt $unplugwarning" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
+    }
 
     if ($isDisk) {
        my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
@@ -736,11 +750,34 @@ my $vmconfig_update_disk = sub {
 my $vmconfig_update_net = sub {
     my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
 
-    if ($conf->{$opt}) {
-       #if online update, then unplug first
-       die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
-    }
+    if ($conf->{$opt} && PVE::QemuServer::check_running($vmid)) {
+       my $oldnet = PVE::QemuServer::parse_net($conf->{$opt});
+       my $newnet = PVE::QemuServer::parse_net($value);
 
+       if($oldnet->{model} ne $newnet->{model}){
+           #if model change, we try to hot-unplug
+            die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
+       }else{
+               
+           if($newnet->{bridge} && $oldnet->{bridge}){
+               my $iface = "tap".$vmid."i".$1 if $opt =~ m/net(\d+)/;
+
+               if($newnet->{rate} ne $oldnet->{rate}){
+                   PVE::Network::tap_rate_limit($iface, $newnet->{rate});
+               }
+
+               if(($newnet->{bridge} ne $oldnet->{bridge}) || ($newnet->{tag} ne $oldnet->{tag})){
+                   eval{PVE::Network::tap_unplug($iface, $oldnet->{bridge}, $oldnet->{tag});};
+                   PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag});
+               }
+
+           }else{
+               #if bridge/nat mode change, we try to hot-unplug
+               die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
+           }
+       }
+       
+    }
     $conf->{$opt} = $value;
     PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
     $conf = PVE::QemuServer::load_config($vmid); # update/reload
@@ -910,6 +947,12 @@ __PACKAGE__->register_method({
 
                } else {
 
+                   if($opt eq 'tablet' && $param->{$opt} == 1){
+                       PVE::QemuServer::vm_deviceplug(undef, $conf, $vmid, $opt);
+                   }elsif($opt eq 'tablet' && $param->{$opt} == 0){
+                       PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
+                   }
+
                    $conf->{$opt} = $param->{$opt};
                    PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
                }
@@ -1918,6 +1961,9 @@ __PACKAGE__->register_method({
 
            die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
 
+           die "you can't online resize a virtio windows bootdisk\n" 
+               if PVE::QemuServer::check_running($vmid) && $conf->{bootdisk} eq $disk && $conf->{ostype} =~ m/^w/ && $disk =~ m/^virtio/;
+
            my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
 
            $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
@@ -2298,4 +2344,72 @@ __PACKAGE__->register_method({
        return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
     }});
 
+__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} or on the VM pool /pool/{pool}.",
+       check => [ 'or', 
+                  [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
+                  [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
+           ],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           disk => {
+               optional => 1,
+               type => 'string',
+               description => "If you want to convert only 1 disk to base image.",
+               enum => [PVE::QemuServer::disknames()],
+           },
+
+       },
+    },
+    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 $disk = extract_param($param, 'disk');
+
+       my $updatefn =  sub {
+
+           my $conf = PVE::QemuServer::load_config($vmid);
+
+           PVE::QemuServer::check_lock($conf);
+
+           die "unable to create template, because VM contains snapshots\n" 
+               if $conf->{snapshots};
+
+           die "you can't convert a template to a template\n" 
+               if PVE::QemuServer::is_template($conf) && !$disk;
+
+           my $realcmd = sub {
+               PVE::QemuServer::template_create($vmid, $conf, $disk);
+           };
+           return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
+
+           PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+       };
+
+       PVE::QemuServer::lock_config($vmid, $updatefn);
+       return undef;
+    }});
+
+
+
 1;