使用virt-install安装虚拟机

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

Installing virtual Machine with virt-install

问题

以下是将该命令转换为Go语言执行的示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. )
  7. func main() {
  8. cmd := exec.Command("virt-install",
  9. "-n", "NAME",
  10. "-r", "1024",
  11. "--import",
  12. "--disk", "path=1703_Disk.img",
  13. "--accelerate",
  14. "--network", "network=default",
  15. "--connect=qemu:///system",
  16. "--vnc",
  17. "-v",
  18. )
  19. cmd.Stdout = os.Stdout
  20. cmd.Stderr = os.Stderr
  21. err := cmd.Run()
  22. if err != nil {
  23. fmt.Println("命令执行失败:", err)
  24. return
  25. }
  26. fmt.Println("命令执行成功")
  27. }

你可以将上述代码保存为一个Go文件(例如main.go),然后使用Go编译器进行编译和执行。执行该程序将会执行相应的virt-install命令。

英文:
  1. virt-install \
  2. -n "NAME" \
  3. -r 1024 \
  4. --import \
  5. --disk path="1703_Disk.img" \
  6. --accelerate \
  7. --network network=default \
  8. --connect=qemu:///system \
  9. --vnc \
  10. -v

Can someone explain me how to execute this in Go.

答案1

得分: 3

os/exec 包是你要找的:

  1. cmdName := "virt-install"
  2. args := []string{
  3. "-n", "NAME",
  4. "-r", "1024",
  5. "--import",
  6. "--disk", "path=1703_Disk.img",
  7. "--accelerate",
  8. "--network", "network=default",
  9. "--connect=qemu:///system",
  10. "-vnc",
  11. "-v",
  12. }
  13. cmd := exec.Command(cmdName, args...)
  14. if err := cmd.Start(); err != nil {
  15. log.Fatal(err)
  16. }
  17. if err := cmd.Wait(); err != nil {
  18. log.Fatal(err)
  19. }
英文:

The os/exec package is what you're looking for:

  1. cmdName := "virt-install"
  2. args := []string{
  3. "-n", "NAME",
  4. "-r", "1024",
  5. "--import",
  6. "--disk", "path=1703_Disk.img"
  7. "--accelerate",
  8. "--network", "network=default",
  9. "--connect=qemu:///system",
  10. "-vnc",
  11. "-v",
  12. }
  13. cmd := exec.Command(cmdName, args...)
  14. if err := cmd.Start(); err != nil {
  15. log.Fatal(err)
  16. }
  17. if err := cmd.Wait(); err != nil {
  18. log.Fatal(err)
  19. }

答案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:

确定