From 04a69bb4fecdb417cf5d02e178ac93f6c0c5b7d6 Mon Sep 17 00:00:00 2001 From: Alexandre Derumier Date: Thu, 14 Feb 2013 11:58:49 +0100 Subject: [PATCH] add template_create qm template [-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 --- 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 487fde28..924af264 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 a4b56000..2202b7ac 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -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 76bc2296..93f621c1 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 ], -- 2.39.5