How to automate virtual machine creation and runing on virtualbox by command line

June 29, 2011 by · 2 Comments
Filed under: linux, virtualization 

Many times we need to create a linux virtual machine by command line.
It is very useful for batch vm operation, automation, regression test, integration test…
Here’s the steps to create a functional linux virtual machine (works on windows host && linux host).

These steps has been tested on Debian squeeze x64 with kernel version of 2.6.32, and windows server 2003 x64 .
Note for windows users: Make sure you have put virtualbox directory to PATH variable. (Press Win+Break , click “Advanced”-> “Environment Variables”, find the PATH variable, and append virtualbox path to the variable.)

1.Create a virtualbox vm and register the vm

vboxmanage createvm -name testvm -register
vboxmanage showvminfo testvm

The showvminfo command shows information about created virtual machine.

2.Specifiy network adapter and setting boot order

vboxmanage modifyvm “testvm” –memory 768 –vram 64 –acpi on –boot1 dvd –nic1 bridged –bridgeadapter1 eth0

vboxmanage list ostypes

vboxmanage modifyvm “testvm” –ostype “linux26”

The list command shows all available system types. If you don’t want your vm connect to network directly by bridged network,
then change the arguments after –nicX, see manual: http://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvm

3.Create virtual disk for file storage with specified size(2G)

vboxmanage createvdi –filename “/data/vm/testvm-disk01.vdi” –size 2048 –remember

4.Set boot up order and add an IDE controller
vboxmanage storagectl testvm –name “IDE Controller” –add ide
vboxmanage modifyvm “testvm” –boot1 dvd –hda “/data/vm/testvm-disk01.vdi” –sata on

Now a virtual machine has been created and register, let’s go to step 5 specify the booting iso file,
and boot into linux.

5.Specify ISO file as DVD (the installation iso or live iso)
I use tinycore, and put the iso at /data/vm.

Attach virtual disk testos.vdi to the 0th port at device 0:
vboxmanage storageattach testvm –storagectl “IDE Controller” –port 0 –device 0 –type hdd –medium “/data/vm/testvm-disk01.vdi”

Attach the ISO at the 1th port at device 0:
vboxmanage storageattach testvm –storagectl “IDE Controller” –port 1 –device 0 –type dvddrive –medium /data/vm/tinycore_3.6.iso

vboxmanage modifyvm “testvm” –dvd /data/vm/tinycore_3.6.iso

6、Boot the virtual machine

vboxmanage startvm “testvm”

For linux users , if error like “The character device /dev/vboxdrv does not exist, lease install the virtualbox-ose-dkms package and the appropriate headers, most likely linux-headers” occured.

Then you probably run the following command by root:
modprobe vboxdrv
modprobe vboxnetflt

To poweroff the virtual machine, just run the command:
vboxmanage controlvm testvm poweroff

To boot the virtual machine in headless, so it starts without virtual box gui, it is useful for virtual server.
And you can put the command to start up script, it works on windows and linux:
vboxmanage startvm “testvm” –headless

To show detail information of the virtual machine:
vboxmanage showvminfo testvm –details

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Speed up your eclipse as a super fast IDE

June 25, 2011 by · 57 Comments
Filed under: java 

Follow these steps to speed up your eclipse as a super fast IDE , it works for 32 & 64 bit version eclipse/jdk on my windows 7 x64.

1.Disable antivirus software, or add your jdk & eclipse & workspace & other related folder to white list in your antivirus software.

2.Disable unecessary validations and startup action in eclipse reference.

For validations: on eclipse menu Windows -> Preferences -> Validation, click “Disable All”, and then select the validator which you need. I selected “Classpath Dependency Validator” only.

For startup action: on eclipse menu Windows -> Preferences, type “startup”, select “Startup and Shutdown”, untick the options you don’t need. (Thanks for David’s suggest,  I’ve added the above 2 detail steps for readers)

