8 size=$(fs_size "${fromdev}" "" "used")
10 if [ -b "${fromdev}" ]
12 log_success_msg "Copying snapshot ${fromdev} to ${todir}..."
15 if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]
17 todev=$(awk -v pat="$(base_path ${todir})" '$2 == pat { print $1 }' /proc/mounts)
18 freespace=$(df -k | awk '/'${todev}'/{print $4}')
20 freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
23 tomount="/mnt/tmpsnap"
25 if [ ! -d "${tomount}" ]
30 fstype=$(get_fstype "${fromdev}")
35 mount -o ro -t "${fstype}" "${fromdev}" "${tomount}" || log_warning_msg "Error in mount -t ${fstype} -o ro ${fromdev} ${tomount}"
36 cp -a "${tomount}"/* ${todir}
39 log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
44 if echo ${fromdev} | grep -qs loop
46 losetup -d "${fromdev}"
51 log_warning_msg "Unable to find the snapshot ${snap_type} medium"
58 # copy the contents of previously found snapshot to ${snap_mount}
59 # and remember the device and filename for resync on exit in live-boot.init
66 if [ -z "${snap_relpath}" ]
68 # root snapshot, default usage
71 # relative snapshot (actually used just for "/home" snapshots)
72 snap_mount="${2}${snap_relpath}"
75 if [ -n "${snapdata}" ] && [ ! -b "${snapdata}" ]
77 log_success_msg "found snapshot: ${snapdata}"
78 snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
79 snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
80 snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
82 if ! try_mount "${snapdev}" "${snapback}" "ro"
89 if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\|ext3\|ext4\|jffs2\)'
91 # squashfs, jffs2 or ext2/ext3/ext4 snapshot
92 dev=$(get_backing_device "${snapback}/${snapfile}")
94 do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
99 # Unfortunately klibc's cpio is incompatible with the
100 # rest of the world; everything else requires -u -d,
101 # while klibc doesn't implement them. Try to detect
102 # whether it's in use.
103 cpiopath="$(which cpio)" || true
104 if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"
108 cpioargs='--unconditional --make-directories'
111 if [ -s "${snapback}/${snapfile}" ]
114 cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse 2>/dev/null
118 log_warning_msg "${snapback}/${snapfile} is empty, adding it for sync on reboot."
122 if [ "${RES}" != "0" ]
124 log_warning_msg "failure to \"zcat ${snapback}/${snapfile} | $cpiopath $cpioargs --extract --preserve-modification-time --no-absolute-filenames --sparse\""
128 umount "${snapback}" || log_warning_msg "failure to \"umount ${snapback}\""
130 if [ "${RES}" != "0" ]
132 log_warning_msg "Impossible to include the ${snapfile} Snapshot file"
135 elif [ -b "${snapdata}" ]
137 # Try to find if it could be a snapshot partition
139 log_success_msg "found snapshot ${snap_type} device on ${dev}"
140 if echo "${dev}" | grep -qs loop
142 # strange things happens, user confused?
143 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
144 snapfile=$(basename ${snaploop})
145 snapdev=$(awk -v pat="$( dirname ${snaploop})" '$2 == pat { print $1 }' /proc/mounts)
150 if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"
152 log_warning_msg "Impossible to include the ${snap_type} Snapshot (i)"
155 if [ -n "${snapfile}" ]
157 # it was a loop device, user confused
162 log_warning_msg "Impossible to include the ${snap_type} Snapshot (o)"
166 if [ -z ${PERSISTENCE_READONLY} ]
168 echo "export ${snap_type}SNAP=${snap_relpath}:${snapdev}:${snapfile}" >> snapshot.conf # for resync on reboot/halt