Perl和CPAN Sys :: Virt - 连接到vm控制台

时间:2013-09-19 10:40:17

标签: perl console perl-module kvm libvirt

我正在使用Sys :: Virt CPAN模块与我的kvm虚拟机管理程序“交谈”并管理域名。除了virsh控制台之外,我几乎可以使用bash virsh命令可以执行的所有操作,其中(谁会猜到它)打开域的文本控制台。但无论我对sys :: virt做什么,我只是得到一个闪烁的光标,没有任何反应。我也无法在整个网络上找到一个例子......所以我想我会问你我错过了什么。这是我到目前为止的代码 - 以及我测试的内容:

use 5.010; 
use warnings; 
use Sys::Virt; 
use Sys::Virt::Stream; 

my $data; 
my $url = "someurl"; 

my $con = Sys::Virt->new('uri' => "qemu+tls://$url/system"); 
my $dom = $con->get_domain_by_name("name"); 
#Until here everything is just like in every other function i wrote. 

my $st = $con->new_stream(); 
$dom->open_console($st, undef); 
#I guess that i'm also fine until here - because there are no errors.. 

#And now i'm trying to do something with this stream object i made... 
#If i would type virsh console i would get a promt to log in, so i try to fetch this promt. 
#Here are two examples of how i was trying to get it: 

#Result of this block: Blinking cursor - no "test" 
my $rv = $st->recv($data, "1"); 
say "test"; 
say $rv; 
say $data; 

#Result of this block: Blinking cursor - no "test" 
$st->recv_all(sub{ 
    my ($st, $data, $nbytes) = @_; 
}); 
say "test"; 
say $st; 
say $data; 
say $nbytes;

$st->finish();

我错过了什么 - 或者做错了什么?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好吧,我终于发现了我做错了什么,我只是使用了默认流。正确定义流后,我可以发送和接收文本。

my $st=$con->new_stream(Sys::Virt::Stream::NONBLOCK);
$dom->open_console($st, undef, 0);

当一个流被正确定义并且程序不只是采用默认值 - 显然不起作用 - 一切正常 - 非常慢但很好。

以防有人遇到同样的问题。

相关问题