Proper origin snapshots destroy. Extended checking for origin snapshot if BE was created for existing snapshot.

This commit is contained in:
Slawomir Wojciech Wojtczak (vermaden) 2012-08-27 09:18:43 +02:00
parent 632714f1dd
commit 72f90612c8
1 changed files with 58 additions and 17 deletions

55
beadm
View File

@ -186,7 +186,7 @@ case ${1} in
| sed '/0$/d;s/K$/ 1/;s/M$/ 1024/;s/G$/ 1048576/;s/T$/ 1073741824/;s/P$/ 1099511627776/;s/E$/ 1125899906842624/;s/Z$/ 1152921504606846976/' \ | sed '/0$/d;s/K$/ 1/;s/M$/ 1024/;s/G$/ 1048576/;s/T$/ 1073741824/;s/P$/ 1099511627776/;s/E$/ 1125899906842624/;s/Z$/ 1152921504606846976/' \
| sed 's/$/~/' ) | sed 's/$/~/' )
echo "${LIST}" \ echo "${LIST}" \
| while read NAME MOUNTPOINT Y m d H M | while read NAME MOUNTPOINT a b d HM Y
do do
TOTAL=0 TOTAL=0
NAME=${NAME##*/} NAME=${NAME##*/}
@ -225,7 +225,7 @@ case ${1} in
printf ("%.1f%s",total,unit); printf ("%.1f%s",total,unit);
}' ) }' )
printf "%-${WIDTH_NAME}s %-6s %-10s %6s %-6s " ${NAME} ${ACTIVE} ${MOUNT} ${TOTAL} "static" printf "%-${WIDTH_NAME}s %-6s %-10s %6s %-6s " ${NAME} ${ACTIVE} ${MOUNT} ${TOTAL} "static"
date -j -f "%a %b %d %H:%M %Y" "${Y} ${m} ${d} ${H} ${M}" +"%Y-%m-%d %H:%M" date -j -f "%a %b %d %H:%M %Y" "${a} ${b} ${d} ${HM} ${y}" +"%Y-%m-%d %H:%M"
done done
;; ;;
@ -236,6 +236,12 @@ case ${1} in
then then
__usage __usage
fi fi
# check if argument for -e option is full path dataset
# argument for -e option must be 'beName' or 'beName@snapshot'
if echo ${3} | grep -q "/" 2> /dev/null
then
__usage
fi
__be_exist ${POOL}/ROOT/${3} __be_exist ${POOL}/ROOT/${3}
if zfs list -H -o name ${POOL}/ROOT/${4} 1> /dev/null 2> /dev/null if zfs list -H -o name ${POOL}/ROOT/${4} 1> /dev/null 2> /dev/null
then then
@ -353,12 +359,19 @@ EOF
;; ;;
(destroy) # ----------------------------------------------------------------- (destroy) # -----------------------------------------------------------------
if [ "${2}" != "-F" ]
then
__be_exist ${POOL}/ROOT/${2}
DESTROY=${2}
else
__be_exist ${POOL}/ROOT/${3}
DESTROY=${3}
fi
case ${#} in case ${#} in
(2) (2)
echo "Are you sure you want to destroy '${2}'?" echo "Are you sure you want to destroy '${2}'?"
echo -n "This action cannot be undone (y/[n]): " echo -n "This action cannot be undone (y/[n]): "
read CHOICE read CHOICE
DESTROY=${2}
;; ;;
(3) (3)
if [ "${2}" != "-F" ] if [ "${2}" != "-F" ]
@ -366,13 +379,11 @@ EOF
__usage __usage
fi fi
CHOICE=Y CHOICE=Y
DESTROY=${3}
;; ;;
(*) (*)
__usage __usage
;; ;;
esac esac
__be_exist ${POOL}/ROOT/${DESTROY}
if [ "${BOOTFS}" = "${POOL}/ROOT/${DESTROY}" ] if [ "${BOOTFS}" = "${POOL}/ROOT/${DESTROY}" ]
then then
echo "ERROR: Cannot destroy active boot environment" echo "ERROR: Cannot destroy active boot environment"
@ -404,8 +415,35 @@ EOF
# get origins used by destroyed boot environment # get origins used by destroyed boot environment
ORIGIN_SNAPSHOTS=$( zfs list -H -t all -o origin -r ${POOL}/ROOT/${DESTROY} | grep -v '^-$' | awk -F "@" '{print $2}' | sort -u ) ORIGIN_SNAPSHOTS=$( zfs list -H -t all -o origin -r ${POOL}/ROOT/${DESTROY} | grep -v '^-$' | awk -F "@" '{print $2}' | sort -u )
fi fi
# check if boot environment was created from existing snapshot
ORIGIN=$( zfs list -H -o origin ${POOL}/ROOT/${DESTROY} )
CREATION=$( zfs list -H -o creation ${POOL}/ROOT/${DESTROY} )
CREATION=$( date -j -f "%a %b %d %H:%M %Y" "${CREATION}" +"%Y-%m-%d-%H:%M" )
SNAPSHOT_NAME=$( echo "${ORIGIN}" | cut -d '@' -f 2 | sed -E 's/:[0-9]{2}$//g' )
if [ "${2}" = "-F" ]
then
CHOICE=1
elif [ "${SNAPSHOT_NAME}" != "${CREATION}" ]
then
ORIGIN=$( basename ${ORIGIN} )
echo "Boot environment '${DESTROY}' was created from existing snapshot"
echo -n "Destroy '${ORIGIN}' snapshot? (y/[n]): "
read CHOICE
case ${CHOICE} in
(Y|y|[Yy][Ee][Ss])
CHOICE=1
;;
(*)
CHOICE=0
echo "Origin snapshot '${ORIGIN}' will be preserved"
;;
esac
else
CHOICE=0
fi
# destroy boot environment # destroy boot environment
zfs destroy -r ${POOL}/ROOT/${DESTROY} zfs destroy -r ${POOL}/ROOT/${DESTROY}
# check if boot environment is a clone
if __be_clone ${POOL}/ROOT/${DESTROY} if __be_clone ${POOL}/ROOT/${DESTROY}
then then
# promote datasets dependent on origins used by destroyed boot environment # promote datasets dependent on origins used by destroyed boot environment
@ -421,6 +459,7 @@ EOF
zfs promote ${I} zfs promote ${I}
done done
done done
fi
# destroy origins used by destroyed boot environment # destroy origins used by destroyed boot environment
SNAPSHOTS=$( zfs list -H -t snapshot -o name ) SNAPSHOTS=$( zfs list -H -t snapshot -o name )
echo "${ORIGIN_SNAPSHOTS}" \ echo "${ORIGIN_SNAPSHOTS}" \
@ -430,10 +469,12 @@ EOF
| grep "@${S}$" \ | grep "@${S}$" \
| while read I | while read I
do do
if [ ${CHOICE} -eq 1 ]
then
zfs destroy ${I} zfs destroy ${I}
done
done
fi fi
done
done
fi fi
echo "Destroyed successfully" echo "Destroyed successfully"
;; ;;