Dualboot Mountain Lion + Ubuntu 12.04 LTS on the same hard drive howto

September 4, 2012 Leave a comment

Shortly after Mountain Lion was released I did a clean installation onto my HP 4530S and have been very happy with the result, but I am also a huge Ubutu fan and I’ve been dual booting Ubuntu 12.04 LTS through a SD card on the same machine. All was working fine except I don’t like the Ubuntu booting speed (it’s one of those 95MB/s 16GB Sandisk SD cards, fast on the paper but it takes over 30-second to boot into Ubuntu) so I decided to dual boot ML and Ubuntu from the same hard drive (Agility 3 SATAIII 120GB, http://www.ocztechnology.com/ocz-agility-3-sata-iii-2-5-ssd.html) and it turned out to be not very difficult.

What is needed:
1) ML Unibeast installation disk
2) HP Installer (Or Multibeast if on non-HP4530s systems)
3) Ubuntu installation disk

Steps:
1) Re-partition hard drive ML installation, when I did the ML installation, I started with 1-partition scheme as I was not planning for dual-booting. To re-size the ML partition is a piece of cake (I was amazed how Mac OS X can handle this so well, online resize the OS drive, looking at you Window$), just launch disk utility -> click on the hard drive, click the + sign, adjust the divider, click on the new partition and adjust the size through its size from the text field if needed. The new partition will be created as free space and no other changes are required. Hit Apply.
2) Reboot with Ubuntu installation disk, choose “Something else” when asked how to install, create a ext4 / partition and a 2GB swap partition (adjust the sizes if you want, I have 8GB of RAM so I am not worried about swap that much). My root partition is created as /dev/sdb3, when it comes to boot loader installation, make sure you choose this as where GRUB would be installed, if you choose the wrong partition, first partition, for example, you will need to do some extra work to fix it.
3) Once Ubuntu is installed, reboot with Unibeast, make sure to choose the hard drive ML installation when Chimera menu comes up. Log in to ML and install Chimera again from HP installer (or Multibeast).
4) Shutdown, remove Unibeast installation disk, power up, when Chimera boot logo appears, hit any key you will see the Ubuntu partition along with the original ML partition.

Note: This post is created after I successfully dual boot into the newly installed Ubuntu 12.04 LTS. Cheers.

Advertisements

Install node.js on Raspberry PI in two simple steps

July 24, 2012 13 comments

[ Updated Jan 20, 2013 ] Check out my git repository https://github.com/midnightcodr/rpi_node_install if you are interested in putting newer version of node to raspberry pi (tested with 0.8.14, os runs raspbian)

1) Install the newest wheezy image onto the sd card (currently 2012-07-15-wheezy-raspbian.zip) from http://www.raspberrypi.org/downloads

2)

sudo apt-get install nodejs npm

, press y, Enter when prompted, you will get node.js (v0.6.19) and npm (1.1.4) installed in a few minutes, which should be the easiest way to get node.js and npm up and running on RP.

Note: If you want newer version of node.js you might want to check this out: http://www.raspberrypi.org/phpBB3//viewtopic.php?f=34&t=9929, I have tested the method described in this post in the previous release of wheezy (http://www.raspberrypi.org/archives/1435) and I assume it should work with the current image as well.

Categories: node.js, raspberry pi

Install Postgres on Mac OS X Lion with homebrew howto

July 20, 2012 3 comments

1) This is the easiest part, simply

$ brew install postgres

2) this step is not obvious but you can’t move on without it (would be nice if brew can automate this step too :D)

$ initdb /usr/local/var/postgres

3) add /usr/local/bin to $PATH if it’s not already there, one way to do that is

$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile && . ~/.bash_profile

without this step you will end up using /usr/bin/psql, which is installed by default in Lion, while trying to access db, which would give you the following error:

psql: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket “/var/pgsql_socket/.s.PGSQL.5432”?

