Sunday 10 October 2010

xenconsole-ng

Hi there, long time I haven't used this blog!!

I'll explain how to create an Ubuntu 10.04 installation on a XenServer without using XenConsole at all!!

First of all, I'll assume you already have XenServer 5.6.0 or newer installed, and also the linux tools templates.

Then, we will create the template for the network install of our Ubuntu 10.04. Thanks to Anthony Francis for his post and his script makeubuntu.sh

Basically what this script does is to create a new template using xe commands based on the Debian Lenny template already existent.

Once done, we can create our own installation by issuing the following commands:


VM_NAME="TheVMNameIWant"
VM_UUID=$(xe vm-install new-name-label=$VM_NAME template="Ubuntu 10.04 (32-bit)")
PIF_UUID=$(xe network-list bridge=xenbr0 --minimal)
VIF_UUID=$(xe vif-create network-uuid=PIF_UUID vm-uuid=$VM_UUID device=0)
xe vm-param-set uuid=$VM_UUID other-config:disable_pv_vnc=1 #To disable automatic XenCenter console. Be careful as if we do not do this, we will experience insane behaviors when connecting through SSH


Now you already have your new fresh vm installation, but waiting to be started and controlled so everything gets installed. To do so you can issue these commands or better, use my scripts at the bottom of this post ;)


#so now start the new VM
xe vm-start uuid=$VM_UUID
#search for the new domain id
DOM_ID=$(xe vm-list params=dom-id uuid=$VM_UUID --minimal)
#and connect to it
/usr/lib/xen/bin/xenconsole $DOM_ID


Scripts section:

cat << EOF >/root/bin/xenconsole-ng
#!/bin/bash

RUNNING_VM_NAMES=$(xe vm-list power-state=running is-control-domain=false params=name-label --minimal)

echo "List of running virtual machines to attach to:"
i=0
for vmn in $RUNNING_VM_NAMES
do
echo "$i - $vmn"
i=$(expr $i + 1)
done
i=$(expr $i - 1)
echo -n "Select a number (0-$i): "
read s

i=0
for vmn in $RUNNING_VM_NAMES
do
if [[ $i -eq $s ]] ;
then
echo -n "Searching vm $i ($vmn) ... "
VM_UUID=$(xe vm-list name-label=$vmn params=uuid --minimal)
DOM_ID=$(xe vm-list name-label=$vmn params=dom-id --minimal)
if [[ -z "$DOM_ID" ]]
then
echo "FATAL!! No Domain found for that vm!"
exit -2
else
echo "(if nothing happends press enter key)"
echo "Remember, Ctrl+] on guest or xenconsole-dettach-ng on host escapes this console"
/usr/lib/xen/bin/xenconsole $DOM_ID 2>/dev/null
reset
fi
exit 0
else
i=$(expr $i + 1)
fi
done
echo "Virtual Machine not found!"
exit -1
EOF

cat << EOF > /root/bin/xenconsole-dettach-ng
#!/bin/bash

RUNNING_VM_NAMES=$(xe vm-list power-state=running is-control-domain=false params=name-label --minimal)

ATTACHED_CONSOLES=$(ps aux | grep "/xenconsole " | sed "s/ */ /g" | cut -f 12 -d " ") #field 2=pid, 12=dom_id

echo "List of running virtual machines to dettach from:"
i=0
avmn=""
avmp=""
for vmn in $RUNNING_VM_NAMES
do
DOM_ID=$(xe vm-list name-label=$vmn params=dom-id --minimal)

if [[ -z "$(echo $ATTACHED_CONSOLES | grep $DOM_ID)" ]]
then
echo "$vmn not attached"
else
echo -n "$vmn attached with pid "
PID=$(ps aux | grep "/xenconsole $DOM_ID" | sed "s/ */ /g" | cut -f 2 -d " ")
echo "$PID"
avmn="$avmn $vmn"
avmp="$avmp $PID"
fi
i=$(expr $i + 1)
done

i=0
for vmn in $avmn
do
echo "$i - $vmn"
i=$(expr $i + 1)
done
i=$(expr $i - 1)
echo -n "Select a number (0-$i): "
read s

i=0
for pid in $avmp
do
if [[ $i -eq $s ]] ;
then
echo -n "Dettaching console $i ($pid) ..."
kill -9 $pid
echo " Done!"
exit 0
else
i=$(expr $i + 1)
fi
done
echo "Console not found!"
exit -1
EOF


Sure they can be polished, or even completely rewritten, but they work for me and I just wanted to share with you all ;) and have them for the records :P

Thanks also to this document site where I finally found a way to connect to the text-based console and much much more information ;)

Of course, you can alo use OpenXenManager for Linux or XenCenter for Windows, but my way is more geeky ;)