]> git.proxmox.com Git - qemu-server.git/commitdiff
add template_create
authorAlexandre Derumier <aderumier@odiso.com>
Thu, 14 Feb 2013 10:58:49 +0000 (11:58 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 15 Feb 2013 06:50:32 +0000 (07:50 +0100)
qm template <vmid> [-disk virtio0]

convert a full vm to a template (or only a disk if specify)

we orignal disk to /base (file) or base- (lvm,rbd,sheepdog,nexenta)
we create a snapshot @base if storage need it for clone
we protect the volume or snapshot

Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
PVE/API2/Qemu.pm
PVE/QemuServer.pm
qm

index 487fde2893fc1307673f9fee5f0c1f44b06ad9e2..924af264f47aa3ebd55682b223ccb09d0cc55287 100644 (file)
@@ -2298,4 +2298,63 @@ __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 => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Template' ]],
+    },
+    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 "you can't convert a template to a template" 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;
index a4b560000cf27a42aad6cc3599719b1eb15b5e6e..2202b7ac673854c86fe160746416383a43386b28 100644 (file)
@@ -4410,4 +4410,35 @@ sub has_feature{
 
     return 1 if !$err;
 }
+
+sub template_create {
+    my ($vmid, $conf, $disk) = @_;
+
+    my $running = check_running($vmid);
+    die "you can't convert a vm to template if vm is running vm" if $running;
+
+    my $storecfg = PVE::Storage::config();
+    my $i = 0;
+
+    foreach_drive($conf, sub {
+       my ($ds, $drive) = @_;
+
+       return if drive_is_cdrom($drive);
+       return if $disk && $ds ne $disk;
+
+       my $volid = $drive->{file};
+       my $voliddst = PVE::Storage::vdisk_create_base($storecfg, $volid);
+       $drive->{file} = $voliddst;
+       $conf->{$ds} = PVE::QemuServer::print_drive($vmid, $drive);
+       PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+
+    });
+    if($conf->{snapshots}){
+       delete $conf->{parent};
+       delete $conf->{snapshots};
+       PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+       #fixme : do we need to delete disks snapshots ?
+    }
+}
+
 1;
diff --git a/qm b/qm
index 76bc22966cde977618dfd56cc2fc39efca07307b..93f621c10e0de51d81ea89afb6a9d4d8eb81f792 100755 (executable)
--- a/qm
+++ b/qm
@@ -379,6 +379,8 @@ my $cmddef = {
 
     rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
 
+    template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }],
+
     start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ],
 
     stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ],