diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 1687a07a..c682c27f 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -271,12 +271,13 @@ __PACKAGE__->register_method({ properties => PVE::QemuServer::json_config_properties( { node => get_standard_option('pve-node'), - vmid => get_standard_option('pve-vmid'), + vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), archive => { description => "The backup file.", type => 'string', optional => 1, maxLength => 255, + completion => \&PVE::QemuServer::complete_backup_archives, }, storage => get_standard_option('pve-storage-id', { description => "Default storage.", diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index b2a83565..a5033113 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -6385,4 +6385,62 @@ sub scsihw_infos { return ($maxdev, $controller, $controller_prefix); } +# bash completion helper + +sub complete_backup_archives { + my ($cmdname, $pname, $cvalue) = @_; + + my $cfg = PVE::Storage::config(); + + my $storeid; + + if ($cvalue =~ m/^([^:]+):/) { + $storeid = $1; + } + + my $data = PVE::Storage::template_list($cfg, $storeid, 'backup'); + + my $res = []; + foreach my $id (keys %$data) { + foreach my $item (@{$data->{$id}}) { + next if $item->{format} !~ m/^vma\.(gz|lzo)$/; + push @$res, $item->{volid} if defined($item->{volid}); + } + } + + return $res; +} + +my $complete_vmid_full = sub { + my ($running) = @_; + + my $idlist = vmstatus(); + + my $res = []; + + foreach my $id (keys %$idlist) { + my $d = $idlist->{$id}; + if (defined($running)) { + next if $d->{template}; + next if $running && $d->{status} ne 'running'; + next if !$running && $d->{status} eq 'running'; + } + push @$res, $id; + + } + return $res; +}; + +sub complete_vmid { + return &$complete_vmid_full(); +} + +sub complete_vmid_stopped { + return &$complete_vmid_full(0); +} + +sub complete_vmid_running { + return &$complete_vmid_full(1); +} + 1;