Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #
- # Author: Peter Maloney
- # License: GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
- # Purpose: list labels and disks in a nicer tabular format than available on FreeBSD
- # Similar to how you can use ls -l on devices in Linux and you see the symlink, so you can figure out what is what, or "blkid".
- # version 2 - to fix a bug, converted old 'print as you go' style to collecting data in hashmaps and an array, and then printing later.
- # glabel list format:
- # Geom name: da0
- # Providers:
- # 1. Name: label/tank1d5
- # 1. Name: da0
- #
- # Geom name: da5p2
- # Providers:
- # 1. Name: gpt/swap4
- # 1. Name: da5p2
- #
- # Geom name: da5p2
- # Providers:
- # 1. Name: gptid/a45616f7-9904-11e1-8814-0025900edbca
- # 1. Name: da5p2
- # Desired format:
- #
- # Device Slice Label gpt gptid
- # da0 - tank1d5 - -
- # da5 - - - -
- # da5 1 - swap4 a45616f7-9904-11e1-8814-0025900edbca
- if [ -z "$debug" ]; then
- debug=0
- fi
- decho() {
- if [ "$debug" = 1 ]; then
- echo "$@"
- fi
- }
- longest() {
- # replace everything with X so uniq kills them and sort will only be based on length
- # the result of this is XXXXX... rather than the longest input
- longest=$(sed -r "s/./X/g" | sort -r | uniq | head -n1)
- # echo the length of longest result
- echo ${#longest}
- }
- longestFile() {
- # length of longest file name in given directory
- dir="$1"
- if [ -e "$dir" ]; then
- ( cd "$dir" ; find * -not -type d ) | longest
- else
- echo 0
- fi
- # #old - non-recursive
- # ls -1 "$1" | longest
- }
- # Find the longest of each, so the columns line up nicely
- deviceMax=$(glabel list | egrep "Geom name:" | longest)
- deviceMax=$(($deviceMax-11))
- labelmax=$(longestFile /dev/label)
- gptmax=$(longestFile /dev/gpt)
- # adjust lengths that are shorter than the header labels (eg. label "" is shorter than "Label")
- if [ "$deviceMax" -lt "6" ]; then
- deviceMax=6
- fi
- if [ "$labelmax" -lt "5" ]; then
- labelmax=5
- fi
- if [ "$gptmax" -lt "5" ]; then
- gptmax=5
- fi
- printfFormat="%-${deviceMax}s %-${labelmax}s %-${gptmax}s %-s\n"
- printf "$printfFormat" Device Label GPT GPTID
- deviceIdx=-1
- devices=()
- declare -A device
- declare -A label
- declare -A gpt
- declare -A gptid
- IFS=$'\n'
- for line in $(glabel list | egrep "Geom name:|Providers:|Name:|^[ ]*\$"); do
- #for line in $(cat glabellist.txt | egrep "Geom name:|Providers:|Name:|^[ ]*\$"); do
- decho "Line: $line"
- field=$(echo "$line" | cut -f1 -d':')
- decho "Field: $field"
- if [ "$field" = "Geom name" ]; then
- # if [ -n "$device" ]; then
- # printf "$printfFormat" "$device" "$label" "$gpt" "$gptid"
- # device=-
- # label=-
- # gpt=-
- # gptid=-
- # fi
- device="${line#*: }"
- duplicate=0
- for d in "${devices[@]}"; do
- if [ "$d" = "$device" ]; then
- duplicate=1
- fi
- done
- if [ "$duplicate" = 0 ]; then
- let deviceIdx++
- devices[$deviceIdx]="$device"
- fi
- continue
- fi
- value[$device]="${line#*: }"
- decho "value is $value"
- if [ "$field" = "1. Name" ]; then
- #echo found $field
- type=$(echo "$value" | cut -d'/' -f1)
- decho "type is $type"
- if [ "$type" = "label" ]; then
- label[$device]="${value#*/}"
- decho "label is $label"
- elif [ "$type" = "gpt" ]; then
- gpt[$device]="${value#*/}"
- decho "gpt is $gpt"
- elif [ "$type" = "gptid" ]; then
- gptid[$device]="${value#*/}"
- decho "gptid is $gptid"
- fi
- fi
- done
- for device in "${devices[@]}"; do
- labelstr="${label[$device]}"
- gptstr="${gpt[$device]}"
- gptidstr="${gptid[$device]}"
- if [ -z "$labelstr" ]; then
- labelstr="-"
- fi
- if [ -z "$gptstr" ]; then
- gptstr="-"
- fi
- if [ -z "$gptidstr" ]; then
- gptidstr="-"
- fi
- printf "$printfFormat" "${device}" "${labelstr}" "${gptstr}" "${gptidstr}"
- device=
- done
Add Comment
Please, Sign In to add comment