英文:
Installing virtual Machine with virt-install
问题
以下是将该命令转换为Go语言执行的示例代码:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("virt-install",
"-n", "NAME",
"-r", "1024",
"--import",
"--disk", "path=1703_Disk.img",
"--accelerate",
"--network", "network=default",
"--connect=qemu:///system",
"--vnc",
"-v",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("命令执行失败:", err)
return
}
fmt.Println("命令执行成功")
}
你可以将上述代码保存为一个Go文件(例如main.go
),然后使用Go编译器进行编译和执行。执行该程序将会执行相应的virt-install
命令。
英文:
virt-install \
-n "NAME" \
-r 1024 \
--import \
--disk path="1703_Disk.img" \
--accelerate \
--network network=default \
--connect=qemu:///system \
--vnc \
-v
Can someone explain me how to execute this in Go.
答案1
得分: 3
os/exec
包是你要找的:
cmdName := "virt-install"
args := []string{
"-n", "NAME",
"-r", "1024",
"--import",
"--disk", "path=1703_Disk.img",
"--accelerate",
"--network", "network=default",
"--connect=qemu:///system",
"-vnc",
"-v",
}
cmd := exec.Command(cmdName, args...)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
英文:
The os/exec
package is what you're looking for:
cmdName := "virt-install"
args := []string{
"-n", "NAME",
"-r", "1024",
"--import",
"--disk", "path=1703_Disk.img"
"--accelerate",
"--network", "network=default",
"--connect=qemu:///system",
"-vnc",
"-v",
}
cmd := exec.Command(cmdName, args...)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
答案2
得分: 0
有一个用于libvirt的Go API,可以在https://gitlab.com/libvirt/libvirt-go-module或https://github.com/digitalocean/go-libvirt找到。对于某些任务,使用该API可能更合理,而不是作为子进程运行libvirt命令。
然而,对于virt-install
情况,作为子进程可能更合适。
英文:
There is a Go API for libvirt, either the one at https://gitlab.com/libvirt/libvirt-go-module or the https://github.com/digitalocean/go-libvirt one. For some tasks, it makes more sense to use that, instead of running libvirt commands as a subprocess.
The virt-install
case probably makes most sense as a subprocess, however.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论