Sunday, December 27, 2015

How to Compile the First Version of Git on Linux

Here, we will learn how to use git, the widely-used version control system initially developed by Linus Torvalds for Linux kernel development.

First, obtain the software git by
$ sudo apt-get install git -y

Next, clone git repository from GitHub by
$ git clone

When download is complete, it will have create a folder, so let's enter.
$ cd git

Well, we could simply build the current version of git, but for the sake of learning how to use git and getting the taste of what it is capable of doing, let's check out the very first version of git written by Linus in 2 weeks.

To do this, we first need to search for different commits:
$ git log --oneline

Now, scroll the to the very bottom by pressing 'end' key several times.When you reach the end, press 'q' key. You should see the following output:
e83c516 Initial revision of "git", the information manager from hell

This is the very first version (present in the repository). Let's check out this version by
$ git checkout e83c516

Now, you will see that the files in the folder have been de-dated back to 2005! How do I know? Well, run
$ git log

Because we will make changes and commit this soon, we will create a branch here, so simply run
$ git checkout -b ver1.0

Let's take a look at what branches we have now
$ git branch
Note that ver1.0 branch has been added, where master is the current version of git.

There are only a handful of files here at this very early stage. Feel free to read Linus' code. When done, let's compile. First, you will need to install libraries:
$ sudo apt-get install libssl-dev zlib1g-dev -y

When you run
$ make

It will probably complain about some define references. We will need to edit the Makefile file and include some libraries:
$ vim Makefile

Add -lz and -lcrypto parameters for LIBS flag, so line #11 should read
LIBS= -lssl -lz -lcrypto

Save the file and let's try to compile again
$ make

This should compile! Now, let's commit the change to the local repository:
$ git add Makefile
$ git commit -m "edit Makefile to compile in Ubuntu 14.04LTS"

To go back to the current version of git, we check out the master branch$ git checkout master

To go back to the compilable first version of git, simply run
$ git checkout ver1.0

I will go through some more advanced git tutorials later.

Sunday, December 13, 2015

Changing Linux Swappiness

As the RAM price is going down very fast, most modern computers can be equipped with plenty of RAM, perhaps more than enough for most of people using RAM-efficient operating systems, such as Linux. I personally have 8GB of RAM installed, and I barely use even half of this unless I run virtual machines.

However, for whatever the reason, I still find some instances where my Linux system makes use of the swap space although I have plenty of RAM still unused. Here, I will show you how to minimize the use of the swap space. After all, accessing RAM should be incomparably faster than accessing the hard disk drive or solid state drive, so you probably want to minimize swap usage if your system has a plenty of RAM installed.

There is a variable called swappiness in Linux that determines the likelihood of swap to be used by the system. This value is saved in /proc/sys/vm/swappiness file. So, to check your system's swappiness value, simply run
$ cat /proc/sys/vm/swappiness

The default will be most likely 60. You may want to adjust this to 0 to minimize.
$ sudo echo 0 > /proc/sys/vm/swappiness

That's it!

Installing Android Studio on Ubuntu and Debian 64bit

Update: refer to official documentation from Google.

First, download the zip file from Google

I downloaded the All Android Studio Packages for Linux at the very bottom.
Then, in the download folder, unzip the zip file
$ cd ~/Downloads
$ unzip -x android-studio-ide-*.zip

We also need Oracle JDK 1.7, so download this as well.

Download jdk-7u79-linux-x64.tar.gz file. Unzip it as well.
$ tar xzvf jdk-7u79-linux-x64.tar.gz

This will create jdk1.7.0_79 folder. You can either leave this folder here or move it to the system folder. I personally moved it to
$ sudo mv jdk1.7.0_76 /opt/

Now, we need to set the environment JAVA_HOME to point to this folder. To do this, either
$ export JAVA_HOME=~/Downloads/jdk1.7.0_76
$ export JAVA_HOME=/opt/jdk1.7.0_76
depending on where you put the folder. To make this permanent, refer to my previous post.

Now, to run the Android Studio, you simply run the script in the android-studio/bin folder
$ ./android-studio/bin/

This is not it yet. You will probably encounter some error saying
Unable to run mksdcard SDK tool.
while additional setup & installation within Android Studio because it will depend on some of 32bit libraries. Therefore, you will need to install the following 32bit libraries as well
$ sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6

Update: For Debian Jessie or Ubuntu 16.04, run
$ sudo apt-get install lib32z1 lib32ncurses5 lib32stdc++6

Now, you will be able to successfully install and run Android Studio and related packages.

Friday, December 11, 2015

Connect to L2TP/IPSEC server (VPN) from Ubuntu

Step 1, install necessary package
$ sudo apt-get install openswan xl2tpd

Step 2, create a backup of /etc/ipsec.conf file
$ sudo cp /etc/ipsec.conf /etc/ipsec.conf.bk

Step 3, copy the following to the config file and modify according to the comment
$ sudo vim /etc/ipsec.conf

version    2.0

config setup
       # replace eth1 below with your network interface
