Friday, December 24, 2010

Backup a Windows system

1. Create a Fedora LiveCD on USB flash driver
2. Reboot the machine to Fedora
3. Backup partition information

# sfdisk -d /dev/sda | ssh serverip 'cat > /folder/sda.ptb'

4. Backup MBR or MBR+Bootloader

# dd if=/dev/sda bs=512 count=1 | ssh serverip 'cat > /folder/sda.mbr'
# dd if=/dev/sda bs=512 count=63 | ssh serverip 'cat > /folder/sda.vbr'

5. Backup the restore image (count is set to the value from sfdisk output for /dev/sda1)

# dd if=/dev/sda bs=512 skip=63 count=20980827 | ssh serverip 'cat > /folder/sda1.img'

6. Check NTFS system size and if it needs run 'chkdsk /f /r'

# ntfsresize --info /dev/sda2

7. Backup Windows NTFS system

# ntfsclone -s -o - /dev/sda2 | ssh serverip 'cat > /folder/sda2.ntfs.img'

8. Run sha256sum to verify if the hashes match

# dd if=/dev/sda bs=512 skip=63 count=20980827 | sha256sum | ssh 'cat > /tmp/sda.sum'

Monday, December 20, 2010

Install ibm_db gem on Cygwin 1.7 on Windows 7

Just got a brand new Windows 7 laptop. Had troubles in installation of ibm_db gem.

Here is the configuration

* Windows 7 64bit
* Cygwin 1.7.7-1
* DB2 Express-C 64bit v9.72 (db2exc_972_WIN_x86_64.exe)
* Cygwin Ruby 1.8.6
* Bash

Here are the steps how to install ibm_db:

* copy c:\Program Files\IBM\SQLLIB\lib\Win32\db2cli.lib to db2.lib
* export IBM_DB_LIB=/cygdrive/c/Program\ Files/IBM/SQLLIB/lib/Win32
* export IBM_DB_INCLUDE=/cygdrive/c/Program\ Files/IBM/SQLLIB/include
* gem install ibm_db

* Because the ruby of Cygwin is still 32bit, you have to link to 32bit library.
* the native extension build uses "-ldb2", which means db2cli.lib is not recognized.
* w32api package in Cygwin (not sure if it is needed, may be not)

Wednesday, October 27, 2010

JRuby include_package doesn't define constants

Using include_package in a module to import java classes in JRuby usually works as expected, but not always. Try this code in JRuby 1.5.3
$ jruby ns.rb

require 'java'
require 'pp'

class Rule

module MyCompany
module JavaModel
include_package 'com.model'

pp MyCompany::JavaModel.const_get('Foo')
pp MyCompany::JavaModel.const_get('Rule')
You can simple create two java files and compile them using javac.

