Online Promoters™ :: Web Development made simple

XenServer: Adding and removing associated network cards to Virtual Machines


Scripts for adding and removing associated network cards to Virtual Machines 

In order to easily add and remove virtual network cards for your Virtual Machines in XenServer I made 2 bash scripts.
Be careful when handling scripts on the host (XenServer) !

What you have to do:

- find below the 2 bash scripts I made;
- put them in a folder on your HOST machine (the XenServer itself);
- change the scripts permissions to: chmod +x

<< Bash script for Adding the network interface >>


#!/bin/bash

## http://online.promoters.ro/
## 17 January 2013
## Adding a network interface associated to a VM ###

if [ "$1" != "" ];then
### note: depending on you xenserver version you might want to use "xe list-network"
xe network-list | grep -A1 -e uuid
namelabel=`xe vm-param-get uuid=$1 param-name=name-label`
if [[ "$2" != "" && "$3" != "" ]];then
echo "Adding the new network interface with uuid $2 to the $namelabel VM"
xe vif-create vm-uuid=$1 network-uuid=$2 device=$3
echo "Done!"
else
echo "Please specify the network's uuid you want to ADD for the $namelabel VM and the device number"
echo "Usage: xe vif-create network-uuid=3f180a28-357d-2a4f-54fc-c0f420ee7659 vm-uuid=$1 device=0"
fi
else
echo "Syntax: `basename $0` "
echo ""
echo "Please specify VM's uuid from which you want to remove the network interface:"
xe vm-list is-control-domain=false |grep -A1 uuid

fi

<< Bash script for Removing the network interface >>


#!/bin/bash

## http://online.promoters.ro/
## 17 January 2013
## Removing a network interface associated to a VM ###
if [ "$1" != "" ];then
        xe vif-list vm-uuid=$1 | grep -A1 -e uuid
        namelabel=`xe vm-param-get uuid=$1 param-name=name-label`
                if [ "$2" != "" ];then
                echo "Removing the network interface with uuid $2"
                xe vif-destroy uuid=$2
                echo "Done!"
                else
                echo "Please specify the Network vif uuid you want to REMOVE for the $namelabel VM"
                fi
        else
        echo ""
        echo "Syntax: `basename $0`  "
        echo ""
        echo "Please specify VM's uuid from which you want to remove the network interface:"
        xe vm-list is-control-domain=false |grep -A1 uuid

fi