conn L2TP-PSK

        # replace with your ip address below

        # replace with the server ip address below

Note that in order to find out your network interface and ip address, you can simply do
$ ifconfig

Step 4, create a backup of /etc/ipsec.secrets
$ sudo cp /etc/ipsec.secrets /etc/ipsec.secrets.bk

Step 5, add the following line and modify according to the comment
$ sudo vim /etc/ipsec.secrets

# replace with your ip address, server ip address, and pre-shared key below
your_ip_address server_ip_address : PSK "pre-shared_key"

Step 6, create a backup of /etc/xl2tpd/xl2tpd.conf
$ sudo cp /etc/xl2tpd/xl2tpd.conf /etc/xl2tpd/xl2tpd.conf.bk

Step 7, copy the following to the conf file and replace with the server ip address
$ sudo vim /etc/xl2tpd/xl2tpd.conf

[lac l2tpd]
lns = server_ip_address
ppp debug = yes
pppoptfile = /etc/ppp/options.l2tpd.client
length bit = yes

Step 8, create /etc/ppp/options.l2tpd.client with the following and enter username and password
$ sudo vim /etc/ppp/options.l2tpd.client

idle 1800
mtu 1410
mru 1410
connect-delay 5000
name user_name
password user_password

Step 9,create a route to the server by typing the following and replacing the values accordingly
$ sudo ip ro ad server_ip_address via your_default_gateway

In order to find out your default gateway, simply run
$ route

Step 10, you will need to restart the services in order to apply the changes in the settings
$ sudo invoke-rc.d ipsec restart
$ sudo invoke-rc.d xl2tpd restart

Step 11, create a shell file for connection
$ vim

ipsec auto --up LT2P-PSK
echo "c l2tpd" > /var/run/xl2tpd/l2tpd-control

Step 12,  create a shell file for disconnection
$ vim

echo "d l2tpd" > /var/run/xl2tpd/l2tp-control
ipsec auto --down L2TP-PSK

Step 13, set the shell files excutable
$ sudo chmod u+x
$ sudo chmod u+x

You are finally ready! To establish the connection, simply run the shell
$ sudo ./

To disconnect, run the disconnect shell
$ sudo ./

Finally, you should be able to check the status via
$ ip link
where if connection has been established, then it will be indicated by link/ppp.

Thursday, December 10, 2015

How to Debug Qemu

In order to debug Qemu, one first runs the -s and -S options in qemu:
$ qemu -s -S -hda hda.img

This will pause qemu and wait for gdb connection tcp port 1234.

Open up another terminal and run the following db command
$ gdb
(gdb) target remote localhost:1234

Now, we may want to setup the break point. If you want to setup the break point as soon as the system starts, that is memory address 0x7c00, so set the break point and continue!
(gdb) b *0x7c00
(gdb) c

How to Find Assembly Code for Master Boot Record (MBR)

For IBM-compatible PCs, MBR is the first set of instructions during the boot process. In order to view the assembly code stored in MBR, one can do the following:

First, find out the file system by
$ df -h

This provides the name of the hard disk, either hda1 or sda1. The MBR is located in the very first sector of the hard disk (or solid state drive) so we will remove the number (which indicates the partition) in the following command.
$ sudo dd if=/dev/sda of=MBR.bin bs=512 count=1

This will create a raw file MBR.bin on the current directory which is the exact copy of the MBR of the computer.

Now, to examine this file, we use objdump
$ objdump -D -b binary -m i8086 MBR.bin

If you want intel syntax, add -M intel option
$ objdump -D -b binary -m i8086 -M intel MBR.bin

Note that the last two bytes should be 0x55 and 0xAA, which make up the boot signature that tells BIOS that this disk is bootable.

Saturday, December 5, 2015

Setting System-Wide Environment Variables in Linux

When I download binary files that I would like to add to the PATH variable, I could simply edit ~.bashrc file. However, this will not be executed if I were to run from GUI programs, such as nautilus. In fact, this is what happened to me:

I wanted to learn Java, so I downloaded eclipse binary which should be run using Java. Well, I did not have Java installed, so I simply downloaded JDK binary files from Oracle and I edited ~/.bashrc file to add the JDK bin folder to the PATH environment. I then tried to run eclipse binary from Caja (which is MATE version of nautilus), but I got an error saying that Java could not be found in the PATH environment. This is because the environment variables are process-specific, so editing the ~/.bashrc added the Java binary folder to the PATH environment specific to the bash process, but not to the Caja process.

So, now I want to add the JDK binary folder to the system-wide PATH environment variable. To do this, I need to create a shell script in /etc/profile.d folder. Here, I added a file with the following line where JDK_binary_folder means refers to where JDK has been installed, such as /opt/jdk1.7.0_76.

export PATH=$PATH:JDK_binary_folder

After logging out and re-logging in, I simply double-clicked the eclipse binary from Caja, and it finally ran it without complaints!

There is another option for this. In Ubuntu, /etc/environment is a file that also sets the system-wide environment variables. Thus, one can edit the file by
$ sudo vim /etc/environment
and add the system environment as desired.