Adding casper 1.81+debian-1.
[live-boot-grml.git] / bin / casper-snapshot
old mode 100644 (file)
new mode 100755 (executable)
index d49034c..e912493
@@ -1,9 +1,10 @@
-#!/bin/sh
+#! /bin/bash
 
-# casper-snapshot - utility to do Debian Live systems snapshots
+# casper-snapshot - utility to manage Debian Live systems snapshots
 #
-#   This program mount a /tmpfs under ~/Desktop and save the /cow
-#   filesystem in it for reusing in another session.
+#   This program mount a device (fallback to /tmpfs under /mnt/snapshot
+#   and save the /cow (or a different dir) filesystem in it for reusing
+#   in another casper session. Look at manpage for more info.
 #
 # Copyright (C) 2006 Marco Amadori <marco.amadori@gmail.com>
 #
 # On Debian systems, the complete text of the GNU General Public License
 # can be found in /usr/share/common-licenses/GPL file.
 
-PROGRAM="`basename ${1}`"
+PROGRAM="`basename $0`"
 VERSION=0.0.1
 
+
 # Source casper conf
 if [ -e /etc/casper.conf ]; then
        . /etc/casper.conf
 else
-       USERNAME=`cat /etc/passwd | grep "999" | cut -f1 -d ':'`
+       USERNAME=$(cat /etc/passwd | grep "999" | cut -f1 -d ':')
+       HOSTNAME=$(hostname)
+       BUILD_SYSTEM="Debian"
+fi
+
+export USERNAME USERFULLNAME HOSTNAME BUILD_SYSTEM
+
+# Source helper functions
+helpers="/usr/share/initramfs-tools/scripts/casper-helpers"
+if [ -e "${helpers}" ]; then
+       . "${helpers}"
+else
+       echo "Error: I cannot found helper functions \"${helpers}\"."
+       exit 1
 fi
 
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+MOUNTP=""
+COW=""
+DEV=""
+DEST=""
+TYPE=""
+DESKTOP_LINK=""
+
 Header ()
 {
        echo "${PROGRAM} - utility to do Debian Live snapshots"
        echo
-       echo "Usage: ${PROGRAM} [-c|--cow DIRECTORY] [-d|--dest DIRECTORY] [-o|--output FILE] [-t|--type TYPE]"
+       echo "Usage: ${PROGRAM} [-c|--cow DIRECTORY] [-d|--device DEVICE] [-o|--output FILE] [-t|--type TYPE]"
+       echo "Usage: ${PROGRAM} [-r|--resync-string STRING]"
        echo "Usage: ${PROGRAM} [-h|--help]"
        echo "Usage: ${PROGRAM} [-u|--usage]"
        echo "Usage: ${PROGRAM} [-v|--version]"
@@ -64,9 +91,11 @@ Help ()
        echo
        echo "Options:"
        echo "  -c, --cow: specifies the copy on write directory (default: /cow)."
-       echo "  -d, --destination: specifies the output snapshot directory (default: /home/\$USERNAME/Desktop/casper-snapshot)."
+       echo "  -d, --device: specifies the output snapshot device (default: none)."
        echo "  -o, --output: specifies the output image file (default: $type dependent)."
-       echo "  -t,--type: specifies the snapshot type between \'squashfs\', \'ext2\' or \'cpio\'.gz archive (default: cpio)"
+    echo "  -r, --resync-string: internally used to resync previous made snapshots."
+       echo "  -t, --type: specifies the snapshot type between \"squashfs\", \"ext2\" or \"cpio\".gz archive (default: cpio)"
+    echo -e "\nLook at casper-snapshot(1) man page for more information."
        exit 0
 }
 
@@ -101,10 +130,13 @@ Do_snapshot ()
 {
        case "${TYPE}" in
                squashfs)
-                       mksquashfs "${COW}" "${DEST}" || exit 1
+                       echo "./tmp/exclude_list" > /tmp/exclude_list
+                       ( cd "${COW}" && find . -name '*.wh.*' >> /tmp/exclude_list )
+                       mksquashfs "${COW}" "${DEST}" -ef /tmp/exclude_list || exit 1
+                       rm /tmp/exclude_list
                        ;;
                cpio)
-                       (cd "${COW}" && find . | cpio --quiet -o -H newc | gzip -9 > "${DEST}") || exit 1
+                       ( cd "${COW}" && find . -path '*.wh.*' -prune -o -print0 | cpio --quiet -o0 -H newc | gzip -9c > "${DEST}" ) || exit 1
                        ;;
                ext2)
                        DU_DIM="`du -ks ${COW} | cut -f1`"
@@ -118,35 +150,10 @@ Do_snapshot ()
        esac
 }
 
