block-snapshot: fix ShellCheck warnings (1/3)
Forward-ported from qubes-core-agent-linux: commit e95b6f8d0357ac9a32fb7e35e5734781460d8f24 Author: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com> Date: Sat Sep 30 04:53:38 2017 +0200 Fix shellcheck warnings in block-snapshot script
This commit is contained in:
parent
342c36ed36
commit
45a06e7fad
@ -28,10 +28,10 @@ get_dev() {
|
|||||||
if [ -f "$dev" ]; then
|
if [ -f "$dev" ]; then
|
||||||
file=$dev
|
file=$dev
|
||||||
|
|
||||||
loopdev=$(losetup -j $file | head -1 | cut -d : -f 1)
|
loopdev=$(losetup -j "$file" | head -1 | cut -d : -f 1)
|
||||||
if [ -n "$loopdev" ]; then
|
if [ -n "$loopdev" ]; then
|
||||||
# found existing loop to this file
|
# found existing loop to this file
|
||||||
echo $loopdev
|
echo "$loopdev"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ get_dev() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
do_or_die losetup "$loopdev" "$file"
|
do_or_die losetup "$loopdev" "$file"
|
||||||
echo $loopdev
|
echo "$loopdev"
|
||||||
else
|
else
|
||||||
test -e "$dev" || fatal "$dev does not exist."
|
test -e "$dev" || fatal "$dev does not exist."
|
||||||
test -b "$dev" || fatal "$dev is not a block device nor file."
|
test -b "$dev" || fatal "$dev is not a block device nor file."
|
||||||
@ -73,12 +73,12 @@ create_dm_snapshot() {
|
|||||||
base=$2
|
base=$2
|
||||||
cow=$3
|
cow=$3
|
||||||
|
|
||||||
if [ ! -e /dev/mapper/$dm_devname ]; then
|
if [ ! -e "/dev/mapper/$dm_devname" ]; then
|
||||||
# prepare new snapshot device
|
# prepare new snapshot device
|
||||||
base_dev=$(get_dev $base)
|
base_dev=$(get_dev "$base")
|
||||||
cow_dev=$(get_dev $cow)
|
cow_dev=$(get_dev "$cow")
|
||||||
base_sz=$(blockdev --getsz $base_dev)
|
base_sz=$(blockdev --getsz "$base_dev")
|
||||||
do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
|
do_or_die dmsetup create "$dm_devname" --table "0 $base_sz snapshot $base_dev $cow_dev P 256"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -89,11 +89,11 @@ create_dm_snapshot_origin() {
|
|||||||
dm_devname=$1
|
dm_devname=$1
|
||||||
base=$2
|
base=$2
|
||||||
|
|
||||||
if [ ! -e /dev/mapper/$dm_devname ]; then
|
if [ ! -e "/dev/mapper/$dm_devname" ]; then
|
||||||
# prepare new snapshot-origin device
|
# prepare new snapshot-origin device
|
||||||
base_dev=$(get_dev $base)
|
base_dev=$(get_dev "$base")
|
||||||
base_sz=$(blockdev --getsz $base_dev)
|
base_sz=$(blockdev --getsz "$base_dev")
|
||||||
do_or_die dmsetup create $dm_devname --table "0 $base_sz snapshot-origin $base_dev"
|
do_or_die dmsetup create "$dm_devname" --table "0 $base_sz snapshot-origin $base_dev"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ case "$command" in
|
|||||||
claim_lock "block"
|
claim_lock "block"
|
||||||
|
|
||||||
# prepare snapshot device
|
# prepare snapshot device
|
||||||
create_dm_snapshot $dm_devname "$base" "$cow"
|
create_dm_snapshot "$dm_devname" "$base" "$cow"
|
||||||
|
|
||||||
if [ -n "$cow2" ]; then
|
if [ -n "$cow2" ]; then
|
||||||
dm_devname_full=$(get_dm_snapshot_name "$base" "$cow" "$cow2")
|
dm_devname_full=$(get_dm_snapshot_name "$base" "$cow" "$cow2")
|
||||||
@ -149,14 +149,14 @@ case "$command" in
|
|||||||
#that's all for snapshot, store name of prepared device
|
#that's all for snapshot, store name of prepared device
|
||||||
xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
|
xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
|
||||||
echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
|
echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
|
||||||
write_dev /dev/mapper/$dm_devname
|
write_dev "/dev/mapper/$dm_devname"
|
||||||
elif [ "$t" == "origin" ]; then
|
elif [ "$t" == "origin" ]; then
|
||||||
# for origin - prepare snapshot-origin device and store its name
|
# for origin - prepare snapshot-origin device and store its name
|
||||||
dm_devname=origin-$(stat -c '%D:%i' "$base")
|
dm_devname=origin-$(stat -c '%D:%i' "$base")
|
||||||
create_dm_snapshot_origin $dm_devname "$base"
|
create_dm_snapshot_origin "$dm_devname" "$base"
|
||||||
xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
|
xenstore_write "$XENBUS_PATH/node" "/dev/mapper/$dm_devname"
|
||||||
echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
|
echo "/dev/mapper/$dm_devname" > "$HOTPLUG_STORE-node"
|
||||||
write_dev /dev/mapper/$dm_devname
|
write_dev "/dev/mapper/$dm_devname"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
release_lock "block"
|
release_lock "block"
|
||||||
@ -196,7 +196,7 @@ case "$command" in
|
|||||||
claim_lock "block"
|
claim_lock "block"
|
||||||
|
|
||||||
# prepare snapshot device
|
# prepare snapshot device
|
||||||
create_dm_snapshot $dm_devname "$base" "$cow"
|
create_dm_snapshot "$dm_devname" "$base" "$cow"
|
||||||
|
|
||||||
if [ -n "$cow2" ]; then
|
if [ -n "$cow2" ]; then
|
||||||
dm_devname_full=$(get_dm_snapshot_name "$base" "$cow" "$cow2")
|
dm_devname_full=$(get_dm_snapshot_name "$base" "$cow" "$cow2")
|
||||||
@ -210,7 +210,7 @@ case "$command" in
|
|||||||
elif [ "$t" == "origin" ]; then
|
elif [ "$t" == "origin" ]; then
|
||||||
# for origin - prepare snapshot-origin device and store its name
|
# for origin - prepare snapshot-origin device and store its name
|
||||||
dm_devname=origin-$(stat -c '%D:%i' "$base")
|
dm_devname=origin-$(stat -c '%D:%i' "$base")
|
||||||
create_dm_snapshot_origin $dm_devname "$base"
|
create_dm_snapshot_origin "$dm_devname" "$base"
|
||||||
echo "/dev/mapper/$dm_devname"
|
echo "/dev/mapper/$dm_devname"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -223,7 +223,7 @@ case "$command" in
|
|||||||
if [ "$command" = "cleanup" ]; then
|
if [ "$command" = "cleanup" ]; then
|
||||||
t=$2
|
t=$2
|
||||||
else
|
else
|
||||||
t=$(cat $HOTPLUG_STORE-type 2>/dev/null || echo 'MISSING')
|
t=$(cat "$HOTPLUG_STORE-type" 2>/dev/null || echo 'MISSING')
|
||||||
fi
|
fi
|
||||||
case "$t" in
|
case "$t" in
|
||||||
snapshot|origin)
|
snapshot|origin)
|
||||||
@ -245,7 +245,7 @@ case "$command" in
|
|||||||
|
|
||||||
claim_lock "block"
|
claim_lock "block"
|
||||||
|
|
||||||
use_count=$(dmsetup info $node 2>/dev/null|grep Open|awk '{print $3}')
|
use_count=$(dmsetup info "$node" 2>/dev/null|grep Open|awk '{print $3}')
|
||||||
|
|
||||||
if [ -z "$use_count" ]; then
|
if [ -z "$use_count" ]; then
|
||||||
log info "Device $node already removed"
|
log info "Device $node already removed"
|
||||||
@ -254,7 +254,7 @@ case "$command" in
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# 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 -e "/dev/mapper/origin-$(echo $node|cut -d- -f2)" ]; then
|
if [ "${node/snapshot/}" != "$node" ] && [ -e "/dev/mapper/origin-$(echo "$node"|cut -d- -f2)" ]; then
|
||||||
use_count=1
|
use_count=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -265,26 +265,26 @@ case "$command" in
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# get list of used (loop) devices
|
# get list of used (loop) devices
|
||||||
deps="$(dmsetup deps $node -o blkdevname | cut -d: -f2 | sed -e 's#(\([a-z0-9-]\+\))#/dev/\1#g')"
|
deps="$(dmsetup deps "$node" -o blkdevname | cut -d: -f2 | sed -e 's#(\([a-z0-9-]\+\))#/dev/\1#g')"
|
||||||
|
|
||||||
# if this is origin
|
# if this is origin
|
||||||
if [ "${node/origin/}" != "$node" ]; then
|
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 -o blkdevname | cut -d: -f2 |\
|
deps="$deps $(dmsetup deps "$snap" -o blkdevname | cut -d: -f2 |\
|
||||||
sed -e 's#(\([a-z0-9-]\+\))#/dev/\1#g')"
|
sed -e 's#(\([a-z0-9-]\+\))#/dev/\1#g')"
|
||||||
log debug "Removing $snap"
|
log debug "Removing $snap"
|
||||||
dmsetup remove $snap
|
dmsetup remove "$snap"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -e $node ]; then
|
if [ -e "$node" ]; then
|
||||||
log debug "Removing $node"
|
log debug "Removing $node"
|
||||||
dmsetup remove $node
|
dmsetup remove "$node"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# try to free unused devices
|
# try to free unused devices
|
||||||
@ -293,17 +293,17 @@ case "$command" in
|
|||||||
log debug "Removing $dev"
|
log debug "Removing $dev"
|
||||||
case $dev in
|
case $dev in
|
||||||
/dev/loop*)
|
/dev/loop*)
|
||||||
losetup -d $dev 2> /dev/null || true
|
losetup -d "$dev" 2> /dev/null || true
|
||||||
;;
|
;;
|
||||||
/dev/dm-*)
|
/dev/dm-*)
|
||||||
dmsetup remove $dev 2> /dev/null || true
|
dmsetup remove "$dev" 2> /dev/null || true
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -n "$HOTPLUG_STORE" ]; then
|
if [ -n "$HOTPLUG_STORE" ]; then
|
||||||
rm $HOTPLUG_STORE-*
|
rm "$HOTPLUG_STORE-"*
|
||||||
fi
|
fi
|
||||||
release_lock "block"
|
release_lock "block"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user