5
0
mirror of git://git.proxmox.com/git/qemu-server.git synced 2025-01-11 05:17:57 +03:00

add template_create

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>
This commit is contained in:
Alexandre Derumier 2013-02-14 11:58:49 +01:00 committed by Dietmar Maurer
parent ef1c835d19
commit 04a69bb4fe
3 changed files with 92 additions and 0 deletions

View 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;

View 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;

2
qm
View File

@ -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 ],