shiftdot515

dl.awk

Apr 12th, 2020
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 6.33 KB | None | 0 0
  1. #!/usr/bin/awk -f
  2. # Input disklabel file, output disklabel file but w/ # 8142.32 - 8238.79 mib
  3. # instead of # (Cyl.  16543*-  16739*)
  4. # The 1st part of file upto and including the line "N partitions:" is passed
  5. # thru to newfile unchanged, from that point on, only lines that
  6. # match partition ( or slice ) definitions will be written to the newfile
  7. # how the fields are spaced, or formatted before the comment, will remain
  8. # the same in the newfile, except any comment will be stripped off
  9. # and a comment describing what megabyte the slice would be in, is added.
  10. # With  v option , adds additional informative comments after each
  11. # slice definition, with % of diskused, and size and offset in mib.
  12. # plus adds a comment describing the fields after the partition line.
  13. # With  x option, performs some calculus on lines that match ' a+'
  14. # etc, these lines are saved when they are encountered, and processed
  15. # and inserted (out of place )  at the end of the newfile, as " a:"
  16. # with the fields translated.  Data items from slice definitions can
  17. # be refered to by their 2 letter disktab variable name, for example
  18. #  e+   pa    oa    ta     fa    ba 0
  19. # could be replaced with a's size, offset, type, fragment, and blocksize
  20. # in the size field a number with an "m" or a "g" suffix will be
  21. # replaced with that number, but, in 512 byte sectors.
  22. # in the offset place, a single letter will be replaced with a offset
  23. # that would have it start immediately after the referenced partition
  24. # again in the size field something like "-c", will be replaced with
  25. # a size would fill in space to that point.  
  26. # If given an input file that isnt a disk label, it will likely
  27. # need to be CTRL-C'ed, as it will go into an infinite loop looking
  28. # for a partition: line.  
  29. # Successive runs on the same file, don't add progressively more
  30. # and more lines, and running with no args against, an output file
  31. # should clean it up, removing all comment cruft added.
  32. BEGIN {
  33.   options="xv"
  34.   #options=""
  35.   #print "*** begin"
  36.   looping = 1
  37.   BytesPerSec = 512 # we could read this from file
  38.   while( looping ) { #  read 1st part of file
  39.     getline
  40.     print
  41.     if ( match($0,"^[0-9]+ partitions:")) {
  42.         looping = 0
  43.     }
  44.         if ( match($0,"^bytes/sector: [0-9]*$")) {
  45.     #print "*** match bytes/sector"
  46.         i = index($0,": ") # going to leave this unfinished
  47.     }
  48.     if ( match($0,"^total sectors: [0-9]*$")) {
  49.         #print "*** match total sectors"
  50.         i = index($0,": ") # ignoring case with comment on this line
  51.         totstring = substr($0, i+2)
  52.         #print totstring
  53.     totsec = + totstring
  54.         totmib = totsec / 2048.0
  55.         }
  56.   }
  57.  #print totmib
  58.  if ( totmib <= 0 )
  59.   exit
  60.  i=index($0," ") # still holds partition line
  61.  #print i
  62.  partstring = substr($0, 1,i-1)
  63.  numpart = + partstring
  64.  #print numpart
  65.  if ( numpart <= 0 )
  66.   exit  
  67.  numspec = 0
  68.  if ( index(options,"v") )
  69.    print "#L:######size####offset#######type###-f####-b#####-a###"
  70. }  
  71. END {
  72.  if ( index(options,"v") ) {
  73.     printf("# total %.3f GB or %.3f MB\n", totmib/1024.0, totmib)
  74.     printf("# %d partitions specified\n",numspec)
  75.     printf("# maxmax partitions = %d\n",22)
  76.     "sysctl kern.maxpartitions" | getline maxstring
  77.     printf("# %s\n",maxstring)
  78.  }
  79.  #for ( item in disktab )
  80.  #   print "** " item, disktab[item]
  81.  if ( index(options,"x") )
  82.     for ( item in calctab )
  83.         docalc(item,calctab[item])
  84. }
  85. /^ [a-z]:/{
  86.  #chop off the comment
  87.  i = index($0,"#")
  88.  if ( i > 1 ) {
  89.   $0 = substr($0,1,i)
  90.  } else
  91.   $0 = $0 "# "
  92.  ORS=" "
  93.  print
  94.  ORS="\n"
  95.  print mib($3) " - " mib($3+$2) " mib"
  96.  ++numspec
  97. }
  98. function mib(blocks){
  99.  
  100.  return ( (+blocks)/ 2048 )
  101.  
  102. }
  103. function cent(blocks){
  104.  return ( ((+blocks) * 100.0 ) / totsec )
  105. }
  106.  
  107. /^ [a-z]:/{
  108.   if ( index(options,"v") ) {
  109.      #OFS = "\t"
  110.      OFMT = "%.2f"
  111.      printf("# %2.2f%% = ", cent($2))
  112.      print mib($2) "mb","+" mib($3) "mb"
  113.   }
  114. }
  115. /^ [a-ce-z]:/{
  116.   item = "p" substr($1,1,1)
  117.   disktab[item]= $2 # size
  118.   item = "o" substr($1,1,1)
  119.   disktab[item]= $3 # offset  
  120.   item = "t" substr($1,1,1)
  121.   disktab[item]= $4 # type
  122.   item = "f" substr($1,1,1)
  123.   disktab[item]= $5 # -f
  124.   item = "b" substr($1,1,1)
  125.   disktab[item]= $6 # -b
  126. }
  127. /^ d:/{
  128.   disktab["su"]=totsec
  129.   disktab["se"]=BytesPerSec
  130.   disktab["dp"]=totsec
  131.   disktab["do"]="0"
  132. }
  133. /^ [a-z]\+/{  # save lines to process later , a+
  134.   slice = substr($1,1,1)
  135.   calctab[slice]=$0
  136. }
  137. function docalc(slice,line){
  138.  split(line,field)
  139.  ORS=" "  
  140.  print " " slice ": "
  141.  if ( match( field[2],"^[0-9]+m$" )) {
  142.    i = index( field[2], "m")
  143.    f = substr( field[2], 1, i-1)
  144.    field[2] = (+f) * 2048
  145.  } else if ( match(field[2],"^[0-9]+g$" )) {
  146.    i= index( field[2],"g")  
  147.    f = substr( field[2], 1, i-1)
  148.    field[2] = (+f) * 2048 * 1024
  149.  } else if ( match(field[2],"^-[a-z]$" )) {
  150.    #print "*** match -letter fill in partition"
  151.    L = substr(field[2],2,1)
  152.    f = "o" slice
  153.    g = "o" L
  154.    o =  (+disktab[f])
  155.    oo = (+disktab[g])
  156.    f = "p" slice
  157.    g = "p" L
  158.    p =  (+disktab[f])
  159.    pp = (+disktab[g])
  160.    # two cases slice is either lies inside of partition
  161.    if (  (o >= oo || slice == "c" ) && o <= ( oo + pp ) )
  162.        field[2] = (( ( oo + pp  ) - 1 ) - o )
  163.    else {  # or, slice lies outside of it, so should be before it
  164.        print "*oo*" oo "*o*" o "**"
  165.        field[2] = (( oo - 1 ) - o )
  166.    }
  167.  } else
  168.       field[2] = anyfield(field[2])
  169.  print      field[2]           #size  
  170.  
  171.  if ( match(field[3], "^[a-z]$" )) { # offset + size of referenced slice
  172.     #print "*** match field 3 single letter"
  173.     f = "o" field[3]
  174.     g = "p" field[3]
  175.     if ( (f in disktab) && (g in disktab) ) {  
  176.       #print "*** both in tab"
  177.       o = (+disktab[f])
  178.       s = (+disktab[g])
  179.       field[3] = o + s  #  offset + size
  180.    } else
  181.       field[3] = "???"
  182.  } else
  183.     field[3] = anyfield(field[3])
  184.  print "  " field[3]           #offset
  185.  
  186.  field[4] = anyfield(field[4])
  187.  print "  " field[4]           #type
  188.  field[5] = anyfield(field[5])
  189.  print "  " field[5]           #fragment
  190.  field[6] = anyfield(field[6])
  191.  print "  " field[6]           #block
  192.  #ORS="\n"
  193.  print " " "0 "
  194.  # it would be better to print mib comments here as well
  195.  ORS="\n"
  196.  print "# "  mib(field[3]) " - " mib(field[3]+field[2]) " mib"
  197. }
  198. function anyfield(s){
  199.   if (s in disktab)
  200.    return disktab[s]  
  201.   else
  202.    return s
  203. }
Add Comment
Please, Sign In to add comment