1/10/2013

RESOLVE issue of Mac OS X stuck on 'IOAPIC' while booting

I was very mad at this problem since I upgraded my Mac OS version to 10.7.4 ( a higher version may also have this problem including Mountain Lion) :

--when you are booting with '-v' parameter, it will stuck at here for like 20s-30s or even more
MAC Framework successfully initialized
using 16384 buffer headers and 4096 cluster IO buffer headers
IOAPIC: Version 0x20 Vectors 0:23
--

To resolve this, I just replaced my AppleACPIPlatform.kent from /System/Library/Extensions with into the one I got from Snow Leopard version 10.6.8

This issue happens if you have a motherboard from ASUS with an i7 CPU ( or Xeon ) after upgrading your OS to 10.7.4 or above

8/05/2012

Setup L2TP/IPsec VPN server on Ubuntu


1. Installing OpenSWAN
There is a couple of IPSEC protocol in Linux: FreeSWAN, OpenSWAN, StrongSWAN. Here we are going to deal with OpenSWAN.
The current version of OpenSWAN in Ubuntu Lucid is 2.6.23. Unfortunately it is coming with a bug in this version, make it doesn't work well with L2TP server, we have at least upgrade it to version 2.6.24. 
1.1. Add this 3rd party apt source of OpenSWAN:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:openswan/ppa
sudo apt-get update
1.2. Here we check the version, should be at least 2.6.24
# apt-cache policy openswan
openswan:
  Installed: (none)
  Candidate: 1:2.6.28-1xelerance3
  Version table:
     1:2.6.28-1xelerance3 0
        500 http://ppa.launchpad.net/openswan/ppa/ubuntu/ lucid/main Packages
1.3. Install
sudo apt-get install openswan
If you are asked for "Use an X.509 certificate for this host", just say NO.

2. Configuring OpenSWAN

2.0. Set bash variables
You may have to set your own variables, I'm here just going to use random values as examples:
L2TP Server IP: 192.168.1.1, VPN subnet prefix: 10.1.1.0/24, username: user, password: 1234. 
export SERVER=192.168.1.1
export NET=10.1.1
export USER1=user
export PASS1=1234
2.1. Setup sysctl
sudo mv /etc/sysctl.conf /etc/sysctl.conf.bak
 
sudo bash -c "cat > /etc/sysctl.conf <
2.2. Refresh sysctl
sudo sysctl -p
 
sudo bash -c 'for each in /proc/sys/net/ipv4/conf/*
do
    echo 0 > $each/accept_redirects
    echo 0 > $each/send_redirects
done
'
2.3. Setup up iptables
sudo mv /etc/rc.local /etc/rc.local.bak
 
sudo bash -c "cat > /etc/rc.local <$NET.0/24 -o eth0 -j MASQUERADE
exit 0
EOF
"
  sudo chmod +x /etc/rc.local
2.4. Refresh iptables
sudo /etc/rc.local
2.5. Setup ipsec.secrets
sudo mv /etc/ipsec.secrets /etc/ipsec.secrets.bak
sudo bash -c "cat > /etc/ipsec.secrets <$SERVER %any: PSK \"$PASS1\"
EOF
"
2.6. Setup ipsec.conf
sudo mv /etc/ipsec.conf /etc/ipsec.conf.bak
sudo bash -c "cat > /etc/ipsec.conf <$SERVER
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any
EOF
"
2.7. Make ipsec service started by default
运行下面的命令, 让ipsec服务可以开机自动启动:
sudo update-rc.d ipsec defaults

3. Installing xl2tpd

We all know how to do this:
sudo apt-get install xl2tpd

4. Configuring xl2tpd

