3 # live-snapshot - utility to manage Debian Live systems snapshots
5 # This program mount a device (fallback to /tmpfs under /mnt/snapshot
6 # and save the /live/cow (or a different dir) filesystem in it for reusing
7 # in another live-initramfs session. Look at manpage for more info.
9 # Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>
10 # Copyright (C) 2008 Chris Lamb <chris@chris-lamb.co.uk>
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 # On Debian systems, the complete text of the GNU General Public License
27 # can be found in /usr/share/common-licenses/GPL-2 file.
31 . /usr/share/initramfs-tools/scripts/live-helpers
34 export USERNAME USERFULLNAME HOSTNAME
36 PROGRAM="$(basename $0)"
39 MOUNTP="/mnt/live-snapshot"
42 DEST="${MOUNTP}/live-sn.cpio.gz"
44 DESKTOP_LINK="/home/${USERNAME}/Desktop/live-snapshot"
49 echo "${PROGRAM}: error:" ${@}
60 echo "${PROGRAM} - utility to perform snapshots of Debian Live systems"
62 echo "usage: ${PROGRAM} [-c|--cow DIRECTORY] [-d|--device DEVICE] [-o|--output FILE] [-t|--type TYPE]"
63 echo " ${PROGRAM} [-r|--resync-string STRING]"
64 echo " ${PROGRAM} [-h|--help]"
65 echo " ${PROGRAM} [-u|--usage]"
66 echo " ${PROGRAM} [-v|--version]"
75 echo " -c, --cow: copy on write directory (default: ${SNAP_COW})."
76 echo " -d, --device: output snapshot device (default: ${SNAP_DEV:-auto})."
77 echo " -o, --output: output image file (default: ${DEST})."
78 echo " -r, --resync-string: internally used to resync previous made snapshots."
79 echo " -t, --type: snapshot filesystem type. Options: \"squashfs\", \"ext2\", \"ext3\", \"jffs2\" or \"cpio\".gz archive (default: ${SNAP_TYPE})"
81 echo "Look at live-snapshot(1) man page for more information."
91 echo "Try \"${PROGRAM} --help\" for more information."
99 echo "${PROGRAM}, version ${VERSION}"
101 echo "Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>"
103 echo "This program is free software; you can redistribute it and/or modify"
104 echo "it under the terms of the GNU General Public License as published by"
105 echo "the Free Software Foundation; either version 2 of the License, or"
106 echo "(at your option) any later version."
108 echo "This program is distributed in the hope that it will be useful,"
109 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
110 echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
111 echo "GNU General Public License for more details."
113 echo "You should have received a copy of the GNU General Public License"
114 echo "along with this program; if not, write to the Free Software"
115 echo "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA"
117 echo "On Debian systems, the complete text of the GNU General Public License"
118 echo "can be found in /usr/share/common-licenses/GPL-2 file."
120 echo "Homepage: <http://debian-live.alioth.debian.org/>"
127 dir1="$(Base_path ${1})"
128 dir2="$(Base_path ${2})"
130 if [ "${dir1}" = "${dir2}" ]
142 ARGUMENTS="$(getopt --longoptions cow:,device:,output,resync-string:,type:,help,usage,version --name=${PROGRAM} --options c:d:o:t:r:,h,u,v --shell sh -- ${ARGS})"
144 eval set -- "${ARGUMENTS}"
170 SNAP_RESYNC_STRING="${2}"
192 Error "internal error."
202 # Parse resync string
203 if [ -n "${SNAP_RESYNC_STRING}" ]
205 SNAP_COW=$(echo "${SNAP_RESYNC_STRING}" | cut -f1 -d ':')
206 SNAP_DEV=$(echo "${SNAP_RESYNC_STRING}" | cut -f2 -d ':')
207 DEST=$(echo "${SNAP_RESYNC_STRING}" | cut -f3 -d ':')
226 Error "unrecognized resync string"
231 # Set target file based on image
232 case "${SNAP_TYPE}" in
234 DEST="${MOUNTP}/live-sn.cpio.gz"
238 DEST="${MOUNTP}/live-sn.${SNAP_TYPE}"
242 DEST="${MOUNTP}/live-sn.ext2"
251 case "${SNAP_TYPE}" in
252 cpio|squashfs|jffs2|ext2|ext3)
255 Error "invalid filesystem type \"${SNAP_TYPE}\""
259 if [ ! -d "${SNAP_COW}" ]
261 Error "${SNAP_COW} is not a directory"
264 if [ "$(id -u)" -ne 0 ]
266 Error "you are not root"
274 case "${SNAP_DEV}" in
277 mount -t tmpfs -o rw tmpfs "${MOUNTP}"
280 if [ -b "${SNAP_DEV}" ]
282 try_mount "${SNAP_DEV}" "${MOUNTP}" rw
290 case "${SNAP_TYPE}" in
292 echo "./tmp/exclude_list" > /tmp/exclude_list
293 ( cd "${SNAP_COW}" && find . -name '*.wh.*' >> /tmp/exclude_list )
294 mksquashfs "${SNAP_COW}" "${DEST}" -ef /tmp/exclude_list
299 ( cd "${SNAP_COW}" && find . -path '*.wh.*' -prune -o -print0 | cpio --quiet -o0 -H newc | gzip -9c > "${DEST}" ) || exit 1
303 DU_DIM="$(du -ks ${SNAP_COW} | cut -f1)"
304 REAL_DIM="$(expr ${DU_DIM} + ${DU_DIM} / 20)" # Just 5% more to be sure, need something more sophistcated here...
305 genext2fs --size-in-blocks=${REAL_DIM} --reserved-blocks=0 --root="${SNAP_COW}" "${DEST}"
309 mkfs.jffs2 --root="${SNAP_COW}" --output="${DEST}"