block-snapshot fixes
This commit is contained in:
parent
e0b50db2ee
commit
24ed4df11c
51
common/block-snapshot
Normal file → Executable file
51
common/block-snapshot
Normal file → Executable file
@ -7,7 +7,6 @@
|
|||||||
dir=$(dirname "$0")
|
dir=$(dirname "$0")
|
||||||
. "$dir/block-common.sh"
|
. "$dir/block-common.sh"
|
||||||
|
|
||||||
|
|
||||||
get_dev() {
|
get_dev() {
|
||||||
dev=$1
|
dev=$1
|
||||||
|
|
||||||
@ -26,10 +25,10 @@ get_dev() {
|
|||||||
fatal "Unable to lookup $file: dev: $devnum inode: $inode"
|
fatal "Unable to lookup $file: dev: $devnum inode: $inode"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
dev_list=$(losetup -a | grep ' \[0*'${dev}'\]:'${inode} | cut -d : -f 1)
|
dev_list=$(losetup -a | grep ' \[0*'${devnum}'\]:'${inode} | cut -d : -f 1)
|
||||||
for dev in $dev_list; do
|
for loopdev in $dev_list; do
|
||||||
# found existing loop to this file
|
# found existing loop to this file
|
||||||
echo $dev
|
echo $loopdev
|
||||||
return
|
return
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -141,40 +140,52 @@ case "$command" in
|
|||||||
fatal "No device node to remove"
|
fatal "No device node to remove"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ! -e "$node" ]; then
|
||||||
|
fatal "Device $node does not exists"
|
||||||
|
fi
|
||||||
|
|
||||||
claim_lock "block"
|
claim_lock "block"
|
||||||
|
|
||||||
use_count=$(dmsetup info $node|grep Open|awk '{print $3}')
|
use_count=$(dmsetup info $node|grep Open|awk '{print $3}')
|
||||||
|
|
||||||
# do not remove snapshot if snapshot origin is still present
|
# do not remove snapshot if snapshot origin is still present
|
||||||
if [ "${node/snapshot/}" != "$node" -a
|
if [ "${node/snapshot/}" != "$node" -a -e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
|
||||||
-e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
|
use_count=1
|
||||||
((use_count++))
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$use_count" -gt 0 ]; then
|
if [ "$use_count" -gt 0 ]; then
|
||||||
log info "Device $node still in use - not removing"
|
log info "Device $node still in use - not removing"
|
||||||
|
release_lock "block"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# get list of used (loop) devices
|
# get list of used (loop) devices
|
||||||
deps="$(dmsetup deps $node | cut -d: -f2 | sed -e 's#(7, \([0-9]+\))#/dev/loop\1#g')"
|
deps="$(dmsetup deps $node | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
|
||||||
|
|
||||||
# remove unused snapshots
|
# if this is origin
|
||||||
for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2); do
|
if [ "${node/origin/}" != "$node" ]; then
|
||||||
use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
|
# remove unused snapshots
|
||||||
if [ $use_count -eq 0 ]; then
|
for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2)-*; do
|
||||||
# unused snapshot - remove it
|
use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
|
||||||
deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]+\))#/dev/loop\1#g')"
|
if [ "$use_count" -eq 0 ]; then
|
||||||
dmsetup remove $snap
|
# unused snapshot - remove it
|
||||||
fi
|
deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
|
||||||
done
|
log debug "Removing $snap"
|
||||||
|
dmsetup remove $snap
|
||||||
do_or_die dmsetup remove $node
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -e $node ]; then
|
||||||
|
log debug "Removing $node"
|
||||||
|
dmsetup remove $node
|
||||||
|
fi
|
||||||
|
|
||||||
# try to free loop devices
|
# try to free loop devices
|
||||||
for dev in $deps; do
|
for dev in $deps; do
|
||||||
if [ -b "$dev" ]; then
|
if [ -b "$dev" ]; then
|
||||||
losetup -d $dev 2> /dev/null
|
log debug "Removing $dev"
|
||||||
|
losetup -d $dev || true 2> /dev/null
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user