Multi Boot vs Virtual Machine

When I bought a 1TB hard drive last year, I had a decision to make. How would I slice up the new 1TB drive? I was running multiple operating systems on my computer desktop. I was using Linux 95% of the time and the other 5% on Windows, if at all.

So, I partitioned my drive and gave Windows 160GB. The rest went to Ubuntu. Knowing what I know now, I wouldn’t do it the way I did it. I would format all 1TB for the Linux partition. What about Windows? From hindsight, I could easily install Windows as a virtual machine instead of having a multi boot setup.

There are several advantages to using virtual machines over multi boot.

  1. You can easily launch a virtual machine without rebooting your computer.
  2. You can have both Linux and Windows running at the same time.
  3. You can clone as many instances of Windows.
  4. You can easily delete a virtual machine and free up the partition.

These are good enough reasons for me to prefer virtual machines over a multi boot setup. Knowing what I know, I would rather install Windows in a virtual machine using Virtualbox. So, if you’re at the same juncture of trying to make a decision whether to partition your drive. Don’t. Use virtual machines instead.

White Screen of Death

Remember the good old days when Windows had problems with the “Blue Screen of Death.” Years later, XBox followed with its “Red Ring of Death.” Now, it’s Ubuntu turn, what others are coining as the “White Screen of Death.”

Yesterday, I upgraded to Ubuntu 11.10 Oneiric Ocelot. Prior to the upgrade, I was using Ubuntu 11.04 and the Gnome desktop (not Unity). Somehow, for some reason, when I upgraded to Ubuntu 11.10, it wiped out my Gnome default environment and switched me back to Unity. Argh!

To make matters worse, I was getting a blank white screen every time I switch to full screen mode. Outside of the full screen, everything seemed ok. I’ve only had a few hours to test the system. But, I think the problem is stemming from the video drivers that were introduced in Ubuntu 11.10.

What’s the workaround for now? After several hours, I ended up using Ubuntu 2D as my desktop environment instead of the default Ubuntu 3D. I should switch back to Gnome and get rid of Unity all together. But, like a good supporter, I’m trying hard to like Unity. I should give it one more chance.

Update: It turned out to be the NVidia video driver. I rolled it back to version 173 and it seems to work fine.

Increase Disk Space Of A Virtual Machine

One of the cooler technologies to arrive on the desktop the past  ten years is virtualization. With virtualization software, desktops are able to run multiple virtual environments on a host computer. You can easily run Windows on top of Linux and vice versa. Two of the most popular virtualization software that come to mind are VMWare and Virtualbox. I use the latter because it’s open-source.

My host system is Ubuntu 11.04 and I run several Linux distributions on it, as well as a single instance of Windows XP. Unfortunately, I’ve only allocated a 10GB for my Windows XP virtual machine, which is the default size when you create a new virtual machine or VM. After several weeks of normal use, I found out that I needed more disk space.

Increasing the disk space on the VM is not quite the easy as I thought it would be. In fact, the process was more elaborate than first conceived. I’m not going to write every detail of what I did, but I will explain the high level process. Hopefully, you’re able to get the idea. The process was trial and error, but the result was successful. I was able to get results twice now, on two different systems.

Tools

5 Step Process

  1. Clone the Windows XP virtual machine to a USB hard drive.
  2. Create a new virtual machine with a bigger disk space.
  3. Use GParted to create a new partition. NTFS in this case.
  4. Restore the Clonezilla image to the new virtual machine.
  5. Run GParted again to allocate the increased disk space.

Step 1.

Clonezilla a free software disaster recovery and disk cloning utility that you can readily download online. Choose the latest stable version from the website. Make a bootable CD from the ISO that’s provided on the download. Boot Clonezilla on your old virtual machine. You may need to disable the hard drive from your boot up options to make the virtual machine boot from Clonezilla. Make sure you are able to add the USB drive to the virtual machine. Follow the instructions how to clone your old partition to the USB drive.

Step 2.

Create a new virtual machine with a bigger disk space. I used 50GB this time around. I assume you are familiar with Virtualbox how to create a new virtual machine. Don’t load any OS just yet. Just leave it blank.

Step 3.

Boot the GParted on the new virtual machine. Just follow all the instructions on how to create a new partition. Allocate all 50GB to the new partition using the NTFS file system. NTFS is the native file systems for Windows.

Step 4.

Boot Clonezilla on the new virtual machine. Restore your Clonezilla image that you stored on your USB drive. Just follow the instructions how to restore a Clonezilla image.

Step 5.

Run GParted again. The current OS (in this case, Windows XP) is still using the older and smaller partition. It doesn’t recognize the new and unallocated partition on the drive. So, run GParted again and increase the size of your current partition. Use all of the unallocated disk space on the partition. Reboot. Windows XP ran a Chkdsk on bootup, and then rebooted. I checked the disk space and sure enough, it says 50GB.

Done

There you have it. How to increase drive space of your existing virtual machine.

