3 # casper-snapshot - utility to do Debian Live systems snapshots
5 # This program mount a /tmpfs under ~/Desktop and save the /cow
6 # filesystem in it for reusing in another session.
8 # Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 # On Debian systems, the complete text of the GNU General Public License
25 # can be found in /usr/share/common-licenses/GPL file.
27 PROGRAM="`basename ${0}`"
31 if [ -e /etc/casper.conf ]; then
34 USERNAME=`cat /etc/passwd | grep "999" | cut -f1 -d ':'`
39 echo "${PROGRAM} - utility to do Debian Live snapshots"
41 echo "Usage: ${PROGRAM} [-c|--cow DIRECTORY] [-d|--dest DIRECTORY] [-o|--output FILE] [-t|--type TYPE]"
42 echo "Usage: ${PROGRAM} [-h|--help]"
43 echo "Usage: ${PROGRAM} [-u|--usage]"
44 echo "Usage: ${PROGRAM} [-v|--version]"
52 echo "Try \"${PROGRAM} --help\" for more information."
53 if [ ! -z "${MESSAGE}" ]; then
66 echo " -c, --cow: specifies the copy on write directory (default: /cow)."
67 echo " -d, --destination: specifies the output snapshot directory (default: /home/\$USERNAME/Desktop/casper-snapshot)."
68 echo " -o, --output: specifies the output image file (default: $type dependent)."
69 echo " -t,--type: specifies the snapshot type between \'squashfs\', \'ext2\' or \'cpio\'.gz archive (default: cpio)"
75 echo "${PROGRAM}, version ${VERSION}"
77 echo "Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>"
79 echo "This program is free software; you can redistribute it and/or modify"
80 echo "it under the terms of the GNU General Public License as published by"
81 echo "the Free Software Foundation; either version 2 of the License, or"
82 echo "(at your option) any later version."
84 echo "This program is distributed in the hope that it will be useful,"
85 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
86 echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
87 echo "GNU General Public License for more details."
89 echo "You should have received a copy of the GNU General Public License"
90 echo "along with this program; if not, write to the Free Software"
91 echo "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA"
93 echo "On Debian systems, the complete text of the GNU General Public License"
94 echo "can be found in /usr/share/common-licenses/GPL file."
96 echo "Homepage: <http://live.debian.net/>"
104 echo "./tmp/exclude_list" > /tmp/exclude_list
105 ( cd "${COW}" && find . -name '*.wh.*' >> /tmp/exclude_list )
106 mksquashfs "${COW}" "${DEST}" -ef /tmp/exclude_list || exit 1
110 ( cd "${COW}" && find . -path '*.wh.*' -prune -o -print0 | cpio --quiet -o0 -H newc | gzip -9c > "${DEST}" ) || exit 1
113 DU_DIM="`du -ks ${COW} | cut -f1`"
114 REAL_DIM="`expr ${DU_DIM} + ${DU_DIM} / 20`" # Just 5% more to be sure, need something more sophistcated here...
115 genext2fs --size-in-blocks=${REAL_DIM} --reserved-blocks=0 --root="${COW}" "${DEST}" || exit 1
118 echo "Internal error."
126 while read lines ; do
135 mounts="`awk '{print $2}' /proc/mounts`"
136 testpath="`realpath ${testpath}`"
139 if echo "${mounts}" | grep -qs "^${testpath}" ; then
140 set -- `echo "${mounts}" | grep "^${testpath}" | Lastline`
144 testpath=`dirname $testpath`
151 dir1="`Base_path ${1}`"
152 dir2="`Base_path ${2}`"
153 if [ "${dir1}" == "${dir2}" ]; then
164 ARGUMENTS="`getopt --longoptions cow:,destination:,output:,type:,help,usage,version --name=${PROGRAM} --options c:d:o:t:,h,u,v --shell sh -- ${ARGS}`"
166 if [ "${?}" != "0" ]; then
167 echo "Terminating." >&2
171 eval set -- "${ARGUMENTS}"
176 SNAP_COW="${2}"; shift 2 ;;
178 SNAP_DEST="${2}"; shift 2 ;;
180 SNAP_OUTPUT="${2}"; shift 2 ;;
182 SNAP_TYPE="${2}"; shift 2 ;;
192 echo "Internal error."; exit 1 ;;
201 # Bad options handling
202 if [ -z "${SNAP_COW}" ]; then
207 if [ ! -d "${COW}" ]; then
208 Usage "Error: ${COW} is not a directory"
211 case "${SNAP_TYPE}" in
212 "cpio"|"squashfs"|"ext2")
218 Usage "Error: unrecognized snapshot type"