+do_snap_copy ()
+{
+ fromdev="${1}"
+ todir="${2}"
+ snap_type="${3}"
+
+ size=$(used_fs_size "${fromdev}")
+
+ if [ -b "${fromdev}" ]; then
+ # look for free mem
+ if [ ! -z "${HOMEMOUNTED}" ] && [ "${snap_type}" = "HOME" ]; then
+ freespace="$(df -k ${copytodev} | grep -s ${copytodev} | awk '{print $4}')"
+ else
+ freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
+ fi
+ tomount="/mnt/tmpsnap"
+ mkdir -p "${tomount}"
+ mount -t $(get_fstype "${fromdev}") -o ro "${fromdev}" "${tomount}"
+ cp -a "${tomount}"/* ${todir}
+ umount "${tomount}"
+
+ if echo ${fromdev} | grep -qs loop; then
+ losetup -d "${fromdev}"
+ fi
+
+ return 0
+ else
+ return 1
+ [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
+ fi
+}
+
+try_snap ()
+{
+ snap_label="${1}"
+ snap_mount="${2}"
+ snap_type="${3}"
+
+ snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.cpz ${snap_label}.gz")
+ if [ ! -z "${snapdata}" ]; then
+ snapdev=$(echo ${snapdata} | cut -f1 -d ' ')
+ snapback=$(echo ${snapdata} | cut -f2 -d ' ')
+ snapfile=$(echo ${snapdata} | cut -f3 -d ' ')
+ if echo "${snapfile}" | grep -qs "squashfs" ; then
+ # squashfs snapshot
+ if ! do_snap_copy $( get_backing_device "${snapback}/${snapfile}" ) "${snap_mount}" "${snap_type}"; then
+ log_warning_msg "Impossible to include the ${snapfile} Snapshot"
+ return 1
+ fi
+ else
+ # cpio.gz snapshot
+ if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d ) ; then
+ log_warning_msg "Impossible to include the ${snapfile} Snapshot"
+ return 1
+ fi
+ fi
+ umount "${snapback}"
+ else # try pure snapshot device better elif.. rework all that routine
+ if ! do_snap_copy $(find_cow_device "${snap_label}") "${snap_mount}" "${snap_type}"; then
+ log_warning_msg "Impossible to include the ${snap_label} Snapshot"
+ return 1
+ fi
+ fi
+ echo "export ${snap_type}SNAP=${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
+ return 0
+}
+
+do_others_persistences ()
+{
+ # directly mount /home
+ # FIXME: add a custom mounts configurable system
+ homecow=$(find_cow_device "${home_persistence}" )
+ if [ -b "${homecow}" ]; then
+ mount ${homecow} -t $(get_fstype "${homecow}") -o rw "${rootmnt}/home"
+ export HOMEMOUNTED=1
+ else
+ [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
+ fi
+
+ # Look for snapshots to copy in
+ try_snap "${root_snapshot}" "${rootmnt}" "ROOT"
+ try_snap "${home_snapshot}" "${rootmnt}/home" "HOME"
+}
+