Tuesday, January 10, 2012

Migrating between several Linux distributions

Lets say you're migrating between two totally different Linux distributions. You've copied all of the settings and other stuff. And then you realise that you forgot to migrate databases from PostgreSQL or MySQL. And of course you don't want to reboot every time you want anything from the old system.

QEMU can help you here. What you need - is to create a new VM which will use real partitions.

In my case I've used /dev/sda as device with MBR, /dev/sda2 as /, /dev/sda3 as swap, /dev/sda5 as /home.



Just launch it.



Now you're able to boot your previous Linux installation without any need to reboot. It's really flexible.

Friday, December 30, 2011

Windows Server 2008 r2 ip management from the shell

Hello everybody :)

Sometimes we have to do some not ordinary things, and it is really difficult to find the solution over the network.

Perhaps, this post will help somebody, who wants to change the outgoing IP at his server based on Windows 2008. The main issue, that Windows server has no "primary" IP anymore. If your server's NIC has a set of IPs, numerically less IP will be set automatically as a default outgoing IP. And it is not cool! So you are really not allowed to change the outgoing IP on the Windows server in general! To do that, you have to do the following steps:
a) download the hotfix: http://support.microsoft.com/kb/975808
b) install it
c) reboot your server
d) reconfigure the server using provided in the article shell command.

To simplify console management I will improve you with the following commands:
1) ipconfig - will list all IPs on your server
ipconfig | find /i "ipv4" - will find and show only IPv4 Ips on your server, use find command to cut not necessary information from the shell command output, find will left only strings, which contains the expression you will enter in quotes.
2) Netsh int ipv4 add address skipassource=true
example: netsh int ipv4 add address "Local Area Connection" 192.168.1.2 255.255.255.255 skipassource=true
where:
2.1) "Local Area Connection" - your NIC's name,
2.2) skipassource=true - magical flag, which makes selected IP to be ignored as an outgoing IP address. In simple words, next numerically bigger IP becomes a default outgoing IP for traffic.
3) netsh int ipv4 delete address "Local Area Connection" 192.168.1.2
Will remove the IP from the server's configuration

So, if you have on your Windows 2008 r2 server 2 IPs: 1.1.1.1 and 1.1.1.2, after the installed hotfix and rebooted server you will be able to do the following, to make your .2 IP source ip:
0) check your IP at ipchicken.com, it should be 1.1.1.1
1) netsh int ipv4 delete address "Local Area Connection" 1.1.1.1
2) netsh int ipv4 add address "Local Area Connection" 1.1.1.1 255.255.255.255 skipassource=true
3) check your IP at ipchicken.com, it should become 1.1.1.2

Happy server administrating! (:

Best regards,
Yahor

Tuesday, December 6, 2011

Making Java web application development easier

If you have some web server where you deploy the web application in the exploded form and your source code is placed in another place for example in the local SVN copy - you have several options:
  1. Configure your IDE to deploy web application directly into your web server.
  2. Change your files in IDE and manually copy them into your exploded web application.
  3. Change your files in the exploded web application and manually copy them into your local SVN copy.

But there's a much simpler way to have both copies in synchronize. If you're on Linux - using symbolic links is a very easy way.

Just do:

ln -s ~/Sources/SITE/main.css ~/Tools/tomcat/webapps/ROOT/main.css

and once you edit ~/Sources/SITE/main.css in your IDE - the same contents will appear in ~/Tools/tomcat/webapps/ROOT/main.css. No need for manual synchronization.

You can do absolutely the same with the folders also.

However by default Tomcat does not follow symbolic links. To make it following symbolic links your need to add allowLinking="true" attribute for your application context. Or you can do it globally for all of the Tomcat apps by adding allowLinking="true" into conf/context.xml


<?xml version='1.0' encoding='utf-8'?>
<Context allowLinking="true">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

Friday, December 2, 2011

Cool JDK tools you probably never used

Lets say you have some remote server running Java. How would you see what is happening with JVM? Connecting profiler remotely requires open ports and definitely won't be quick. Running profiler on the remote server is not usually possible since the servers rarely have X11 installed. In this case a bunch of tools from JDK may help you.

These include:
  • jps
  • jstat
  • jinfo
  • jstack
  • jmap
  • jhat

Let's have a look at what these tools can do.

jps

This tool displays all of the JVM running on your host

jps -v

13397 Jps -Dapplication.home=/data/Tools/jdk1.6.0_26 -Xms8m
11170 smartsvn.jar -XX:-UseSSE42Intrinsics -Dsun.io.useCanonCaches=false ...
9762 Main -Djdk.home=/data/Tools/jdk1.6.0_26 -Dnetbeans.system_http_proxy=...

jstat

This tool displays JVM information mostly about memory

jstat -gcutil 9762

S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 6.03 41.22 72.27 75.29 57 6.608 6 4.599 11.206

This example shows various data on garbage collection.

jinfo

Perfect tool to see general information on JVM

jinfo 9762

Attaching to process ID 9762, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.1-b02
Java System Properties:

java.vendor = Sun Microsystems Inc.
netbeans.user = /home/andy/.netbeans/dev
...
plugin.manager.check.updates = false

VM Flags:

-Djdk.home=/data/Tools/jdk1.6.0_26 -Dnetbeans.system_http_proxy=DIRECT
...
-XX:HeapDumpPath=/home/andy/.netbeans/dev/var/log/heapdump.hprof

jstack

Unbelievable tool to see all of the threads and stack of these threads. You can also dump you threads when you run JVM in foreground by pressing Ctrl+\ in Linux terminal however usually it is not the case.

jstack 9762

Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.1-b02 mixed mode):

