+setup_unionfs ()
+{
+ image_directory="${1}"
+ rootmnt="${2}"
+
+ modprobe "${MP_QUIET}" -b ${UNIONTYPE}
+
+ # run-init can't deal with images in a subdir, but we're going to
+ # move all of these away before it runs anyway. No, we're not,
+ # put them in / since move-mounting them into / breaks mono and
+ # some other apps.
+
+ croot="/"
+
+ # Let's just mount the read-only file systems first
+ rofsstring=""
+ rofslist=""
+ minor_kernel_version=$(uname -r|cut -c 5-|sed 's/[^0-9].*//')
+
+ if [ "${NETBOOT}" = "nfs" ] && [ "${minor_kernel_version}" -lt 22 ]
+ then
+ # go aroung a bug in nfs-unionfs locking for unionfs <= 1.4
+ roopt="nfsro"
+ else
+ roopt="ro"
+ fi
+
+ # Read image names from ${MODULE}.module if it exists
+ if [ -e "${image_directory}/filesystem.${MODULE}.module" ]
+ then
+ for IMAGE in $(cat ${image_directory}/filesystem.${MODULE}.module)
+ do
+ image_string="${image_string} ${image_directory}/${IMAGE}"
+ done
+ elif [ -e "${image_directory}/${MODULE}.module" ]
+ then
+ for IMAGE in $(cat ${image_directory}/${MODULE}.module)
+ do
+ image_string="${image_string} ${image_directory}/${IMAGE}"
+ done
+ else
+ # ${MODULE}.module does not exist, create a list of images
+ for FILESYSTEM in squashfs ext2 ext3 xfs dir
+ do
+ for IMAGE in "${image_directory}"/*."${FILESYSTEM}"
+ do
+ if [ -e "${IMAGE}" ]
+ then
+ image_string="${image_string} ${IMAGE}"
+ fi
+ done
+ done
+
+ # Now sort the list
+ image_string="$(echo ${image_string} | sed -e 's/ /\n/g' | sort )"
+ fi
+
+ [ -n "${MODULETORAMFILE}" ] && image_string="${image_directory}/$(basename ${MODULETORAMFILE})"
+
+ mkdir -p "${croot}"
+
+ for image in ${image_string}
+ do
+ imagename=$(basename "${image}")
+
+ if [ -d "${image}" ]
+ then
+ # it is a plain directory: do nothing
+ rofsstring="${image}=${roopt}:${rofsstring}"
+ rofslist="${image} ${rofslist}"
+ elif [ -f "${image}" ]
+ then
+ backdev=$(get_backing_device "${image}")
+ fstype=$(get_fstype "${backdev}")
+
+ if [ "${fstype}" = "unknown" ]
+ then
+ panic "Unknown file system type on ${backdev} (${image})"
+ fi
+
+ mkdir -p "${croot}/${imagename}"
+ echo "debug: Can not mount backdev ${backdev} (image = ${image}) on croot/imagename ${croot}/${imagename}"
+ mount -t "${fstype}" -o ro,noatime,nodiratime "${backdev}" "${croot}/${imagename}" || panic "Can not mount ${backdev} (${image}) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
+ fi
+ done
+
+ rofsstring=${rofsstring%:}
+
+ mkdir -p /cow
+ cowdevice="tmpfs"
+ cow_fstype="tmpfs"
+
+ # Looking for "${root_persistence}" device or file
+ if [ -n "${PERSISTENT}" ]
+ then
+ cowprobe=$(find_cow_device "${root_persistence}")
+
+ if [ -b "${cowprobe}" ]
+ then
+ cowdevice=${cowprobe}
+ cow_fstype=$(get_fstype "${cowprobe}")
+ else
+ [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent medium"
+ fi
+ fi
+
+ mount ${cowdevice} -t ${cow_fstype} -o rw,noatime,nodiratime /cow || panic "Can not mount ${cowdevice} on /cow"
+
+ mount -t ${UNIONTYPE} -o noatime,nodiratime,dirs=/cow=rw:${rofsstring} ${UNIONTYPE} "${rootmnt}" || panic "${UNIONTYPE} mount failed"
+
+ # Adding other custom mounts
+ if [ -n "${PERSISTENT}" ]
+ then
+ # directly mount /home
+ # FIXME: add a custom mounts configurable system
+ homecow=$(find_cow_device "${home_persistence}" )
+
+ if [ -b "${homecow}" ]
+ then
+ mount -t $(get_fstype "${homecow}") -o rw,noatime,nodiratime "${homecow}" "${rootmnt}/home"
+ export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
+ else
+ [ "${quiet}" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
+ fi
+
+ # Look for other snapshots to copy in
+ try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
+ try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
+ fi
+
+ if [ -n "${SHOWMOUNTS}" ]
+ then
+ for d in ${rofslist}
+ do
+ mkdir -p "${rootmnt}/live/${d##*/}"
+
+ case d in
+ *.dir)
+ # do nothing # mount -o bind "${d}" "${rootmnt}/live/${d##*/}"
+ ;;
+
+ *)
+ mount --move "${d}" "${rootmnt}/live/${d##*/}"
+ ;;
+ esac
+ done
+ fi
+
+ # shows cow fs on /cow for use by live-snapshot
+ mkdir -p "${rootmnt}/live/cow"
+ mount -o move /cow "${rootmnt}/live/cow"