Creating RHEL4 chroot on RHEL6

I needed to run some Python script (from package rhn-applet) which was lastly distributed for RHEL4. I have started with just extracting content of rpm packages, but that ended with kinda dependency hell.

mkdir mycontent
cd myconten
wget .../rhn-applet-2.1.29-4.el4.x86_64.rpm
rpm2cpio rhn-applet-2.1.29-4.el4.x86_64.rpm | cpio -idmv

Creating the chroot is quite easy (assuming you have yum repo rhel4-chroot.repo of RHEL4 packages available so yum, although RHEL6 version, can install these RHEL4 packages):

ROOT="$( pwd )/$AFFAIR/"
mkdir $ROOT
rpm --root $ROOT --initdb   # this creates $ROOT/var/lib/rpm database
yum --disablerepo '*' --enablerepo $AFFAIR --installroot=$ROOT -y install rhn-applet   # install desired package from our repository to chroot and into RPM database from previous step
echo $( hostname -i ) $( hostname ) > $ROOT/etc/hosts   # script I run later needs to be able to connect to localhost

Now my chroot was behaving as I needed. One problem I had is that I had rpm database created by RHEL6 version of rpm there. That is bad, because it is unreadable for rpm present in RHEL4 and currently installed in the chroot.

# chroot $ROOT rpm -qa
rpmdb: /var/lib/rpm/Packages: unsupported hash version: 9
error: cannot open Packages index using db3 - Invalid argument (22)
error: cannot open Packages database in /var/lib/rpm
no packages

To fix it, you can follow these steps. Because I did not needed rpm database to be correct, only needed redhat-release package in there, I just reinstalled it with empty rpm database:

rm -rf $ROOT/var/lib/rpm/*
chroot $ROOT rpm --initdb   # create empty RHEL4 rpm formatted database
wget -P $ROOT http://repos.example.com/released/RHEL-4/U9/Desktop/x86_64/repo-Desktop-x86_64/RPMS/redhat-release-4Desktop-10.x86_64.rpm
chroot $ROOT rpm -ivh redhat-release-*.rpm --nodeps --justdb