github.com/vmware/govmomi@v0.37.2/govc/device/boot.go (about)

     1  /*
     2  Copyright (c) 2014-2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package device
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"strings"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/govc/vm"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type boot struct {
    31  	*flags.VirtualMachineFlag
    32  
    33  	firmware string
    34  	order    string
    35  	types.VirtualMachineBootOptions
    36  }
    37  
    38  func init() {
    39  	cli.Register("device.boot", &boot{})
    40  }
    41  
    42  func (cmd *boot) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    44  	cmd.VirtualMachineFlag.Register(ctx, f)
    45  
    46  	f.Int64Var(&cmd.BootDelay, "delay", 0, "Delay in ms before starting the boot sequence")
    47  	f.StringVar(&cmd.order, "order", "", "Boot device order [-,floppy,cdrom,ethernet,disk]")
    48  	f.Int64Var(&cmd.BootRetryDelay, "retry-delay", 0, "Delay in ms before a boot retry")
    49  
    50  	cmd.BootRetryEnabled = types.NewBool(false)
    51  	f.BoolVar(cmd.BootRetryEnabled, "retry", false, "If true, retry boot after retry-delay")
    52  
    53  	cmd.EnterBIOSSetup = types.NewBool(false)
    54  	f.BoolVar(cmd.EnterBIOSSetup, "setup", false, "If true, enter BIOS setup on next boot")
    55  
    56  	f.Var(flags.NewOptionalBool(&cmd.EfiSecureBootEnabled), "secure", "Enable EFI secure boot")
    57  	f.StringVar(&cmd.firmware, "firmware", "", vm.FirmwareUsage)
    58  }
    59  
    60  func (cmd *boot) Description() string {
    61  	return `Configure VM boot settings.
    62  
    63  Examples:
    64    govc device.boot -vm $vm -delay 1000 -order floppy,cdrom,ethernet,disk
    65    govc device.boot -vm $vm -order - # reset boot order
    66    govc device.boot -vm $vm -firmware efi -secure
    67    govc device.boot -vm $vm -firmware bios -secure=false`
    68  }
    69  
    70  func (cmd *boot) Process(ctx context.Context) error {
    71  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    72  		return err
    73  	}
    74  	return nil
    75  }
    76  
    77  func (cmd *boot) Run(ctx context.Context, f *flag.FlagSet) error {
    78  	vm, err := cmd.VirtualMachine()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	if vm == nil {
    84  		return flag.ErrHelp
    85  	}
    86  
    87  	devices, err := vm.Device(ctx)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if cmd.order != "" {
    93  		o := strings.Split(cmd.order, ",")
    94  		cmd.BootOrder = devices.BootOrder(o)
    95  	}
    96  
    97  	spec := types.VirtualMachineConfigSpec{
    98  		BootOptions: &cmd.VirtualMachineBootOptions,
    99  		Firmware:    cmd.firmware,
   100  	}
   101  
   102  	task, err := vm.Reconfigure(ctx, spec)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	return task.Wait(ctx)
   108  }