peetaur

FreeBSD listlabels.bash

Jun 25th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.59 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Author: Peter Maloney
  4. # License: GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
  5. # Purpose: list labels and disks in a nicer tabular format than available on FreeBSD
  6. #   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".
  7. # 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.
  8.  
  9.  
  10. # glabel list format:
  11. # Geom name: da0
  12. # Providers:
  13. # 1. Name: label/tank1d5
  14. # 1. Name: da0
  15. #
  16. # Geom name: da5p2
  17. # Providers:
  18. # 1. Name: gpt/swap4
  19. # 1. Name: da5p2
  20. #
  21. # Geom name: da5p2
  22. # Providers:
  23. # 1. Name: gptid/a45616f7-9904-11e1-8814-0025900edbca
  24. # 1. Name: da5p2
  25.  
  26.                                                                                                                                                                                              
  27. # Desired format:                                                                                                                                                                            
  28. #                                                                                                                                                                                            
  29. # Device     Slice    Label          gpt         gptid                                                                                                                                        
  30. # da0        -        tank1d5        -           -                                                                                                                                            
  31. # da5        -        -              -           -                                                                                                                                            
  32. # da5        1        -              swap4       a45616f7-9904-11e1-8814-0025900edbca                                                                                                        
  33.                                                                                                                                                                                              
  34. if [ -z "$debug" ]; then                                                                                                                                                                      
  35.     debug=0
  36. fi
  37.  
  38. decho() {
  39.     if [ "$debug" = 1 ]; then
  40.         echo "$@"
  41.     fi
  42. }
  43.  
  44. longest() {
  45.     # replace everything with X so uniq kills them and sort will only be based on length
  46.     # the result of this is XXXXX... rather than the longest input
  47.     longest=$(sed -r "s/./X/g" | sort -r | uniq | head -n1)
  48.    
  49.     # echo the length of longest result
  50.     echo ${#longest}
  51. }
  52.  
  53. longestFile() {
  54.     # length of longest file name in given directory
  55.     dir="$1"
  56.     if [ -e "$dir" ]; then
  57.         ( cd "$dir" ; find * -not -type d ) | longest
  58.     else
  59.         echo 0
  60.     fi
  61.  
  62.     # #old - non-recursive
  63.     # ls -1 "$1" | longest
  64. }
  65.  
  66. # Find the longest of each, so the columns line up nicely
  67. deviceMax=$(glabel list | egrep "Geom name:" | longest)
  68. deviceMax=$(($deviceMax-11))
  69. labelmax=$(longestFile /dev/label)
  70. gptmax=$(longestFile /dev/gpt)
  71.  
  72. # adjust lengths that are shorter than the header labels (eg. label "" is shorter than "Label")
  73. if [ "$deviceMax" -lt "6" ]; then
  74.     deviceMax=6
  75. fi
  76. if [ "$labelmax" -lt "5" ]; then
  77.     labelmax=5
  78. fi
  79. if [ "$gptmax" -lt "5" ]; then
  80.     gptmax=5
  81. fi
  82.  
  83. printfFormat="%-${deviceMax}s %-${labelmax}s %-${gptmax}s %-s\n"
  84. printf "$printfFormat" Device Label GPT GPTID
  85.  
  86. deviceIdx=-1
  87. devices=()
  88. declare -A device
  89. declare -A label
  90. declare -A gpt
  91. declare -A gptid
  92.  
  93. IFS=$'\n'
  94. for line in $(glabel list | egrep "Geom name:|Providers:|Name:|^[ ]*\$"); do
  95. #for line in $(cat glabellist.txt | egrep "Geom name:|Providers:|Name:|^[ ]*\$"); do
  96.     decho "Line: $line"
  97.     field=$(echo "$line" | cut -f1 -d':')
  98.     decho "Field: $field"
  99.     if [ "$field" = "Geom name" ]; then
  100. #        if [ -n "$device" ]; then
  101. #            printf "$printfFormat" "$device" "$label" "$gpt" "$gptid"
  102. #            device=-
  103. #            label=-
  104. #            gpt=-
  105. #            gptid=-
  106. #        fi
  107.        
  108.         device="${line#*: }"
  109.         duplicate=0
  110.         for d in "${devices[@]}"; do
  111.             if [ "$d" = "$device" ]; then
  112.                 duplicate=1
  113.             fi
  114.         done
  115.         if [ "$duplicate" = 0 ]; then
  116.             let deviceIdx++
  117.             devices[$deviceIdx]="$device"
  118.         fi
  119.         continue
  120.     fi
  121.     value[$device]="${line#*: }"
  122.     decho "value is $value"
  123.     if [ "$field" = "1. Name" ]; then
  124.         #echo found $field
  125.         type=$(echo "$value" | cut -d'/' -f1)
  126.        
  127.         decho "type is $type"
  128.         if [ "$type" = "label" ]; then
  129.             label[$device]="${value#*/}"
  130.             decho "label is $label"
  131.         elif [ "$type" = "gpt" ]; then
  132.             gpt[$device]="${value#*/}"
  133.             decho "gpt is $gpt"
  134.         elif [ "$type" = "gptid" ]; then
  135.             gptid[$device]="${value#*/}"
  136.             decho "gptid is $gptid"
  137.         fi
  138.     fi
  139. done
  140.  
  141.  
  142. for device in "${devices[@]}"; do
  143.     labelstr="${label[$device]}"
  144.     gptstr="${gpt[$device]}"
  145.     gptidstr="${gptid[$device]}"
  146.     if [ -z "$labelstr" ]; then
  147.         labelstr="-"
  148.     fi
  149.     if [ -z "$gptstr" ]; then
  150.         gptstr="-"
  151.     fi
  152.     if [ -z "$gptidstr" ]; then
  153.         gptidstr="-"
  154.     fi
  155.     printf "$printfFormat" "${device}" "${labelstr}" "${gptstr}" "${gptidstr}"
  156.     device=
  157. done
Add Comment
Please, Sign In to add comment