英文:
golang exec incorrect behavior
问题
我正在使用以下代码段来获取在XEN Hypervisor
上运行的虚拟机的XML
定义。该代码尝试执行命令virsh dumpxml Ubutnu14
,该命令将给出名为Ubuntu14
的虚拟机的XML
定义。
virshCmd := exec.Command("virsh", "dumpxml", "Ubuntu14")
var virshCmdOutput bytes.Buffer
var stderr bytes.Buffer
virshCmd.Stdout = &virshCmdOutput
virshCmd.Stderr = &stderr
err := virshCmd.Run()
if err != nil {
fmt.Println(err)
fmt.Println(stderr.String())
}
fmt.Println(virshCmdOutput.String())
对于给定的域名,该代码总是进入错误条件,并且我得到以下输出。
exit status 1
error: failed to get domain 'Ubuntu14'
error: Domain not found: no domain with matching name 'Ubuntu14'
但是,如果我运行独立的命令virsh dumpxml Ubuntu14
,我会得到正确的XML
定义。
如果有人能给我一些关于我做错了什么的提示,我将不胜感激。我的主机机器是Ubuntu-16.04
,golang
版本是go1.6.2 linux/amd64
。
英文:
I'm using following code segment to get the XML
definition of a virtual machine running on XEN Hypervisor
. The code is trying to execute the command virsh dumpxml Ubutnu14
which will give the XML
of the VM named Ubuntu14
virshCmd := exec.Command("virsh", "dumpxml", "Ubuntu14")
var virshCmdOutput bytes.Buffer
var stderr bytes.Buffer
virshCmd.Stdout = &virshCmdOutput
virshCmd.Stderr = &stderr
err := virshCmd.Run()
if err != nil {
fmt.Println(err)
fmt.Println(stderr.String())
}
fmt.Println(virshCmdOutput.String())
This code always goes into the error condition for the given domain name and I get the following output.
exit status 1
error: failed to get domain 'Ubuntu14'
error: Domain not found: no domain with matching name 'Ubuntu14'
But if I run the standalone command virsh dumpxml Ubuntu14
, I get the correct XML
definition.
I would appreciate if someone could give me some hints on what I'm doing wrong. My host machine is Ubuntu-16.04
and golang
version is go1.6.2 linux/amd64
答案1
得分: 0
我预计在这两种情况下,您正在以不同的用户身份运行virsh,并且由于您没有提供任何URI,它正在连接到不同的libvirtd实例。如果您以非root用户身份运行virsh,它通常会连接到qemu:///session,但如果您以root用户身份运行virsh,它通常会连接到qemu:///system。注册在一个URI上的虚拟机在连接到另一个URI时将不可见。
顺便说一句,如果您正在使用Go语言,最好使用libvirt的本机Go库绑定,而不是执行virsh命令。您的"virsh dumpxml"调用与以下代码几乎等效:
import (
"github.com/libvirt/libvirt-go"
)
conn, err := libvirt.NewConnect("qemu:///system")
dom, err := conn.LookupDomainByName("Ubuntu14")
xml, err := dom.GetXMLDesc(0)
(当然,还要进行错误处理)
英文:
I expect you are running virsh as a different user in these two scenarios, and since you don't provide any URI, it is connecting to a different libvirtd instance. If you run virsh as non-root, then it'll usually connect to qemu:///session, but if you run virsh as root, then it'll usually connect to qemu:///system. VMs registered against one URI, will not be visible when connecting to the other URI.
BTW, if you're using go, you'd be much better off using the native Go library bindings for libvirt instead of exec'ing virsh. Your "virsh dumpxml" invokation is pretty much equivalent to this:
import (
"github.com/libvirt/libvirt-go"
)
conn, err := libvirt.NewConnect("qemu:///system")
dom, err := conn.LookupDomainByName("Ubuntu14")
xml, err := dom.GetXMLDesc(0)
(obviously do error handling too)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论