block-snapshot fixes
This commit is contained in:
parent
e0b50db2ee
commit
2aec587027
37
common/block-snapshot
Normal file → Executable file
37
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')"
|
||||||
|
|
||||||
|
# if this is origin
|
||||||
|
if [ "${node/origin/}" != "$node" ]; then
|
||||||
# remove unused snapshots
|
# remove unused snapshots
|
||||||
for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2); do
|
for snap in /dev/mapper/snapshot-$(echo $node|cut -d- -f2)-*; do
|
||||||
use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
|
use_count=$(dmsetup info $snap|grep Open|awk '{print $3}')
|
||||||
if [ $use_count -eq 0 ]; then
|
if [ "$use_count" -eq 0 ]; then
|
||||||
# unused snapshot - remove it
|
# unused snapshot - remove it
|
||||||
deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]+\))#/dev/loop\1#g')"
|
deps="$deps $(dmsetup deps $snap | cut -d: -f2 | sed -e 's#(7, \([0-9]\+\))#/dev/loop\1#g')"
|
||||||
|
log debug "Removing $snap"
|
||||||
dmsetup remove $snap
|
dmsetup remove $snap
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
do_or_die dmsetup remove $node
|
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