使用virt-install安装虚拟机

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年8月27日 22:48:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/32252604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定