+#!/bin/sh
+#
+# This script is used to build a "custom" initrd for mkcdrec
+# It essentially builds a full Bacula environment, but then
+# when mkcdrec boots, it will load our initrd instead of the
+# trimmed down mkcdrec version.
+#
+
+TOPDIR=`pwd`
+MKCDREC=../../mkcdrec
+
+LOOP2=/tmp/bacula_rescue_loop2
+rm -rf $LOOP2
+mkdir -p $LOOP2
+
+# Assume that everything to be loaded into memory with the
+# RAM disk image (initrd) is is in the roottree directory.
+
+echo "Creating the Initial RAM disk image.... "
+
+# first find out how much space we need.
+ISIZE=`du -s -k roottree/ | awk '{print $1}'`
+
+# Make a copy for mkcdrec
+rm -rf mkcdrectree
+cp -dpR roottree/ mkcdrectree
+
+#
+# Now copy in the mkcdrec files
+# Note, we use a number of the mkcdrec scripts
+#
+cp -fp ${MKCDREC}/etc/fstab mkcdrectree/etc
+# cp -fp ${MKCDREC}/etc/inittab mkcdrectree/etc
+cp -fp ${MKCDREC}/linuxrc mkcdrectree/
+mkdir -p mkcdrectree/etc/rc.d
+cp -fdpR ${MKCDREC}/etc/rc.d/* mkcdrectree/etc/rc.d/
+touch mkcdrectree/etc/rc.d/rc.inits
+chmod +x mkcdrectree/etc/rc.d/rc.inits
+cat >>mkcdrectree/etc/rc.d/rc.local <<EOF
+# This file is sourced
+if [ ! -d /etc/recovery ] ; then
+ cd /
+ if [ -d /mnt/cdrom/recovery.tgz ] ; then
+ tar xfz /mnt/cdrom/recovery.tgz
+ fi
+fi
+EOF
+
+# add 2 Meg for extra
+ISIZE=`expr $ISIZE + 2048`
+echo "Initial RAM disk contents will be $ISIZE KB"
+
+# delete the existing RAM disk image, if there is one
+rm -f root
+
+dd if=/dev/zero of=$TOPDIR/root bs=1k count=$ISIZE
+
+# cleanup any prior left over stuff
+umount $LOOP2 2>/dev/null >/dev/null
+losetup -d /dev/loop2 2>/dev/null >/dev/null
+
+# associate it with /dev/loop2
+losetup /dev/loop2 $TOPDIR/root
+
+# make an ext2 filesystem on it. Set reserve to 0
+mke2fs -q -m 0 /dev/loop2 $ISIZE
+if [ $? != 0 ] ; then
+ echo "Build failed."
+ exit 1
+fi
+
+# we mount it...
+mount /dev/loop2 $LOOP2
+# ... and delete the lost+found directory
+rm -rf $LOOP2/lost+found
+
+# then we copy the contents of our roottree to this filesystem
+cp -dpR mkcdrectree/* $LOOP2/
+cprtn=$?
+
+# and unmount and divorce /dev/loop2
+umount $LOOP2
+losetup -d /dev/loop2
+rm -rf $LOOP2
+
+# If above copy failed, bail out
+if [ $cprtn != 0 ] ; then
+ echo "RAM disk build failed."
+ exit 1
+fi
+
+# This is a newer way of creating a ramfs, which we don't use
+# (cd roottree; find . | cpio --quiet -c -o) >root
+
+echo "Building initial RAM disk done"
+
+# Now we have the image of the RAM disk in $TOPDIR/loopfiles/root. We
+# compress this one and write the compressed image to the boot tree:
+
+echo "Compressing the RAM disk image.... "
+
+# and gzip our RAM disk image and put it in the right place.
+# Bacula gzip -9 -c root >cdtree/boot/isolinux/initrd.img
+dd if=root bs=1k | bzip2 -v9 > custom-rd.img.bz2
+if [ $? != 0 ] ; then
+ echo "Build failed"
+ exit 1
+fi
+
+
+# we are done with the RAM disk image, delete it
+rm -f root
+rm -rf mkcdrectree
+
+echo "Initial RAM disk custom-rd.img.bz2 is built."