bootcommand

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2019 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const KeyLeftShift uint32 = 0xFFE1

Variables

This section is empty.

Functions

func GenerateExpressionSequence

func GenerateExpressionSequence(command string) (expressionSequence, error)

GenerateExpressionSequence generates a sequence of expressions from the given command. This is the primary entry point to the boot command parser.

func NewPCXTDriver

func NewPCXTDriver(send SendCodeFunc, chunkSize int, interval time.Duration) *pcXTDriver

NewPCXTDriver creates a new boot command driver for VMs that expect PC-XT keyboard codes. `send` should send its argument to the VM. `chunkSize` should be the maximum number of keyboard codes to send to `send` at one time.

func NewVNCDriver

func NewVNCDriver(c VNCKeyEvent, interval time.Duration) *vncDriver

func Parse

func Parse(filename string, b []byte, opts ...Option) (interface{}, error)

Parse parses the data from b using filename as information in the error messages.

func ParseFile

func ParseFile(filename string, opts ...Option) (i interface{}, err error)

ParseFile parses the file identified by filename.

func ParseReader

func ParseReader(filename string, r io.Reader, opts ...Option) (interface{}, error)

ParseReader parses the data from r using filename as information in the error messages.

Types

type BCDriver

type BCDriver interface {
	SendKey(key rune, action KeyAction) error
	SendSpecial(special string, action KeyAction) error
	// Flush will be called when we want to send scancodes to the VM.
	Flush() error
}

BCDriver is our access to the VM we want to type boot commands to

type BootConfig

type BootConfig struct {
	// Time to wait after sending a group of key pressses. The value of this
	// should be a duration. Examples are `5s` and `1m30s` which will cause
	// Packer to wait five seconds and one minute 30 seconds, respectively. If
	// this isn't specified, a sensible default value is picked depending on
	// the builder type.
	BootGroupInterval time.Duration `mapstructure:"boot_keygroup_interval"`
	// The time to wait after booting the initial virtual machine before typing
	// the `boot_command`. The value of this should be a duration. Examples are
	// `5s` and `1m30s` which will cause Packer to wait five seconds and one
	// minute 30 seconds, respectively. If this isn't specified, the default is
	// `10s` or 10 seconds.
	BootWait time.Duration `mapstructure:"boot_wait"`
	// This is an array of commands to type when the virtual machine is first
	// booted. The goal of these commands should be to type just enough to
	// initialize the operating system installer. Special keys can be typed as
	// well, and are covered in the section below on the boot command. If this
	// is not specified, it is assumed the installer will start itself.
	BootCommand []string `mapstructure:"boot_command"`
}

The boot configuration is very important: `boot_command` specifies the keys to type when the virtual machine is first booted in order to start the OS installer. This command is typed after boot_wait, which gives the virtual machine some time to actually load.

The boot_command is an array of strings. The strings are all typed in sequence. It is an array only to improve readability within the template.

There are a set of special keys available. If these are in your boot command, they will be replaced by the proper key:

- `<bs>` - Backspace

- `<del>` - Delete

- `<enter> <return>` - Simulates an actual "enter" or "return" keypress.

- `<esc>` - Simulates pressing the escape key.

- `<tab>` - Simulates pressing the tab key.

- `<f1> - <f12>` - Simulates pressing a function key.

- `<up> <down> <left> <right>` - Simulates pressing an arrow key.

- `<spacebar>` - Simulates pressing the spacebar.

- `<insert>` - Simulates pressing the insert key.

- `<home> <end>` - Simulates pressing the home and end keys.

  • `<pageUp> <pageDown>` - Simulates pressing the page up and page down keys.

- `<menu>` - Simulates pressing the Menu key.

- `<leftAlt> <rightAlt>` - Simulates pressing the alt key.

- `<leftCtrl> <rightCtrl>` - Simulates pressing the ctrl key.

- `<leftShift> <rightShift>` - Simulates pressing the shift key.

- `<leftSuper> <rightSuper>` - Simulates pressing the ⌘ or Windows key.

  • `<wait> <wait5> <wait10>` - Adds a 1, 5 or 10 second pause before sending any additional keys. This is useful if you have to generally wait for the UI to update before typing more.
  • `<waitXX>` - Add an arbitrary pause before sending any additional keys. The format of `XX` is a sequence of positive decimal numbers, each with optional fraction and a unit suffix, such as `300ms`, `1.5h` or `2h45m`. Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`. For example `<wait10m>` or `<wait1m20s>`.
  • `<XXXOn> <XXXOff>` - Any printable keyboard character, and of these "special" expressions, with the exception of the `<wait>` types, can also be toggled on or off. For example, to simulate ctrl+c, use `<leftCtrlOn>c<leftCtrlOff>`. Be sure to release them, otherwise they will be held down until the machine reboots. To hold the `c` key down, you would use `<cOn>`. Likewise, `<cOff>` to release.
  • `{{ .HTTPIP }} {{ .HTTPPort }}` - The IP and port, respectively of an HTTP server that is started serving the directory specified by the `http_directory` configuration parameter. If `http_directory` isn't specified, these will be blank!

- `Name` - The name of the VM.

Example boot command. This is actually a working boot command used to start an CentOS 6.4 installer:

``` json "boot_command": [

   "<tab><wait>",
   " ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos6-ks.cfg<enter>"
]

