Auto-incremented fields in apache’s derby

Unlike MySQL and other SQL dialects, apache derby doesn’t support the AUTO_INCREMENT keyword. To create an auto incremented filed in derby, you can use

CREATE TABLE customer
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(40) NOT NULL,
address VARCHAR(1024),
city VARCHAR(30),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;

The value of id is now incremented automagically on every insert. You don’t have to specify it manually in the insert command

INSERT INTO customer(name , address, city)
   VALUES('Mr. Peanutbutter', '221 W 31st St', New York City);

For instruction on how to create auto incremented fields in other SQL dialects, have a look at the SQL Dialects Reference.

Converting an ext3 partition to ext4

      No Comments on Converting an ext3 partition to ext4

Mounting an ext3 partition using ext4 fs drivers will usually speed-up your filesystem without actually changing the on-disk structures. Therefore it’s possible to revert to the ext3 driver without any problem. This allows you to easily benefit from delayed allocation (delalloc) and multi-block allocation (mballoc).

Further performance enhancements are possible if you do change the on-disk structure, e.g. by using extents. The downside is that you can’t mount the filesystem any longer with kernel lacking ext4 support and you can’t revert.

To enable ext4 features on an existing ext3 filesystem, invoke:

tune2fs -O extents,uninit_bg,dir_index /dev/DEV 

Unfotunately, features like flex_bg and >16TB fs support can only be enabled at format time1.

Your filesystem is now indeed converted to ext4, however, only files that are written after conversion will benefit from ext4 extends. Existing files would have to be rewritten. e4defrag is being developed to take care of this but in now meant for production environments as of now.

A workaround would be to use chattr to rewrite the files using extends. From the archlinux wiki:

find /home -xdev -type f -print0 | xargs -0 chattr +e
find /home -xdev -type d -print0 | xargs -0 chattr +e

It is being recommended though, “to test this command on a small number of files first, and check if everything is going all right. It may also be useful to check the filesystem after conversion.2. Please note their warnings about mercurial repositories and hardlinks in general, too.

[1] https://wiki.archlinux.org/index.php/Ext4#Migrating_files_to_extents
[2] https://ext4.wiki.kernel.org/index.php/Ext4_Howto#Converting_an_ext3_filesystem_to_ext4

Bootstrapping a Fedora 15 rootserver

      1 Comment on Bootstrapping a Fedora 15 rootserver

In certain situations where you don’t have physical access to your server it can be quite helpful to know how to install a new os without physical media like CD/DVD/USB or pxe.

This brief example shows you, how to bootstrap a rootserver (that of course already has a linux os installed that you can access through ssh) to install Fedora 15.

First, you’ll need the initial ramdisk and the kernel images:

cd /boot/
wget http://dl.fedoraproject.org/pub/fedora/linux/releases/15/Fedora/x86_64/os/isolinux/initrd.img
wget http://dl.fedoraproject.org/pub/fedora/linux/releases/15/Fedora/x86_64/os/isolinux/vmlinuz

Next, you add a new boot target to your grub.conf:

[...]
 title Fedora Install
  root (hd0,0)
  kernel /vmlinuz vnc vncpassword=SECRET ksdevice=eth0 ks=http://server/ks.cfg
  initrd /initrd.img

Don’t forget to change the boot target (default=n) in your grub.conf, too.

After rebooting the server, you can observe the installation process with your favourite vnc viewer.

F15vncInstall

click for full size image

Free reserved space on ext4 partitions (or change reserved block percentage)

By default, 5% of a new ext2/3/4 partition will be reserved for important root processes and for fs performance reasons. However, there may be sound reasons to lower that percentage or even disable it completely (non-root partition, data-only storage, huge files, ext4 etc.).

Let me quote Linux filesystem guru Ted Ts’o on this:

If you set the reserved block count to zero, it won’t affect performance much except if you run for long periods of time (with lots of file creates and deletes) while the filesystem is almost full (i.e., say above 95%), at which point you’ll be subject to fragmentation problems. Ext4’s multi-block allocator is much more fragmentation resistant, because it tries much harder to find contiguous blocks, so even if you don’t enable the other ext4 features, you’ll see better results simply mounting an ext3 filesystem using ext4 before the filesystem gets completely full.

If you are just using the filesystem for long-term archive, where files aren’t changing very often (i.e., a huge mp3 or video store), it obviously won’t matter.

You can change the reserved block percentage by invoking

tune2fs -m 1 /dev/sdb1

or disable it completely:

tune2fs -m 0 /dev/sdb1

To check, how many block are reserved on a given filesystem, run

tune2fs -l /dev/sdb1 | grep "Reserved block count"

Of course, you have to adjust /dev/sdb1 to your needs.

Resources:
http://unix.stackexchange.com/questions/7950/reserved-space-for-root-on-a-filesystem-why
See also tune2fs manpage

Starting a minimal X

      No Comments on Starting a minimal X

Just because I always forget and the parameters are somewhat non-trivial, here’s how you start an xterm inside X:

startx $(which xterm) -- :0 vt01

Of course, the display name :0 is usually used by default, so you may need to use another one (:1 etc.) if you get an error like “Server is already active for display 0“. You can also select a different virtual terminal.

Note that the absolute path to xterm (or whatever application you’d like to start inside X) is essential.

Multiple main methods in a single jar archive

A Java archive file usually holds a manifest file that specifies the main class to start when you invoke java -jar jarfile. It’s something similar to this:

Main-Class: com.yourcom.package.MyClass

When you distribute your jar file, a user can simply execute it (possibly even with a double click) without knowing anything about your package structure or location of the main class.

But what if you want to execute a different main method in another class inside your jar file, maybe for testing purposes? Well, you don’t have to hack the manifest and re-package. Simply do

java -cp jarfile com.yourcom.package.MyClass

and the main method inside MyClass will be executed.

Batch converting files from iso-8859-1 to utf-8

I’ve already posted on how to convert a single file from one encoding to another (iso-8859-1 to utf-8 in this example). But how to do it for a large amount of files, let’s say LaTeX source files for example, in one go?

Here’s a little bash one-liner that could help:

find ~ -type f -name "*.tex" -exec sh -c '
  f="{}"
  iconv --from-code=ISO-8859-1 --to-code=UTF-8 -- "$f" > "$f".tmp
  mv -v -- "$f".tmp "$f"
  ' \;

Shutting down kvm guest on host shutdown

      No Comments on Shutting down kvm guest on host shutdown

Per default, kvm instances are suspended when the host OS shuts down. This is down through an init scripot (libvirt-guests) that comes out of the box with Fedora and RHEL 6.

To change the guests’ shutdown behaviour, have a look at /etc/sysconfig/libvirt-guests. If you would like to send an ACPI system_powerdown event to the guest (i.e. a regular shutdown) instead of suspending it, change the ON_SHUTDOWN and SHUTDOWN_TIMEOUT varibale to

[...]
ON_SHUTDOWN=shutdown
SHUTDOWN_TIMEOUT=60

Of course, ymmv with the timeout value.

Where are user based crontab entries stored?

      No Comments on Where are user based crontab entries stored?

Well, assumed that the user is either listed in /etc/cron.allow or not listed in /etc/cron.deny, i.e. the user is allowed to create his own cron table, there should be a file for every user that is allowed to and has a crontab in /var/spool/cron/crontabs/.

Besides editing (crontab -e) or listing (crontab -l) his own crontab entries, root can also specify the name of the user whose crontab is to be tweaked with

crontab -u user -e

See also crontab manpage