[added 7/26/2012]
3.5) start postgres server

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

4) access postgres db

psql postgres your_username

or

psql postgres `whoami`

assuming your_username is the one who runs step 2

5) if everything’s ok you should be able to see

psql (9.1.3)
Type “help” for help.

postgres=#

type \q to exit

6) if you want to create your first db, do (under system shell, not postgres) a

createdb mydb

and access it through

psql mydb your_username

Note: your_username in command psql is optional if the shell user is the creator of the db to be accessed, that is, you can simply type psql postgres or psql mydb to access the dbs.

Enjoy.

References:
http://archives.postgresql.org/pgsql-general/2009-09/msg00990.php
http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/ (didn’t use the method describe in this post but use the PATH method instead)
http://www.postgresql.org/docs/9.0/static/tutorial-createdb.html

Categories: lion, mac osx, postgresql

PHP templating with Smarty

July 4, 2012 Leave a comment

I found using Smarty in PHP the most effective way to avoid spaghetti codes (codes that mix programming logic and html output that rely heavily on string concatenation), especially in the following two use cases
1) Generate html output in a structured, organized way
Let’s look at an example:

cat test.php

<?php
    require_once '/usr/share/php/smarty3/Smarty.class.php';
    // stock path installed with apt-get install smarty3 in ubuntu, you might need to adjust the require_once path
    // if you install smarty in other distros or through other means
    $smarty=new Smarty();
    $smarty->assign('label', 'my favorite tools');
    $smarty->assign('friends', array('node.js', 'underscore.js', 'backbone.js', 'Smarty', 'swift mailer', 'express'));
    $smarty->display('test.tpl');

cat templates/layout.tpl

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>{block name=title}{/block}</title>
        <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" charset="utf-8">
        {block name=morecss}{/block}
    </head>
    <body>
    {block name=body}{/block}   
    <script type="text/javascript" src="path/to/jquery.min.js" charset="utf-8"></script>
    {block name=morejs}{/block} 
    </body>
</html>

cat templates/test.tpl

{extends file='layout.tpl'}
{block name=title}{$label|capitalize}{/block}
{block name=morejs}
	<script type="text/javascript" src="path/to/my.main.js" charset="utf-8"></script>
{/block}
{block name=body}
Say hello to {$label}:
<ul>
	{foreach $friends as $friend}
	<li>{$friend}</li>
	{/foreach}
</ul>
{/block}

2) Prepare html (or plain text) email message to send out
cat email.php

<?php
    require_once '/usr/share/php/smarty3/Smarty.class.php';
    $smarty=new Smarty();
    $smarty->assign('url', 'http://a.link.to.reset/your/password');
    $message=$smarty->fetch('email/forget.tpl');
    // send_email_to($someaddress, $somesuject, $message);
    // you can echo it out as well
    // echo $message;

cat templates/email/master.tpl

<html>
	<body>
		<div id="content" style="width:400px;margin:40px auto"> 
			{block name=body}{/block}
		</div>
	</body>
</html>

cat templates/email/forget.tpl

{extends file='email/master.tpl'}
{block name=body}
<p>
Dear User,
</p><p>
Here's the url to reset your password:<br />
<a href="{$url}">{$url}</a>
</p><p>
&nbsp;
</p></p>
Sincerely,
</p></p>
Blah Blah
{/block}

Notes on directory structure:
1) in the above examples, the file/folder structure is as follows (generated through this little powerful tool called tree, installed via ‘apt-get install tree‘)

2) folders cache and templates_c are given the following ownership/perimssion:
ls -ld cache templates_c/
drwxr-xr-x 2 www-data www-data 4096 2012-07-03 18:23 cache/
drwxr-xr-x 2 www-data www-data 4096 2012-07-03 22:35 templates_c/

Categories: php

PHP: removed trailing zeros from numeric strings (code illustration)