4.1. Setup xl2tpd.conf
sudo mv /etc/xl2tpd/xl2tpd.conf /etc/xl2tpd/xl2tpd.conf.bak
sudo bash -c "cat > /etc/xl2tpd/xl2tpd.conf <$NET.2-$NET.255
local ip = $NET.1
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
EOF
"
4.2. Setup options.xl2tpd
sudo mv /etc/ppp/options.xl2tpd /etc/ppp/options.xl2tpd.bak
sudo bash -c "cat > /etc/ppp/options.xl2tpd <
4.3. Setup chap-secrets
sudo mv /etc/ppp/chap-secrets /etc/ppp/chap-secrets.bak
sudo bash -c "cat > /etc/ppp/chap-secrets <$USER1       l2tpd       $PASS1       *
EOF
"
5. Start L2TP server
Till here, we have done configuring L2TP/IPsec server. We will could use it after the server is stared:
sudo invoke-rc.d xl2tpd restart
sudo invoke-rc.d ipsec restart
Check the IPsec server status by this command: "ipsec verify", and it should be like this:
# sudo ipsec verify
Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path                              [OK]
Linux Openswan U2.6.28/K2.6.32.16-linode28 (netkey)
Checking for IPsec support in kernel                         [OK]
NETKEY detected, testing for disabled ICMP send_redirects    [OK]
NETKEY detected, testing for disabled ICMP accept_redirects  [OK]
Checking that pluto is running                               [OK]
Pluto listening for IKE on udp 500                           [OK]
Pluto listening for NAT-T on udp 4500                        [OK]
Two or more interfaces found, checking IP forwarding         [OK]
Checking NAT and MASQUERADEing                              
Checking for 'ip' command                                    [OK]
Checking for 'iptables' command                              [OK]
Opportunistic Encryption Support                             [DISABLED]

6. Testing L2TP server

Just grab a device that support connecting to a L2TP server, Android/iPhone could done this well. If you are doing it with all the variables just like mine, the server IP address should be 192.168.1.1, username is user, and L2TP password and the IPsec pre-shared key are both 1234.

4/09/2012

Setup text mate environment with ruby on rails

Get to preferences page of textmate, click advanced - shell variables,
set the following in order to get most of your textmate bundle work:

PATH -> paths with your git & mysql & ruby
DYLD_LIBRARY_PATH -> path of your mysql lib
ARCHFLAGS -> -arch x86_64
RUBYOPT -> rubygems

3/20/2012

set timezone on Ubuntu

Ubuntu server uses UTC for the default timezone, this blog will guide
you if you wanna change the default timezone

first select your timezone interactively by using 'tzselect', you may
need 'sudo'
after that, we wanna make the change permanently

sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

finally we change 'UTC=yes' into 'UTC=no' in the file located at
'/etc/default/rcS'

now you have it done! do a 'date' for sure

3/02/2012

Rails 3 issue: 'Content-Length' field not exist in HTTP head when using 'send_data' method

I found this issue in one of my Rails 3 app, here is a simple demo:
class AnyController < ApplicationController
  def any_action
    send_data [SOME-BINARY-DATA]
  end
end

when browser make any request to this controller, Rails 3 app will
REMOVE your 'Content-Length' field of the HTTP response header, even
you add content-length in the logic code explicitly

To fix this, we could add a Rack middleware and plug it in to the app,
in your #{Rails.root}/config/application.rb file, find code like below:

module XX
    class Application < Rails::Application
        config.middleware.use "HttpHeaderFix" # add this line

then create a file named 'http_header_fix.rb' at #{Rails.root}/middleware

class HttpHeaderFix
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, response = @app.call(env)
    if headers["Content-Type"] == "application/octet-stream"
      if headers["Content-Length"].blank?
        if response.respond_to? :length
          length = response.length
        elsif response.respond_to? :count
          length = response.count
        elsif response.respond_to? :size
          length = response.size
        elsif response.respond_to? :body
          length = response.body.length
        else
          raise "unknown response: #{response.class}"
        end
        headers["Content-Length"] = length.to_s
      end
    end
    [status, headers, response]
  end
end
OK, there you go!!

3/01/2012

Setup ssh proxy under linux

First setup OpenSSH
check your /etc/shells file if it contains a line '/sbin/nologin'

Then you want just add your SSH account like this:

useradd -M -s /sbin/nologin -n username
some version of Linux may like this:
useradd -s /sbin/nologin username
and even the 'nologin' shell is located at '/usr/sbin/nologin' in some
Linux version

Now, you have a SSH server & account setup

Recommended web browser plugins for SSH proxy:
Firefox users checkout autoproxy:
https://addons.mozilla.org/en-US/firefox/addon/autoproxy/
Chrome users checkout Switchy!:
https://chrome.google.com/webstore/detail/caehdcpeofiiigpdhbabniblemipncjj

2/12/2012

Setup RVM on Mac OS X