3.Modify eclipse.ini to set the Xmn(new generation size), Xms and Xmx, enable parallel GC
-vm
C:/jdk1.6.0_25/bin
-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
–launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502
-product
org.eclipse.epp.package.jee.product
–launcher.defaultAction
openFile
–launcher.XXMaxPermSize
256M
-showsplash
org.eclipse.platform
–launcher.XXMaxPermSize
256m
–launcher.defaultAction
openFile
-vmargs
-server
-Dosgi.requiredJavaVersion=1.5
-Xmn128m
-Xms1024m
-Xmx1024m
-Xss2m
-XX:PermSize=128m
-XX:MaxPermSize=128m
-XX:+UseParallelGC

4.Create a ram disk(virtual disk) on memory, and put your jdk on the ram disk.
You can use imdisk to create ram disk. Firstly download it from: http://www.ltr-data.se/opencode.html/#ImDisk,  and install it.

Then set the ImDisk driver to auto-load at Command Prompt with administrative privileges (for example right-click, start as Administrator), type:

sc config imdisk start= auto
net start imdisk

create a 200m ram disk as driver n by:
imdisk -a -t vm -s 300m -m n:

then you should format the ram disk manually.

later ,if you would like to remove the ram disk, just run the command:
imdisk -D -m n:

5.Make sure you are using sun jdk, not open jdk/gcj nor other jdk on linux.
If you are not satified with the after the above steps, install jrockit instead of sun jdk, and change some of the vm options by jrocket specification,
jrockit is faster than sun jdk.

Some note:

On my laptop(Aspire 4745G 4G mem ), windows 7 x64, by default, it takes more than 30 seconds to start eclipse.  After the vm options tuned,  takes only 15 seconds.

And after jdk moved to ram disk, it takes 10 seconds to startup.

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to start with wireless network on Debian squeeze

June 17, 2011 by · 2 Comments
Filed under: linux 

How to automatically keep an active network always on desktop, && use wireless network

sudo apt-get install network-manager
sudo apt-get install wicd
sudo apt-get install firmware-iwlwifi

Option 1:
sudo apt-get install network-manager-gnome

To use network-manager-gnome in Xfce (or Gnome) you need to have the applet running in the sytem tray.
In Gnome this is automatic but in Xfce you need to set it up initially. Fortunately this is very easy:

Alt + F2 to run a command:  nm-applet –sm-disable

Network manager is used for keeping an active network always for desktop.
Wireless network is disabled by default, rith click on the desktop network icon, choose “Enable Wireless”, it shall works.

Note for Chinese readers: 默认无线是关闭的,右键点击桌面工具栏上的网络连接图标,选中”Enable Wireless”,即可使用无线网络.

Option 2:

For WICD network manager, network connected and disconnected notification is disabled by default, you can enable the option in Preference settings.

If you haven’t input wireless interface, WICD could not find network by default, just input wlan0 in your wireless interface in WICD Preference settings.

Then click Wicd network manager -> Network -> Find a hidden network, wicd shall find wireless networks.

Note for Chinese readers: 网络的连接和中断通知默认是关闭的,可以在Preferences里面勾上 Notification.

如果没有在Preferences里面输入wireless interface,WICD默认是找不到无线网络的,通常输入wlan0即可找到无线网络。

点击无线网络的属性,配置好后,点击connect即可连接到无线。

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to fix battery indication in debian squeeze on Acer Aspire

June 17, 2011 by · Comments Off on How to fix battery indication in debian squeeze on Acer Aspire
Filed under: linux 

Problem:   acpi could not detect battery status, and shows unkown battery as:

$acpi

Battery 0: Unknown, 0%

Firstly, if you are on Acer Aspire xxxx (mine is Aspire 4745G), you need to upgrade the bios to a new version (such as BIOS_Acer_1.25_A_A.zip for 4745G).Just download the bios program here:

http://support.acer.com/us/en/product/default.aspx?modelId=2070
Secondly, enable acpi on boot option in /boot/grub/grub.cfg:

linux   /boot/vmlinuz-2.6.32-5-amd64 root=/dev/hda9 ro  acpi=on quiet
Then install powertop to check which process consumes lots of power,and cpufrequtils, laptop-mode-tools to automatically save your power.
sudo apt-get install powertop cpufrequtils laptop-mode-tools
restart you system.

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

maven打包web项目时同时打包为war和jar文件

June 9, 2011 by · 2 Comments
Filed under: java 