package com.model;
public class Foo {

package com.model;
public class Rule {
$ javac com/model/
$ javac com/model/
Here is the output:
Why the second line is not Java::ComModel::Rule? include_package just overrides const_missing instead of defining constants in module MyCompany::JavaModel. When you call MyCompany::JavaModel.const_get("Rule"), the constant "Rule" is defined, const_missing is skipped.

Using java_import will work correctly although you have to list each class.

module MyCompany
module JavaModel
java_import 'com.model.Foo'
java_import 'com.model.Rule'
Note: if you use java_import outside the module, you will overwrite the constant Rule.

Saturday, August 28, 2010

Spring IDE "Build is incomplete" and

I use sts 2.3.2 and always got "Build path is incomplete" issues. And I found this was caused by this exception:

at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(
at java.lang.reflect.Constructor.newInstance(Construc
at java.lang.Class.newInstance0(
at java.lang.Class.newInstance(
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.initAspectJE xpressionPointcut( )
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.createAspect JPointcutExpression( 86)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.internalMatc hes(
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AspectDefinitionMatcher.matches(Aspe
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob$1.doWith ActiveProjectClassLoader(AopReferenceModelBuilderJ
at $DefaultProjectClassLoaderSupport.executeCallback(
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForBean( :209)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForBeans(AopReferenceModelBuilderJob.jav a:281)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForBeansConfig(AopReferenceModelBuilderJ
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesFromBeansConfigSets(AopReferenceModelBui
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop ReferencesForFile( :353)
at org.springframework.ide.eclipse.aop.core.internal. model.builder.AopReferenceModelBuilderJob.buildAop Model(
at org.springframework.ide.eclipse.aop.core.internal.
at ava:55)
Caused by: org.apache.commons.logging.LogConfigurationExcepti on: User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable.
at org.apache.commons.logging.impl.LogFactoryImpl.dis coverLogImplementation(
at Instance(
at org.apache.commons.logging.impl.LogFactoryImpl.get Instance(
at org.apache.commons.logging.impl.LogFactoryImpl.get Instance(
at org.apache.commons.logging.LogFactory.getLog(LogFa
at p.aspectj.AspectJExpressionPointcut.(Aspec
... 20 more

Even upgrading to 2.3.3.M3 didn't help.

And I found org.apache.commons.logging.impl.Log4JLogger was not in
.metadata/.plugins/org.springframework.ide.eclipse.osgi.targetdefinition/ That explains why the exception happens.

My project uses to config log4j with this line,


I tried to remove the dependency of common-logging, and from my maven project, and cleaned all maven projects to remove from target/classes and target/test-classes.

Then everything become ok. No "Build path is incomplete" any more.

Unfortunately, I couldn't reproduce it when I put back to the project, and built the maven project.

I guess somehow was picked by SPRING IDE.

Friday, August 27, 2010

My Cygwin/Xwin settings

This is my ~/.Xdefaults file:

Xft.dpi: 96.0
Xft.hinting: 1
Xft.rgba : rgb
Xft.antialias: 1
Xft.hintstyle: hintfull

XTerm*faceName: Bitstream Vera Sans Mono
XTerm*faceSize: 10
XTerm*scrollBar: True
XTerm*rightScrollBar: True
XTerm*renderFont: True

URxvt*background: #EFFFEF
URxvt*foreground: #000000
URxvt.font: xft:Bitstream Vera Sans Mono:size=10:light:roman:antialias=true,xft:NSimSun:size=16:medium:antialias=true
URxvt*boldFont: xft:Bitstream Vera Sans Mono:size=10:bold:roman:antialias=true,xft:SimHei:size=16:medium:antialias=true
URxvt*italicFont: xft:Bitstream Vera Sans Mono:size=10:medium:italic:autohint=true:antialias=true
URxvt*boldItalicFont: xft:Bitstream Vera Sans Mono:size=10:bold:italic:autohint=true:antialias=true
URxvt.scrollBar: True
URxvt.scrollBar_right: True
URxvt.Shading: 50
URxvt.fading: 50

Friday, August 6, 2010

Without cropping in Cygwin Xwin during switch monitors

I have a laptop with a LCD 1400x900 and a monitor with 1280x1024 connecting to the dock. When I dock and undock the laptop, my Cygwin Xwin always has something cropped. XWin doesn't change the resolution automatically, and I cannot use xrandr to change it too. For example, after starting xwin using LCD, and docking the laptop, xrandr still returns:

bash-3.2$ xrandr -q
Screen 0: minimum 1440 x 900, current 1440 x 900, maximum 1440 x 900
default connected 1440x900+0+0 0mm x 0mm
1440x900 0.0*

and the following command will fail because the size can only be 1440x900.

bash-3.2$ xrandr -fb 1280x1024

This is annoying because I have to restart xwin after switching the monitor. Finally I found this thread solved my problem.

add the following options into /usr/bin/startx

serverargs="-screen 0 1440x1024x32 -multiwindow -clipboard"

Basically it creates a screen large enough to cover any monitor.

Thursday, August 5, 2010

Use mock_model outside of Rails

I want to use RSpec's mock_model outside of Rails, but only found this mail thread.

Actually it is not hard to use mock_model without rails. You need to install rspec-rails, and active_record gem first. Here is an example,

require 'active_support/core_ext/hash'
require 'spec/rails/mocks'
include Spec::Rails::Mocks

class TestTable

class View
def initialize(table)
@table = table

def print_keys
"name is #{} #{@table.class.primary_keys.join(",")}"

def print_name
"name is #{}"

describe "TestTable" do
before do
table = mock_model(TestTable, :name=>"table_abc")
@view =

it "should fail the expectation check" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_keys.should == "name is table_abc"

it "should complain that no primary_keys is called" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_name.should == "name is table_abc"

it "should print the table name" do
@view.print_name.should == "name is table_abc"

it "should print the table name and keys" do
TestTable.should_receive(:primary_keys).and_return(["pk1", "pk2"])
@view.print_keys.should == "name is table_abc pk1,pk2"

Monday, July 26, 2010

$ gem list gherkin cucumber
*** LOCAL GEMS ***
gherkin (2.2.0, 2.0.2)

$ gem install cucumber
ERROR: Error installing cucumber:
cucumber requires gherkin (~> 2.1.4, runtime)

The following command didn't work too
$gem install gherkin cucumber
You have to install gherkin 2.1.4 or 2.1.5 first.
$gem install gherkin --version 2.1.5

See "Specifying Versions" of RubyGems here

Thursday, July 22, 2010

Run cuke4duke from Eclipse

For a project using cuke4duke-maven-plugin, you can run integration test using Eclipse launcher, but you need to install the dependent packages into local repository. This is not convenient because you may change the dependent package in your workspace to make testes pass.

Just turn on "Resolve workspace artifacts" in "Run Configurations" dialog, and you don't have to install them.

Maven build without installing dependent packages in local repository

If you split your large project into several maven projects, and put them in sibling folders as follows:

+- parent
+- api
+- persistence
+- core

and core depends on api and persistence, while persistence depends on api. You can run maven command in parent/ to build them all without installing the packages into local maven repository:

parent $ mvn clean package

If you just want to build persistence, and you go to the folder core, then run maven, you will be asked to install api into repository. You can overcome this by running this command in parent folder:

parent $ mvn -o -am -pl groupId:core clean package

because all packages are in sibling folders, you may want to try this command:

parent $ mvn -o -am -pl ../core clean package

But this won't work and you may get an error message like "Couldn't find specified project in module list: M:\..\parent\..\core". Check this: MNG-4262.

Thursday, April 15, 2010

Debug Ruby Test::Unit::TestCase in Eclipse DLTK

It took me a while to figure out how to debug unit test using DLTK in eclipse.

Originally I tried to debug as "Ruby Test" and the breakpoints in unit test never worked, but it did stop in "rubygems.rb", you can step into the unit test file, but you cannot stop into a test method.

Because using "rdebug test-case.rb" did work in command line, I thought it might be something wrong with my gems. Tried to install a brand new ruby(mingw32 version) and install all gems ruby-debug and ruby-debug-ide. Unfortunately, it still didn't work.

Finally I found it is pretty ease to debug the unit test. The trick is "Debug as Ruby Script", you will stop at the breakpoint you set.

Wednesday, April 14, 2010

Install ibm_db on Ruby-mingw32

Here are the steps:

1. Install mingw compiled Ruby from
2. Install Ruby Development Kit from
3. follow the instruction of Development Kit
4. If you install IBM DB2 client in "c:\Program Files", you can do like this:

set IBM_DB_LIB=%ProgramFiles%/IBM/SQLLIB/LIB
gem install ibm_db

Because there is a space in "Program Files", adding quotes or escaping the space like "\ " do not work for me.

Install ibm_db on Cygwin 1.7

Here is how to install ibm_db on Cygwin 1.7

bash-3.2$ export IBM_DB_INCLUDE=/cygdrive/c/Program\ Files/IBM/SQLLIB/include
bash-3.2$ export IBM_DB_LIB=/cygdrive/c/Program\ Files/IBM/SQLLIB/lib
bash-3.2$ gem install ibm_db

Tuesday, April 13, 2010

Compile ruby-odbc on Cygwin 1.7

The ruby-odbc gem won't compile in Cygwin 1.7 because the uft8 version failed.

You can solve this by run the followings in a shell
$ gem install ruby-odbc
And you can also use this modified "extconf.rb"

require 'mkmf'

if ! defined? PLATFORM

def have_library_ex(lib, func="main", headers=nil)
checking_for "#{func}() in -l#{lib}" do
libs = append_library($libs, lib)
if !func.nil? && !func.empty? && COMMON_LIBS.include?(lib)
elsif try_func(func, libs, headers)
$libs = libs

def try_func_nolink(func, libs, headers = nil, &b)
headers = cpp_include(headers)
try_compile(<<"SRC", libs, &b)
int t() { void ((*volatile p)()); p = (void ((*)()))#{func}; return 0; }

def have_func_nolink(func, headers = nil, &b)
checking_for "#{func}()" do
if try_func_nolink(func, $libs, headers, &b)
$defs.push(format("-DHAVE_%s", func.upcase))

def headers(header)
if PLATFORM !~ /(mingw|cygwin)/ then
hdrs = header
hdrs = ["windows.h", header]

def check_for_type(type)
if defined? have_type
have_type(type, headers("sqltypes.h"))
puts "WARNING: please check sqltypes.h for #{type} manually,"
puts "WARNING: if defined, modify CFLAGS in Makefile to contain"
puts "WARNING: the option -DHAVE_TYPE_#{type}"

have_header("sql.h") || begin
puts "ERROR: sql.h not found"
exit 1
have_header("sqlext.h") || begin
puts "ERROR: sqlext.h not found"
exit 1
testdlopen = enable_config("dlopen", true)


$have_odbcinst_h = have_header("odbcinst.h")

if PLATFORM =~ /mswin32/ then
if !have_library_ex("odbc32", "SQLAllocConnect", "sql.h") ||
!have_library_ex("odbccp32", "SQLConfigDataSource", "odbcinst.h") ||
!have_library_ex("odbccp32", "SQLInstallerError", "odbcinst.h") ||
!have_library("user32", "CharUpper") then
puts "Can not locate odbc libraries"
exit 1
have_func("SQLConfigDataSourceW", "odbcinst.h")
have_func("SQLWriteFileDSNW", "odbcinst.h")
have_func("SQLReadFileDSNW", "odbcinst.h")
have_func("SQLInstallerError", "odbcinst.h")
have_func("SQLInstallerErrorW", "odbcinst.h")
# mingw untested !!!
elsif PLATFORM =~ /(mingw|cygwin)/ then
have_library("odbc32", "")
have_library("odbccp32", "")
have_library("user32", "")
hdrs = headers("odbcinst.h")
have_func("SQLConfigDataSourceW", hdrs)
have_func("SQLWriteFileDSNW", hdrs)
have_func("SQLReadFileDSNW", hdrs)
have_func("SQLInstallerError", hdrs)
have_func("SQLInstallerErrorW", hdrs)
elsif (testdlopen && PLATFORM !~ /(macos|darwin)/ && CONFIG["CC"] =~ /gcc/ && have_func("dlopen", "dlfcn.h") && have_library("dl", "dlopen")) then
$LDFLAGS+=" -Wl,-init -Wl,ruby_odbc_init -Wl,-fini -Wl,ruby_odbc_fini"
# but test the UNICODE installer functions w/o linking
# in case we need to provide fwd declarations
have_func_nolink("SQLConfigDataSourceW", "odbcinst.h")
have_func_nolink("SQLWriteFileDSNW", "odbcinst.h")
have_func_nolink("SQLReadFileDSNW", "odbcinst.h")
have_func_nolink("SQLInstallerErrorW", "odbcinst.h")
have_library("odbc", "SQLAllocConnect") ||
have_library("iodbc", "SQLAllocConnect")
($have_odbcinst_h &&
have_library("odbcinst", "SQLConfigDataSource")) ||
($have_odbcinst_h &&
have_library("iodbcinst", "SQLConfigDataSource"))
$have_odbcinst_h &&
have_func("SQLConfigDataSourceW", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLWriteFileDSNW", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLReadFileDSNW", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLInstallerError", "odbcinst.h")
$have_odbcinst_h &&
have_func("SQLInstallerErrorW", "odbcinst.h")


Tuesday, April 6, 2010

Don't let Aptana's XML Editor mess your Eclipse

After installing Aptana's RadRails in my Eclipse 3.5, I found my Maven project's pom.xml didn't behave as previous any more. Double click invoked a XML editor instead of Maven POM editor. After an investigation, I found that RadRails installed "Aptana XML editor" and set it as default, which caused Eclipse never chose the correct editor according to content-type of XML files. This is annoying for some XML files, such as POM and JUnit surefire-reports.

After you remove RadRails, Eclipse's default behavior comes back. However, you cannot remove "Aptana XML editor" only because it is packed with other packages. Finally I figured out a way so that you don't need to remove RadRails.

The following steps show how to fix this problem:
  1. Go to Windows->Preferences
  2. Then General->Editors->File Associations
  3. Find "*.xml" in File types, click it
  4. You will find the list of XML editors, you may find "Aptana XML Editor" with default
  5. Click "Remove" button for "File Types" to delete the file type "*.xml"
  6. Click "Add .." button for "File types", and fill "*.xml" in the dialog
  7. Then you will find all editors are listed in "Associated Editors", but "Aptana XML editor" is not default any more.
  8. Restart Eclipse, this configuration should be kept for your workspace.

The following two solutions are tested and didn't work:
  1. Just deleting "*.xml" file type doesn't work because Aptana XML editor becomes default again when you restart Eclipse.
  2. Setting another editor as default cause the same problem because no content-type detection if there exists an default editor.
Aptana should respect Eclipse's content-type.