-Lastline()
-{
-       while read lines ; do
-               line=${lines}
-       done
-       echo "${line}"
-}
-
-Base_path ()
-{
-       testpath="${1}"
-       mounts="`awk '{print $2}' /proc/mounts`"
-       testpath="`realpath ${testpath}`"
-
-       while true ; do
-               if echo "${mounts}" | grep -qs "^${testpath}" ; then
-                       set -- `echo "${mounts}" | grep "^${testpath}" | Lastline`
-                       echo ${1}
-                       break
-               else
-                       testpath=`dirname $testpath`
-               fi
-       done
-}
-
 Is_same_mount ()
 {
-       dir1="`Base_path ${1}`"
-       dir2="`Base_path ${2}`"
+       dir1="`Base_path $1`"
+       dir2="`Base_path $2`"
        if [ "${dir1}" == "${dir2}" ]; then
                return 0
        else
@@ -157,9 +164,10 @@ Is_same_mount ()
 Parse_args ()
 {
        # Parse command line
-       ARGUMENTS="`getopt --longoptions cow:,destination:,output:,type:,help,usage,version --name=${PROGRAM} --options c:d:o:t:,h,u,v --shell sh -- ${@}`"
+       ARGS="$1"
+       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}`"
 
-       if [ "${?}" != "0" ]; then
+       if [ "$?" != "0" ]; then
                echo "Terminating." >&2
                exit 1
        fi
@@ -167,15 +175,17 @@ Parse_args ()
        eval set -- "${ARGUMENTS}"
 
        while true; do
-               case "${1}" in
+               case "$1" in
                        -c|--cow)
-                               SNAP_COW="${2}"; shift 2 ;;
-                       -d|--destination)
-                               SNAP_DEST="${2}"; shift 2 ;;
+                               SNAP_COW="$2"; shift 2 ;;
+                       -d|--device)
+                               SNAP_DEV="$2"; shift 2 ;;
                        -o|--output)
-                               SNAP_OUTPUT="${2}"; shift 2 ;;
+                               SNAP_OUTPUT="$2"; shift 2 ;;
                        -t|--type)
-                               SNAP_TYPE="${2}"; shift 2 ;;
+                               SNAP_TYPE="$2"; shift 2 ;;
+                       -r|--resync-string)
+                               SNAP_RSTRING="$2"; break ;;
                        -h|--help)
                                Help; shift ;;
                        -u|--usage)
@@ -190,37 +200,100 @@ Parse_args ()
        done
 }
 
+Mount_device ()
+{
+       dev="$1"
+
+       if [ ! -d "${MOUNTP}" ]; then
+               mkdir -p "${MOUNTP}"
+       fi
+
+       if [ -z "${dev}" ]; then
+               # create a temp
+               mount -t tmpfs -o rw tmpfs "${MOUNTP}"
+               if [ ! -L /home/$USERNAME/Desktop/casper-snapshot ]; then
+                       ln -s "${MOUNTP}" /home/$USERNAME/Desktop/casper-snapshot
+               fi
+       else
+               if [ -b "${dev}" ] ; then
+                       try_mount "${dev}" "${MOUNTP}" rw
+               fi
+       fi
+}
+
 Defaults ()
 {
-       DEF_COW="/cow"
+       MOUNTP="/mnt/casper-snapshot"
+       COW="/cow"
+       DEV=""
+       DEST="${MOUNTP}/casper-sn.cpio.gz"
+       TYPE="cpio"
+       DESKTOP_LINK=/home/$USERNAME/Desktop/casper-snapshot
 
-       # Bad options handling
-       if [ -z "${SNAP_COW}" ]; then
-               COW="${DEF_COW}"
+       if [ -n "${SNAP_RSTRING}" ]; then
+               COW=$(echo "${SNAP_RSTRING}" | cut -f1 -d ':')
+               DEV=$(echo "${SNAP_RSTRING}" | cut -f2 -d ':')
+               DEST=$(echo "${SNAP_RSTRING}" | cut -f3 -d ':')
+               
+               case "${DEST}" in
+                       *.cpio.gz)
+                               TYPE="cpio" ;;
+                       *.squashfs)
+                               TYPE="squashfs" ;;
+                       "")
+                               TYPE="ext2" ;;
+                       *.ext2)
+                               TYPE="ext2" ;;
+                       *)
+                               Usage "Unregognized String" ;;
+               esac
        else
-               COW="${SNAP_COW}"
+               DEF_COW="/cow"
+               # Bad options handling
+               if [ -z "${SNAP_COW}" ]; then
+                       COW="${DEF_COW}"
+               else
+                       COW="${SNAP_COW}"
+               fi
+       
+               case "${SNAP_TYPE}" in
+                       "cpio"|"squashfs"|"ext2")
+                               TYPE="${SNAP_TYPE}"
+                               ;;
+                       "")
+                               TYPE="cpio" ;;
+                       *)
+                               Usage "Error: unrecognized snapshot type"
+                               ;;
+               esac
+               #if [ -d 
+               #if Is_same_mount 
        fi
+
+       # check vars
        if [ ! -d "${COW}" ]; then
-               Usage "Error: ${COW} is not a directory\nMaybe you booted with \"hide-cow\" as kernel parameter?"
+               Usage "Error: ${COW} is not a directory"
        fi
 
-       case "${SNAP_TYPE}" in
-               "cpio"|"squashfs"|"ext2")
-                       TYPE="${SNAP_TYPE}"
-                       ;;
-               "")
-                       TYPE="cpio" ;;
-               *)
-                       Usage "Error: unrecognized snapshot type"
-                       ;;
-       esac
+       Mount_device $DEV
+
+}
+
+Clean ()
+{
+       if [ -n "$DEV" ]; then
+               umount "${MOUNTP}"
+               rmdir "${MOUNTP}"
+               rm 
+       fi
 }
 
 Main ()
 {
-       Parse_args "${@}"
+       Parse_args "$@"
        Defaults
        Do_snapshot
+       Clean
 }
 
-Main "${@}"
+Main "$@"