[pve-devel] [PATCH 01/13] add template_create

Alexandre Derumier aderumier at odiso.com
Sun Feb 3 09:54:46 CET 2013


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 at odiso.com>
---
 PVE/API2/Qemu.pm  |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/QemuServer.pm |   31 ++++++++++++++++++++++++++++
 qm                |    2 ++
 3 files changed, 92 insertions(+)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 487fde2..924af26 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -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;
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 20a6dfd..9ae8d7a 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4413,4 +4413,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 76bc229..93f621c 100755
--- 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 ],
-- 
1.7.10.4




More information about the pve-devel mailing list