|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "golang.org/x/sys/unix" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + rcnf RunConf |
| 17 | + |
| 18 | + ports []string |
| 19 | +) |
| 20 | + |
| 21 | +func RunCommand() *cobra.Command { |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "run", |
| 24 | + Short: "run/start VMs based on generated base images and kernels", |
| 25 | + SilenceUsage: true, |
| 26 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | + var err error |
| 28 | + |
| 29 | + rcnf.Logger = logrus.New() |
| 30 | + |
| 31 | + rcnf.ForwardedPorts, err = parsePorts(ports) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("Port flags: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + t0 := time.Now() |
| 37 | + |
| 38 | + err = StartQemu(rcnf) |
| 39 | + dur := time.Since(t0).Round(time.Millisecond) |
| 40 | + fmt.Printf("Execution took %v\n", dur) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("Qemu exited with an error: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + cmd.Flags().StringVar(&rcnf.Image, "image", "", "VM image file path") |
| 50 | + cmd.MarkFlagRequired("image") |
| 51 | + cmd.Flags().StringVar(&rcnf.KernelFname, "kernel", "", "kernel filename to boot with. (if empty no -kernel option will be passed to qemu)") |
| 52 | + cmd.Flags().BoolVar(&rcnf.QemuPrint, "qemu-cmd-print", false, "Do not run the qemu command, just print it") |
| 53 | + cmd.Flags().BoolVar(&rcnf.DisableKVM, "qemu-disable-kvm", false, "Do not use KVM acceleration, even if /dev/kvm exists") |
| 54 | + cmd.Flags().BoolVar(&rcnf.Daemonize, "daemonize", false, "daemonize QEMU after initializing") |
| 55 | + cmd.Flags().StringVar(&rcnf.HostMount, "host-mount", "", "Mount the specified host directory in the VM using a 'host_mount' tag") |
| 56 | + cmd.Flags().StringArrayVarP(&ports, "port", "p", nil, "Forward a port (hostport[:vmport[:tcp|udp]])") |
| 57 | + |
| 58 | + return cmd |
| 59 | +} |
| 60 | + |
| 61 | +func parsePorts(flags []string) ([]PortForward, error) { |
| 62 | + var forwards []PortForward |
| 63 | + for _, flag := range flags { |
| 64 | + hostPortStr, vmPortAndProto, found := strings.Cut(flag, ":") |
| 65 | + if !found { |
| 66 | + hostPort, err := strconv.Atoi(flag) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("'%s' is not a valid port number", flag) |
| 69 | + } |
| 70 | + forwards = append(forwards, PortForward{ |
| 71 | + HostPort: hostPort, |
| 72 | + VMPort: hostPort, |
| 73 | + Protocol: "tcp", |
| 74 | + }) |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + hostPort, err := strconv.Atoi(hostPortStr) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("'%s' is not a valid port number", hostPortStr) |
| 81 | + } |
| 82 | + |
| 83 | + vmPortStr, proto, found := strings.Cut(vmPortAndProto, ":") |
| 84 | + if !found { |
| 85 | + vmPort, err := strconv.Atoi(vmPortAndProto) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("'%s' is not a valid port number", vmPortAndProto) |
| 88 | + } |
| 89 | + forwards = append(forwards, PortForward{ |
| 90 | + HostPort: hostPort, |
| 91 | + VMPort: vmPort, |
| 92 | + Protocol: "tcp", |
| 93 | + }) |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + vmPort, err := strconv.Atoi(vmPortStr) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("'%s' is not a valid port number", vmPortStr) |
| 100 | + } |
| 101 | + |
| 102 | + proto = strings.ToLower(proto) |
| 103 | + if proto != "tcp" && proto != "udp" { |
| 104 | + return nil, fmt.Errorf("port forward protocol must be tcp or udp") |
| 105 | + } |
| 106 | + |
| 107 | + forwards = append(forwards, PortForward{ |
| 108 | + HostPort: hostPort, |
| 109 | + VMPort: vmPort, |
| 110 | + Protocol: proto, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + return forwards, nil |
| 115 | +} |
| 116 | + |
| 117 | +const qemuBin = "qemu-system-x86_64" |
| 118 | + |
| 119 | +func StartQemu(rcnf RunConf) error { |
| 120 | + qemuArgs, err := BuildQemuArgs(rcnf.Logger, &rcnf) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + if rcnf.QemuPrint { |
| 126 | + var sb strings.Builder |
| 127 | + sb.WriteString(qemuBin) |
| 128 | + for _, arg := range qemuArgs { |
| 129 | + sb.WriteString(" ") |
| 130 | + if len(arg) > 0 && arg[0] == '-' { |
| 131 | + sb.WriteString("\\\n\t") |
| 132 | + } |
| 133 | + sb.WriteString(arg) |
| 134 | + } |
| 135 | + |
| 136 | + fmt.Printf("%s\n", sb.String()) |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + qemuPath, err := exec.LookPath(qemuBin) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + return unix.Exec(qemuPath, append([]string{qemuBin}, qemuArgs...), nil) |
| 146 | +} |
0 commit comments