+#################### utility to setup CRAMFS related support
+function prepare_cramfs() {
+ [ -n "$CRAMFS_PREPARED" ] && return 0
+ local custom=$1;
+
+ echo "* Setting up CRAMFS-based images"
+ local tmp="${BUILDTMP}/cramfs-tree"
+ mkdir -p "$tmp"
+ push_cleanup rm -rf $tmp
+ pushd $tmp
+ gzip -d -c $ISOFS/bootcd.img | cpio -diu
+ gzip -d -c $ISOFS/overlay.img | cpio -diu
+ [ -n "$custom" ] && \
+ gzip -d -c $ISOFS/custom.img | cpio -diu
+
+ # clean out unnecessary rpm lib
+ echo "* clearing var/lib/rpm/*"
+ rm -f var/lib/rpm/*
+
+ # bootcd requires this directory
+ mkdir -p mnt/confdevice
+
+ # relocate various directory to /tmp
+ rm -rf root
+ ln -fs /tmp/root root
+ ln -fs /sbin/init linuxrc
+ ln -fs /tmp/resolv.conf etc/resolv.conf
+ ln -fs /tmp/etc/mtab etc/mtab
+
+ # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
+ # make /tmp/etc
+ echo "* renaming dirs in ./etc"
+ pushd etc
+ for dir in `find * -type d -prune | grep -v rc.d`; do
+ mv ${dir} ${dir}_o
+ ln -fs /tmp/etc/${dir} ${dir}
+ done
+ popd
+
+ echo "* renaming dirs in ./var"
+ # rename all top-level directories and put in a symlink to /tmp/var
+ pushd var
+ for dir in `find * -type d -prune`; do
+ mv ${dir} ${dir}_o
+ ln -fs /tmp/var/${dir} ${dir}
+ done
+ popd
+
+ # overwrite fstab to mount / as cramfs and /tmp as tmpfs
+ echo "* Overwriting etc/fstab to use cramfs and tmpfs"
+ rm -f ./etc/fstab
+ cat >./etc/fstab <<EOF
+/dev/ram0 / cramfs ro 0 0
+none /dev/pts devpts gid=5,mode=620 0 0
+none /proc proc defaults 0 0
+none /sys sysfs defaults 0 0
+EOF
+
+ pushd dev
+ rm -f console
+ mknod console c 5 1
+ #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
+ #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
+ #ln -fs ram1 ram
+ #ln -fs ram0 ramdisk
+ popd
+
+ # update etc/inittab to start with pl_rsysinit
+ sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
+
+ # modify inittab to have a serial console
+ if [ -n "$serial" ] ; then
+ echo "T0:23:respawn:/sbin/agetty -L $console_dev $console_baud vt100" >> etc/inittab
+ # and let root log in
+ echo "$console_dev" >> etc/securetty
+ fi
+
+ # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
+ etcsize=$(du -s ./etc | awk '{ print $1 }')
+ varsize=$(du -s ./var | awk '{ print $1 }')
+ let msize=($varsize+$etcsize+8192)/1024
+
+ # make dhclient happy
+ for i in $(seq 0 9); do ln -fs /tmp/etc/dhclient-eth${i}.conf etc/dhclient-eth${i}.conf ; done
+ ln -fs /tmp/etc/resolv.conf etc/resolv.conf
+ ln -fs /tmp/etc/resolv.conf.predhclient etc/resolv.conf.predhclient
+
+ # generate pl_rsysinit
+ cat > etc/rc.d/init.d/pl_rsysinit <<EOF
+#!/bin/sh
+# generated by build.sh
+echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
+mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
+mkdir -p /tmp/root
+mkdir -p /tmp/etc
+touch /tmp/etc/resolv.conf
+touch /tmp/etc/mtab
+mkdir -p /tmp/var
+
+# make mtab happy
+echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
+
+# copy over directory contents of all _o directories from /etc and /var
+# /tmp/etc and /tmp/var
+pushd /etc
+for odir in \$(cd /etc && ls -d *_o); do dir=\$(echo \$odir | sed 's,\_o$,,'); (mkdir -p /tmp/etc/\$dir && cd \$odir && find . | cpio -p -d -u /tmp/etc/\$dir); done
+popd
+pushd /var
+for odir in \$(cd /var && ls -d *_o); do dir=\$(echo \$odir | sed 's,\_o$,,'); (mkdir -p /tmp/var/\$dir && cd \$odir && find . | cpio -p -d -u /tmp/var/\$dir); done
+popd
+
+echo "done"
+
+# hand over to pl_sysinit
+echo "pl_rsysinit: handing over to pl_sysinit"
+/etc/init.d/pl_sysinit
+EOF
+ chmod +x etc/rc.d/init.d/pl_rsysinit
+
+ popd
+
+ # create the cramfs image
+ echo "* Creating cramfs image"
+ mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
+ cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
+ rm -rf $tmp
+ pop_cleanup