首先在pom.xml文件中指定war的打包方式,war

然后在pom文件的plugins节点下面增加如下内容即可mvn package时同时生成war, jar包。为了 mvn package install, mvn package deploy能够同时部署jar包,我们增加了后面2节点的配置:

<!--  package jar on package -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <executions>
  <execution>
    <id>make-a-jar</id>
    <phase>compile</phase>
    <goals>
      <goal>jar</goal>
    </goals>
  </execution>
  </executions>
</plugin>
<!--  install jar to local repository -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>
<!--  deploy jar to remote repository -->
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-deploy-plugin</artifactId>
 <executions>
 <execution>
 <phase>deploy</phase>
 <goals>
  <goal>deploy-file</goal>
 </goals>
 <configuration>
 <packaging>jar</packaging>
 <generatePom>true</generatePom>
 <url>${project.distributionManagement.repository.url}</url>
 <artifactId>${project.artifactId}</artifactId>
 <groupId>${project.groupId}</groupId>
 <version>${project.version}</version>
 <file>${project.build.directory}/${project.artifactId}.jar</file>
 </configuration>
 </execution>
 </executions>
</plugin>
Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

配置无密码的ssh登录时仍然需要输入密码

June 9, 2011 by · Comments Off on 配置无密码的ssh登录时仍然需要输入密码
Filed under: linux 

配置了无密码的ssh后,登录服务器时仍然需要密码。

出于安全的考虑,~/.ssh 和~/.ssh/authorized_keys必须只有所有者有权限,其他人没有任何权限,才可以使用无密码ssh登录。 可以尝试如登录服务器上,运行如下命令来解决:

chmod 700 ~/.ssh