```

The example shown below is a working boot command used to start an Ubuntu 12.04 installer:

``` json [

"<esc><esc><enter><wait>",
"/install/vmlinuz noapic ",
"preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg ",
"debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
"hostname={{ .Name }} ",
"fb=false debconf/frontend=noninteractive ",
"keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ",
"keyboard-configuration/variant=USA console-setup/ask_detect=false ",
"initrd=/install/initrd.gz -- <enter>"

] ``` For more examples of various boot commands, see the sample projects from our [community templates page](/community-tools.html#templates).

func (*BootConfig) FlatBootCommand

func (c *BootConfig) FlatBootCommand() string

func (*BootConfig) Prepare

func (c *BootConfig) Prepare(ctx *interpolate.Context) (errs []error)

type Cloner

type Cloner interface {
	Clone() interface{}
}

Cloner is implemented by any value that has a Clone method, which returns a copy of the value. This is mainly used for types which are not passed by value (e.g map, slice, chan) or structs that contain such types.

This is used in conjunction with the global state feature to create proper copies of the state to allow the parser to properly restore the state in the case of backtracking.

type KeyAction

type KeyAction int

KeysAction represents what we want to do with a key press. It can take 3 states. We either want to: * press the key once * press and hold * press and release

const (
	KeyOn KeyAction = 1 << iota
	KeyOff
	KeyPress
)

func (KeyAction) String

func (k KeyAction) String() string

type Option

type Option func(*parser) Option

Option is a function that can set an option on the parser. It returns the previous setting as an Option.

func AllowInvalidUTF8

func AllowInvalidUTF8(b bool) Option

AllowInvalidUTF8 creates an Option to allow invalid UTF-8 bytes. Every invalid UTF-8 byte is treated as a utf8.RuneError (U+FFFD) by character class matchers and is matched by the any matcher. The returned matched value, c.text and c.offset are NOT affected.

The default is false.

func Debug

func Debug(b bool) Option

Debug creates an Option to set the debug flag to b. When set to true, debugging information is printed to stdout while parsing.

The default is false.

func Entrypoint

func Entrypoint(ruleName string) Option

Entrypoint creates an Option to set the rule name to use as entrypoint. The rule name must have been specified in the -alternate-entrypoints if generating the parser with the -optimize-grammar flag, otherwise it may have been optimized out. Passing an empty string sets the entrypoint to the first rule in the grammar.

The default is to start parsing at the first rule in the grammar.

func GlobalStore

func GlobalStore(key string, value interface{}) Option

GlobalStore creates an Option to set a key to a certain value in the globalStore.

func InitState

func InitState(key string, value interface{}) Option

InitState creates an Option to set a key to a certain value in the global "state" store.

func MaxExpressions

func MaxExpressions(maxExprCnt uint64) Option

MaxExpressions creates an Option to stop parsing after the provided number of expressions have been parsed, if the value is 0 then the parser will parse for as many steps as needed (possibly an infinite number).

The default for maxExprCnt is 0.

func Memoize

func Memoize(b bool) Option

Memoize creates an Option to set the memoize flag to b. When set to true, the parser will cache all results so each expression is evaluated only once. This guarantees linear parsing time even for pathological cases, at the expense of more memory and slower times for typical cases.

The default is false.

func Recover

func Recover(b bool) Option

Recover creates an Option to set the recover flag to b. When set to true, this causes the parser to recover from panics and convert it to an error. Setting it to false can be useful while debugging to access the full stack trace.

The default is true.

func Statistics

func Statistics(stats *Stats, choiceNoMatch string) Option

Statistics adds a user provided Stats struct to the parser to allow the user to process the results after the parsing has finished. Also the key for the "no match" counter is set.

Example usage:

input := "input"
stats := Stats{}
_, err := Parse("input-file", []byte(input), Statistics(&stats, "no match"))
if err != nil {
    log.Panicln(err)
}
b, err := json.MarshalIndent(stats.ChoiceAltCnt, "", "  ")
if err != nil {
    log.Panicln(err)
}
fmt.Println(string(b))

type SendCodeFunc

type SendCodeFunc func([]string) error

SendCodeFunc will be called to send codes to the VM

type Stats

type Stats struct {
	// ExprCnt counts the number of expressions processed during parsing
	// This value is compared to the maximum number of expressions allowed
	// (set by the MaxExpressions option).
	ExprCnt uint64

	// ChoiceAltCnt is used to count for each ordered choice expression,
	// which alternative is used how may times.
	// These numbers allow to optimize the order of the ordered choice expression
	// to increase the performance of the parser
	//
	// The outer key of ChoiceAltCnt is composed of the name of the rule as well
	// as the line and the column of the ordered choice.
	// The inner key of ChoiceAltCnt is the number (one-based) of the matching alternative.
	// For each alternative the number of matches are counted. If an ordered choice does not
	// match, a special counter is incremented. The name of this counter is set with
	// the parser option Statistics.
	// For an alternative to be included in ChoiceAltCnt, it has to match at least once.
	ChoiceAltCnt map[string]map[string]int
}

Stats stores some statistics, gathered during parsing

type VNCConfig

type VNCConfig struct {
	BootConfig `mapstructure:",squash"`
	// Whether to create a VNC connection or not. A boot_command cannot be used
	// when this is true. Defaults to false.
	DisableVNC bool `mapstructure:"disable_vnc"`
	// Time in ms to wait between each key press
	BootKeyInterval time.Duration `mapstructure:"boot_key_interval"`
}

The boot command "typed" character for character over a VNC connection to the machine, simulating a human actually typing the keyboard.

Keystrokes are typed as separate key up/down events over VNC with a default 100ms delay. The delay alleviates issues with latency and CPU contention. You can tune this delay on a per-builder basis by specifying "boot_key_interval" in your Packer template.

func (*VNCConfig) Prepare

func (c *VNCConfig) Prepare(ctx *interpolate.Context) (errs []error)

type VNCKeyEvent

type VNCKeyEvent interface {
	KeyEvent(uint32, bool) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL