From 6ce33622f33265884269ba5c4dacc1ea4fad5276 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Wed, 10 Jul 2013 18:35:56 +0200 Subject: [PATCH] overlaydevice: Initial version. --- overlaydevice/overlaydevice | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 overlaydevice/overlaydevice diff --git a/overlaydevice/overlaydevice b/overlaydevice/overlaydevice new file mode 100644 index 0000000..d91020e --- /dev/null +++ b/overlaydevice/overlaydevice @@ -0,0 +1,44 @@ +#!/bin/bash + +start() { + DEVICE=$1 + BASE=$(basename $DEVICE) + OVERLAY=overlay-$BASE + + SIZE=$(blockdev --getsize $DEVICE) + BYTESIZE=$(blockdev --getsize64 $DEVICE) + + # Create sparse file as big as possible + OVLSIZE=$BYTESIZE + while ! truncate -s${OVLSIZE} $OVERLAY; do + echo $OVLSIZE failed + OVLSIZE=$(( OVLSIZE/2 )) + done + + if ! LOOPDEV=$(losetup --show -f -- $OVERLAY); then + LOOPS=$(ls /dev/loop* | wc -l) + mknod -m 660 /dev/loop$LOOPS b 7 $LOOPS + LOOPDEV=$(losetup --show -f -- $OVERLAY) + fi + + echo 0 $SIZE snapshot $DEVICE $LOOPDEV P 8 | dmsetup create $BASE + echo Overlay device: /dev/mapper/$BASE + echo Overlay file: $OVERLAY + echo Stop with: + echo dmsetup remove $BASE; rm $OVERLAY + echo losetup -d $LOOPDEV +} + +stop() { + DEVICE=$1 + BASE=$(basename $DEVICE) + OVERLAY=overlay-$BASE + dmsetup remove $BASE + # Find the loop device with $OVERLAY and stop it + losetup -d $(losetup -j $OVERLAY) + rm $OVERLAY +} + +start $1 + +