Turning Off Post Revisions in WordPress

Post Revision is a WordPress feature that was introduced with WordPress version 2.6. Post revisions allowed WordPress users to go back to previous saved versions of a post. It’s a life-saving feature to those needing to roll back to a previous post.

Although this feature have saved countless individuals from potential lost work, it also can add some overhead to the database tables because it inserts a new record every time a post is saved.

I was more than curious to how much data could be saved if post revisions were deleted from the database. So, I embarked on a project to delete post revisions from my main blog, which happens to be a multi-site blog.

Prior to doing anything, I recommend that you backup your database via PHPMyAdmin. Backup all the tables. This is very important. I can’t stress this enough.

I use the instructions from Lester Chan’s article to turn off and delete post revisions. I didn’t hesitate using Lester’s instructions because he is well-known and trusted member in the WordPress community having written many valuable plugins.

Turn Off Revisions

You can turn off revisions by editing your wp-config.php file and adding the following:

define('WP_POST_REVISIONS', false);

Delete Post Revisions

You can delete post revisions from the wp_posts table by running this SQL statement from PHPMyAdmin. You may need to repeat the process if you have a multi-site blog.

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';

Results

I have a total of 5 blogs running on my multi-site blog. Prior to deleting post revisions, my backup SQL file was 16.6MB. After deleting the post revisions, I was able to shave off 2.3MB. The file is now only 14.3MB. It doesn’t seem like a lot of saved space, although it deleted over 1000 rows.

Which brings me to the question, was it all worth it? Probably not. For the tiny amount of space saved, you are probably better off leaving post revisions alone, because you’ll never know if you need to revert to an older post. After all, it’s a nice feature to have.

A Newly Redesigned GMail

Look for Google to announce in the upcoming weeks the newly redesigned GMail. Google recently slipped a video (see below) of the newly redesigned GMail. The new look and feel will give users the power to customize their email that way they want it. Most likely, integration with other Google services such as Google+ and Google Docs is in order.

I can’t wait for this to happen. What do you think?

Display On WordPress Single Pages Only

I recently had to display some content on my blog, only when my blog is displaying a single article or a single post. The content does not appear on the home page or any other page. Since my blog is powered by WordPress, there are several WordPress functions that I can use to detect if the current page is a single post, a home page or a WordPress Page, e.g. the About page. Here’s the code:

Single Posts

<?php if(is_single()) : ?>
My single post content goes here!
<?php endif; ?>

Home Page

<?php if(is_home()) : ?>
My home page content goes here!
<?php endif; ?>

Pages

<?php if(is_page()) : ?>
My Page content goes here!
<?php endif; ?>

The content located between the if and endif statements will be displayed only if the conditional is true. You can use this code to customize the content of your blog. You can place this code in your WordPress theme files such as index.php, home.php, single.php, and page.php.

Sanitize Your Input In PHP

Here’s a quick and tiny PHP function that I’ve used on many projects to sanitize my input forms. As you are aware of, HTML forms are one source for injecting malicious code in programs to manipulate databases or traverse server directories. To make your programs much more secure, you’ll need to sanitize your inputs before doing anything, especially when dealing with databases. One function I’ve used repeatedly in my scripts is called sanitize(). Here’s the code:

The Code

// Sanitize input
function sanitize($in) {
 return addslashes(htmlspecialchars(strip_tags(trim($in))));
}

The addslashes function returns a string with backslashes to single quote (‘), double quote (“), backslash (\) and NUL (the NULL byte). This is particularly helpful when escaping special characters when dealing with database queries. The htmlspecialchars function converts special characters to HTML entities. For example & (ampersand) becomes &amp; and ‘”‘ (double quote) becomes &quot. This function prevents user-supplied text from containing unintended HTML markup.

The strip_tags function strips HTML and PHP tags from a string. It suppresses unwanted HTML markups from being displayed and prevents malicious PHP code from being executed. The trim function strips white space from the beginning and end of a string. For example, the string ” apple ” with white spaces will become “apple” without white spaces when the trim function is applied.

Usage

You can use the sanitize function to clean up the $_GET, $_POST, $_REQUEST and $_COOKIE input variables. In this example, we will use the sanitize function to clean up the form input called $_POST[‘name’].

$name = sanitize($_POST['name']);

Database Use

Before you can query, insert or update the database, you can use mysql_real_escape_string to escape special characters within your SQL statement to prevent SQL injections.

$name = sanitize($_POST['name']);
$name = mysql_real_escape_string($name);

There you have it. Two short and deliciously simple functions to sanitize your input and prevent malicious code from wrecking your programs. Let me know what you think.

PHP: Generate Random Alphanumeric Keys

Occasionally, you might need to generate a random alphanumeric key in your project or within your script. This article will show you how to generate a random key using several PHP functions such as: mt_rand(), in_array() and the staple while and foreach loops.

The mt_rand() function generates a random key based on the characters supplied to it. mt_round() is a direct replacement for the original rand() function. The mt_rand() function is recommended since it’s considerably faster (4 times faster) than rand().

