a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#!/bin/sh
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncset -e
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncimgloop=
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynctmpfile=
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynctmploop=
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncdmname=
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynccowlink=
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncfunction cleanup () {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync set +e
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync [ -n "$cowlink" ] && rm $cowlink
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync [ -n "$dmname" ] && dmsetup remove $dmname
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync [ -n "$tmploop" ] && losetup -d $tmploop
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync [ -n "$tmpfile" ] && rm $tmpfile
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync [ -n "$imgloop" ] && losetup -d $imgloop
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynctrap cleanup EXIT
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncimgfile=$1 ; shift
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynccommand=$1 ; shift
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncif [ -z "$imgfile" -o -z "$command" ] ; then
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo Syntax: $0 /path/to/image/file command [args..]
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync exit 1
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncfi
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Set up image loop device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncx=`losetup -f` ; losetup -r $x $imgfile ; imgloop=$x
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Create temporary file and set up temporary loop device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynctmpfile=`mktemp $imgfile.XXXXXXXXXX`
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsynctruncate -r $imgfile $tmpfile
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncx=`losetup -f` ; losetup $x $tmpfile ; tmploop=$x
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Create snapshot device
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncimgsize=`blockdev --getsz $imgloop`
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncx=`basename $imgfile` ; echo 0 $imgsize snapshot $imgloop $tmploop N 16 | \
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync dmsetup create $x ; dmname=$x
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncchown --reference=$imgfile /dev/mapper/$dmname
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncchmod --reference=$imgfile /dev/mapper/$dmname
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Create symlink
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncx=$imgfile.cow ; ln -s /dev/mapper/$dmname $x ; cowlink=$x
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Wait until killed
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncecho "Created $cowlink"
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync$command "$@" $cowlink