Wednesday, April 24, 2013

[ Level 1 ] A good editor, Sublime.

Here is a power editor called "Sublime", please follow the link to get more information.

http://www.sublimetext.com/

Some useful settings:
1. enable vim.
In Perferences -> Settings-Default, empty the value of ignored_packages,
e.g. "ignored_packages": [],
Default ignore the package Vintage, the vim pakcage.

2. default with command mode.
Add one line in Perferences -> Settings-User:
"vintage_start_in_command_mode": true,
The default mode of vintage would be "command mode"

3. tab for 4 spaces.
In Perferences -> Settings-Default, modify the value of tab_size with 4,
e.g. "tab_size": 4,
And also modify the value of translate_tabs_to_spaces with true,
e.g. "translate_tabs_to_spaces": true,

4. auto-trim tailing spaces.
Add one line in Perferences -> Settings-User:
"trim_trailing_white_space_on_save": true,

Wish this helps.

regards,
Stanley Huang

Saturday, April 20, 2013

[ Level 3 ] The script for creating virtualbox envrionment (Ubuntu 12.04) with lauch new OS image.

In my company, I need to test it when I build a new image.
Therefore I have to setup my testing environment for integration test.

I wrote a script to automatically setup the environment from new install Ubuntu.
The steps are:
1. Download and setup tftp server.
2. Download and setup dhcp server with netboot option.
3. Install virtualbox.

After that you could create a VM then choose netboot. (the first interface must connect to tap0 to get boot server information)

#!/bin/bash

######## utilities ########
_getTasks() {
  indent="$1"
  cat $0 | grep '()' | grep -v "^_" | grep "^[^ ]" | cut -d'(' -f1 | sed -e "s/^/$indent/"
}

_showUsage() {
  indent="  "
  cat <<EOF
Usage:
  $0 task
Ex.
  $0 `_getTasks | head -1`
tasks:
${indent}[ all ]
`_getTasks "$indent"`
EOF
}

_die() {
  echo "$1"
  exit ${2:-1}
}

_checkID() {
  uid=`id | cut -d'(' -f1 | cut -d'=' -f2`
  [ $uid -ne 0 ] && _die "Need root to execute this command, exit!"
}
######## utilities ########

######## create tftpd configuration ########
TFTP_DIRECTORY=/tftpboot

installTftpd() {
  apt-get -y remove tftpd ## conflict with tftpd-hpa
  apt-get -y install tftpd-hpa tftp
}
#installTftpd

createTftpdConf() {
  echo "Run createTftpdConf()"
  cat > /etc/default/tftpd-hpa <<EOF
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="$TFTP_DIRECTORY"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-c --secure"
EOF
}
#createTftpdConf
######## create tftpd configuration ########

######## create /tftpboot content ########
installSyslinux() {
  apt-get -y install syslinux syslinux-common
}

testTftp() {
  test_filename="$1"
  cd /tmp
  rm -f ./$test_filename
  tftp localhost <<EOF
get /$test_filename
EOF
  [ ! ./$test_filename ] && echo "Get file ($test_filename) from tftp server fail..." && exit 1
  rm -f ./$test_filename
}

