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