"Attach Listener" daemon prio=10 tid=0x00007f3be4003000 nid=0x35b6 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Inactive RequestProcessor thread [Was:org.netbeans.spi.project.ui.support.NodeFactorySupport/org.netbeans.spi.project.ui.support.NodeFactorySupport$DelegateChildren$2]" daemon prio=10 tid=0x00007f3bfc60f000 nid=0x35a9 in Object.wait() [0x00007f3bc3ffe000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:1942)
- locked <0x00000007e86a2940> (a java.lang.Object)
...

Newer versions of jstack can also try to detect deadlocks in your code.

jmap

Prints information on the shared libraries loaded by JVM. Can be useful if you use JNI.

jmap 9762

0x0000000040000000 49K /data/Tools/jdk1.6.0_26/bin/java
0x00000030e9200000 87K /lib64/libgcc_s-4.6.1-20110908.so.1
0x000000377f000000 42K /lib64/libcrypt-2.14.so
...

But it's also able to create a detailed JVM dump which can be viewed by jhat tool later. You can download this dump to your developer host.

jmap -dump:file=test.dmp 9762
Heap dump file created


jhat

It's not just a tool, it's a small server which parses the dump created by jmap and provides HTML interface to view dump data.

jhat test.dmp



It will show you lots of details on your objects and relations between them in various dimensions.

As you see there's a bunch of interesting tools which will allow you to understand what's happening with the remote JVM. Of course they are more complex in comparison to VisualVM or JProfiler but there are definitely some circumstances when these tools can be useful. And you don't need to install anything additional since all these tools bundled with the JDK.

Wednesday, November 30, 2011

Hudson/Jenkins clash

We're using Hudson for the purpose of CI. We've updated it not long ago and started getting errors like:

java.lang.NoClassDefFoundError: org/jenkinsci/plugins/tokenmacro/DataBoundTokenMacro

These problems are caused by plugins we use to get PMD/FindBugs/CheckStyle results.

The fix is to download and install token-macro plugin from:
http://maven.jenkins-ci.org/content/repositories/releases/org/jenkins-ci/plugins/token-macro/1.5.1/token-macro-1.5.1.hpi

This plugin is not even available in the list of Hudson plugins displayed at Hudson instance.

I quite understand that split of the community affects this marvellous CI in a bad way but with the Oracle and Sonatype behind, it could be way more tested. We're using a separate Jenkins instance for one of our customers and it recommended to be more stable in comparison to Hudson.

Time to switch to Jenkins fully?

Tuesday, November 15, 2011

Differences in PKG_CONFIG_PATH between MinGW and Cygwin

When you're compiling some Linux library for Windows you can get the errors like these:

Package gthread-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gthread-2.0.pc' to the PKG_CONFIG_PATH environment variable
No package 'gthread-2.0' found


It's pretty straightforward for MinGW. Just do "export PKG_CONFIG_PATH = ~/lib/pkgconfig" where ~/lib/pkgconfig is the place your pkgconfig files are stored.

For Cygwin it does not work this way. It should be a real Windows path, not the Linux-style path. For example "export PKG_CONFIG_PATH=c:\\cygwin\\home\\mlt\\lib\\pkgconfig" will work.

It took a while to realize since ProcessMonitor does not show any attempts to find gthread-2.0.pc file. Who knows why?

Wednesday, September 21, 2011

Android Instrumentation, catch assertation error

Hello Everybody,

For a long time I have no single guess that it is possible to catch the error from the assertTrue(false) very simply.

Yep, it is possible. Saneesh Joseph has opened my eyes!

Sample snippet:

try {
assertTrue("Catch me if you can!", false);
} catch(Error er) {
Log.i(er.toString());
}

Thanks, Saneesh Joseph!

Happy testing, folk! (:

Best regards,
Yahor