The in_array() function checks to see if the value is already in array. in_array() prevents duplication of the randomly chosen characters within the script.

Let’s get started.

Assign Characters

In this code, several alphanumeric characters are assigned to the variable $characters. I omitted all vowels and will use both uppercase and lowercase alpha characters. In addition, I’m using numbers 1 through 9 only. I ommited zero since it can be confused with the letter O, although I’m not using the letter O in this example.

// Random characters
$characters = array("B","C","D","F","G","H","J","K","L","M","N",
"P","Q","R","S","T","V","W","X","Y","Z","b","c","d","f","g","h",
"j","k","l","m","n","p","q","r","s","t","v","w","x","y","z",
"1","2","3","4","5","6","7","8","9");

Set The Array

We will set the variable $keys as an array.

// set the array
$keys = array();

Set The Key Length

This is the length of the random key. It’s originally set for 7 characters and can be changed to a length of your choosing.

// set length
$length = 7;

Generate The Random Key

This is main code that generates the random key. The code loops 7 times and assigns a random character to the variable $x. If $x is not in the array, it will assign the value of $x to the array called $keys[].

// loop to generate random keys and assign to an array
while(count($keys) < $length) {
       $x = mt_rand(0, count($characters)-1);
       if(!in_array($x, $keys)) {
       $keys[] = $x;
    }
}

Display The Random Key

We will use the foreach loop to display the random key stored in the $keys array. We will loop and extract each key and assign it the $random_chars variable. Finally, we will echo the $random_chars variable to display our random generated key.

// extract each key from array
foreach($keys as $key){
   $random_chars .= $characters[$key];
}
 
// display random key
echo $random_chars;

The Script

All together now. Here’s the entire script pieced together.

// Random characters
$characters = array("B","C","D","F","G","H","J","K","L","M","N",
"P","Q","R","S","T","V","W","X","Y","Z","b","c","d","f","g","h",
"j","k","l","m","n","p","q","r","s","t","v","w","x","y","z",
"1","2","3","4","5","6","7","8","9");
 
// set the array
$keys = array();
 
// set length
$length = 7;
 
// loop to generate random keys and assign to an array
while(count($keys) < $length) {
	$x = mt_rand(0, count($characters)-1);
	if(!in_array($x, $keys)) {
       $keys[] = $x;
    }
}
 
// extract each key from array
foreach($keys as $key){
   $random_chars .= $characters[$key];
}
 
// display random key
echo $random_chars;

There you have it, a random generated key based on the assigned characters and length that we indicated in our tiny PHP script. I hope you find this short article useful on future projects. By the way, you can also use this script to generate random passwords.

Dennis Ritchie Will Be Remembered

Last week, we heard the passing of Steve Jobs. This week, another technology icon, a lesser known one, is now gone. Dennis Ritchie, the founder the Unix operating system, as well as the founder of the C programming language has died. 

Ritchie along with Ken Thompson created the Unix operating system during their tenure at Bell Labs. Shortly after, Ritchie started working on the C programming language. His contributions to Unix and C were monumental.

I still have old C books written by Dennis and Brian Kernighan stashed in a box somewhere.
For nostalgia purposes, I should re-read those books again.

Dennis Ritchie didn’t receive the fanfare or the credit that Steve Jobs have had, but Ritchie’s impact to the tech industry will be long lasting. After all, Microsoft Windows and PHP, two technologies impacted by Ritchie’s creation, are written in C.

Adding Windows Shares in Linux

I was recently asked to access Windows Share from within Linux. It’s not an unusual task, but it’s not common either. Windows shares are a bit confusing to Linux users because Microsoft use backslashes which are designated as an escape key in Linux. Windows shares typically use this \\Windows\Directory\Structure.

The easiest way to access a Window share is to mount it from within Linux. In this example, we have a Windows share called \\Windows\Directory\Structure. We want to mount it in Linux, so that it is available on /mnt/share. The following are the steps to take to mount Windows shares on a Linux box. I’m using Ubuntu 11.04 on this example.

Create A Directory

First, we need to create a destination mount. In this case, /mnt/share.

sudo mkdir /mnt/share

Mount The Share

Next, we will mount the Windows share using CIFS or Common Internet File System. CIFS is similar to the SMB protocol. You can use SMB as well, but CIFS worked for me.

sudo mount -t cifs //Windows/Directory/Structure
  -o username=your_username,password=your_password /mnt/share

Add To Fstab

To have a more permanent structure, we will add the Windows share to /etc/stab, so that every time Linux is rebooted, the shared drive will be mounted automatically. Jus edit the /etc/fstab file and add the following:

//Mounted/Directory/Structure /mnt/share cifs
  u<var id="yiv1712973071yui-ie-cursor"></var>sername=your_username,password=your_password, 0 0

Once the changes are made, you can mount all using this command

mount -a

Finally, you can then check if the Windows share drive is available from within /mnt/share.