Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active April 2, 2019 23:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/2c78cf54a1e22b6f05270bd3fead8a5c to your computer and use it in GitHub Desktop.
Save smoser/2c78cf54a1e22b6f05270bd3fead8a5c to your computer and use it in GitHub Desktop.
fix lxc ZFS filesystem cannot destroy error.

Fix LXD error when cannot destroy filesystem dataset busy

Sometimes lxd with zfs gets hosed in a way that:

$ lxc delete --force golden-piranha
Error: Failed to destroy ZFS filesystem: cannot destroy 'default/containers/golden-piranha': dataset is busy

The script lxcl-fix-filesystem-busy below helps to fix that.

#!/bin/sh
#
# lxc delete --force b3
# Error: Failed to destroy ZFS filesystem: cannot destroy
# 'default/containers/b3': dataset is busy
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
error() { echo "$@" 1>&2; }
[ "$1" = "--help" -o "$1" = "-h" ] && { echo "give path for dataset"; exit 1; }
[ -n "$1" ] || fail "give path for 'dataset busy'"
path="$1"
out=$(gawk '
BEGINFILE { if (ERRNO != "") nextfile; }
$9 == path {
t=FILENAME;
sub(/\/[^/]*\//, "", t);
sub(/\/.*/, "", t);
printf("%s %s\n", t, $5); nextfile; }' "path=$path" \
/proc/*/mountinfo)
[ -n "$out" ] || { error "no mounts found of $path"; exit 1; }
if [ "$(id -u)" = "0" ]; then
dry=""
else
echo "# dry run, be root"
dry="error"
fi
echo "$out" |
while read pid path; do
grep -q "$path" "/proc/$pid/mountinfo" || {
#error "$pid: already unmounted";
continue;
}
$dry nsenter -t "$pid" -m -- umount "$path" ||
fail "failed nsenter -t $pid -m -- umount $path"
if [ "$dry" = "error" ]; then
# normally just one umount needs doing. so in dry mode just exit.
exit 0
fi
error "umounted $path in $pid"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment