X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=bin%2Fcasper-snapshot;h=c0c8662135cec3cae2344555c73a89ec45b8f349;hb=beec4f9fb766be23d575b070e398639d5a4a9c9d;hp=d49034c4a30099582ea1995c92390fc3d06807b9;hpb=4042b2eda2107dda511a29ae376d9724e0fd7a33;p=live-boot-grml.git diff --git a/bin/casper-snapshot b/bin/casper-snapshot index d49034c..c0c8662 100644 --- a/bin/casper-snapshot +++ b/bin/casper-snapshot @@ -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 # @@ -24,21 +25,47 @@ # 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,31 +150,6 @@ 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}`" @@ -157,7 +164,8 @@ 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 echo "Terminating." >&2 @@ -170,12 +178,14 @@ Parse_args () case "${1}" in -c|--cow) SNAP_COW="${2}"; shift 2 ;; - -d|--destination) - SNAP_DEST="${2}"; shift 2 ;; + -d|--device) + SNAP_DEV="${2}"; shift 2 ;; -o|--output) SNAP_OUTPUT="${2}"; shift 2 ;; -t|--type) SNAP_TYPE="${2}"; shift 2 ;; + -r|--resync-string) + SNAP_RSTRING="${2}"; break ;; -h|--help) Help; shift ;; -u|--usage) @@ -190,30 +200,92 @@ 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 () @@ -221,6 +293,7 @@ Main () Parse_args "${@}" Defaults Do_snapshot + Clean } Main "${@}"