chmod 600 ~/.ssh/*

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

如何使得apt-get install从多个repository并行下载

June 6, 2011 by · Comments Off on 如何使得apt-get install从多个repository并行下载
Filed under: linux 

http://www.mattparnell.com/linux/apt-fast/ 下载apt-fast.sh,拷贝到/usr/local/bin目录:
wget http://www.mattparnell.com/linux/apt-fast/apt-fast.sh
chmod +x apt-fast.sh
sudo cp apt-fast.sh /usr/local/bin

推荐使用 apt fat with aria2c 来从多个源下载来提高速度,
wget http://www.beyondlinux.com/blog/wp-content/uploads/2011/06/apt-fast_aria2c.txt -O apt-fast_aria2c.sh
chmod +x apt-fast_aria2c.sh
sudo cp apt-fast_aria2c.sh /usr/local/bin

然后使用 apt-fast_aria2c.sh install xxxx来安装软件包,设置多个mirrors时,速度会比apt-get install快上很多倍。 如:

sudo apt-fast_aria2c.sh install mplayer smplayer cdcd

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

不同的开发语言在性能上的两个重要差别

June 5, 2011 by · Comments Off on 不同的开发语言在性能上的两个重要差别
Filed under: c++, design, java 

不同的开发语言在性能上的重要差别,除了编译执行与解释执行的效率之外,还有两个重要的差别,一个是方法调用的性能差别,另一个是对象的创建与回收效率。

前阵子用C++(我用的VC 2010)写一个搜索本地文件的小程序,把一段代码提取为方法之后(参数使用了引用避免对象拷贝的开销),竟然发现性能有非常明显的下降,这说明C++(准确地说是VC++)的方法调用成本是相当高的。

我们在用Java处理大量的计算和IO操作时,大量的对象产生与销毁,经常会导致频繁的Full GC,Full GC的时候系统的服务几乎停滞,大大影响了系统的性能。 因此Java的内存回收效率是非常低的。 另一个方面,我们在用java时,不论方法调用的层次有多深,都几乎不会影响系统的执行性能,说明java的方法调用性能是非常高的。

Perl社区曾尝试使用纯Perl移植Lucene,然后试图通过不断的profiling和tuning来改善Perl版本Lucene的性能,最终发现由于Perl语言方法调用的成本过高,且对象的频繁创建和销毁导致性能低下而未能如愿。 参考:
http://wiki.apache.org/lucy/MinimizingObjectOverhead

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

面向对象的纯C语言

June 5, 2011 by · Comments Off on 面向对象的纯C语言
Filed under: c++, search engine 

前几天lucene的c语言版本Lucy发布0.1版本。Lucy的代码中使用了面向对象的C扩展Clownfish。

Clownfish是一种很小的面向对象的语言,用于定义面向对象的接口。Clownfish包含一个编译器,允许类用C语言或者一种host语言实现,或者两者的组合。最早Clownfish用于Kino Search中,后来KinoSearch演变为Lucy,Lucy中继续使用Clownfish。参考: http://search.cpan.org/~creamyg/KinoSearch-0.311/clownfish/lib/Clownfish.pm

其他的使用面向对象的C的方法:
http://www.bolthole.com/OO-C-programming.html
http://www.bolthole.com/OO-C-2.html
http://home.comcast.net/~fbui/OOC.html
http://www.accu.informika.ru/acornsig/public/articles/oop_c.html

当然也可以使用C++或者objective c,但object c能运行的平台比较有限。

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

通信协议与序列化方案的纠结: Protocol Buffer, thrift or hessian?

June 2, 2011 by · 2 Comments
Filed under: java 

最早我用hessian作为java搜索服务中的通信协议和序列化方案。松耦合,部署,调用方便。这两年很多人跟我提起protocol buffer,thrift。特别是最近从不少地方的信息看到很多地方在用,也找到一些改用protocol buffer或者apache thrift的理由:

1.在趋势科技的时候,病毒清理引擎里面用的也是protocol buffer作为序列化,实际上在Trend,PB用的很广泛。

2. Jeff Dean推荐分布式系统中使用Protocol Description Language。 (理由是协议描述语言支持接口的版本演进,即可以自动忽略不认识的属性,但不是如很多中文文章翻译中所说的为了减少通信的代码量。) 参考:
http://www.parallellabs.com/2010/12/02/jeff-dean-on-google-system-architecture/
http://peopleyun.com/?p=794

3.Quora也在用Protocol Buffer。参考Quora’s Technology Examined

更为重要的是,在我们项目中.net以hessian协议调用java的过程中多次出现接口中的类版本变化,导致无法调用,且hessian并不显示有提示作用的错误信息。 调试和定位问题相当的困难和耗时。因此决定尝试 PB和thrift, 对比了两者, 发现thrift更好用,
1. thrift支持map
2. thrift提供各种语言的RPC实现,不用另外再写RPC服务器。
3. thrift官方原生版本可以自动生成各种语言的客户端,大大减少开发工作量。
参考 ”thrift与protocol buffer的比较
(注意两点,1.有评论者说thrift runtime不支持windows,实际上现在支持的 2.另一个说开始的测试结果表明Hessian比PB要快,但后来重新测试PB要快。)

但不方便的是thrift也同protocol buffer一样,需要编写定义文件, 需要生成代码, 代码中有一堆生成的方法和属性, 不如直接使用Hessian方便, 使用hessian的时候, 要开放服务的接口中压根就不需要增加任何额外的方法和属性。

使用hessian过程中,最大的问题在于前面所描述的,.net调用java hessian服务,当接口中的类版本有变化时,会调用失败。但java调用java hessian并没有这个问题。

后来到stackoverflow上查关于hessian, protocol, buffer的选择问题, stackoverflow上几个人推荐使用简洁、清晰、松耦合的hessian。最终决定仍然使用hessian,不迷信google,迷信quora,不再考虑使用thrift/java。

总结在几者之间选择的考量:
1. 如果你不需要很多语言相互调用, 希望保持清晰的java接口代码(无任何业务不相关的接口继承和方法,属性定义),减少开放工作量,推荐Hessian。

2. 如果你的系统之间传输的数据量不是很大(<2M都不算大), 推荐Hessian。 3. 如果需要支持大数据量的传输,多语言调用,极高的并发支持,推荐使用thrift/protocol buffer。 通常我们并发很难超过1000 req/s,如果超过1000 req/s,在国内互联网排名绝对前5,那么恭喜你。因此一般而言,用Hessian就够了。 Conclusion: don't bother me with Protocol Buffer or Thrift.

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)