Archive for June, 2009

HyperVM Manual Alternative for creating Xen Domu using LVM

Posted on June 26th, 2009 in Linux | 2 Comments »

After the sad death of K T Ligesh I decided, like many people, to move away from HyperVM.

I searched and tried many control panels.

Openqrm – I found this over kill
karesansui – This is very nice looking and polished but seemed quite slow.
VDS Manager – Still in beta for Xen

I’ve now decided to create the VM’s by hand. I then hope to make a PHP control panel of my own. Here are my steps for creating Xen VPS’s using the command line and Centos 5.

I’m going to create a vm called testvm

First I needed to find out what Volume groups exist on my system and what the VG Name was

lvdisplay

VG Name                VolGroup00

Create the disks for the VM and format

lvcreate -L8.5G -n testvm_rootimg VolGroup00
lvcreate -L 384M -n testvm_vmswap VolGroup00
mke2fs /dev/VolGroup00/testvm_rootimg
tune2fs -j /dev/VolGroup00/testvm_rootimg
mkswap /dev/VolGroup00/testvm_vmswap

mkdir /mnt/img
mkdir /mnt/vm

Now mount your disk image. I’ve created a custom hardened Centos distro, but you could use Jailtime Images

mount -o loop centos.5-3.64-smudge.img  /mnt/img
mount /dev/VolGroup00/looop_rootimg /mnt/vm

Copy the image onto your new partition

cp -a /mnt/img/* /mnt/vm/

Now all you need to do is configure the hostname, ip etc on the VM.

vi /mnt/vm/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=static
IPADDR=87.xxx.xxx.xxx
NETMASK=255.255.255.xxx
ONBOOT=yes

Change the hostname

vi /mnt/vm/etc/sysconfig/network

If your using Jailtime images people have advised to change this line to the following:

vi /mnt/vm/etc/inittab
1:2345:respawn:/sbin/mingetty console

vi /mnt/vm/etc/ssh/sshd_config
PermitRootLogin yes

vi /mnt/vm/etc/fstab

Add the line:

/dev/sda2     swap   swap     defaults     0     0

umount /mnt/img /mnt/vm

You will need a ram disk to boot from. This can be created by running:

mkinitrd /boot/xen-guest-initrd `uname -r` –with xenblk –with xennet –preload xenblk –preload xennet.

Now all you need to do is create the config file and give it an IP/ Mac address.Please check which kernel you are using first and change – kernel = “/boot/vmlinuz-2.6.18-128.1.14.el5xen”…

kernel = “/boot/vmlinuz-2.6.18-128.1.14.el5xen”
ramdisk    = ‘/boot/xen-guest-initrd’
maxmem = 192
memory = 192
vcpus = 1
name = “testvm”
vif = [ 'ip=87.xxx.xxx.xxx,bridge=xenbr0, rate = 1000KB/s, mac=aa:00:23:b0:de:34' ]
serial     = ‘pty’
disk       = ['phy:/dev/VolGroup00/testvm_rootimg,sda1,w', 'phy:/dev/VolGroup00/testvm_vmswap,sda2,w']
root = ‘/dev/sda1 ro’
on_poweroff = ‘destroy’
on_reboot   = ‘restart’
on_crash    = ‘restart’

All is ready to go.

Just run:

xm create testvm

And you VPS should be now running:

check with
xm console testvm

Hope this helps.

Smudge IT will be lauching  a new site and Xen Vps systems soon. Watch this space..

Share/Save/Bookmark

Archiving Mysql Data Tables using CakePHP or just SQL

Posted on June 5th, 2009 in Cakephp, MYSQL, PHP | No Comments »

I used to go down the road of doing a for each on my source table, which I always hated and thought was bad.

This way I can use table locking for MyISAM or transactions, for InnoDB, to make sure the data gets copied across.

I mainly use this method for archiving tables and keep the primary keys the same.

I have 2 tables created called ads and adsarchives.

What you do is make a copy of your database structure called adsarchives, and change the primary key so that it doesn’t auto increment. This mean that you can’t copy the same data twice. Also the second query won’t delete your data from the source on a fail.

( Oh by the way, my first example is using Cakephp on a MyISAM table) It would be much better to do transactions for this. But I wasn’t updating an InnoDB table.


function archiveads($monthsold=false){

if(is_numeric($monthsold) & $monthsold > 1){

$range = date('Y-m-d H:i:s', strtotime("-{$monthsold} months"));

$db =& ConnectionManager::getDataSource($this->useDbConfig);

$this->query('LOCK TABLES ads READ, adsarchives WRITE');
$this->query("INSERT INTO adsarchives SELECT ads.* FROM ads WHERE ads.enddate < '{$range}'");

$error = $db->lastError();

$this->query('UNLOCK TABLES');

if(!$error) { $this->query("DELETE FROM ads WHERE  ads.enddate < '{$range}'") ; return true;}else{

$this->log('FATAL Error archiving the Ads |'.$error);

}

}else{

return false;
}

}

If you want the raw SQl way of doing it try this. I’m archiving 3 month old data.


LOCK TABLES ads READ, adsarchives WRITE;
SET @age=(CURRENT_DATE()- INTERVAL 3 MONTH), @today=CURRENT_DATE();

INSERT INTO adsarchives
SELECT ads.*
FROM ads WHERE ads.enddate < @age;

UNLOCK TABLES;
DELETE FROM ads WHERE  ads.enddate < @age;

Share/Save/Bookmark

Using MYSQL Transactions in Cakephp

Posted on June 5th, 2009 in Cakephp, MYSQL, PHP | No Comments »

After plenty of research and trials, it is possible to use mysql transactions for custom queries.

You still have to create the table with InnoDB for row based locking to work.


ALTER TABLE table_name type=InnoDB;

And set the transactions variable in your model


var $transactional = true;

Now in your method use the following:


class YourModel extends AppModel {

var $transactional = true;

function mymethod()
{

  $db =& ConnectionManager::getDataSource($this->useDbConfig);

 #start the transaction
  $db->begin($this);

 $success = $db->query('my sql');
  if ($success  === true) {
       return $db->commit($this);
  } else {
       $db->rollback($this);
       return false;
  }
}

}

Share/Save/Bookmark