2016/10/28

Createa a bootable USB stick for ubuntu on ubuntu

Things below are just an alternative. There is an official way to do this.

Get a USB stick

Make sure you have an usb stick with enough storage. Mine is 8 GB.

Format the USB Stick

There are plenty options to do this. My solution is using the built in one disks
  • Click the USB device
  • Click the square button
  • Select EXT4 as the type of parition
  • Done

Download Ubuntu ISO

Bootable USB

  • Install and Use UnetBootin
sudo apt-get install unetbootin
  • Launch UnetBootin
  • Select the Ubuntu ISO
  • Select your USB drive
  • OK
  • Done
Now, you have a Bootable USB for ubuntu :D

2016/02/26

Lua string.match vs string.gmatch

Overview

Just now, I spent a while on coding with string.gmatch and string.match. In my opinion, they are not quite intuitive for their usage. So, I tried to placed my example here for future references.

Code example

  • gmatch aims for global search while match ends at 1st match.
> line = "Jan  1 08:17:02 ABC hotplug: [OK] ifup: 'lan' => 'br-lan'"
> pat = '([^ ]+)'

# End at Jan
> return line:match('([^ ]+)')
Jan

# Equivalent to split by ' '
> for w in line:gmatch(pat) do print(w) end
Jan
1
08:17:02
ABC
hotplug:
[OK]
ifup:
'lan'
=>
'br-lan'
  • gmatch does not good at dealing with ^
> line = "Jan  1 08:17:02 ABC hotplug: [OK] ifup: 'lan' => 'br-lan'"
> pat = '^([A-Z][a-z][a-z])'
# Able to get Jan
> =line:match(pat)
Jan
# Unable to get anything
> for w in line:gmatch(pat) do print(w) end
>

Reference

  1. http://stackoverflow.com/questions/28593385/lua-string-match-vs-string-gmatch
  2. http://lua-users.org/wiki/StringLibraryTutorial

2016/02/09

Notes on studying LTN12

Background
Suppose you don’t know what LTN is, LTN stands for Lua technical note.
Below are notes dropped from LTN12 which is the 12th documents talking about filters, sink , source, chain and pump.
Source codes from the author is available here
Interpretations
XXX: My interpretations may or not may correct. Let me know if there are any misunderstands.
Functions accept successive chunks of input, and produce successive chunks of output.

Mathematically speaking,

Suppose f1, f2, ..., fn are filters s.t. F = f1 + f2 + ... + fn
Suppose x1, x2, ... xn are input s.t. X = x1 + x2 + ... + xn

F(x) == f1(x1) + f2(x2) + ... + fn(xn)
  • What is a chain?
A function combines two *similar* functions. By *similar*, I mean same function signatures and return data type.

Mathematically speaking,

Suppose f1, f2, ..., fn are functions s.t. C = f1 + f2 + ... + fn
Suppose x1, x2, ... xn are input s.t. X = x1 + x2 + ... + xn

C(x) == f1(x1) + f2(x2) + ... + fn(xn)
  • What are sources and sinks?
Filters form internal nodes inside an arbitrary *data flow*. *Data* in *data flow* means things shared between producer and consumer while *flow* means the direction of that things which is from *Producer* to *Consumer*

Given picture above, we can interpret *Sources* and *Sinks* as initial and final nodes of the *data flow*.
  • What is pump?
The driving force between internal nodes (filters)
  • Example application?
-- Goal: convert any input text ends with \r\n

-- source.file() returns a kind of filter
-- normalize() returns a kind of filter
-- source.chain() shows how to combine *similar* filters
-- input is a kind of *source*
input = source.chain(source.file(io.stdin), normalize("\r\n"))

-- sink.file() returns a kind of filter
-- output is a kind of *sink*
output = sink.file(io.stdout)

-- Driving force between input and output
-- Or, start the jobs
pump(input, output)

2016/01/15

Rescue vim process from other dead terminal session

Due to the fact that my office’s PC always down with no reason and leaving bunch of vim zombie sessions in my remote machine, I am tired for rescuing them times by times.
Finally, I found a way to resolve this issue.

Setup

  • OS: Ubuntu 14.04
  • Command required: reptyr and ps

Find PIDs of those zombie vim session

$> ps aux | grep vi

Rescue them

$> reptyr PID

Permission Problem

