+do_snap_copy ()
+{
+ fromdev="${1}"
+ todir="${2}"
+ snap_type="${3}"
+
+ size=$(fs_size "${fromdev}" "" "used")
+
+ if [ -b "${fromdev}" ]; then
+ # look for free mem
+ if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
+ todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
+ freespace=$(df -k | grep -s ${todev} | 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"
+ if [ ! -d "${tomount}" ] ; then
+ mkdir -p "${tomount}"
+ fi
+
+ fstype=$(get_fstype "${fromdev}")
+ if [ -n "${fstype}" ]; then
+ # Copying stuff...
+ mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
+ cp -a "${tomount}"/* ${todir}
+ umount "${tomount}"
+ else
+ log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
+ fi
+
+ rmdir "${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 ()
+{
+ # Look for $snap_label.* in block devices and copy the contents to $snap_mount
+ # and remember the device and filename for resync on exit in casper.init
+
+ snap_label="${1}"
+ snap_mount="${2}"
+ snap_type="${3}"
+
+ snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
+ 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\|ext2\)'; then
+ # squashfs or ext2 snapshot
+ dev=$(get_backing_device "${snapback}/${snapfile}")
+ if ! do_snap_copy "${dev}" "${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 2>/dev/null) ; then
+ log_warning_msg "Impossible to include the ${snapfile} Snapshot"
+ return 1
+ fi
+ fi
+ umount "${snapback}"
+ else
+ dev=$(find_cow_device "${snap_label}")
+ if [ -b ${dev} ]; then
+ if echo "${dev}" | grep -qs loop; then
+ # strange things happens, user confused?
+ snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
+ snapfile=$(basename ${snaploop})
+ snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
+ else
+ snapdev="${dev}"
+ fi
+ if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
+ log_warning_msg "Impossible to include the ${snap_label} Snapshot"
+ return 1
+ else
+ if [ -n "${snapfile}" ]; then
+ # it was a loop device, user confused
+ umount ${snapdev}
+ fi
+ fi
+ else
+ log_warning_msg "Impossible to include the ${snap_label} Snapshot"
+ return 1
+ fi
+ fi
+ echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
+ return 0
+}
+