How to Mount Windows Partitions on Ubuntu

This tutorial describes how to mount a Windows partition on Ubuntu if you find it isn’t mounted automatically. We will use the UUID of the partition in mounting. I will describe how to mount Windows partition whenever Ubuntu is booted.

1. Find the device you want to mount. To do this, run the following in a terminal

$ sudo fdisk -l

Windows partitions will usually be in ntfs format. Usually it is at /dev/sda2 or /dev/sda3 and you may be able to differentiate the windows partition from a windows recovery partition by comparing the sizes.

2. Find the UUID of the windows partition by running the following in the terminal:

$ sudo blkid

Find the one corresponding to the Windows partition and have it handy.

3. Back up your file systems table configuration file (fstab) by running the following the following in a terminal:

$ sudo cp /etc/fstab /etc/fstab.orig

4. Edit fstab using your favourite editor. e.g.

$ sudo gedit /etc/fstab

5. Assuming we want to set the mount point for Windows to /media/windows, add the following line to fstab

$ UUID=the-uuid /media/windows ntfs-3g defaults,user,locale=en_US.utf8 0 0

Where the-uuid is the UUID you found in step 2. Save the close the file.

6. create that directory by running :

$ sudo mkdir /media/windows/

7. Unmount the partition and remount it by running these in a terminal:


$ sudo umount /media/windows
$ sudo mount /media/windows

Reference:
MountingWindowsPartitions – Community Ubuntu Documentation https://help.ubuntu.com/community/MountingWindowsPartitions

How to Enable ping on Amazon ec2 server

This post describes how to enable pinging on your amazon ec2 server.

  • Log in to amazon aws console and click ec2 tab
  • Find the security group  of your ec2 instance under Security Groups on the wc2 page
  • Create a key and certificate which you will use with ec2-authorize command.
    • On the top right of the screen click the down arrow next to your name
    • Click Security Credentials on the dropdown menu. You will be redirected to Security Credentials page
    • Click X.509 Certicates tab under Access Credentials section
    • Click Create a new Certificate
    • Download the private key and certificate in the resulting dialog that appears
  • Upload the private key and certificate to your ec2 server by sftp
  • Install ec2-api-tools package if not already installed
    • To do this, enable ubuntu multiverse repository if not already enabled
      • To do this, back up your sources list by running
        $sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup 
      • Open /etc/apt/sources.list
      • Uncomment the lines for multiverse list by removing the leading # symbols
      • Save and close the file
      • Retrieve updated package lists by running
        $sudo apt-get update
  • Enable icmp ping by running
    $ec2-authorize -K /path/to/private/key \
    -C /path/to/certificate your-instance-security-group \
    -P icmp -t -1:-1 -s 0.0.0.0/0

error unknown option ‘–no-crunch’ android asset packaging tool

On my eclipse running on Ubuntu, I had just updated my Android SDK and was trying to run my android applications when I ran into the following error

error unknown option ‘–no-crunch’

android asset packaging tool

I thought I may be missing something, so I tried updating both my Android AVD and SKD but noticed another error while updating the SDK. The message was similar to the following:

Failed to fetch url https://dl-ssl.google.com/android/repository/repository.xml

In order to fix the problem of fetching the repository, I set the option to force https downloads to take place by http as advised by Vikram Pant in his blog.

To do this, I opened the Android SDK and AVD manager by going to <path to sdk>/tools/ directory.

Then I ran the following command :

$ ./android

Then I clicked Settings and checked the option to force https downloads to use http.

Next, I opened the Android SDK manager in eclipse. There were a few packages to install and others to update.

I first pressed the Delete Packages… button to delete the obsolete ones. Next, I pressed the Install Packages… to install fresh ones. That solved the problem.


MySQL Query Logging in Ubuntu

This blog covers briefly how to log queries in MySQL.

As of MySQL 5.1.12. It is possible to switch on or switch off query logging during runtime. The following switches on query logging in runtime:

SET GLOBAL general_log=’ON’;

The following switches off general log in runtime:

SET GLOBAL general_log=’OFF’;

By default, the general log file used by MySQL is located at /var/lib/mysql/host_name.log in ubuntu, where host_name is be the name of your machine.

HelloGridView – Android Tutorial

