Introduce BEADM LIST -A. Add listing mount points in BEADM LIST. Minor fixes.

This commit is contained in:
Slawomir Wojciech Wojtczak (vermaden) 2012-08-29 11:04:03 +02:00
parent 5ad74d72ad
commit f3acd7781b
1 changed files with 85 additions and 40 deletions

125
beadm
View File

@ -163,7 +163,7 @@ fi
BOOTFS=$( zpool list -H -o bootfs ${POOL} ) BOOTFS=$( zpool list -H -o bootfs ${POOL} )
if [ "${BOOTFS}" = "" -o "${BOOTFS}" = "-" ] if [ -z "${BOOTFS}" -o "${BOOTFS}" = "-" ]
then then
echo "ERROR: ZFS boot pool '${POOL}' has unset 'bootfs' property" echo "ERROR: ZFS boot pool '${POOL}' has unset 'bootfs' property"
exit 1 exit 1
@ -172,17 +172,31 @@ fi
case ${1} in case ${1} in
(list) # -------------------------------------------------------------------- (list) # --------------------------------------------------------------------
if [ ${#} -ne 1 ] if [ ${#} -ne 1 -a "${2}" != "-a" ]
then then
__usage __usage
fi fi
BENAME_STARTS_WITH="${POOL}/ROOT" if [ "${2}" = "-a" ]
LIST=$( zfs list -o name,mountpoint,creation -s creation -H -d 1 -r ${POOL}/ROOT | grep -E "^${POOL}/ROOT/" ) then
WIDTH_CREATION=$( echo "${LIST}" | awk '{print $5}' | wc -L ) ZFS_LIST=$( zfs list -o name,mountpoint,used,creation -H -t all -r ${POOL}/ROOT | sed 1d )
WIDTH_NAME=$( echo "${LIST}" | awk '{print $1}' | wc -L ) else
WIDTH_NAME=$(( ${WIDTH_NAME} - ${#BENAME_STARTS_WITH} - 1 )) ZFS_LIST=$( zfs list -o name,mountpoint,used,creation -s creation -H -d 1 -r ${POOL}/ROOT | sed 1d )
printf "%-${WIDTH_NAME}s %-6s %-10s %6s %6s %s\n" \ fi
BE Active Mountpoint Space Policy Created WIDTH_CREATION=$( echo "${ZFS_LIST}" | awk '{print $5}' | wc -L )
WIDTH_NAME=$( echo "${ZFS_LIST}" | awk '{print $1}' | wc -L )
ZFS_MOUNT_LIST=$( zfs mount | grep "^${POOL}/ROOT/" )
if [ "${2}" = "-a" ]
then
WIDTH_MOUNT=$( echo "${ZFS_MOUNT_LIST}" | awk '{print $2}' | wc -L )
else
BENAMES=$( echo "${ZFS_LIST}" | awk '{print $1}' | xargs basename | tr '\n' '|' | sed 's/.$//' )
WIDTH_MOUNT=$( echo "${ZFS_MOUNT_LIST}" | grep -E "(${BENAMES}) " | awk '{print $2}' | wc -L )
WIDTH_NAME=$(( ${WIDTH_NAME} - ${#POOL} - 6 ))
if [ ${WIDTH_MOUNT} -lt 10 ]
then
WIDTH_MOUNT=10
fi
fi
# get list of USEDBYDATASET and USED properties # get list of USEDBYDATASET and USED properties
USED_ALL=$( zfs list -H -t all -o name,usedbydataset,used -r sys/ROOT \ USED_ALL=$( zfs list -H -t all -o name,usedbydataset,used -r sys/ROOT \
| sed 1d \ | sed 1d \
@ -199,17 +213,26 @@ case ${1} in
}' ) }' )
# get the list of names and origins for all boot environments # get the list of names and origins for all boot environments
SNAPSHOT_ALL=$( zfs list -H -t all -o name,origin -d 1 ${POOL}/ROOT ) SNAPSHOT_ALL=$( zfs list -H -t all -o name,origin -d 1 ${POOL}/ROOT )
echo "${LIST}" \ if [ "${2}" = "-a" ]
| while read NAME MOUNTPOINT a b d HM Y then
printf "%-${WIDTH_NAME}s %-6s %-${WIDTH_MOUNT}s %6s %s\n" \
BE/Dataset/Snapshot Active Mountpoint Space Created
else
printf "%-${WIDTH_NAME}s %-6s %-${WIDTH_MOUNT}s %6s %s\n" \
BE Active Mountpoint Space Created
fi
echo "${ZFS_LIST}" \
| while read NAME MOUNTPOINT USED a b d HM Y
do do
TOTAL=0 TOTAL=0
DATASET=${NAME}
NAME=${NAME##*/} NAME=${NAME##*/}
ACTIVE='' ACTIVE=''
if [ "${BENAME_STARTS_WITH}/${NAME}" = "${ROOTFS}" ] if [ "${POOL}/ROOT/${NAME}" = "${ROOTFS}" ]
then then
ACTIVE="${ACTIVE}N" ACTIVE="${ACTIVE}N"
fi fi
if [ "${BENAME_STARTS_WITH}/${NAME}" = "${BOOTFS}" ] if [ "${POOL}/ROOT/${NAME}" = "${BOOTFS}" ]
then then
ACTIVE="${ACTIVE}R" ACTIVE="${ACTIVE}R"
fi fi
@ -217,30 +240,52 @@ case ${1} in
then then
ACTIVE="-" ACTIVE="-"
fi fi
case ${ACTIVE} in if [ "${2}" = "-a" ]
(N|NR) MOUNT="/" ;; then
(*) MOUNT="-" ;; MOUNT=$( echo "${ZFS_MOUNT_LIST}" | grep -m 1 "^${DATASET}" | awk '{print $2}' )
esac else
MOUNT=$( echo "${ZFS_MOUNT_LIST}" | grep -m 1 "^${POOL}/ROOT/${NAME}" | awk '{print $2}' )
fi
if [ -z "${MOUNT}" ]
then
MOUNT="-"
fi
# get the name of origin snapshot for boot environment # get the name of origin snapshot for boot environment
SNAPSHOT=$( echo "${SNAPSHOT_ALL}" | awk "/^${POOL}\/ROOT\/${NAME}\t/" | awk -F '@' '{print $2}' ) SNAPSHOT=$( echo "${SNAPSHOT_ALL}" | awk "/^${POOL}\/ROOT\/${NAME}\t/" | awk -F '@' '{print $2}' )
# calculate space total usage for boot environment if [ "${2}" = "-a" ]
TOTAL=$( echo "${USED_ALL}" \ then
| awk -v name="^${POOL}\/ROOT\/${NAME}" -v snapshot="@${SNAPSHOT}$" \ # use the USED field from ZFS LIST
'BEGIN {total = 0} TOTAL=${USED}
($1 ~ name) {total += $2 * $3} else
($1 ~ snapshot) {total += $4 * $5} # calculate space total usage for boot environment
END { TOTAL=$( echo "${USED_ALL}" \
if (total <= 1024) { unit = "K"; } | awk -v name="^${POOL}\/ROOT\/${NAME}" -v snapshot="@${SNAPSHOT}$" \
else if (total <= 1048576) { total = total / 1024; unit = "M"; } 'BEGIN {total = 0}
else if (total <= 1073741824) { total = total / 1048576; unit = "G"; } ($1 ~ name) {total += $2 * $3}
else if (total <= 1099511627776) { total = total / 1073741824; unit = "T"; } ($1 ~ snapshot) {total += $4 * $5}
else if (total <= 1125899906842624) { total = total / 1099511627776; unit = "P"; } END {
else if (total <= 1152921504606846976) { total = total / 1125899906842624; unit = "E"; } if (total <= 1024) { unit = "K"; }
else { total = total / 1152921504606846976; unit = "Z"; } else if (total <= 1048576) { total = total / 1024; unit = "M"; }
printf ("%.1f%s",total,unit); else if (total <= 1073741824) { total = total / 1048576; unit = "G"; }
}' ) else if (total <= 1099511627776) { total = total / 1073741824; unit = "T"; }
printf "%-${WIDTH_NAME}s %-6s %-10s %6s %-6s " ${NAME} ${ACTIVE} ${MOUNT} ${TOTAL} "static" else if (total <= 1125899906842624) { total = total / 1099511627776; unit = "P"; }
date -j -f "%a %b %d %H:%M %Y" "${a} ${b} ${d} ${HM} ${y}" +"%Y-%m-%d %H:%M" else if (total <= 1152921504606846976) { total = total / 1125899906842624; unit = "E"; }
else { total = total / 1152921504606846976; unit = "Z"; }
printf ("%.1f%s",total,unit);
}' )
fi
if [ "${2}" = "-a" ]
then
if echo "${DATASET}" | grep -v "@" | grep -q "${POOL}/ROOT/${NAME}" 2> /dev/null
then
echo ${NAME}
fi
printf " %-${WIDTH_NAME}s %-6s %-${WIDTH_MOUNT}s %6s " ${DATASET} ${ACTIVE} ${MOUNT} ${TOTAL}
date -j -f "%a %b %d %H:%M %Y" "${a} ${b} ${d} ${HM} ${y}" +"%Y-%m-%d %H:%M"
else
printf "%-${WIDTH_NAME}s %-6s %-${WIDTH_MOUNT}s %6s " ${NAME} ${ACTIVE} ${MOUNT} ${TOTAL}
date -j -f "%a %b %d %H:%M %Y" "${a} ${b} ${d} ${HM} ${y}" +"%Y-%m-%d %H:%M"
fi
done done
;; ;;
@ -302,7 +347,7 @@ case ${1} in
then then
# boot environment is not current root and its mounted # boot environment is not current root and its mounted
echo "ERROR: Boot environment '${2}' is mounted at '${MNT}'" echo "ERROR: Boot environment '${2}' is mounted at '${MNT}'"
echo "ERROR: Cannot activate mounted boot environment" echo "ERROR: Cannot activate manually mounted boot environment"
exit 1 exit 1
fi fi
fi fi
@ -330,7 +375,7 @@ EOF
then then
zfs set canmount=noauto ${POOL}/ROOT/${2} zfs set canmount=noauto ${POOL}/ROOT/${2}
zfs set mountpoint=${TMPMNT} ${POOL}/ROOT/${2} zfs set mountpoint=${TMPMNT} ${POOL}/ROOT/${2}
zfs mount ${POOL}/ROOT/${2} 1> /dev/null 2> /dev/null zfs mount ${POOL}/ROOT/${2}
else else
TMPMNT=${MOUNT} TMPMNT=${MOUNT}
fi fi
@ -340,20 +385,20 @@ EOF
then then
LOADER_CONFIGS="${LOADER_CONFIGS} ${TMPMNT}/boot/loader.conf.local" LOADER_CONFIGS="${LOADER_CONFIGS} ${TMPMNT}/boot/loader.conf.local"
fi fi
sed -i '' -E s/"^vfs.root.mountfrom=.*$"/"vfs.root.mountfrom=\"zfs:${POOL}\/ROOT\/${2##*/}\""/g ${LOADER_CONFIGS} 2> /dev/null sed -i '' -E s/"^vfs.root.mountfrom=.*$"/"vfs.root.mountfrom=\"zfs:${POOL}\/ROOT\/${2##*/}\""/g ${LOADER_CONFIGS}
if [ ${MOUNT} -eq 0 ] if [ ${MOUNT} -eq 0 ]
then then
zfs umount ${POOL}/ROOT/${2} zfs umount ${POOL}/ROOT/${2}
zfs set mountpoint=legacy ${POOL}/ROOT/${2} zfs set mountpoint=legacy ${POOL}/ROOT/${2}
fi fi
fi fi
if ! zpool set bootfs=${POOL}/ROOT/${2} ${POOL} 2> /dev/null if ! zpool set bootfs=${POOL}/ROOT/${2} ${POOL} 1> /dev/null 2> /dev/null
then then
echo "ERROR: Failed to activate '${2}' boot environment" echo "ERROR: Failed to activate '${2}' boot environment"
exit 1 exit 1
fi fi
fi fi
# disable automatic mount on all inactive datasets # disable automatic mount on all inactive boot environments
zfs list -H -o name -r ${POOL}/ROOT \ zfs list -H -o name -r ${POOL}/ROOT \
| grep -v "${POOL}/ROOT/${2}" \ | grep -v "${POOL}/ROOT/${2}" \
| while read I | while read I