createTftpbootContent() {
  echo "Run createTftpbootContent()"
  mkdir -p $TFTP_DIRECTORY
  cp /usr/lib/syslinux/pxelinux.0 $TFTP_DIRECTORY
  cp /usr/lib/syslinux/*menu.c32 $TFTP_DIRECTORY
  mkdir -p $TFTP_DIRECTORY/pxelinux.cfg && chmod 777 $TFTP_DIRECTORY/pxelinux.cfg
  mkdir -p $TFTP_DIRECTORY/boot && chmod 777 $TFTP_DIRECTORY/boot 
  cat > $TFTP_DIRECTORY/pxelinux.cfg/default <<EOF
prompt 0
default default_install
timeout 3

label default_install
  kernel boot/vmlinuz-2.6.32.24
  append initrd=boot/default-installer.img
EOF
  chown -R tftp:tftp $TFTP_DIRECTORY
  testTftp "pxelinux.0"
}
#createTftpbootContent

######## create /tftpboot content ########

######## install user-mod linux package ########
installUserModPackages() {
  echo "Run installUserModPackages()"
  apt-get -y install uml-utilities
}
#installUserModPackages

_getRealUserName() {
  tty_filter="`tty | sed -e 's|/dev/||'`"
  who | grep "$tty_filter" | awk '{print($1)}'
}

createTap() {
  echo "Run createTap()"
  user_name="`_getRealUserName`"
  subnet=10.10
  netmask=255.255.255.0
  TIMEOUT=5

  interface_file="/etc/network/interfaces"
  interface_orig="$interface_file.orig"
  [ ! -f $interface_orig ] && cp $interface_file $interface_orig
  if [ -f $interface_orig ]
  then
    read -t $TIMEOUT -p "Do you want to use orig interfaces file ($interface_orig) to be the template? (Timeout for $TIMEOUT seconds)) [Y/n]: " Yn
    [ "$Yn" != "n" ] && [ "$Yn" != "N" ] && cp $interface_orig $interface_file
  fi

  for index in `seq 0 1`
  do
  cat >> $interface_file <<EOF

auto tap$index
iface tap$index inet static
pre-up tunctl -t tap$index -u $user_name
up ifconfig tap$index up
down ifconfig tap$index down
address $subnet.$index.254
netmask $netmask
EOF
  done

  echo "New interfaces file:"
  echo "================================================"
  cat $interface_file
  echo "================================================"
}
#createTap
######## install user-mod linux package ########

######## create dhcpd.conf ########
installDhcpd() {
  apt-get -y install isc-dhcp-server ## isc-dhcp-server replaced dhcp3-server
}

createDhcpdConf() {
  echo "Run createDhcpdConf()"
  subnet=10.10 ## for tap0/tap1
  #subnet=192.168 ## for virtualbox vboxnet0
  netmask=255.255.255.0
  TIMEOUT=5

  dhcpd_conf_file="/etc/dhcp/dhcpd.conf"
  dhcpd_conf_orig="$dhcpd_conf_file.orig"
  [ ! -f $dhcpd_conf_orig ] && cp $dhcpd_conf_file $dhcpd_conf_orig
  if [ -f $dhcpd_conf_orig ]
  then
    read -t $TIMEOUT -p "Do you want to use orig dhcpd file ($dhcpd_conf_file) to be the template? (Timeout for $TIMEOUT seconds)) [Y/n]: " Yn
    [ "$Yn" != "n" ] && [ "$Yn" != "N" ] && cp $dhcpd_conf_orig $dhcpd_conf_file
  fi

  for index in `seq 0 1` ## for tap0/tap1
  #for index in `seq 56 56` ## for virtualbox vboxnet0
  do
  cat >> $dhcpd_conf_file <<EOF

subnet $subnet.$index.0 netmask $netmask {
   option subnet-mask $netmask;
   option broadcast-address $subnet.$index.255;
   option routers $subnet.$index.254;
   range $subnet.$index.210 $subnet.$index.220;
 
   filename "/pxelinux.0";
}
EOF
  done
  echo "New dhcpd config file:"
  echo "================================================"
  cat $dhcpd_conf_file
  echo "================================================"
}
#createDhcpdConf

checkDhcpService() {
  netstat -tlunp | grep dhcp || _die "Cannot find binding address of dhcp!"
}
#checkDhcpService

######## install virtualbox ########
installVBox() {
  echo "Run installVBox()"

  ## Url for virtualbox 4.2.12 to download
  vbox_binary_url_4_2_12_for_12_10="http://download.virtualbox.org/virtualbox/4.2.12/virtualbox-4.2_4.2.12-84980~Ubuntu~quantal_amd64.deb"
  ## Url for virtualbox extension pack 4.2.12 to download
  vbox_extpack_url_4_2_12="http://dlc.sun.com.edgesuite.net/virtualbox/4.2.12/Oracle_VM_VirtualBox_Extension_Pack-4.2.12.vbox-extpack "

  ## download virtualbox and install it
  vbox_binary_url=$vbox_binary_url_4_2_12_for_12_10
  vbox_binary_filename=${vbox_binary_url##*/}
  wget $vbox_binary_url
  dpkg -i ./$vbox_binary_filename

  ## setup vbox driver
  apt-get -y install linux-headers-3.5.0-27 linux-headers-3.5.0-27-generic
  /etc/init.d/vboxdrv setup

  ## download virtualbox extension and install it
  vbox_extpack_url=$vbox_extpack_url_4_2_12
  vbox_extpack_filename=${vbox_extpack_url##*/}
  wget $vbox_extpack_url
  vboxmanage extpack install ./$vbox_extpack_filename
}
#installVBox
######## install virtualbox ########

######## main ########
_checkID

## Start install vm
echo "Start install vm..."
if [ -z "$1" ]
then
  _showUsage
elif [ "$1" == "all" ]
then
  task_list="$@"
else
  task_list="$1"
fi

for task in $task_list
do
  eval $task
done
## Install done
echo "Install vm done! Please restart the machine~"


Wish this helps.

regards,
Stanley Huang

Friday, April 19, 2013

[ Level 3 ] The script for setup the Ubuntu for Juniper VPN.

There is an paper told us how to setup Ubuntu 12.04 for Juniper VPN.
Then I follow the guide and also write a script for installation/setup.
The steps as the following:
1. Download ans install ia32-libs to support 32 bit libraries.
2. Download and install x64 JRE, jre-7u21-linux-x64.tar.gz.
3. Create plugin link for firefox/google chromium.
4. Update alternative Java.
5. Download and install i586 JRE, jre-7u21-linux-i586.tar.gz.

