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 ${1}`"
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 mksquashfs "${COW}" "${DEST}" || exit 1
107 (cd "${COW}" && find . | cpio --quiet -o -H newc | gzip -9 > "${DEST}") || exit 1
110 DU_DIM="`du -ks ${COW} | cut -f1`"
111 REAL_DIM="`expr ${DU_DIM} + ${DU_DIM} / 20`" # Just 5% more to be sure, need something more sophistcated here...
112 genext2fs --size-in-blocks=${REAL_DIM} --reserved-blocks=0 --root="${COW}" "${DEST}" || exit 1
115 echo "Internal error."
123 while read lines ; do
132 mounts="`awk '{print $2}' /proc/mounts`"
133 testpath="`realpath ${testpath}`"
136 if echo "${mounts}" | grep -qs "^${testpath}" ; then
137 set -- `echo "${mounts}" | grep "^${testpath}" | Lastline`
141 testpath=`dirname $testpath`
148 dir1="`Base_path ${1}`"
149 dir2="`Base_path ${2}`"
150 if [ "${dir1}" == "${dir2}" ]; then
160 ARGUMENTS="`getopt --longoptions cow:,destination:,output:,type:,help,usage,version --name=${PROGRAM} --options c:d:o:t:,h,u,v --shell sh -- ${@}`"
162 if [ "${?}" != "0" ]; then
163 echo "Terminating." >&2
167 eval set -- "${ARGUMENTS}"
172 SNAP_COW="${2}"; shift 2 ;;
174 SNAP_DEST="${2}"; shift 2 ;;
176 SNAP_OUTPUT="${2}"; shift 2 ;;
178 SNAP_TYPE="${2}"; shift 2 ;;
188 echo "Internal error."; exit 1 ;;
197 # Bad options handling
198 if [ -z "${SNAP_COW}" ]; then
203 if [ ! -d "${COW}" ]; then
204 Usage "Error: ${COW} is not a directory\nMaybe you booted with \"hide-cow\" as kernel parameter?"
207 case "${SNAP_TYPE}" in
208 "cpio"|"squashfs"|"ext2")
214 Usage "Error: unrecognized snapshot type"