Powershell on Cumulus Linux

  • 23 August 2016
  • 5 replies
  • 525 views

Userlevel 2
Microsoft recently released Powershell for Linux. Since Cumulus Linux is Linux, you can install Powershell directly on your Cumulus Linux switch.

Quick Links:
Microsoft Powershell for Linux: https://github.com/PowerShell/PowerShell
Ansible Playbook to install Powershell on Cumulus Linux: https://github.com/plumbis/demos/blob/master/install_powershell.yml
Example Powershell script: https://github.com/plumbis/demos/blob/master/powershell_interfaces.ps1

This was my first experience with Powershell, but the power of Powershell as a Cumulus Linux scripting language is clear. For those coming from a Windows admin background this is a big deal.

First, to install Powershell on Cumulus Linux:
  1. Add Debian Sid and Debian Jessie Apt repos to /etc/apt/sources.list
    deb http://ftp.us.debian.org/debian/ sid main contrib non-free 
    deb http://ftp.us.debian.org/debian/ jessie main contrib non-free
  2. Apt pin libstdc++ to Debian Sid. This is required for the prerequisites for Powershell. To do this, create the file /etc/apt/preferences.d/30_powershell_sid and put the following in the file
    Package: libstdc++6 
    Pin: release n=sid 
    Pin-Priority: 999
  3. Refresh the Apt cache apt-get update
  4. Install libunwind8 and libicu55 Powershell prerequisites
    apt-get install libunwind8 libicu55 
    
  5. Download Powershell .deb for Ubuntu 16.04 from the Powershell Github page
  6. Install Powershell using dpgk -i
  7. Finally, modify the virtual memory limit for the system. This can be done in a temporary (for the length of your SSH session) or permanent manner. Temporary increase is good for one-time Powershell usage or testing. Permanent increase could create issues if a process is out of control. Change with caution. Temporarily Increase Virtual Memory
    ulimit -v unlimited
    Permanently Increase Virtual Memory Edit /etc/security/limits.d/99-cumulus.conf and replace
    1:  soft  as        1048576
    with
    1:  soft  as        unlimited
    This change will take effect on the next user login. Failure to change the virtual memory limits will result in the following error:
    vagrant@spine1:~$ powershell 
    cannot allocate memory for thread-local data: ABORT
  8. Now you can run Powershell with the powershell command!
    vagrant@spine1:~$ powershell 
    PowerShell 
    Copyright (C) 2016 Microsoft Corporation. All rights reserved. 
     
    PS /home/vagrant>
I also created an Ansible playbook that will execute the same steps. This can be found on github.

With Powershell installed we can start automating. I wanted to tackle something simple, since I'm still new to Powershell. A common need for customers is to bring up all of the interfaces on their switch after applying a license. To do this you must add an auto and iface line for every port in the interfaces file. Sounds like a perfect task to automate.

I went about writing a Powershell script to look at the output of /proc/net/dev to find a list of all swp interfaces, and then put populate /etc/network/interfaces with "auto swp" and "iface swp"

My script can be found on my personal github. A few highlights:
First pulling the output of /proc/net/dev and placing it in the variable $proc_dev
$proc_dev = (get-content /proc/net/dev)

Next, looping through the output, splitting the line based on the ":" delimiter and then grabbing only the lines with "swp" in them.
foreach($line in $proc_dev){ 
$temp = $line.split("\:")[0].Trim()
if($temp.contains("swp")){
$iface_array += $temp
}
}
Finally, writing the output to the /etc/network/interfaces file
Write-Output $output_array | Out-File -encoding ASCII /etc/network/interfaces
This is a basic example of what Powershell can do, but beyond this example it highlights the power of an open platform like Cumulus Linux. When the Linux community releases a new tool like Powershell, you can utilize immediately without waiting for patches or updates from the vendor.

Happy Powershelling!

5 replies

Nice work Plumbis.
Hi, I am trying to install PowerShell on Cumulus Linux 3.3.2. Missing libicu55?
Userlevel 2
Hi, I am trying to install PowerShell on Cumulus Linux 3.3.2. Missing libicu55?It looks like a package may have changed in the repo. Let me dig into it and see if I can get an answer for you.
Userlevel 2
Hi, I am trying to install PowerShell on Cumulus Linux 3.3.2. Missing libicu55?SHE, please try again. It looks like we updated the packages in our repo with the 3.4 release and I was able to follow the Microsoft instructions and install Powershell on both 3.3.2 and 3.4.0. You will still need to adjust the memory limits as described in this KB
Hi, I am trying to install PowerShell on Cumulus Linux 3.3.2. Missing libicu55?Now it's working! Thanks.

Reply