()
| 14 | ) |
| 15 | |
| 16 | func runHyperVCmd() *cobra.Command { |
| 17 | var ( |
| 18 | vmName string |
| 19 | keep bool |
| 20 | switchName string |
| 21 | ) |
| 22 | |
| 23 | cmd := &cobra.Command{ |
| 24 | Use: "hyperv", |
| 25 | Short: "launch a VM in Hyper-V", |
| 26 | Long: `Launch a VM in Hyper-V. |
| 27 | 'path' specifies the path to a EFI ISO file. |
| 28 | `, |
| 29 | Args: cobra.ExactArgs(1), |
| 30 | Example: "linuxkit run hyperv [options] path", |
| 31 | RunE: func(cmd *cobra.Command, args []string) error { |
| 32 | isoPath := args[0] |
| 33 | // Sanity checks. Errors out on failure |
| 34 | hypervChecks() |
| 35 | |
| 36 | vmSwitch, err := hypervGetSwitch(switchName) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | log.Debugf("Using switch: %s", vmSwitch) |
| 41 | |
| 42 | if vmName == "" { |
| 43 | vmName = filepath.Base(isoPath) |
| 44 | vmName = strings.TrimSuffix(vmName, ".iso") |
| 45 | // Also strip -efi in case it is present |
| 46 | vmName = strings.TrimSuffix(vmName, "-efi") |
| 47 | } |
| 48 | |
| 49 | log.Infof("Creating VM: %s", vmName) |
| 50 | _, out, err := poshCmd("New-VM", "-Name", fmt.Sprintf("'%s'", vmName), |
| 51 | "-Generation", "2", |
| 52 | "-NoVHD", |
| 53 | "-SwitchName", fmt.Sprintf("'%s'", vmSwitch)) |
| 54 | if err != nil { |
| 55 | return fmt.Errorf("failed to create new VM: %w\n%s", err, out) |
| 56 | } |
| 57 | log.Infof("Configure VM: %s", vmName) |
| 58 | _, out, err = poshCmd("Set-VM", "-Name", fmt.Sprintf("'%s'", vmName), |
| 59 | "-AutomaticStartAction", "Nothing", |
| 60 | "-AutomaticStopAction", "ShutDown", |
| 61 | "-CheckpointType", "Disabled", |
| 62 | "-MemoryStartupBytes", fmt.Sprintf("%dMB", mem), |
| 63 | "-StaticMemory", |
| 64 | "-ProcessorCount", fmt.Sprintf("%d", cpus)) |
| 65 | if err != nil { |
| 66 | return fmt.Errorf("failed to configure new VM: %w\n%s", err, out) |
| 67 | } |
| 68 | |
| 69 | for i, d := range disks { |
| 70 | id := "" |
| 71 | if i != 0 { |
| 72 | id = strconv.Itoa(i) |
| 73 | } |
no test coverage detected