In case you got error like below
$ reptyr 1851
Unable to attach to pid 1851: Operation not permitted
The kernel denied permission while attaching. If your uid matches
the target's, check the value of /proc/sys/kernel/yama/ptrace_scope.
For more information, see /etc/sysctl.d/10-ptrace.conf
Fix this by editing configuration from 1 to 0
$vi /etc/sysctl.d/10-ptrace.conf

 kernel.yama.ptrace_scope = 0
Reload rule
$ sudo sysctl -p /etc/sysctl.d/10-ptrace.conf
Done.

References

2015/10/02

octave on MAC OS X

About

octave is a GNU project which is a kind of alternative for MatLab. Below covers how I install this stuff in my MBA.

Tutorials

Since I am using port, I follow guides from the official wiki here. Below are the actual commands I have typed.
sudo port selfupdate
sudo port upgrade outdated

sudo port install octave
sudo port install octave +gui

sudo port install arpack -accelerate+atlas
If you can run below commands successfully, congratulation. This post is end. However, if you have gotchas like me, checkout below notes.

Gotcha

  • There are errors like missing fortran compiler
      port install gcc49
    
Gcc other than 49 may work too. But I test with this one.
  • There are errors like missing tex
     port install texlive-basic
     port install texlive-latex
    
After running above calls, run again
   port install octave
It should fix all the problems.

Open octave

You can simply type octave in your spotlight and octave will be there.

2015/07/17

How to use git to do development #2

Recall

Last time I have shown you my configurations. This time I am going to share my visions about git merge and git rebase.

What is merge and what is rebase?

If you are confused about these 2 operations, I suggest you to go through slides here.
For merge, it adds a node (commit) in order to join two parallel branches (with a parent node some where).
For rebase, it moves one of the parallel branches onto another one. “NO” new nodes will be added during this operation.

Workflow?

By workflows, I mean how you tactic and regulate branches in your project. For example, master is a production branch while hotfix are branches that branched off from master. Those hotfix branches must be merged or rebased eventually etc.
The concept of workflow is a kind of regulations or agreements between developers.
IMHO, you do not need this if you are one man band :) Do whatever you like.
If you are not sure which one to start, I suggest you to go through this one first. Even you do not agree with the writer, he gives you hints for modeling your workflow with git.

So, which workflow I have adopted?

Actually, I am a fans of terminal. I would like to keep all my history as linear as possible which is easier for me to read. Therefore, I will embrace git rebase instead of git merge.
However, this does not means I do not git merge at all. Since result of git merge and git rebase are the same eventually, I will use git merge if I am running out of time.
Alternatively, if you are good at using GUI tools like SourceTree, go for git merge instead of git rebase since those GUI is able to tell you commits coming from which branch.

2015/06/21

How to install Mongodb via macport

Warning

This blog post is useless for guys using Homebrew. Actually, according to the official document, you should use homebrew for installing mongodb.
However, if you are the fans of macport, it should be useful for you :).

Steps

  • Follow instructions from here
Congratulation. You have successfully installed Mongodb.

Gotcha

  • Missing some binaries same as issue here
    $> sudo port install mongo-tools
    

2015/06/03

How to setup LuCI web framework with Apache on Ubuntu 12.04

While developing LuCI web services, I found this is quite annoying for moving codes back and forth from my machine to the router.

So, I decided to setup a LuCI development environment on my own machine. Below are steps for doing so.

Prerequisites

  • Ubuntu 12.04

OS I have tested

  • LuCI

If you havn’t installed, please refer to my another blog post

  • Apache2 (Version: 2.2.22-1ubuntu1)

Version of Apache2 is not important if you can take care the PATH issues. For example, the HTML root directory.

Command to install apache2


apt-get install apache2

How it work

Below is a simplified diagram. I have not drawn all the components involved.


           CGI

|APACHE| <-----> |         LuCI           |

# Inside LuCI

|cgi-bin/luci| <--> |luci.sgi.cgi| <--> |luci core|

According to above diagram, we only need to setup a CGI script for briding up apache and luci

Steps

1. Checkout cgi directory location

We need to put luci (a cgi script) in there such that Apache can run.


$> cat /etc/apache2/sites-enabled/000-default

...

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

...

ScriptAlias means apaches will run a corresponding script in /usr/lib/cgi-bin when there are requests to URL http://HOST/cgi-bin/ANY_CGI_SCRIPT

Therefore, we should put luci in /usr/lib/cgi-bin

2. Get a LuCI repository

I am using version LuCI.0.12 from the github here

3. Get luci from the luci repository

Define <LUCI> to be the path for luci repository


# Get CGI script

$> sudo cp <LUCI>/host/www/cgi-bin/luci /usr/lib/cgi-bin/

