Skip to content

Commit

Permalink
Add support for file backed serialport devices (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm authored Dec 6, 2016
1 parent f49bd56 commit 5b4d521
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
4 changes: 4 additions & 0 deletions govc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# changelog

### unreleased

* Add support for file backed serialport devices

### 0.12.0 (2016-12-01)

* Add optional '-host' flag to datastore download/tail commands
Expand Down
10 changes: 9 additions & 1 deletion govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,22 @@ Options:
## device.serial.connect

```
Usage: govc device.serial.connect [OPTIONS]
Usage: govc device.serial.connect [OPTIONS] URI
Connect service URI to serial port.
If "-" is given as URI, connects file backed device with file name of
device name + .log suffix in the VM Config.Files.LogDirectory.
Defaults to the first serial port if no DEVICE is given.
Examples:
govc device.ls | grep serialport-
govc device.serial.connect -vm $vm -device serialport-8000 telnet://:33233
govc device.info -vm $vm serialport-*
govc device.serial.connect -vm $vm "[datastore1] $vm/console.log"
govc device.serial.connect -vm $vm -
govc datastore.tail -f $vm/serialport-8000.log
Options:
-client=false Use client direction
Expand Down
35 changes: 33 additions & 2 deletions govc/device/serial/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package serial
import (
"context"
"flag"
"fmt"
"path"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/mo"
)

type connect struct {
Expand All @@ -45,13 +48,25 @@ func (cmd *connect) Register(ctx context.Context, f *flag.FlagSet) {
f.BoolVar(&cmd.client, "client", false, "Use client direction")
}

func (cmd *connect) Usage() string {
return "URI"
}

func (cmd *connect) Description() string {
return `Connect service URI to serial port.
If "-" is given as URI, connects file backed device with file name of
device name + .log suffix in the VM Config.Files.LogDirectory.
Defaults to the first serial port if no DEVICE is given.
Examples:
govc device.ls | grep serialport-
govc device.serial.connect -vm $vm -device serialport-8000 telnet://:33233
govc device.info -vm $vm serialport-*`
govc device.info -vm $vm serialport-*
govc device.serial.connect -vm $vm "[datastore1] $vm/console.log"
govc device.serial.connect -vm $vm -
govc datastore.tail -f $vm/serialport-8000.log`
}

func (cmd *connect) Process(ctx context.Context) error {
Expand All @@ -62,6 +77,10 @@ func (cmd *connect) Process(ctx context.Context) error {
}

func (cmd *connect) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}

vm, err := cmd.VirtualMachine()
if err != nil {
return err
Expand All @@ -81,5 +100,17 @@ func (cmd *connect) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

return vm.EditDevice(ctx, devices.ConnectSerialPort(d, f.Arg(0), cmd.client, cmd.proxy))
uri := f.Arg(0)

if uri == "-" {
var mvm mo.VirtualMachine
err = vm.Properties(ctx, vm.Reference(), []string{"config.files.logDirectory"}, &mvm)
if err != nil {
return err
}

uri = path.Join(mvm.Config.Files.LogDirectory, fmt.Sprintf("%s.log", devices.Name(d)))
}

return vm.EditDevice(ctx, devices.ConnectSerialPort(d, uri, cmd.client, cmd.proxy))
}
2 changes: 1 addition & 1 deletion govc/flags/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"
)

const Version = "0.12.0"
const Version = "0.12.1"

type version []int

Expand Down
9 changes: 4 additions & 5 deletions govc/test/boot_order_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ govc vm.power -off $id

govc device.cdrom.eject -vm $id

host_ip=$(echo $vnc | awk -F@ '{print $2}' | awk -F: '{print $1}')
serial_port=33233
govc device.serial.add -vm $id > /dev/null
govc device.serial.connect -vm $id $uri telnet://:$serial_port
govc device.serial.connect -vm $id -

echo "booting from network, will timeout then boot from disk..."
govc vm.power -on $id

# capture serial console
echo | nc $host_ip $serial_port 2>/dev/null &
# serial console log
device=$(govc device.ls -vm "$id" | grep serialport- | awk '{print $1}')
govc datastore.tail -f "$id/$device.log" &

ip=$(govc vm.ip $id)

Expand Down
6 changes: 6 additions & 0 deletions govc/test/device.bats
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ load test_helper
run govc device.info -vm $vm $id
assert_success

run govc device.serial.connect -vm $vm -
assert_success

run govc device.info -vm $vm $id
assert_line "Summary: File [$GOVC_DATASTORE] $vm/${id}.log"

uri=telnet://:33233
run govc device.serial.connect -vm $vm -device $id $uri
assert_success
Expand Down
10 changes: 10 additions & 0 deletions object/virtual_device_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,16 @@ func (l VirtualDeviceList) CreateSerialPort() (*types.VirtualSerialPort, error)

// ConnectSerialPort connects a serial port to a server or client uri.
func (l VirtualDeviceList) ConnectSerialPort(device *types.VirtualSerialPort, uri string, client bool, proxyuri string) *types.VirtualSerialPort {
if strings.HasPrefix(uri, "[") {
device.Backing = &types.VirtualSerialPortFileBackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: uri,
},
}

return device
}

direction := types.VirtualDeviceURIBackingOptionDirectionServer
if client {
direction = types.VirtualDeviceURIBackingOptionDirectionClient
Expand Down

0 comments on commit 5b4d521

Please sign in to comment.