Archive for December, 2009

MySQL Datetime precision… I think not!

Posted in Computers, IT, Programming on December 24th, 2009 by matt – Be the first to comment

We use Hibernate at work, and I’ve been working on getting all our JUnit tests to work on MySQL… Yes hibernate takes care of most of that, and it does, BUT some of our unit tests were failing when I pointed it at MySQL rather then PostgreSQL.

After some debugging I noticed that when we pulled one of our objects out of the database via hibernate, the Date object inside wasn’t the same as the Date inserted:
originalDate.getTime(); // = 1261613807262
retrievedDate.getTime(); // = 1261613807000

The two epoch dates are almost the same, except the last three digits are zero’d out. The keen observers might have already figured out these are microseconds.
It seems MySQL isn’t storing or retrieving the microseconds from the datetime datatype.  Even though MySQL does have the MICROSECOND() function. A quick search on Google supports my findings there is a “Feature Request” opened on the MySQL bug tracker, annoyingly though it was opened on 15 Feb 2005, yet nothing is yet implemented.

For those who want to get around this issue, and want to Assert the dates, and want them to actually work, you simply need to zero out the microseconds component of the date, here is the simple function I used:
private Date removeMicroseconds(Date date) {
return new Date(date.getTime() - (date.getTime() % 1000));
}

Australian FTP RPMFusion Repository

Posted in Computers, IT, Linux on December 10th, 2009 by matt – Be the first to comment

There is an Australian FTP RPMFusion mirror:
baseurl=ftp://mirror.transact.net.au/rpmfusion/free/fedora/releases/$releasever/Everything/$basearch/os/
baseurl=ftp://mirror.transact.net.au/rpmfusion/nonfree/fedora/releases/$releasever/Everything/$basearch/os/

For updates:
baseurl=ftp://mirror.transact.net.au/rpmfusion/free/fedora/updates/$releasever/$basearch/
baseurl=ftp://mirror.transact.net.au/rpmfusion/nonfree/fedora/updates/$releasever/$basearch/

NOTE: This purpose of this post is simply for future reference.

Installing Sun Java on Fedora 12

Posted in Computers, IT, Linux, Programming on December 8th, 2009 by matt – 3 Comments

By default Fedora 12 doesn’t install Sun’s Java, and it isn’t in the repository. This isn’t a mistake, in fact I think this is a good decision! Fedora is only dealing with free open source software. You can add other repositories to give you the extra non OSS software you want, for those who cannot live without certain software.

Fedora uses the OpenJDK, which I think is awesome.. but unfortunately as I am a Java developer at the moment, and it seems some of the software I work requires the Sun version of Java, at least to compile.

So I needed to install Sun JDK on my 64bit machine.. this is how I did it:

  1. Download the Sun Java JDK 64bit Linux bin installer.
  2. Run it to install.
  3. Even though I ran it as root it installed in the current folder. So move the folder to we it should be installed:
    sudo cp -a jdk1.6.0_16 /usr/lib/jvm/
  4. Use the alternatives command to tell Fedora to use the new Java binary, to do so we need to “install” the new binary as an option in alternatives:
    sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_16/bin/java 20000
    Then use alternatives and make sure our new binary is selected:
    sudo /usr/sbin/alternatives --config java
  5. Use the following command to verify that Fedora is pointing to the right binary:
    java -version

That’s it, Sun’s Java should now be installed!

Iptables NAT routing

Posted in Computers, IT, Linux on December 7th, 2009 by matt – 2 Comments

Intro

Over the years I have been turning my desktop into a NAT router.. why for virtual machines of course!

If your using 1 virtual machine the virtual machine software does it for ya, but when you want to simulate your own virtual network, separate from the LAN your desktop is on this is how to do it.
I used to do this a lot back when I was working at the ANU, to enable a test network.. and now suddenly I need it again to work on multi master MySQL replication at work.

It easy to do, only a few commands really, but it something I like to have filed away, I have it on a private wiki, but thought why not post it here.

Routing in Linux

To turn on routing in your kernel at runtime, without needing to reboot, as root run:
echo 1 > /proc/sys/net/ipv4/ip_forward

To make this permanent, edit ‘/etc/sysctl.conf’  and turn on IP packet forwarding:
net.ipv4.ip_forward = 1

Now that we have packet forwarding (trouting) enabled, we need to use iptables to allow us to connect our private LAN to the internet via NAT.
To do this we need to write an iptables rule on the nat table to MASQUARADE every packet coming from the virtual or internal network interface and out to the world through our public interface.
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Where eth0 is the public interface.

The above rule, after routing changes the source IP address to that of the public interface, so traffic can get back to this machine. once a packet is returned iptables knows to change it back to the hidden LAN machine’s IP address.

In many distributions, iptables default configuration is to ACCEPT all traffic going through the FORWARD chain. But I’ve noticed Fedora doesn’t. it actually rejects forwarded packets. To check this out run the command:
iptables -L

My Fedora 12 Desktop FORWARD chain by default  looked like:
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Which REJECT’s anything being forwarded.

There are many ways to solve this:
Insert a rule to accept all:
iptables -I FORWARD 1 -j ACCEPT

Flush the chain:
iptables  -F FORWARD

If the policy is DROP, then change it to ACCEPT:
iptables -P FORWARD ACCEPT

Finally if you need to port forward to a machine behind your new NAT router then use a rule like:
iptables -t nat -I PREROUTING -p tcp -i eth0 --dport 6346 -j DNAT --to 192.168.0.2:6346

And if your interested in rate-limiting then read: http://www.debian-administration.org/articles/187

Fedora 12 + Nvidia

Posted in Computers, IT, Linux on December 4th, 2009 by matt – 7 Comments

By default Fedora 12 comes with the nouveau driver.. which is awesome, but my machine at work needed a bit more video card grunt so I needed to install the Nvidia driver.

Now I’m new to Fedora.. as in installed it an hour before I wrote this post, so thought I’d document here how to do it.
It wasn’t as straight forward as it _should_ have been, as apparently there is a bug in the current (at time of writing) version of Xorg, which causes X to run really slow. Anyway this is what you do:

  1. Add the rpmfusion repositories (to gain access to proprietary and other packages not supported by Fedora).
    su -c 'rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm'
  2. Update yum:
    sudo yum update
  3. Now install the Nvidia drivers:
    sudo yum install kmod-nvidia xorg-x11-drv-nvidia-libs.i686 xorg-x11-drv-nvidia-libs.x86_64
  4. This should blacklist the nouveau, but doesn’t remove it from the initrd, so we run:
    sudo dracut -f /boot/initramfs-$(uname -r).img $(uname -r)
  5. Then we can restart, or just restart X (sudo pkill kdm) to see the new Nvidia card in action.

You should now been using the new nvidia module, however on my Fedora 12 KDE installation the X response time was really slow. However on a friends Fedora 12 Gnome installation there wasn’t an issue.
Apparently it is a bug in Xorg, but seeing as it doesn’t effect a friend it makes me wonder if it is a KDE/Xorg/Nvidia bug.

Anyway to fix it up we need to install a patched Xorg:

  1. First we need to add a repository:
    sudo vim /etc/yum.repos.d/xorgFix.repo
    And paste the contents:
    [rdieter]
    name=xorg-x11-server rebuilds for nvidia users
    baseurl=http://rdieter.fedorapeople.org/repo/fedora/$releasever/$basearch/
    enabled=1
    gpgcheck=0
  2. Run ‘yum update’  again, and it should need to update Xorg. Update it then restart X.

X should now run correctly. Phew.. not too hard right.