Here I present are few tips for writing HelloGridView tutorial on Google Android website – http://developer.android.com/resources/tutorials/views/hello-gridview.html

  1. Create ImageAdapter class must be in a new file of same name under same package ( in Java, a public class must be in a file of same name as the class).
  2. Press Ctrl + shift + O to make Eclipse import all required files into the file after writing all code in that file.
  3. When using resources, it’s advisable to put low-res images in res/drawable-ldpi folder, medium-res images in res/drawable-mdpi, and high-res images in res/drawable-hdpi folder. For this tutorial, I put identical images in all 3 folders.

Transfering Data between Multiple Browser Windows with Javascript

With javascript, it is fairly easy to transfer data between a popup window and the window which opened it. You can also call functions, or even set location of the opening window from a popup window. This is possible because a popup window holds a reference to the opening window in a javascript variable called ‘opener’.

So the variable:

window.opener

refers to opener window.

In this tutorial, we have an original webpage win_opener1.php and a popup webpage win_opener2.php. A link in the opening page (win_opener1.php) opens the popup (win_opener2.php). When the user types in some text in a form on win_opener2.php and submits it, the popup window closes, and the original window shows an alert of the text entered in the popup.

In the source code of the popup window javascript, there is also commented code, which when uncommented, makes the opening window redirect to a specified URL.

The source code for win_opener1.php is

<!DOCTYPE HTML>
<html lang="en">

<head>

 <meta charset="UTF-8">

 <title></title>

</head>

<body>

<a id="opener" href="#">Click to open child window</a> 

<form name="parentform" action="" >

 <input name="myinput" type="hidden" />

</form>

<script type="text/javascript" 

src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
</script>

<script type="text/javascript">

$(document).ready(function(){

 $("#opener").click(function(e){

 e.preventDefault();

 window.open("/resources/js/

data-trans-multiple-windows/win_opener2.php",
 "mywindow", "menubar=no,width=500,height=500");

 });

});

function process(){

 alert("You entered "+document.parentform.myinput.value);

}

</script>

</body>

</html>

The code for win_opener2.php is :

<!DOCTYPE HTML>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title></title>

</head>

<body>

<form name="childform" action="">

Enter name: <input id="myname" name="myname" type="text" />

<input type="submit" value="Submit" />

</form>

<script type="text/javascript" 

src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">

</script>

<script type="text/javascript">

$(document).ready(function(){

 $("form").submit(function(e){

 e.preventDefault();

 // to set url on window opener

 // window.opener.location="http://www.yahoo.com";

 // passing data to window opener

 window.opener.document.parentform.myinput.value =
window.document.childform.myname.value;

 window.close();

 // to call a function on window opener

 window.opener.process();

 

 });

});

</script>

</body>

</html>

Setting up and using PHP Memcache

What is Memcache? Memcache is an object-oriented mapping that is used to speed up dynamic database-drive applications by storing data in key-value pairs in RAM. In this blog, I will go through steps involved in setting up Memcache on a machine running Ubuntu and running a simple PHP program with Memcache.

For installing software, I will use Synaptic Package Manager.

Installing Memcache

  • Memcache is a PECL extension , so first, install PEAR. Go to Synaptic, find php-pear, and install it.
  • Next, install PHP development package.  Open Synaptic, find php-dev, and install it.
  • Build memcache module by running the following command:

$ pecl install memcache

  • You will see several lines of text run on the console. When done, you should see a success message and instruction telling you to add a line to your php.ini file. So, add the following line to your php.ini file:

extension=memcache.so

  • Next, we need to install memcached so that we can fire up the memcache process. Open Synaptic Package Manager, and install memcached.
  • Start memcached process by running the following command on a terminal:

$ memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211

  • Now we’re ready to create a php program to test Memcache.

Writing a PHP program using Memcache

Below is a sample program that demonstrates how Memcached works:

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or 
die("Couldn't connect to memcache");

function getCache($key){
	global $memcache;
	return ($memcache) ? $memcache->get($key) : false;
}

function setCache($key, $object, $timeout = 60){
	global $memcache;
	return ($memcache) ? $memcache->set($key,
 $object, MEMCACHE_COMPRESSED, $timeout) : false;
}

$connection = mysql_connect('localhost', 'root', 'foobar')
 or die("can't connect to mysql");
