Programming Snippets

To keep this readable for everyone on this globe, these pages will be in english.

The first snippet is about tinyMCE, a WYSIWYG javascript editor.
Many try to use it with ajax, but get to a point where it won’t be “initialized” after a page refresh.
The problem is, that the page will be refreshed, but the javascript or better, the tinyMCE object isn’t.
My solution first removes all the old editors in the tinyMCE.editors object. This works perfect for me.
Because I use php and jquery, you may find these back in my examples.

 $("#$FormId").submit( function () { if ("object" == typeof tinyMCE) {
                                                    tinyMCE.triggerSave();

                                                    l = tinyMCE.editors.length;
                                                    for (var i = 0;i < l;++i) {
                                                        tinyMCE.editors[0].remove();
                                                    }
                                                }

I wrote this script because I wanted some checks before I wanted to start the actual creating of the new virtual machine.
It has some build in checks, like checking the existance of the image file.

#!/bin/bash

# $1 = name of the virtual machine
# $2 = amount of ram 1024 By default
# $3 = disksize , 10Gig by default

KVMPATH=/home/kvm

NAME=$1
RAM=${2:-1024}
DISKSIZE=${3:-12}

if [ -z "$NAME" ] ; then

cat <<EOT
usage :
kvm-install.sh NAME [[RAM(MB)] [DISKSIZE(GB)]]
EOT

exit 0;

fi

echo "Creating a KVM machine with Name : ${NAME} , RAM : ${RAM}M , Disk : ${DISKSIZE}G"
echo "Is this correct ? [Y/n]"

read key 

if [ "$key" == "n" -o "$key" == "N" ] ; then
	exit 0
fi

IMGFILE=${KVMPATH}/${NAME}.img 


if [ -e ${IMGFILE} ] ; then

	IMGSIZE=`ls -lah ${IMGFILE} | awk '{ print $5}'`

	echo "${IMGFILE} with a size of ${IMGSIZE} already exists.. Remove it manually if needed"
	echo "Do you want to continue ? [Y/n]"
	read key

	if [ "$key" == "n" -o "$key" == "N" ] ; then
        	exit 0
	fi
fi	

virt-install 	--name $1 
		--arch x86_64 
		--vcpus=1,maxvcpus=2 
		--ram ${RAM} 
		--connect qemu:///system 
 		--os-variant=rhel6 
		--os-type=linux 
		--disk ${IMGFILE},size=${DISKSIZE} 
		--location ftp://ftp.nluug.nl/pub/os/Linux/distr/CentOS/6.3/os/x86_64/ 
		-x "console=ttyS0" 
		--nographics

This snippet is about a kvm-image which haas been cloned and does not have a logic sequence of nics anymore

# After cloning a KVM image, it happens that another network nic is created.
# Be logged in via a console ! Do not rely on SSH, because we alter the nic "cards".
# A reboot may be neccassary
# This snippet is done on a CentOS 6 machine.

# The machine is cloned and fired up.
# This is where the network scripts life..
cd /etc/sysconfig/network-scripts/
# Rename the current nic numbered 'x' to zero again.
mv ifcfg-ethx  ifcfg-eth0

# lets change the network nic name. 
vi ifcfg-eth0
# then change DEVICE=ethx to DEVICE=eth0 and save with :wq
:wq

# delete the file which udev uses for naming the nics
rm /etc/udev/rules.d/70-persistent-net.rules

# Start udev again. This is also done with a reboot, but we'll wait with that..
start_udev

#check the contents of 70-persistent-net.rules which should sound something like 
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:30:48:9e:0a:56", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# Restart the network
/etc/init.d/network restart

# If the nic hasn't been altered back to eth0 because of an error.
# for example is complains about a device not existing, then do a reboot.
# If you still get the message from udev that the NIC has been renamed from eth0 to ethX then 
# checkout the hw-address from the corresponding nic by typing
virsh dumpxml <DOMAINNAME>

# Which should give an output with something like below ( mine has 2 NICs )
<interface type='bridge'>
      <mac address='52:54:00:6b:d5:01'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
      <mac address='52:54:00:22:d2:a1'/>
      <source bridge='br1'/>
      <target dev='vnet1'/>
      <model type='virtio'/>
      <alias name='net1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</interface>

# Now copy and paste the hw-address to the /etc/sysconfig/network-scripts/ifcfg-eth0 script.
# With this the problem should be solved.

# that's it..

Leave a Comment

*

Wordpress Code Snippet by Allan Collins