Technology Weblog


There are many times when we need to search and replace certain words from files. Doing it manually will be very time consuming. One way to do it is using VI ( my favorite editor !) search n’ replace.

The better option is using perl command line search and replace option. The syntax is the following.

# perl -i.bak -p -e’s/old/new/g’ filename


what this does is searches for pattern “old” and replace it pattern “new”.The g option in does the serch n replace globally in the file, otherwise perl searches and replaces only the first instance of the pattern.

Lets explain the options used.

-e option allows you to define Perl code to be executed by the compiler. For example, it’s not necessary to write a “Hello World” program in Perl when you can just type this at the command line.

# perl -e ‘print “Hello World\n”‘

-p option, adds loops around your -e code.It creates code like this:

while (<>) {
# your code goes here
} continue {
print or die “-p destination: $!\n”;

-i option. Actually, Perl renames the input file and reads from this renamed version while writing to a new file with the original name. If -i is given a string argument, then that string is appended to the name of the original version of the file. For example, to change all occurrences of “PHP” to “Perl” in a data file you could write something like this:

# perl -i -pe ‘s/PHP/Perl/g’ file.txt

Perl reads the input file a line at a time, making the substitution, and then writing the results back to a new file that has the same name as the original file — effectively overwriting it. If you’re not so confident of your Perl abilities you might take a backup of the original file, like this:

# perl -i.bak -pe ‘s/PHP/Perl/g’ file.txt

There are lots of online recources to learn shell scripting. I have list the best 4 of them and they cover mainly the my favourite shell BASH ( the best one too !).

Enjoy !


We can generate random passwords using mkpasswd utility


By default mkpasswd generates alpha-numeric passwords of length 9 with one special character. We can define length using -l option and special characters using option -s. Some times special characters in password may cause problems.

#mkpasswd -l 8 -s 0

The above command creates a random password of length 8 with 0 special characters.


By default, MySQL Server will be installed with root superuser without any password. You can connect to MySQL server as root without requiring password or by keying in blank password. However, if you have set the password for root and forget or unable to recall the password, then you will need to reset the root password for MySQL.Login as root to the Unix-like (Unix, Linux or BSD) machine with the MySQL server.

Stop the MySQL server by using either of the following command

#/etc/init.d/mysql stop

Now you need to Start MySQL server without password

# mysqld_safe --skip-grant-tables &

Connect to mysql server using mysql client with the following command

# mysql -u root

Now you should be having mysql prompt


Now you need to Setup new MySQL root user password

mysql> use mysql;

mysql> update user set password=PASSWORD(”newrootpassword”) where user=’root’;

mysql> flush privileges;

mysql> quit

Note: Replace newrootpassword with the new root password for MySQL server. Flush Privileges is needed to making the password change effect immediately.

Now you need to Stop MySQL Server using the following command

# /etc/init.d/mysql stop

Test Your New Mysql root password

First you need to start mysql server using the following command

# /etc/init.d/mysql start

# mysql -u root -p

Now it will prompt for root password and enter your new root password



Installing and Configuring Xen

Xen is a server virtualisation solution that works on the principle of Paravirtualisation. Paravirtualization requires modifying the operating system to run on Xen. Paravirtualized operating systems “know” that they are virtualized. This is achieved by modifying the kernel. Xen is a virtual machine hypervisor.

Xen Hypervisor Requirements

  • A pre-existing Linux or BSD installation as kernel needs to be modified, preferably something running Kernel 2.6 and having Grub as bootloader.
  • x86-compatible system with at least 256MB RAM. More is better.
  • 5GB of spare disk space additional to the disk space you need to install your favorite distribution.
  • Hardware based Xen virtualisation requires Intel VT or AMD-V ( Pacifica ) Processor. To check for Intel VT support look for the ‘vmx’ flag, or for AMD-V support check for ‘svm’ flag.

Checking if the processor supports hardware virtualisation.

For Intel

cat /proc/cpuinfo|grep vmx


cat /proc/cpuinfo|grep vmx

Installing Xen

There are 3 ways of installing Xen. But installing using yum is the fastest and easiest option. Following are the 3 methods by which Xen can be installed.

Using Source-code.

Using RPM.

Using YUM.

Installing from Source code

Download the tarball of required version of Xen from Then uncompress the tar files.

# tar xzf xen-3.0.3_0-install-x86_32.tgz

Change into the source code directory. Check out the README file for installation procedure. Run the installation script installation script

# sh ./

After the xen kernel is compiled we have to add the entry to grub.conf . An sample grub.conf is given in the end.

Installing using RPM

Download the xen and xen-kernal RPM’s according to your OS from and the Xen version you require



If you plan to develop with Xen, only then download and install the relevant xen-devel-* RPM to get the header files and development libraries. Otherwise the xen-development package is not required.

Install each of the the RPM’s as shown below.

# rpm -ivh xen-3.1.0-1.i386.rpm

# rpm -ivh kernel-xen-2.6.18-3.1.0.i386.rpm

After this you need to edit your system’s grub config and add a stanza for the Xen hypervisor.

Installation using Yum

The easiest way to install Xen and Xen-kernel is using yum. You can install the Xen kernel and tools by running the following command:

# yum install kernel-xen xen

If you need virt-install program or in the GUI program virt-manager then you also need to install the software as given below.

# yum install virt-manager
After this you have to edit the grub.conf and add the Xen hypervisor section.

An example of a correctly configured /etc/grub.conf file is given below.





title Fedora Core (2.6.18-1.2798.fc6xen)

root (hd0,0)

kernel /boot/xen.gz-2.6.18-1.2798.fc6

module /boot/vmlinuz-2.6.18-1.2798.fc6xen ro root=LABEL=/

module /boot/initrd-2.6.18-1.2798.fc6xen.img

title Fedora Core (2.6.18-1.2798.fc6)

root (hd0,0)

kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=LABEL=/

initrd /boot/initrd-2.6.18-1.2798.fc6.img


After reboot the machine into the xen kernel. Once the system is booted into the Xen kernel, check to verify the kernel and that Xen is running:

# uname -r

2.6.18-1.2798.fc6xen ( This shows the Xen kernall is running ).

# xm list

Name ID Mem(MiB) VCPUs State Time(s)

Domain-0 0 492 1 r—– 1041.7 (Shows the setting of Dom-0)

Building a Guest System on Xen

There are many methods of installing the Guest OS like using virt-install , virt-manager ( GUI option) or using images of OS’s. Using the images of OS’s is the easiest option. We can download the images from sites like and use it as a template any number of times. This is also the fastest as we need not install the OS’s.

Building a Guest System using `virt-install`

Start the interactive install process by running the virt-install program:

# /usr/sbin/virt-install

The following questions about the new guest OS will be presented. This information can also be passed as command line options; run with an argument of –help for more details. Lets us consider the case of installing Fedora Core 6 as the Guest OS.

1.What is the name of your virtual machine? This is the label that will identify the guest OS. This label will be used for various xm commands and also appear in virt-manager the Gnome-panel Xen applet. In addition, it will be the name of the /etc/xen/<name> configuration file that stores the guest’s configuration information.

2. How much RAM should be allocated (in megabytes)? This is the amount of RAM to be allocated for the guest instance in megabytes (eg, 256). Installation requires atleast 256 MB of RAM.

3. What would you like to use as the disk (path)? The local path and file name of the file to serve as the disk image for the guest (eg, /home/nitin/xenfc). This will be exported as a full disk to your guest.

4. How large would you like the disk to be (in gigabytes)? The size of the virtual disk for the guest (only appears if the file specified above does not already exist). 4.0 gigabytes is a reasonable size for a “default” install

5. Would you like to enable graphics support (yes or no): Should the graphical installer be used?

6. What is the install location? This is the path to a Fedora Core 6 installation tree in the format used by anaconda. NFS, FTP, and HTTP locations are all supported. Examples include: * * *

The installation will then commence. If graphics were enabled, a VNC window will open and present the graphical installer. If graphics were not enabled, the standard text installer will appear. Proceed as normal with the installation.

Building a Guest System using Images of OS’s

In this process we download the images of root file system of required OS, swap . We then create an an initrd image for the DomU and xm (Xend manager ) to mount it as Root file system. Swap and Initrd of the DomU we are creating. We will consider the case of building a Centos DomU now.

1. Download the DomU image of required OS from or any other site providing OS images for virtualisation in a to a convenient location on the machine.

2. Untar the downloaded file. The tar file from normally contains the following files.

  1. centos.5-0.img (Image of OS file system )
  2. centos.swap (Swap of OS)
  3. centos.cfg ( Xen configuration file for OS)

3. Create the initrd for DomU using mkinitrd command specifying the kernel version of Dom0.

# mkinitrd /boot/initrd-centos-5.img 2.6.20-1.3001.fc6xen

Some times the initrd images created using above command may not be able to mount the root file system causing Kernel panic. To avoid this create the initrd specifying the following options.

# mkinitrd –with=xennet –preload=xenblk /boot/initrd-centos-5.img 2.6.20-1.3001.fc6xen

When installing kernel-xenU, the initrd is built without xenblk, rendering the system unbootable under Xen using that kernel.

4. In most cases tty will not be enabled which we require for using the console of guest OS (DomU). To enable console we will have to edit the etc/initab file of DomU. For this we mount the image of guest OS ( here centos.5-0.img) as a Loop back device.

# mount -o loop=/dev/loop1 /home/CentOS/centos.5-0.img /home/anydir/

We can now access the files in the image. Open the inittab file and add the following line to it

co:2345:respawn:/sbin/mingetty console

and comment the following lines as shown below.

#1:2345:respawn:/sbin/mingetty tty1

#2:2345:respawn:/sbin/mingetty tty2

#3:2345:respawn:/sbin/mingetty tty3

#4:2345:respawn:/sbin/mingetty tty4

#5:2345:respawn:/sbin/mingetty tty5

#6:2345:respawn:/sbin/mingetty tty6

If that does not work try replacing tty with console to the line specifying getty or mingetty like that is given below.

5:2345:respawn:/sbin/mingetty console

After editing the file unmount the image.

5. Now we need to create the configuration file for DomU. Copy the configuration file provided with the image centos.cfg to preferably /etc/xen directory (Default configuration directory of Xen).

Sample configuration file


kernel = “/boot/vmlinuz-2.6.20-1.3001.fc6xen” #Xen Image ( location of kernel of Dom0)

ramdisk = “/boot/initrd-centos-5.img” #DomU’s initrd

memory = 256 # DomU memory

name = “centos.5-0” # Name of DomU

vif = [ ” ]

disk = [‘file:/home/CentOS/centos.5-0.img,sda1,w’ , ‘file:/home/CentOS/centos.swap,sda2,w’] # Location of DomU root filesystem & Swap

ip = “” # DomU IP address

gateway = “”

netmask = “”

root = “/dev/sda1 ro”


Sometimes the network of DomU may not function as xenbr0 ( xen virtual bridge) may not get connected to virtual interface vif. In such case add the following line to xen configuration file.

vif = [ ‘bridge=xenbr0’, ]

6. Create DomU and mount the file system using following command.

# xm create /etc/xen/centos.cfg -c

( Providing the full path to configuration file or just configuration filename if it inside /etc/xen/ )

The Xend manager (xm) creates the DomU reading the configurations provided in config file. The -c option lets us see all the steps involved in creating and booting into the DomU kernel.

7. We can now check and verify if the DomU is running using the xm list command.

# xm list

Name ID Mem(MiB) VCPUs State Time(s)

Domain-0 0 610 1 r—– 12492.1

centos 1 256 1 r—– 256.7

States of domains

  • r – Running
  • b- Blocked
  • c -crashed
  • s -Shutdown
  • p -Paused

We can now enter the console of guest domain using following command.

# xm console <domain name >

Managing Domains with xm

1. Connecting to a Domain

You can use xm to connect to a domain or virtual machine:

# xm console domain-id

This causes the console to attach to the domain-id’s text console.

2. Creating a Domain

You can use xm to make a domain:

# xm create domain001 [-c]

This creates a domain named domain001 with the file residing in the /etc/xen/ directory. The [-c]option aids with troubleshooting by allowing you to connect to the text console.

3. Saving a Domain

You can use xm to save a domain:

# xm save [domain-id] [statefile]

4. Terminating a Domain ID

You can use xm to terminate a domain-id:

# xm destroy [domain-id]

This instantly terminates the domain-id.

5. Shutting Down a Domain

You can use xm to shut down any domain:

# xm shutdown [domain-id]

6. Suspending a Domain

You can use xm to suspend a domain:

# xm suspend [domain-id]

7. Resuming a Domain

You can use xm to resume a previously suspended session:

# xm resume [domain-id]

8. Rebooting a Domain

You can use xm to reboot a domain:

# xm reboot [domain-id]

9. Renaming a Domain

You can use xm to assign a new name to an existing domain:

# xm rename [domain-name] [new domain-name]

Domain renaming will keep the same settings (same hard disk, same memory, etc.).

10. Pausing a Domain

You can use xm to pause a domain:

# xm pause [domain-id]

11. Unpausing a Domain

You can use xm to unpause a domain:

# xm unpause [domain-id]

This makes the domain available for scheduling by a hypervisor.

12. Configuring Memory Allocation

You can use xm to modify a domain’s memory allocation:

# xm mem-set [domain-id] [count]

13. Configuring Maximum Memory

You can use xm to modify a domain’s maximum memory:

# xm mem-max [domain-id] [count]

You must specify the [count] in megabytes.