June 14, 2012 1 comment
<?php
	$nums=array('1.0', '2.300', '4', '5.9000');

	echo "Original format:\n";
	foreach($nums as $num) {
		echo "$num\n";
	}

	echo "\nTrailing zeros removed:\n";
	foreach($nums as $num) {
		echo ($num+0)."\n";
	}

Output:
Original format:
1.0
2.300
4
5.9000

Trailing zeros removed:
1
2.3
4
5.9

Categories: php, Programming

Learning CoffeeScript

June 10, 2012 2 comments

Practice 1: Define a Student class, make some new instances and classify them into two arrays based on the student scores

class Student
    constructor:(@gender, @score)->

s1=new Student('M', 45)
s2=new Student('F', 78)
s3=new Student('F', 59)
s4=new Student('M', 99)

passed=[]
failed=[]
(if s.score>59 then passed else failed).push s for s in [s1, s2, s3, s4]
console.log passed
console.log failed

Practice 2: Array filtering

pr=(items, lbl)->
    console.log "==#{lbl}=="
    for item, i in items
        console.log "Item #{i}: #{item}"
    false

a=[1..9]

odd=(item for item in a when item%2)
even=(item for item in a when item%2==0)
double=(item*2 for item in a)

pr odd, "odd"
pr even, "even"
pr double, "double"

Practice 3: more about class

class Candidate
    constructor:(@approval_rate)->

class Election
    elect:(candidate)->
        candidate.approval_rate>0.5


e=new Election
console.log e.elect( new Candidate(0.34) )
console.log e.elect( new Candidate(0.59) )

Practice 4: serial_maker (based on
JavaScript: The Good Parts by Douglas Crockford
Chap 4.12)

class SerialMaker
    constructor:(@prefix='Q', @start=0)->
    gen_seq:()->
        @start+=1
        @prefix+@start

sm=new SerialMaker('SQ')
console.log sm.gen_seq()
console.log sm.gen_seq()
console.log sm.gen_seq()

Practice 5: (added 7/12/2012) Self-invoking constructor

class Person
    constructor:(name='no name')->
        return new Person(name) if !(@ instanceof Person)
        @name=name

    id:->
        return @name

    isrich:true


p1=Person('Freedom')
# or the above line can be written as 
# p1=new Person('Freedom')
# the result is exactly the same and the
# benefit is user doesn't need to worry about forgetting using the "new" keyword
console.log p1.id() + ','+ p1.isrich

Reference: http://arcturo.github.com/library/coffeescript/index.html

Categories: coffeescript, Programming

Make your tmux look and work better

June 2, 2012 1 comment

I love using tmux. Besides the excellent scripting support (see my other post ), tmux is quite easy to customize to fit your need.

Here’s my tmux config: https://github.com/midnightcodr/my_tmux_conf/blob/master/.tmux.conf

The screenshot: https://github.com/midnightcodr/my_tmux_conf/blob/master/my_tmux_status_bar.png

To use, follow https://github.com/midnightcodr/my_tmux_conf/blob/master/README.md

Notes:
1. Credit goes to http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2/
2. I found using different color settings the most effect way to avoid accidental mistakes when working with test and production server, for example for production environment I use white on red (convenient color for caution?) for the current window tab color, for test environment I use black on yellow for the same element.

Feed-backs and comments are welcome!

[ UPDATE 6/6/2012 ] Finally I came up with the following status line that works on both Mac OSX and Linux regardless what the output of command uptime is:
set -g status-right '#[fg=yellow]#(uptime | egrep -o "[0-9]+ users?, +load.*"|perl -pe "s| averages?||"), %H:%M'

The challenge I found setting tmux status with command lines is that group capturing regex won’t work with tmux. For example, the following config set -g status-right '#[fg=yellow]#(uptime|perl -pe "s#.*,(\s+\d+ users.*)#\1#;s# averages?##"), %H:%M'
would end up 1saverages?#"), 23:44 showing in the status bar (right corner).

Categories: Uncategorized