{"id":904,"date":"2012-02-27T09:00:32","date_gmt":"2012-02-27T14:00:32","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=904"},"modified":"2012-02-25T09:17:05","modified_gmt":"2012-02-25T14:17:05","slug":"how-vcenter-assigns-mac-addresses","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/how-vcenter-assigns-mac-addresses\/","title":{"rendered":"How vCenter assigns MAC addresses"},"content":{"rendered":"<p>I&#8217;ve recently posted several entries on MAC address conflicts within vCenter:<\/p>\n<p><a href=\"http:\/\/enterpriseadmins.org\/blog\/virtualization\/multiple-vcenters-and-mac-address-conflicts\/\">Multiple vCenters and MAC address conflicts<\/a><br \/>\n<a href=\"http:\/\/enterpriseadmins.org\/blog\/virtualization\/virtual-machines-with-duplicate-mac-addresses\/\">Virtual Machines with Duplicate MAC addresses<\/a><\/p>\n<p>If you look at the first post (and VMware KB <a href=\"http:\/\/kb.vmware.com\/kb\/1024025\">1024025<\/a>) you&#8217;ll notice that vCenter uses a random instance ID to come up with the automatically generated MAC addresses. If you look at the second post, you&#8217;ll notice that it is possible for virtual machines to retain their automatic MAC assignment when moving between vCenters &#8212; and for the source vCenter to reissue that MAC. This can lead to problems with multiple MAC addresses on the same network.<\/p>\n<p>What we need to know now is which virtual machines have automatic MAC addresses generated by another vCenter.<\/p>\n<p>I looked at a couple of vCenter instance IDs and compared them to the automatically generated MAC addresses.  Here is what I found:<br \/>\nvCenter with instance ID 50 will generate a MAC prefix of 00:50:56:b2<br \/>\nvCenter with instance ID 60 will generate a MAC prefix of 00:50:56:bc<\/p>\n<p>The important things to note are that:<br \/>\n1.) only the fourth octet of the MAC changed<br \/>\n2.) the fourth octet changed by exactly 10 in hexadecimal<\/p>\n<p>Converting B2 in hexadecimal returned 178 in decimal.  BC in hexadecimal is 188 in decimal.  Since the fourth octet of the MAC address grows linearly with the instance ID, I took 178 and subtracted the instance ID of 50, which returned 128.  To prove this idea, I then changed the instance ID on a test vCenter to 0, rebooted and then added a network adapter to a VM.  As expected, the fourth octet of the MAC address was 80 (or 128 in decimal).  <\/p>\n<blockquote><p>The automatically generated MAC address has a fourth octet of 128 + the vCenter instance ID converted to hexadecimal.<\/p><\/blockquote>\n<p>With this understanding of how the MAC address is generated, I turned back to PowerCLI to help figure out which virtual machines had automatically assigned MAC addresses from other vCenters.<\/p>\n<pre><code class=\"language-powershell\">\r\n# http:\/\/communities.vmware.com\/thread\/339358\r\n# Modified to find MAC addresses generated from other vCenter servers\r\n$myreport = $null\r\n$global:DefaultVIServers | %{\r\n\tWrite-Host \"Starting vCenter $($_.name)\"\r\n\t$si = Get-View ServiceInstance -Server $_.name\r\n\t$set = Get-View $si.Content.Setting -Server $_.name\r\n\t$vCenterInstanceID = ($set.Setting.GetEnumerator() | where {$_.key -eq \"instance.id\"}).value\r\n\t$vCenterMac4 = [Convert]::ToString((128 + $vCenterInstanceID), 16)\r\n\t$vCenterMacPrefix = \"00:50:56:$vCenterMac4\"\r\n\tWrite-Host \"Expected MAC prefix of $vCenterMacPrefix\"\r\n\t$vCenterName = $_.Name\r\n\t\r\n\t$myreport += Get-View -ViewType VirtualMachine -Server $_.name -Property Name, Config.Hardware.Device -Filter @{\"Config.Template\"=\"False\"} | %{\r\n\t    $viewThisVM = $_\r\n\t    $viewThisVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualEthernetCard]} | %{\r\n\t        New-Object -Type PSObject -Property @{\r\n\t            VMname = $viewThisVM.Name\r\n\t            NICtype = $_.GetType().Name\r\n\t            MacAddress = $_.MacAddress\r\n\t            AddressType = $_.AddressType\r\n\t\t\t\tvCenterName = $vCenterName\r\n\t\t\t\tvCenterMacPrefix = $vCenterMacPrefix\r\n\t        } ## end new-object\r\n\t    } ## end foreach-object\r\n\t}\r\n}\r\n\r\n$myreport | where {$_.MacAddress -notmatch \"^$($_.vCenterMacPrefix)\" -AND $_.AddressType -eq \"assigned\"}\r\n#The caret in the notmatch comparison signifies the start of string in regex\r\n<\/code><\/pre>\n<p>The returned list includes the virtual machines that have automatically assigned MAC addresses that do not match their current vCenter.  I know exactly which virtual machines need new\/modified MAC addresses to resolve my duplicate MAC issues &#8212; without them coming back in the future.<\/p>\n<p>I have tested the following code to get vCenter to generate a new automatic MAC address for a virtual machine:<\/p>\n<pre><code class=\"language-powershell\">\r\n$thisAdapter = Get-NetworkAdapter VMNAME\r\n$thisAdapter.ExtensionData.AddressType = \"Generated\"\r\n$thisAdapter.ExtensionData.MacAddress = \"\"\r\nSet-NetworkAdapter $thisAdapter -confirm:$false\r\n<\/code><\/pre>\n<p>Virtual Machines will need to be powered off\/on for the guest operating system to be made aware of this change.  Testing shows that Windows virtual machines retain static IP configuration after this change, but Linux guests detect a new network adapter and required re-setting IP information.  Your mileage may vary, as always, test before making changes in your production environment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently posted several entries on MAC address conflicts within vCenter: Multiple vCenters and MAC address conflicts Virtual Machines with Duplicate MAC addresses If you look at the first post (and VMware KB 1024025) you&#8217;ll notice that vCenter uses a &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/how-vcenter-assigns-mac-addresses\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-904","post","type-post","status-publish","format-standard","hentry","category-scripting","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=904"}],"version-history":[{"count":7,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":912,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/904\/revisions\/912"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}