3 # casper-snapshot - utility to manage Debian Live systems snapshots
5 # This program mount a device (fallback to /tmpfs under /mnt/snapshot
6 # and save the /cow (or a different dir) filesystem in it for reusing
7 # in another casper session. Look at manpage for more info.
9 # Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 # On Debian systems, the complete text of the GNU General Public License
26 # can be found in /usr/share/common-licenses/GPL file.
28 PROGRAM="`basename ${0}`"
33 if [ -e /etc/casper.conf ]; then
36 USERNAME=$(cat /etc/passwd | grep "999" | cut -f1 -d ':')
41 export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM
43 # Source helper functions
44 helpers="/usr/share/initramfs-tools/scripts/casper-helpers"
45 if [ -e "${helpers}" ]; then
48 echo "Error: I cannot found helper functions \"${helpers}\"."
52 # Define LSB log_* functions.
53 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
54 . /lib/lsb/init-functions
57 MOUNTP="/mnt/casper-snapshot"
60 DEST="${MOUNTP}/casper-sn.cpio.gz"
62 DESKTOP_LINK=/home/$USERNAME/Desktop/casper-snapshot
66 echo "${PROGRAM} - utility to do Debian Live snapshots"
68 echo "Usage: ${PROGRAM} [-c|--cow DIRECTORY] [-d|--device DEVICE] [-o|--output FILE] [-t|--type TYPE]"
69 echo "Usage: ${PROGRAM} [-r|--resync-string STRING]"
70 echo "Usage: ${PROGRAM} [-h|--help]"
71 echo "Usage: ${PROGRAM} [-u|--usage]"
72 echo "Usage: ${PROGRAM} [-v|--version]"
80 echo "Try \"${PROGRAM} --help\" for more information."
81 if [ ! -z "${MESSAGE}" ]; then
94 echo " -c, --cow: specifies the copy on write directory (default: /cow)."
95 echo " -d, --destination: specifies the output snapshot directory (default: /home/\$USERNAME/Desktop/casper-snapshot)."
96 echo " -o, --output: specifies the output image file (default: $type dependent)."
97 echo " -r, --resync-string: internally used to resync previous made snapshots."
98 echo " -t,--type: specifies the snapshot type between \'squashfs\', \'ext2\' or \'cpio\'.gz archive (default: cpio)"
99 echo -e "\nLook at casper-snapshot(1) man page for more information."
105 echo "${PROGRAM}, version ${VERSION}"
107 echo "Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>"
109 echo "This program is free software; you can redistribute it and/or modify"
110 echo "it under the terms of the GNU General Public License as published by"
111 echo "the Free Software Foundation; either version 2 of the License, or"
112 echo "(at your option) any later version."
114 echo "This program is distributed in the hope that it will be useful,"
115 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
116 echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
117 echo "GNU General Public License for more details."
119 echo "You should have received a copy of the GNU General Public License"
120 echo "along with this program; if not, write to the Free Software"
121 echo "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA"
123 echo "On Debian systems, the complete text of the GNU General Public License"
124 echo "can be found in /usr/share/common-licenses/GPL file."
126 echo "Homepage: <http://live.debian.net/>"
134 echo "./tmp/exclude_list" > /tmp/exclude_list
135 ( cd "${COW}" && find . -name '*.wh.*' >> /tmp/exclude_list )
136 mksquashfs "${COW}" "${DEST}" -ef /tmp/exclude_list || exit 1
140 ( cd "${COW}" && find . -path '*.wh.*' -prune -o -print0 | cpio --quiet -o0 -H newc | gzip -9c > "${DEST}" ) || exit 1
143 DU_DIM="`du -ks ${COW} | cut -f1`"
144 REAL_DIM="`expr ${DU_DIM} + ${DU_DIM} / 20`" # Just 5% more to be sure, need something more sophistcated here...
145 genext2fs --size-in-blocks=${REAL_DIM} --reserved-blocks=0 --root="${COW}" "${DEST}" || exit 1
148 echo "Internal error."
156 dir1="`Base_path ${1}`"
157 dir2="`Base_path ${2}`"
158 if [ "${dir1}" == "${dir2}" ]; then
169 ARGUMENTS="`getopt --longoptions cow:,destination:,output:,type:,help,usage,version --name=${PROGRAM} --options c:d:o:t:,h,u,v --shell sh -- ${ARGS}`"
171 if [ "${?}" != "0" ]; then
172 echo "Terminating." >&2
176 eval set -- "${ARGUMENTS}"
181 SNAP_COW="${2}"; shift 2 ;;
183 SNAP_DEV="${2}"; shift 2 ;;
185 SNAP_OUTPUT="${2}"; shift 2 ;;
187 SNAP_TYPE="${2}"; shift 2 ;;
189 SNAP_RSTRING="${2}"; break ;;
199 echo "Internal error."; exit 1 ;;
208 if [ ! -d "${MOUNTP}" ]; then
212 if [ -n "${dev}" ]; then
214 mount -t tmpfs -o rw tmpfs "${MOUNTP}"
215 if [ ! -L /home/$USERNAME/Desktop/casper-snapshot ]; then
216 ln -s "${MOUNTP}" /home/$USERNAME/Desktop/casper-snapshot
219 if [ -b "${dev}" ] ; then
220 try_mount "${dev}" "${MOUNTP}" rw
227 if [ -n "${SNAP_RSTRING}" ]; then
228 COW=$(echo "${SNAP_RSTRING}" | cut -f1 -d ':')
229 DEV=$(echo "${SNAP_RSTRING}" | cut -f2 -d ':')
230 DEST=$(echo "${SNAP_RSTRING}" | cut -f3 -d ':')
233 *.cpio.gz|*.cpz|*.gz)
242 Usage "Unregognized String" ;;
246 # Bad options handling
247 if [ -z "${SNAP_COW}" ]; then
253 case "${SNAP_TYPE}" in
254 "cpio"|"squashfs"|"ext2")
260 Usage "Error: unrecognized snapshot type"
268 DEST="${MOUNTP}/${DEST}"
271 if [ ! -d "${COW}" ]; then
272 Usage "Error: ${COW} is not a directory"
278 if [ -n "$DEV" ]; then