# Get CSS html template etc

$> sudo cp -r <LUCI>/host/www/luci-static /var/www

# Change permission

$> sudo chown -R www-data:www-data /var/www/luci-static

$> sudo chmod 755 /usr/lib/cgi-bin/luci

4. Edit Apache configuration /etc/apache2/sites-enabled/000-default

Make sure you have been replaced LUA_PATHLUA_CPATHLUCI_SYSROOT and LD_LIBRARY_PATH with your own path.


....

<Directory "/usr/lib/cgi-bin">

        SetEnvIf \

                Request_URI ".*luci.*" \

                LUA_PATH=/home/mondwan/Documents/git/luci/host//usr/lib/lua/?.lua;/home/mondwan/Documents/git/luci/host//usr/lib/lua/?/init.lua;$LUA_PATH; \

                LUA_CPATH=/home/mondwan/Documents/git/luci/host//usr/lib/lua/?.so;$LUA_CPATH; \

                LUCI_SYSROOT=/home/mondwan/Documents/git/luci/host \

                LD_LIBRARY_PATH=/home/mondwan/Documents/git/luci/host/usr/lib:

        AllowOverride None

        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

        Order allow,deny

        Allow from all

</Directory>

....

5. Restart apache


$> sudo service apache2 restart

6. Create a directory for LuCI

The directory /etc/config simulates a config system in OpenWRT which LuCI relies on.


$> mkdir -p /etc/config

7. Write following default contents into /etc/config/luci


config core 'main'

    option lang 'auto'

    option mediaurlbase '/luci-static/openwrt.org'

    option resourcebase '/luci-static/resources'

config extern 'flash_keep'

    option uci '/etc/config/'

    option dropbear '/etc/dropbear/'

    option openvpn '/etc/openvpn/'

    option passwd '/etc/passwd'

    option opkg '/etc/opkg.conf'

    option firewall '/etc/firewall.user'

    option uploads '/lib/uci/upload/'

config internal 'languages'

    option en 'English'

config internal 'sauth'

    option sessionpath '/tmp/luci-sessions'

    option sessiontime '3600'

config internal 'ccache'

    option enable '1'

config internal 'themes'

    option Bootstrap '/luci-static/bootstrap'

8. View on browser

Errors are expected :)

Ubuntu and Openwrt are 2 different OS. They build up with different set of commands for managing computational resources.

In other words, most of the application from official repository cannot be reused here unless you have ported all the internal details from Openwrt to Ubuntu.

NOTE: Some of them have been port to Ubuntu in this PPA

9. Remove incompatitable components so that we can run admin interface


$> cd <LUCI>/host/usr/lib/lua/luci/controller

$> mv admin/ ..

$> rm -fr *

$> mv ../admin/ .

# Clean up cache

$> sudo rm -fr /tmp/luci-indexcache /tmp/luci-modulecache/

10. Try again

If you can see above picture, you can start developing your own LuCI application under folder <LUCI>/host/usr/lib/lua/luci/controller

NOTES

  • ADMIN interface will not work as expected

As stated above, most commands from openwrt are not ported to Ubuntu (eg: ubus). The admin interface will not work as expected.

  • Missing folders I mentioned above

Make sure you are using luci-0.12 since the latest luci alters the build process somehow.

If you are using GIT for downloading source codes, here are commands for checking out luci-0.12


$> cd PROJECT_ROOT

$> git checkout luci-0.12

Then follow my blog post for installation

  • Apache CGI does not work as expected

Please take a look here and make sure cgi module has been enabled.

  • Unable to run /cgi-bin/luci due to permission problem

Try to replace these lines


Order allow,deny

Allow from all

to this


Require all granted

Extras

  • Disable cache mechanism by comment 2 lines

By default, LuCI caches Lua files as binaries. Modification in your applications will be ignored if binaries had been created. Here is a way for disabling such mechanism.


$> vi /usr/lib/cgi-bin/luci

-- require "luci.cacheloader"

require "luci.sgi.cgi"

-- luci.dispatcher.indexcache = "/tmp/luci-indexcache"

luci.sgi.cgi.run()

References

Updates

  • 2015/6/26

    • Update notes contents about missing folder
  • 2017/3/5

    • Fix a typo in the script

    • Add a link about enable Apache CGI

    • Add how to fix permission problems from Apache (Thanks +Anton Key )

    • Rewrite certain sentances and unify the style

  • 2020/01/05
    • Add a note on ubutnu PPA (from stokito)