One of the things that I do when configuring my hosts after kickstart is setup a kernel interface and enable vMotion for that port group. This isn’t too difficult, but takes a little bit of futzing with some vmware-vim-cmd
results to get the data we need.
Since I’m on the subject, I figured I may as well do the same thing using the SDK, which eliminates one more thing that the rCLI can’t do in order for me to configure a new ESX host completely.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#!/bin/bash # # TODO: # Add the ability to specify parameters at the command line (bash getopt...) # Check to see if the vSwitch exists and throw an error if not # Play with the PG_IDENT line to see if the command piped to "head -1" will work # ### vMotion setup ### VMOTION_VSWITCH='vSwitch1' VMOTION_PG_NAME='VMotion' VMOTION_IP='4.3.2.1' VMOTION_VLAN=1234 # Create the port group esxcfg-vswitch --add-pg=$VMOTION_PG_NAME $VMOTION_VSWITCH esxcfg-vswitch --vlan=$VMOTION_VLAN --pg=$VMOTION_PG_NAME $VMOTION_VSWITCH # Connect the vKernel esxcfg-vmknic --add --ip $VMOTION_IP --netmask 255.255.255.0 $VMOTION_PG_NAME echo "VMotion kernel interface created" # Let's make sure we can get the PG_IDENT by restarting the mgmt service # This should give everything a chance to settle down...probably not necessary # but doesn't hurt also service mgmt-vmware restart sleep 30 # Actually turning on vMotion is a PITA...ESX uses an internal identifier to # determine which group is which, so we must discover that port group id, # and use it to enable vMotion on the correct kernel interface # # What we are actually doing here is asking the kernel for the network config, # greping for the portgroup with the name we just created (along with the two # lines before it, which the second of those contains the ident we want), narrow # it down to the single line we want, then getting only the part of the line we # want. PG_IDENTIFIER=`vmware-vim-cmd hostsvc/vmotion/netconfig_get | grep -B2 -e "portgroup.*$VMOTION_PG_NAME" | grep device | awk '($3 ~ "vmk") { print substr($3, 2, 4) }'` # use the derived interface to configure vmotion if [ ! -z ${PG_IDENTIFIER} ]; then echo "Attempting to configure vmotion using $PG_IDENTIFIER" vmware-vim-cmd hostsvc/vmotion/vnic_set $PG_IDENTIFIER else echo "Unable to determine PG_IDENTIFIER for vmotion port group, skipping vmotion setup" fi |
VMware has a knowledgebase article on how to enable vMotion from the command line, which includes sample output from the vmware-vim-cmd hostsvc/vmotion/netconfig_get
command.
vMotion can also be enabled via the SDK using perl (and POSH, but I’ll leave that to Glenn).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
#!/usr/bin/perl -w # # cfg-host-vmotion.pl - written by Andrew Sullivan, 2008-12-31, http://www.get-admin.com # # Please report bugs and request improvements at http://get-admin.com/blog/?p=271 # # This script will display the VMotion eligible portgroups/virtual NICs, disable VMotion, # or enable VMotion for an eligible portgroup/vNIC. # # Examples: # List the Portgroup and vNIC identifier for eligible for VMotion, will also show # which portgroup is currently enabled for VMotion # ./cfg-host-vmotion.pl --server your.vc.server --vihost your.esx.host # # Disable VMotion for a host # ./cfg-host-vmotion.pl --server your.vc.server --vihost your.esx.host --disable # # Enable VMotion on a host using the portgroup name # ./cfg-host-vmotion.pl --server your.vc.server --vihost your.esx.host --enable pg_name # # Enable VMotion on a host using the vnic identifier # ./cfg-host-vmotion.pl --server your.vc.server --vihost your.esx.host --enable vmk1 # use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../"; use VMware::VIRuntime; use Switch; # add some additional options my %opts = ( 'vnic' => { type => ":s", help => "The adapter to enable vMotion on. Must be one of the valid adaptors " . "found by the --action list option. Can be specified as the internal identifier " . "(vmk0), or as the name of a portgroup that has been assigned to the kernel.", required => 0 }, 'action' => { type => ":s", help => "What to do? list = show vMotion eligable adaptors, disable = disable " . "vMotion, enable = enable vMotion on vnic specified", required => 0, default => "list" }, 'vihost' => { type => "=s", help => "Which ESX host to get or modify vMotion on", required => 1 } ); Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); my $host = Opts::get_option('vihost'); my $hostView = Vim::find_entity_view( view_type => 'HostSystem', filter => { 'name' => qr/($host)/i } ); # yes, I know: globals are evil, yadda, yadda use vars qw($hostVMotionView); $hostVMotionView = Vim::get_view( mo_ref => $hostView->configManager->vmotionSystem ); switch (Opts::get_option('action')) { case "list" { my $candidates = getCandidates(); my $current = ""; print "The following port groups on host " . $hostView->name . " are eligible for VMotion:n"; foreach (@$candidates) { print "t" . $_->portgroup . " ( " . $_->device . " )n"; if ($_->key eq $hostVMotionView->netConfig->selectedVnic) { $current = $_->portgroup; } } if ($current ne "") { print "nCurrent VMotion Portgroup is: " . $current . "n"; } } case "disable" { eval { $hostVMotionView->DeselectVnic(); }; if ($@) { Util::trace(0, "Error: " . $@ . "n"); } } case "enable" { eval { my $candidates = getCandidates(); my $found = "false" ; my $identifier = ""; foreach (@$candidates) { if ($_->device eq Opts::get_option('vnic') || $_->portgroup eq Opts::get_option('vnic')) { $found = "true"; $identifier = $_->device; } } if ($found eq "true") { $hostVMotionView->SelectVnic( 'device' => $identifier ); } else { Util::trace(0, "Unable to find the specified portgroup or vnic"); } }; if ($@) { Util::trace(0, "Error while enabling VMotion on vnic: " . $@ . "n"); } } } Util::disconnect(); sub getCandidates { my $candidates = $hostVMotionView->netConfig->candidateVnic; return $candidates; } |
Using the above perl script, you can configure VMotion from start to finish using the rCLI and Perl Toolkit like so…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# create a new vSwitch vicfg-vswitch.pl --server your.vc.server --vihost your.esx.host --add vSwitch1 # give it some NIC connections vicfg-vswitch.pl --server your.vc.server --vihost your.esx.host --link vmnic0 vSwitch1 vicfg-vswitch.pl --server your.vc.server --vihost your.esx.host --link vmnic4 vSwitch1 # add a portgroup for VMotion vicfg-vswitch.pl --server your.vc.server --vihost your.esx.host --add-pg VMotion vSwitch1 # set the VLAN if necessary vicfg-vswitch.pl --server your.vc.server --vihost your.esx.host --vlan 1234 --pg VMotion vSwitch1 # Give the portgroup to the Kernel vicfg-vmknic.pl --server your.vc.server --vihost your.esx.host --add --ip 1.2.3.4 --netmask 255.255.255.0 VMotion # enable VMotion for that portgroup cfg-host-vmotion.pl --server your.vc.server --vihost your.esx.host --enable VMotion |