Thursday, March 20, 2008

Prototype + JSON + PHP - I Got It :)

This week I haven been playing with the great EditArea, a webbased sourcecode editor (examples here). To fetch the data from my PHP server I decided JSON would be the way to go, due to it's clean nature. I soon ran into trouble parsing the JSON data my PHP server threw at the client. It took me a while to figure out how to do this nicely with Prototype, but I found out how at this page (search for responseJSON, which is the magic keyword...) On the server side the key is to make PHP send the header "Content-type: application/json", and print the json_decoded data as response. Prototype can fetch this data by doing an Ajax.Request. On the client side you can let Prototype make an Ajax.Request to the server, and get the property 'responseJSON' from the object you received. Below is a working example. To get it running you need to create these two files, test.html and test.php . You also need the file prototype-1.6.0.2.js in the same directory. I suspect it to work on all Prototype versions from 1.6RC upwards.
test.html
<html>
 <head>
  <title>Prototype/JSON/PHP test</title>
  <script language="Javascript" type="text/javascript" src="prototype-1.6.0.2.js"></script>
  <script language="Javascript" type="text/javascript">
  function LoadFile(id,target){
      new Ajax.Request("test.php?action=load&id="+id, {
          onSuccess: function(transport) {
              var theObject = transport.responseJSON;
              $(target).innerHTML = theObject.text;
          }, method: "get"
      });
  }
  </script>
 </head>
 <body>
  <a href="javascript:LoadFile(1,'filelist')">File 1</a> 
  <a href="javascript:LoadFile(2,'filelist')">File 2</a> 
  <a href="javascript:LoadFile('third','filelist')">File 3</a>
  <div id="filelist"></div>
 </body>
</html>
test.php
<?php
# Make sure this is the first output.
header('Content-type: application/json');

# The list of files.
$files[1]='This is the first file';
$files[2]='And this the second one!';
$files['third']='This must be the third then...';

# Get ID and create array.
$id=$_GET['id'];
$return['id']=$id;
$return['text']=$files[$id];

# Return the JSON string.
echo json_encode($return); 
?>
Code formatted with Format Code. Note: Make sure you don't send any other text (that includes spaces or newlines) before sending the header with PHP! This will cause errors that sound like: "Warning: Cannot modify header information - headers already sent by......" Good luck! :)

Thursday, March 13, 2008

xampp-dsl: A Damn Small LAMP Appliance

Today I ran across a nice Virtual Appliance that consists of the well-known xampp stack (version 1.6.1) installed on top of the tiny Linux distro Damn Small Linux (DSL 3.3). You can get the appliance here . The download weighs in at 154MB and the unpacked size is 379MB.

Saturday, March 8, 2008

KQemu on Ubuntu Gutsy Gibbon

QEMU is an open source processor emulator in which you can run Virtual Machines. It comes with a kernel module called KQemu which accelerates the VM's to 'near native' speeds. Due to some licensing issues KQemu is not distributed and installed with QEMU, though it is easily available in Ubuntu and a lot of other operating systems. This message appears when starting QEMU while KQemu is not loaded:
Could not open '/dev/kqemu' - QEMU acceleration layer not activated
This howto shows how to get KQemu running on Ubuntu 7.10 (Gutsy Gibbon) First install the following packages:
sudo apt-get install kqemu-source kqemu-common build-essential
Then run the following commands:
sudo module-assistant prepare kqemu sudo module-assistant auto-install kqemu
If everything went right (so no errors occurred) you can load the module with this command:
sudo modprobe kqemu
Now check if the module is loaded correctly. If you get a line of output starting with the word kqemu it means the module is loaded properly:
lsmod | grep qemu
To use this module as a user the right permission need to be set:
sudo chmod a+rw /dev/kqemu
You QEMU VM's should now run without the warning above and run a whole lot faster!

Friday, March 7, 2008

VirtualCenter 2.5 Passthrough Authentication

Stuart Radnidge from vinternals.com discovered how to do pass-through authentication in VMware VC 2.5. This means you will automatically log in to VC 2.5 when the Windows workstation credentials are the same as the ones VC is working with (so this works if you authenticate to the same Windows Domain on both platforms.) The setting is undocumented but is reported to work. You can find the post here. This is how it is done:

To use it, simply add -passthroughAuth -s vchostname to the end of the shortcut used to launch the VI 2.5 client. By default it uses the Negotiate SSPI provider, however since they have fully implemented the interface you can change that behaviour to use Kerberos by adding the following within the <vpxd> node in the vpxd.cfg file on the VC server: <sspiProtocol>Kerberos</sspiProtocol>
Via virtualization.info.


Sunday, March 2, 2008

Creating VM's in a breeze

CreateVM is a shell script I created to automate the process of creating VMware virtual machines (VM's). It makes it trivial to create one or more VM's with specific conditions.

At work I used this script to create VM's with static MAC addresses which are known in our DHCP server, and it helped me a lot to instantly have a clean VM while working on unattended OS installations.

The script is written in bash and runs on any system which supports bash. It depends on vmware-vdiskmanager , which is included in VMware Server and VMware Workstation.

To run the script you first need to download it.
wget http://createvm.googlecode.com/svn/trunk/createvm.sh -O createvm.sh

Once downloaded you have to flag it executable:
chmod +x createvm.sh

Now you can run the script. Run it without any parameters to see the options available:
./createvm.sh

The script has one parameter that needs to be set, and that is the guest OS you want to run in your VM. To see a list of OS'es that are supported run the script with parameter -l :
./createvm.sh -l

To create your first VM decide on which Guest OS you want to run and pass it as the first parameter to the script. In this example I am creating an Ubuntu virtual machine:
./createvm.sh ubuntu

To create the same VM with some more memory pass the -r (for ram) parameter with the wanted RAM value:
./createvm.sh ubuntu -r 512

The script will display a summary of what will be created (see screenshot). The defaults are displayed here. All the settings displayed here can be manipulated by the command line parameters.

To see some of the examples of creating Virtual Machines run the following command:
./createvm.sh -ex

Any major updates on this script will be displayed here.

Screenshot of CreateVM running in an xterm.

Links: