+ # 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 $COMMAND
+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
+}
+
+#################### Create ISO CRAMFS image
+function build_iso_cramfs() {
+ local iso="$1" ; shift
+ local custom="$1"
+
+ prepare_cramfs "$custom"
+ echo "* Creating ISO CRAMFS-based image"
+
+ local tmp="${BUILDTMP}/cramfs-iso"
+ mkdir -p "$tmp"
+ push_cleanup rm -rf $tmp
+ (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
+ cat >$tmp/isolinux.cfg <<EOF
+${console_serial_line}
+DEFAULT kernel
+APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${KERNEL_ARGS}
+DISPLAY pl_version
+PROMPT 0
+TIMEOUT 40
+EOF
+
+ cp ${BUILDTMP}/cramfs.img $tmp
+ mkisofs -o "$iso" \
+ $MKISOFS_OPTS \
+ $tmp
+
+ rm -fr "$tmp"
+ pop_cleanup
+}
+
+#################### Create USB CRAMFS based image
+function build_usb_cramfs() {
+ local usb="$1" ; shift
+ local custom="$1"
+
+ prepare_cramfs "$custom"
+ echo "* Creating USB CRAMFS based image"
+
+ let vfat_size=${cramfs_size}+$FREE_SPACE
+
+ # Make VFAT filesystem for USB
+ mkfs.vfat -C "$usb" $vfat_size
+
+ # Populate it
+ echo "* Populating USB with overlay images and cramfs"
+ mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
+ mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
+
+ # Use syslinux instead of isolinux to make the image bootable
+ tmp="${BUILDTMP}/syslinux.cfg"
+ cat >$tmp <<EOF
+${console_serial_line}
+DEFAULT kernel
+APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${KERNEL_ARGS}
+DISPLAY pl_version
+PROMPT 0
+TIMEOUT 40
+EOF
+
+ mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
+ rm -f "$tmp"
+
+ echo "* Making USB CRAMFS based image bootable"
+ syslinux "$usb"
+}