First of all, install Xcode in anyway.
Installing rvm itself is pretty easy. The rvm homepage gives a quick
install command:
bash < <(curl -s
https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
and then add this to your shell profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
CC=/usr/bin/gcc-4.2
For ruby 1.9.3, we need libksba installed, using Homebrew, just run
'brew install libksba'
installing Homebrew is quite easy too:
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
now try install ruby 1.9.3:
rvm install 1.9.3 --reconfigure --debug -C --enable-pthread --with-gcc=clang
NOTE: in case of any compilation issues
* downgrade to Xcode 4.1
* or install osx-gcc-installer
and reinstall your rubies.

Setup Git on Mac OS X


  1. Check this out http://code.google.com/p/git-osx-installer/ *Don't worry if it's a Snow Leopard version, it works on Lion!
  2. Double click to mount it.
  3. Run the git-1.x.x.x-x.pkg (A basic installer. Note: You'll need administrative privileges.)
  4. Complete the Installation
  5. Open up a terminal
    1. Change directories to the mounted image
      bash$ cd /Volumes/Git\ 1.x.x.x\ xx/
    2. Run the shell script (bash$ ./setup\ git\ PATH\ for\ non-terminal\ programs.sh)

Re-open your terminal (reset your session) and you'll be good to go. Up and running with Git in no time!

2/04/2012

[转] 最大化Dropbox的免费空间

Dropbox免费的2G空间虽然已经可以存不少东西了,但是对于同步控而言,显然还是太少,但是花钱的话,只有50G和100GB两个选择,又太贵了点儿。本文介绍了一些方式,可以将Dropbox的免费空间直接提升到4G以上,并且最多可以有20G+,有兴趣的同学千万别错过。

1. 使用别人的邀请链接注册,这样就是2.25G,否则只有2G。我的Referer链接: http://db.tt/MQB4KVy

2. 安装Dropbox客户端,然后完成Get Started教程里的五六步,获得256M免费空间 https://www.dropbox.com/gs

3. 获得额外的5×128M免费空间 https://www.dropbox.com/free
(1) 绑定Twitter帐户 - 128MB
(2) 绑定Facebook帐户 - 128MB
(3) 在twitter上Follow @Dropbox - 128MB
(4) 写出一句话说明为什么你喜欢Dropbox - 128MB
(5) 把那句话Tweet出去(就是发到twitter上) - 128MB

4. 邀请其他人,每个人可以给你增加256MB空间。

5. 如果你有.edu、.edu.cn等邮箱(学生或者老师,比如武大的@mail.whu.edu.cn就行),访问https://www.dropbox.com/edu 绑定edu邮箱,就可以将每次邀请的量增加,由256增加到512,邀请能获得的最大空间也会翻倍到16G。步骤1的256也会变成512。

12/16/2011

synchronize between your git and svn repository

'git svn' is good to help on this,
if you don't have a git repository yet, you could get a cloned git
repository right from your svn repository by running this following command
git svn clone [YOUR-SVN-REPOSITORY-URL]
if you've got a git repository in hand, just try
git svn init [YOUR-SVN-REPOSITORY-URL]
this may help you set the 'svn-remote' of your git repository

after any of above two steps, you could simply use these commands to
synchronize between your git and svn repository:
git svn fetch
git svn rebase
git svn dcommit

12/08/2011

custom email when generating new ssh public/private key pairs

in your shell:
ssh-keygen -t rsa -P "" -C <your email address>

11/13/2011

how to enable AIRDROP on your old mac

just a simple command to go:
defaults write com.apple.NetworkBrowser BrowseAllInterfaces 1
restart your Finder, or your mac, there you have it!

11/04/2011

Rspec won't run - Error: In `bin_path': can't find executable spec for rspec-2.x (Gem::Exception)

here is a useful tip for people who just migrate from rspec 1.x to 2.x
first of all, the 'spec' command was renamed to 'rspec' in 2.x.
if you're still able to run it, that may because the 1.x gem is still
on your system, it's just "hidden" by bundler when you specify 2.x in
your 'Gemfile'
in this case, you may use the 'rake spec' command instead
for people who are working with outdated projects suffering from this
problem, you can fix this error by doing:
'which spec'
then open that file in your favorite file editor and change line 11
which looks like
version = '>= 0'
TO
version = '< 2'
and your spec command will work again :)

10/26/2011

Sharing session under sub-domains in ruby on rails

this could be done by just add the content below to your
RAILS_ROOT/config/initializers/session_store.rb file
ActionController::Base.session = {
:key => 'YOUR_KEY',
:secret => 'YOUR_SECRET',
:expire_after => 180.minutes,
:domain => '.DOMAIN_NAME.com'
}

10/25/2011

app to sd card for android focusing on the Huawei S7

Note: this how to expects some unix or gnu/linux know how.

Things that need to be done before starting.

Have the Android SDK installed (or droidexplorer if you're on windows for adb, as I've got the sdk this howto uses adb on the command line.)

Root the device. This goes without saying, Z4Root is recommend.

Install busybox, this could be done by simple download a busybox installer from the android market

Step 1: Prepairing the SD card.

Note: I have an 8gb sd card, so my examples use this in mind.

Using which ever partitioning program you wish to use, create two partitions on your sd card.

Partition 1 is 7GB and is fat 32 formatted.
Partition 2 is 1GB and is not formatted (we will do this later on the device.)*

*in my case I've mucked up and my ext2 partition is only 400Mb

Now place the sdcard back into the device and boot.

We need to make an ext2 file system on the second partition. This is done via the command line on
the device. So fire up the adb shell

./adb shell

Type the command su to get root privilages, then type the following command:
busybox mke2fs /dev/block/vold/179:2
This will output some data. Once done on the second partition we now have an ext2 file system.

Once this is all done the SD Card is ready.

Step2: Mounting the card, and moving data to the card.

For this step we need to mount the /system partition as read write as this partition is normally
mounted read only. To do this run the following command:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock1 /system
mkdir /system/sd
Then we need to mount the ext2 partition and we can do this via this command:
mount -t ext2 /dev/block/vold/179:2 /system/sd
We need to now copy the data from the data directory to the ext2 partition, as we want to preserve
permissions we're going to use the command tar (cp can preserve partitions, but I've had more issues
with cp not working correctly in busybox I prefer tar:
cd /data/
tar -cvf /system/sd/app.tar app
tar -cvf /system/sd/data.tar data
tar -cvf /system/sd/dalvik-cache.tar dalvik-cache

cd /system/sd

tar -xvf app.tar
tar -xvf data.tar
tar -xvf dalvik-cache.tar

rm *.tar

Step 3: scripts and cleanup.

To mount the ext2 filesystem at boot, we need two scripts. These both go into /system/etc

The first one is called install-recovery.sh and contains the following:
#!/system/bin/sh
#
/system/etc/init-sd.sh&

The next is called init-sd.sh and contains the following:

#!/system/bin/sh
#
MYLOG=/data/install-recovery.log
echo "$(date) Starting install-recovery.sh" > $MYLOG
echo "$(date) Waiting SD to become ready..." >> $MYLOG
sleep 10
mount -t ext2 /dev/block/vold/179:2 /system/sd 1>>$MYLOG 2>>$MYLOG
mount -o bind /system/sd/app /data/app 1>>$MYLOG 2>>$MYLOG
mount -o bind /system/sd/data /data/data 1>>$MYLOG 2>>$MYLOG
mount -o bind /system/sd/dalvik-cache /data/dalvik-cache 1>>$MYLOG 2>>$MYLOG
mount >> $MYLOG
echo "$(date) Finishing install-recovery.sh" >> $MYLOG
now you need to use a decent text editor (I like vi) on windows I reccomend notepad++

copy both of these into /system/etc

for example:

cp /sdcard/install-recovery.sh /system/etc
cp /sdcard/init-sd.sh /system/etc

Now we need to make sure these files are executable with the following commands:
chmod 755 /system/etc/install-recovery.sh
chmod 755 /system/etc/init-sd.sh
Finally, we need to remove everything in the existing /data/app /data/data/ and /data/dalvik-cache
with the following commands:
cd /data/app
busybox rm -rf *
cd /data/data/
busybox rm -rf *
cd /data/dalvik-cache
busybox rm -rf *
now you will notice some odd things happening on the screen of your tablet but that will be fixed
with the command:
reboot
and if all went well you now have all your applications on the sdcard.

9/23/2011

IPSEC/L2TP VPN with OSX client: xl2tpd reports “maximum retries exceeded”

set up a vpn server for my own use, everything went just fine at the beginning,
but these days vpn dial-in don't work on my Mac OS X client, every
time I'm trying to connect my vpn the OS X client keep saying "can not
make connection to the server blah blah.."
here is my server side info:
Ubuntu 10.04 server
IPSEC supported by openswan: Linux Openswan U2.6.23/K2.6.32-317-ec2 (net key)
L2TP supported by xl2tpd v1.3.0
after the connection get failed, I found there's something like this
in my auth.log:
......

Sep 22 16:07:36 ip-xx pluto[14024]: "L2TP-PSK-NAT"[16] 114.xx.193.xx
#57: transition from state STATE_QUICK_R0 to state STATE_QUICK_R1
Sep 22 16:07:36 ip-xx pluto[14024]: "L2TP-PSK-NAT"[16] 114.xx.193.xx
#57: STATE_QUICK_R1: sent QR1, inbound IPsec SA installed, expecting
QI2
Sep 22 16:07:36 ip-xx pluto[14024]: "L2TP-PSK-NAT"[16] 114.xx.193.xx
#57: transition from state STATE_QUICK_R1 to state STATE_QUICK_R2
Sep 22 16:07:36 ip-xx pluto[14024]: "L2TP-PSK-NAT"[16] 114.xx.193.xx
#57: STATE_QUICK_R2: IPsec SA established transport mode
{ESP=>0x0687f589 <0x85b637f3 ......="......" dpd="none}" natd="114.xx.193.xx:4500" natoa="none" pre="pre" xfrm="AES_256-HMAC_SHA1">
it seems like the client made the ipsec connection successfully, so I
tried to restart the xl2tpd server with the "-D" parameter, then I got
the error like this:
.....
xl2tpd[17006]: Listening on IP address 0.0.0.0, port 1701
xl2tpd[17006]: control_finish: Peer requested tunnel 16 twice,
ignoring second one.
xl2tpd[17006]: control_finish: Peer requested tunnel 16 twice,
ignoring second one.
xl2tpd[17006]: Maximum retries exceeded for tunnel 28197. Closing.
xl2tpd[17006]: Connection 16 closed to 114.xx.193.xx, port 53911 (Timeout)
xl2tpd[17006]: control_finish: Peer requested tunnel 16 twice,
ignoring second one.
xl2tpd[17006]: Unable to deliver closing message for tunnel 28197.
Destroying anyway.
xl2tpd[17006]: control_finish: Peer requested tunnel 16 twice,
ignoring second one.
xl2tpd[17006]: Maximum retries exceeded for tunnel 4768. Closing.
xl2tpd[17006]: Connection 16 closed to 114.xx.193.xx, port 53911 (Timeout)
xl2tpd[17006]: Unable to deliver closing message for tunnel 4768.
Destroying anyway.
.....
I tried google the error message then got this article: http://serverfault.com/questions/178309/ipsec-l2tp-vpn-with-osx-client-xl2tpd-reports-maximum-retries-exceeded saying that just need to add the following lines to the conn L2TP-PSK-noNAT section of ipsec.conf:
leftnexthop=%defaultroute
rightnexthop=%defaultroute
now my vpn works great!


8/23/2011

MySQL with ruby on rails on Mac OS X Lion

1, download a 64bit version of mysql for mac at
http://dev.mysql.com/downloads/mysql/
2, install mysql
3, add the following into your .bash_profile:
export PATH="$PATH:/usr/local/mysql/bin"
export ARCHFLAGS="-arch x86_64"
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"
4, install mysql gem by this command:
gem install -mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

when I was doing all this, I forgot to export the DYLD_LIBRARY_PATH,
and either for the fourth step, I got the following error when running
'rake db:create':
Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8",
"username"=>"root", "adapter"=>"mysql", "database"=>"xxx",
"host"=>"127.0.0.1", "pool"=>5, "password"=>"xxx",
"socket"=>"/tmp/mysql.sock"}, charset: utf8, collation:
utf8_unicode_ci (if you set the charset manually, make sure you have a
matching collation)

hope this may help you :)

8/19/2011

Way to switch your ruby gem version

Here is a common way to upgrade or downgrade your ruby gem
just two simple commands:

1, sudo gem install rubygems-update -v THE_VERSION_YOU_WANT
this will download and install a specified version of ruby gems
2, sudo update_rubygems
this will execute the ruby gems update script to actually update
your ruby-gems

7/15/2011

Command for network state and details

On Windows: netstat -an
On Linux: netstat -tnlp

7/07/2011

Little useful commands of linux

grep is for grep out some line from an input, for some specified keywords

lsof will give you a list of the running processes maps to their ports
which are using

kill is for killing a process, with paramater "-9 [PROCESS_NAME]"

ps will handle the process listing job, often be used with "aux" parameters