VMware Cloud Community
totoroliu
Enthusiast
Enthusiast
Jump to solution

How to use vCLI to add/define "Virtual Serial Port" on Linux?

Hi,

Is there any vSphere CLI command avaiable in ESX 6.5 to add/define "Virtual Serial Port" to a VM?

assume targeting to the host directly without going through vCenter or vMA.

Someone has suggested using PowerCLI, but wondering if vCLI could acheive too.

Rick

Reply
0 Kudos
1 Solution

Accepted Solutions
jasnyder
Hot Shot
Hot Shot
Jump to solution

When you say use vCLI to do this, are you OK with using the perl modules that come with the vCLI package?

If so, then I wrote a perl script to add a serial device to a VM, which you can use.

Put these lines into a new file called add-serial.pl in the vmware-vsphere-cli-distrib/apps/vm folder wherever you unpacked the vCLI package (for instance ~/Downloads/vmware-vsphere-cli-distrib/apps/vm).  chmod +x the file after saving.

#!/usr/bin/perl -w

use strict;

use warnings;

use FindBin;

use lib "$FindBin::Bin/../";

use VMware::VIRuntime;

use AppUtil::HostUtil;

use AppUtil::VMUtil;

$Util::script_version = "1.0";

my %opts = (

   vm => {

      type => "=s",

      help => "The VM name to perform the operation on",

      required => 1,

      default => "",

   }

);

Opts::add_options(%opts);

Opts::parse();

my $vmName = Opts::get_option('vm');

my %filterhash = ();

Util::connect();

my $vm_views = VMUtils::get_vms('VirtualMachine',

                                   $vmName,

                                   undef,

                                   undef,

                                   undef,

                                   undef,

                                   %filterhash);

my $vm_view = shift (@$vm_views);

if($vm_view) {

      my $serial;

   

      my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('add');

      my $serial_backing_info

         = VirtualSerialPortURIBackingInfo->new(direction => "server",

                        serviceURI => "telnet://:7003");

    my $connectInfo = VirtualDeviceConnectInfo->new(allowGuestControl => 1,

                            connected => 1,

                            startConnected => 1);

      $serial = VirtualSerialPort->new(key => -1,

                                  backing => $serial_backing_info,

                   connectable => $connectInfo,

                   yieldOnPoll => 1);

    my $devspec = VirtualDeviceConfigSpec->new(operation => $config_spec_operation,

                                                 device => $serial);

    my @devspecs = ( $devspec );

    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => \@devspecs);

    $vm_view->ReconfigVM( spec => $vmspec );

}

Util::disconnect();

This is run using ./add-serial.pl --server esx_server_name --username root --password root_password -vm vm_name

View solution in original post

11 Replies
jasnyder
Hot Shot
Hot Shot
Jump to solution

When you say use vCLI to do this, are you OK with using the perl modules that come with the vCLI package?

If so, then I wrote a perl script to add a serial device to a VM, which you can use.

Put these lines into a new file called add-serial.pl in the vmware-vsphere-cli-distrib/apps/vm folder wherever you unpacked the vCLI package (for instance ~/Downloads/vmware-vsphere-cli-distrib/apps/vm).  chmod +x the file after saving.

#!/usr/bin/perl -w

use strict;

use warnings;

use FindBin;

use lib "$FindBin::Bin/../";

use VMware::VIRuntime;

use AppUtil::HostUtil;

use AppUtil::VMUtil;

$Util::script_version = "1.0";

my %opts = (

   vm => {

      type => "=s",

      help => "The VM name to perform the operation on",

      required => 1,

      default => "",

   }

);

Opts::add_options(%opts);

Opts::parse();

my $vmName = Opts::get_option('vm');

my %filterhash = ();

Util::connect();

my $vm_views = VMUtils::get_vms('VirtualMachine',

                                   $vmName,

                                   undef,

                                   undef,

                                   undef,

                                   undef,

                                   %filterhash);

my $vm_view = shift (@$vm_views);

if($vm_view) {

      my $serial;

   

      my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('add');

      my $serial_backing_info

         = VirtualSerialPortURIBackingInfo->new(direction => "server",

                        serviceURI => "telnet://:7003");

    my $connectInfo = VirtualDeviceConnectInfo->new(allowGuestControl => 1,

                            connected => 1,

                            startConnected => 1);

      $serial = VirtualSerialPort->new(key => -1,

                                  backing => $serial_backing_info,

                   connectable => $connectInfo,

                   yieldOnPoll => 1);

    my $devspec = VirtualDeviceConfigSpec->new(operation => $config_spec_operation,

                                                 device => $serial);

    my @devspecs = ( $devspec );

    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => \@devspecs);

    $vm_view->ReconfigVM( spec => $vmspec );

}

Util::disconnect();