mysql_select_db('test', $connection);

// test query
$key = md5("SELECT * FROM testtable WHERE id=2");
$get_result = getCache($key);

if($get_result){
	echo $get_result['name'];
	echo $get_result['id'];
	echo "Data pulled from cache";
}else{
	$sql = 	"SELECT * FROM testtable WHERE id=2";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	print_r($row);
	setCache($key, $row, 20);
	echo "Data pulled from database";
}

It is assumed that the table used exists. If all goes well, you will see that the data is pulled from database initially, and from memcached afterwards.

That’s it for now. Till next time, Goodbye.

gVim

gVim is a powerful editor that makes a lot of editing work a piece of cake. Here’s a cheat sheet that can help you get the most off this editor. For those who would like to get the work done without using a mouse, it may be the perfect editor. This cheat sheet is geared towards beginners who use editors a lot, perhaps programmers, and will like an easy introduction.

First, for those from a Windows background, there are 3 modes of working in gVim, command mode, insert mode, and visual mode. On opening gVim, you start off in command mode. To type in text, you must go to insert mode. To select text by highlighting, as you do in Windows, you go to visual mode. Without further ado, to the commands.

Opening, saving, moving between files, and closing files

  • e c:\path\to\your\file  – opens the file, or creates one at that path file doesn’t exist. If you use this on a directory, it opens the directory while showing error message on command bar
  • tabnew c:\path\to\file – opens (creates) file at specified path in a new tab
  • w – save file
  • sav c:\path\to\file – save file as the name given
  • q – close file
  • wq – save file and close it
  • q! – close file without saving (gVim normally issues an error if you try ‘q’ without saving last changes)
  • ctrl pg up – go to previous tab
  • ctrl pg dn – go to next tab

Switching between differnt modes

Switching from command to insert mode

  • i – places the cursor behind it’s current position.
  • a – places cursor after current position

Switching from insert to command mode or from visual mode to command mode

  • esc

Switching from command to visual mode

  • v – to selct text
  • shift+v  – to select line

Moving around documents

  • k – moves up a line
  • j  – moves down a line
  • h – moves left on current line
  • l – moves right on current line
  • w – moving to beginning of words in forward direction
  • e – move to end of words in forward direction
  • b – move to beginning of words in backwards direction
  • gg – move to first line of file
  • G – move to last line of file. Same with :$
  • ^ – move to beginning of line
  • $ – move to end of line.
  • :## – move to line ##

Moving accross screen

  • pg up – moves to previous screen
  • pg dn – moves to next screen

Copying and pasting

  • yy – copies (yanks) current line
  • 3y – this example with yank 3 lines starting from current line
  • P – paste copied text before cursor
  • p  – paste copied text after cursor.
  • “+y – copies to clipboard (making content available to other apps)
  • “+gP – pastes from clipboard. eg from your web browser into gVim

Inserting and Replacing text

  • O – creates(opens) a blank line before cursor and goes to insert mode
  • o – creates(opens) a blank line after cursor and goes to insert mode
  • ggVG – select everthing in current document
  • cw – change current word (deletes current word and puts you in insert mode)
  • ci{ – deletes everything within the nearest matching braces and puts you in insert mode. eg. if you had a function – function x(){ /* old st */ } and you want to quickly change the entire implementation of that function, you move your cursor into that {} and use ci{. Note: ci} accomplishes same task.

Deleting

  • dd – cuts current line into gVim’s clipboard (making content available only within gVim for paste)
  • 3d – example that deletes 3 lines starting from current line

Indenting

  • shift v > – adds indent on current line
  • shift v < – removes one indent from current line

Copy and Paste between gVim and other applications

  • “+y – copies to clipboard
  • “+gP – pastes from clipboard

Searching and Replacing

  • /searchpattern – searches for text with patter ‘searchpatter’
  • rs/searchpattern/replacepattern/o – this does a search and replace where r and s are options of various values. r can be nothing, in which case only current line is searched, % – in which case whole file is searched, a-b (where a and b are line numbers) in which case the range defined by a and b are worked on.   o stands for a series of one or more options (g, c, i,l). g- means all occurrences on lines are worked on. c- means you are asked for confirmation before each replace. i- case insensitive search, l-case sensivitive search

That’s all for now.  Happy gViming.