After that, you could login to your VPN.

#!/bin/bash
## url link for 64 bit JRE.
jre_x64_source_url=http://javadl.sun.com/webapps/download/AutoDL?BundleId=76853
## url link for 32 bit JRE
jre_i586_source_url=http://javadl.sun.com/webapps/download/AutoDL?BundleId=76851

die() {
  echo "$1"
  exit ${2:-1}
}

checkRootID() {
  uid=`id | cut -d'(' -f1 | cut -d'=' -f2`
  [ $uid -ne 0 ] && die "Need root to execute this command, exit!"
}
checkRootID
 
pek2c() {
  bDebug && read -p "Press enter key to continue..." x
}

syncFS() {
  sync;sync;sync
  sleep 3
}

changeOwner() {
  chown -R root:root $java_dir
}

installJREx64() {
  ## install jre x64
  jre_x64_filename=jre_x64.tar.gz
  rm -f $exec_at_dir/${jre_x64_source_url##*/}
  cd $exec_at_dir
  wget $jre_x64_source_url
  mv $exec_at_dir/${jre_x64_source_url##*/} $java_dir/$jre_x64_filename
  cd $java_dir
  tar zxf $java_dir/$jre_x64_filename
  jre_x64_version=`tar ztvf $java_dir/$jre_x64_filename | head -1 | awk '{print($NF)}' | sed -e s'|/$||'`
  syncFS
  pek2c
  update-alternatives --install /usr/bin/java java /usr/java/$jre_x64_version/bin/java 1 
  update-alternatives --set java /usr/java/$jre_x64_version/bin/java
  pek2c
  cd /usr/lib/mozilla/plugins && rm -f ./libnpjp2.so ./libjavaplugin.so ./libjavaplugin_oji.so && ln -sf /usr/java/$jre_x64_version/lib/amd64/libnpjp2.so ./
  cd /usr/lib/chromium-browser/plugins && ln -sf /usr/java/$jre_x64_version/lib/amd64/libnpjp2.so ./
  pek2c
  rm $java_dir/$jre_x64_filename
}

installJREi586() {
  ## install jre i586
  jre_i586_filename=jre_i586.tar.gz
  rm -f $exec_at_dir/${jre_i586_source_url##*/}
  cd $exec_at_dir
  wget $jre_i586_source_url
  mv $exec_at_dir/${jre_i586_source_url##*/} $java_dir/$jre_i586_filename
  cd $java_dir
  tar zxf $java_dir/$jre_i586_filename
  syncFS
  pek2c
  rm $java_dir/$jre_i586_filename
}

######## main ########

[ "$1" == "-d" ] && set -vx && bDebug=true

## global vars, prepare environment
exec_at_dir="`pwd`" ## where to execute this command.
cd $exec_at_dir/./`dirname $0`
run_at_dir="`pwd`" ## where should this script run at.
java_dir="/usr/java"
mkdir -p $java_dir
apt-get install -y ia32-libs

## install JRE x86 first
installJREx64

## install JRE x586 second
installJREi586

## change owner
changeOwner




Wish this helps.

regards,
Stanley Huang

Wednesday, April 17, 2013

[ Level 1 ] How to remove rc listed in dpkg -l @Ubuntu.

You could use dpkg -P to remove it.

e.g.
$ dpkg -l virtualbox
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                               Version                Architecture           Description
+++-==================================-======================-======================-=========================================================================
ii  virtualbox                         4.1.18-dfsg-1ubuntu1.1 amd64                  x86 virtualization solution - base binaries

$ apt-get remove virtualbox
...

$ dpkg -l | grep virtual
...
rc  virtualbox                                4.1.18-dfsg-1ubuntu1.1                    amd64        x86 virtualization solution - base binaries
rc  virtualbox-qt                             4.1.18-dfsg-1ubuntu1.1                    amd64        x86 virtualization solution - Qt based user interface

$ package_name="virtualbox" dpkg -l | grep ^rc | grep "$package_name" | cut -d' ' -f3| sudo xargs dpkg -P
...

$ package_name="" dpkg -l | grep ^rc | grep "$package_name" | cut -d' ' -f3| sudo xargs dpkg -P ## clean all unused rc(s).

Wish this helps.

regards,
Stanley Huang

Tuesday, April 16, 2013

[ Level 2 ] Ubuntu package install script.

I wrote an easy script for install Ubuntu packages.
#!/bin/bash

#
# This script is for install popular packages
#
#$: check package, if exists, skip the following prefix #^
#@: apt-get install packages
#!: unix command
#^@: apt-get install packages, depends on previous #$
#^!: unix command, depends on previous #$
#

showUsage() {
  cat <<EOF
Usage:
  $0
  $0 [package name]
Ex.
  $0
  $0 rdesktop
EOF
}

isPackageNotInstall() {
  dpkg -l $1 >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "Package '$1' exists, skip it!"
    return 1
  else
    return 0
  fi
}

getContent() {
  declare -i begin
  declare -i end
  declare -i size
  begin=$1
  end=$2
  size=$end-$begin+1
  tail -n +$begin $0 | head -$size
}

getPackageName() {
  echo $1 | cut -d' ' -f2- | cut -d'#' -f1
}

getCommand() {
  echo "$line" | cut -d' ' -f2- | cut -d'#' -f1
}

die() {
  echo $1
  exit 1
}

aptGetInstall() {
  echo -n "Start install package '$@'..."
  apt-get -y install $@ && echo "OK" || die "FAIL"
}

commandExecute() {
  echo -n "Start execute command '$@'..."
  $@ && echo "OK" || die "FAIL"
}


########## main ##########
if [ ! -z "$@" ]
then
  apt-get -y install $@
else
  start_from_here_string="Package List, Start from here!"
  stop_at_here_string="Package List, Stop at here!"
  start_line_no=`grep -n "$start_from_here_string" "$0" | tail -1 | cut -d: -f1`
  stop_line_no=`grep -n "$stop_at_here_string" "$0" | tail -1 | cut -d: -f1`
  SKIP_FLAG=false
  CHECK_FLAG=false
  getContent $start_line_no $stop_line_no | while read line
  do
    if echo "$line" | grep -e "^#\\$" >/dev/null 2>&1
    then
      CHECK_FLAG=true 
      if isPackageNotInstall `getPackageName "$line"`
      then
        echo "Package (`getPackageName '$line'`) doesn't exist!"
        SKIP_FLAG=false
      else
        SKIP_FLAG=true
      fi
      continue
    elif echo "$line" | grep -e "^#^" >/dev/null 2>&1
    then
      $SKIP_FLAG && echo "Dependy exists, skip `getPackageName "$line"`" && continue
      line=`echo "\$line" | sed -e 's/^#^/#/'`
    else
      CHECK_FLAG=false
    fi

    echo $line | grep -e "^#@" >/dev/null 2>&1 && isPackageNotInstall `getPackageName "$line"` && aptGetInstall `getPackageName "$line"` 
    echo $line | grep -e "^#!" >/dev/null 2>&1 && commandExecute `getCommand "$line"`
  done
  exit
  
  cat $0 | grep ^#@ | awk '{print($2)}' | while read sPkg
  do
    echo "pkg install $sPkg"
  done
fi

exit
exit

##
## Package List, Start from here!
##

#### app ####
#@ vlc
#@ testdisk
#@ nfs-common
#@ openssh-server
#@ samba

#### system ####
#@ htop
##@ scim scim-pinyin scim-chewing scim-tables-zh scim-qtimm im-switch
##! im-switch -s scim

#### network ####
#@ chromium-browser
##@ openvpn
#@ wireshark
#@ transmission
#@ rdesktop

#### media ####
#@ gimp
#@ avidemux
#@ audacity
#@ freemind
#@ flashplugin-nonfree
##! sudo cp /usr/lib/flashplugin-installer/libflashplayer.so /usr/lib/chromium-browser/plugins
#@ mimms # download mms streaming

#### tools ####
#@ meld
#@ vim
#@ tree
##@ adacontrol # ptree
#@ minicom

#### developer tools ####
#@ maven2
#@ visualvm
#@ libssl-dev

#### install acrobat reader ####
#@ ia32-libs wget
##! wget http://ardownload.adobe.com/pub/adobe/reader/unix/9.x/9.1.1/enu/AdbeRdr9.1.1-1_i486linux_enu.bin
##! chmod +x AdbeRdr9.1.1-1_i486linux_enu.bin
##! ./AdbeRdr9.1.1-1_i486linux_enu.bin

#$ grub-customizer
#^! add-apt-repository ppa:danielrichter2007/grub-customizer
#^! apt-get update
#^@ grub-customizer

##
## Package List, Stop at here!
##

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Recovery disk partition utility in for Ubuntu.

There is a tool for Ubuntu could help you to recover disk partition, called "testdisk'.

$ sudo apt-get -y install testdisk
$ sudo testdisk
and choose:
Create -> [select disk] -> Intel -> Analyse -> Quick Search -> Write
And done~

Wish this helps.

regards,
Stanley Huang

Friday, April 12, 2013

[ Level 2 ] Enable USB 3.0 in Ubuntu 12.04

After I upgrade 12.04, the USB 3.0 port seems not work.
Then I google the solution:
add "blacklist uas" in the file /etc/modprobe.d/blacklist.conf

Wish this helps.

regards,
Stanley Huang