This is run using ./add-serial.pl --server esx_server_name --username root --password root_password -vm vm_name

totoroliu
Enthusiast
Enthusiast
Jump to solution

i am looking for more in bash/shell/python script~

Reply
0 Kudos
totoroliu
Enthusiast
Enthusiast
Jump to solution

jasnyder

by the way,

where could I find the documentation regarding to these Perl API library?

I searched through vSphere CLI for vSphere 6.5 - VMware {code}

it doesn't mention Perl at all.

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

There's no "Perl" API, this is simply a Perl language binding to the vSphere API (http://pubs.vmware.com/vsphere-6-5/index.jsp?topic=/com.vmware.wssdk.apiref.doc/index.html&single=tr... which is where you'll want to look for how the API works and then consume using the vSphere SDK for Perl (which vCLI is uses) or any of our other vSphere SDKs. I've also got plenty of vSphere API examples using variety of SDKs including Perl, which you can have a look here GitHub - lamw/vghetto-scripts: Various virtuallyGhetto scripts for VMware based solutions

Reply
0 Kudos
totoroliu
Enthusiast
Enthusiast
Jump to solution

Is API available directly on the hypervisor host?

or the API available through only vSphere vCenter or vMA VM?

We are using Enterprise license on ESX hypervisor host

but we don't have vSphere vCenter nor vMA VM.

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

There's a vSphere API end point on both ESXi and vCenter. Obviously, certain functionality will only be available w/vCenter. For your use case, you can talk directly to ESXi host (vMA is merely an Appliance w/Perl SDK pre-installed. Its nothing more than that and its been deprecated (vSphere Management Assistant Deprecation - VMware vSphere Blog ) in favor of providing customers with choice of location to install and access their vSphere SDKs .

Take a look at the following blog posts to give you idea of how vSphere API works and recommend to setup a lab and trying them out to get a better understanding of how they work

Introduction to the vSphere API Part 1 - VMware vSphere Blog

Introduction to the vSphere API Part 2: Object Model - VMware vSphere Blog

Introduction to the vSphere API Part 3 - Inventory Hierarchy - VMware vSphere Blog

totoroliu
Enthusiast
Enthusiast
Jump to solution

Does command-line tool have same function/feature sets as API?

or command-line tool is very limited compared to API?

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

I'm not sure what command-line tool you're referring to, please be more specific or give an example

Reply
0 Kudos
jasnyder
Hot Shot
Hot Shot
Jump to solution

I think the issue is that there are numerous command line utilities available from all sorts of places, but none of them succinctly expose all vSphere API functions to allow one-line commands to do actions that are perceived as simple and mundane. 

But if you dig into it a little bit, this would probably not really be possible (or at the very least not practical), and the language bindings available cover every scenario you likely need (whether it's govmomi, pyvmomi, the vCLI Perl bindings, or PowerCLI).  The problem is getting familiarity with the way the vSphere API does business and with one or more of the languages/tools available to consume it.

For one-line script calls, PowerCLI probably comes the closest to meeting this requirement, but many users (usually Linux users) avoid PowerCLI because of the traditional Windows requirement.  Even still, you are able to develop your own command line to do the things you need easily and call it from bash script.  This could be in Go, or Python, or Perl, or Java, or C#, or whatever thing you love to write in.  (all of those can compile and/or run on Linux and Windows)

But, as with anything, flexibility increases complexity, while simplicity decreases flexibility.  True elegance comes from striking the right balance.

totoroliu
Enthusiast
Enthusiast
Jump to solution

lamw

the command-line tools i refer to are like:

esxcli, vmware-cmd, vim-cmd, esxcfg, vicfg, etc

Right now,

I'm able to start/stop/register/unregsiter a VM

either using these command-line tools directly

or using API through scripts.

Comparing with command-line tool to API,

I found command-line tool is more stragihtforward and easy to use.

So I thought there should be one command that corresponding to add/define "Virtual Serial Port".

Reply
0 Kudos
totoroliu
Enthusiast
Enthusiast
Jump to solution

Now I'm using this hacky way to do it directly on ESX hypervisor host:

1. Get VMID

VMID=$(vim-cmd vmsvc/getallvms | sed -n "s:^([0-9])\s${VM_NAME}\s.$:\1:p")

2. Unregister VM

vim-cmd vmsvc/unregister "${VMID}"

3. Update .vmx file to add/define "Virtual Serial Port"

echo "

serial0.fileName = "telnet://:7003"

serial0.fileType = "network"

serial0.present = "TRUE"

serial0.yieldOnMsrRead = "TRUE"

" >> /vmfs/volumes/datastore1/${VM_NAME}/${VM_NAME}.vmx

4. Register VM

vim-cmd solo/registervm /vmfs/volumes/datastore1/${VM_NAME}/${VM_NAME}.vmx