use PVE::INotify;
use PVE::RPCEnvironment;
use PVE::QemuServer;
+use PVE::QemuServer::ImportDisk;
use PVE::API2::Qemu;
+use JSON;
use PVE::JSONSchema qw(get_standard_option);
use Term::ReadLine;
my $nodename = PVE::INotify::nodename();
+sub setup_environment {
+ PVE::RPCEnvironment->setup_default_cli_env();
+}
+
sub run_vnc_proxy {
my ($path) = @_;
return undef;
}});
+__PACKAGE__->register_method ({
+ name => 'nbdstop',
+ path => 'nbdstop',
+ method => 'PUT',
+ description => "Stop embedded nbd server.",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+ },
+ },
+ returns => { type => 'null'},
+ code => sub {
+ my ($param) = @_;
+
+ my $vmid = $param->{vmid};
+
+ PVE::QemuServer::nbd_stop($vmid);
+
+ return undef;
+ }});
+
__PACKAGE__->register_method ({
name => 'mtunnel',
path => 'mtunnel',
return undef;
}
- print "tunnel online\n";
- *STDOUT->flush();
+ my $tunnel_write = sub {
+ my $text = shift;
+ chomp $text;
+ print "$text\n";
+ *STDOUT->flush();
+ };
+
+ $tunnel_write->("tunnel online");
+ $tunnel_write->("ver 1");
while (my $line = <>) {
chomp $line;
- last if $line =~ m/^quit$/;
+ if ($line =~ /^quit$/) {
+ $tunnel_write->("OK");
+ last;
+ } elsif ($line =~ /^resume (\d+)$/) {
+ my $vmid = $1;
+ if (PVE::QemuServer::check_running($vmid, 1)) {
+ eval { PVE::QemuServer::vm_resume($vmid, 1, 1); };
+ if ($@) {
+ $tunnel_write->("ERR: resume failed - $@");
+ } else {
+ $tunnel_write->("OK");
+ }
+ } else {
+ $tunnel_write->("ERR: resume failed - VM $vmid not running");
+ }
+ }
}
return undef;
return undef;
}});
+__PACKAGE__->register_method ({
+ name => 'importdisk',
+ path => 'importdisk',
+ method => 'POST',
+ description => "Import an external disk image as an unused disk in a VM. The
+ image format has to be supported by qemu-img(1).",
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ vmid => get_standard_option('pve-vmid', {completion => \&PVE::QemuServer::complete_vmid}),
+ source => {
+ description => 'Path to the disk image to import',
+ type => 'string',
+ optional => 0,
+ },
+ storage => get_standard_option('pve-storage-id', {
+ description => 'Target storage ID',
+ completion => \&PVE::QemuServer::complete_storage,
+ optional => 0,
+ }),
+ format => {
+ type => 'string',
+ description => 'Target format',
+ enum => [ 'raw', 'qcow2', 'vmdk' ],
+ optional => 1,
+ },
+ },
+ },
+ returns => { type => 'null'},
+ code => sub {
+ my ($param) = @_;
+
+ my $vmid = extract_param($param, 'vmid');
+ my $source = extract_param($param, 'source');
+ my $storeid = extract_param($param, 'storage');
+ my $format = extract_param($param, 'format');
+
+ my $vm_conf = PVE::QemuConfig->load_config($vmid);
+ PVE::QemuConfig->check_lock($vm_conf);
+ die "$source: non-existent or non-regular file\n" if (! -f $source);
+
+ my $storecfg = PVE::Storage::config();
+ PVE::Storage::storage_check_enabled($storecfg, $storeid);
+
+ my $target_storage_config =
+ PVE::Storage::storage_config($storecfg, $storeid);
+ die "storage $storeid does not support vm images\n"
+ if !$target_storage_config->{content}->{images};
+
+ PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format });
+
+ return undef;
+ }});
+
__PACKAGE__->register_method ({
name => 'terminal',
path => 'terminal',
return undef;
}});
+
+my $print_agent_result = sub {
+ my ($data) = @_;
+
+ my $result = $data->{result};
+ return if !defined($result);
+
+ my $class = ref($result);
+
+ if (!$class) {
+ chomp $result;
+ return if $result =~ m/^\s*$/;
+ print "$result\n";
+ return;
+ }
+
+ if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
+ return;
+ }
+
+ print to_json($result, { pretty => 1, canonical => 1});
+};
+
our $cmddef = {
list => [ "PVE::API2::Qemu", 'vmlist', [],
{ node => $nodename }, sub {
delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
+ listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename },
+ sub {
+ my $res = shift;
+ foreach my $e (@$res) {
+ my $headline = $e->{description} || 'no-description';
+ $headline =~ s/\n.*//sg;
+ my $parent = $e->{parent} // 'no-parent';
+ printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
+ }
+ }],
+
rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }],
monitor => [ __PACKAGE__, 'monitor', ['vmid']],
+ agent => [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
+ { node => $nodename }, $print_agent_result ],
+
mtunnel => [ __PACKAGE__, 'mtunnel', []],
+ nbdstop => [ __PACKAGE__, 'nbdstop', ['vmid']],
+
terminal => [ __PACKAGE__, 'terminal', ['vmid']],
+
+ importdisk => [ __PACKAGE__, 'importdisk', ['vmid', 'source', 'storage']],
};
1;