Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/awk -f
- # Input disklabel file, output disklabel file but w/ # 8142.32 - 8238.79 mib
- # instead of # (Cyl. 16543*- 16739*)
- # The 1st part of file upto and including the line "N partitions:" is passed
- # thru to newfile unchanged, from that point on, only lines that
- # match partition ( or slice ) definitions will be written to the newfile
- # how the fields are spaced, or formatted before the comment, will remain
- # the same in the newfile, except any comment will be stripped off
- # and a comment describing what megabyte the slice would be in, is added.
- # With v option , adds additional informative comments after each
- # slice definition, with % of diskused, and size and offset in mib.
- # plus adds a comment describing the fields after the partition line.
- # With x option, performs some calculus on lines that match ' a+'
- # etc, these lines are saved when they are encountered, and processed
- # and inserted (out of place ) at the end of the newfile, as " a:"
- # with the fields translated. Data items from slice definitions can
- # be refered to by their 2 letter disktab variable name, for example
- # e+ pa oa ta fa ba 0
- # could be replaced with a's size, offset, type, fragment, and blocksize
- # in the size field a number with an "m" or a "g" suffix will be
- # replaced with that number, but, in 512 byte sectors.
- # in the offset place, a single letter will be replaced with a offset
- # that would have it start immediately after the referenced partition
- # again in the size field something like "-c", will be replaced with
- # a size would fill in space to that point.
- # If given an input file that isnt a disk label, it will likely
- # need to be CTRL-C'ed, as it will go into an infinite loop looking
- # for a partition: line.
- # Successive runs on the same file, don't add progressively more
- # and more lines, and running with no args against, an output file
- # should clean it up, removing all comment cruft added.
- BEGIN {
- options="xv"
- #options=""
- #print "*** begin"
- looping = 1
- BytesPerSec = 512 # we could read this from file
- while( looping ) { # read 1st part of file
- getline
- print
- if ( match($0,"^[0-9]+ partitions:")) {
- looping = 0
- }
- if ( match($0,"^bytes/sector: [0-9]*$")) {
- #print "*** match bytes/sector"
- i = index($0,": ") # going to leave this unfinished
- }
- if ( match($0,"^total sectors: [0-9]*$")) {
- #print "*** match total sectors"
- i = index($0,": ") # ignoring case with comment on this line
- totstring = substr($0, i+2)
- #print totstring
- totsec = + totstring
- totmib = totsec / 2048.0
- }
- }
- #print totmib
- if ( totmib <= 0 )
- exit
- i=index($0," ") # still holds partition line
- #print i
- partstring = substr($0, 1,i-1)
- numpart = + partstring
- #print numpart
- if ( numpart <= 0 )
- exit
- numspec = 0
- if ( index(options,"v") )
- print "#L:######size####offset#######type###-f####-b#####-a###"
- }
- END {
- if ( index(options,"v") ) {
- printf("# total %.3f GB or %.3f MB\n", totmib/1024.0, totmib)
- printf("# %d partitions specified\n",numspec)
- printf("# maxmax partitions = %d\n",22)
- "sysctl kern.maxpartitions" | getline maxstring
- printf("# %s\n",maxstring)
- }
- #for ( item in disktab )
- # print "** " item, disktab[item]
- if ( index(options,"x") )
- for ( item in calctab )
- docalc(item,calctab[item])
- }
- /^ [a-z]:/{
- #chop off the comment
- i = index($0,"#")
- if ( i > 1 ) {
- $0 = substr($0,1,i)
- } else
- $0 = $0 "# "
- ORS=" "
- print
- ORS="\n"
- print mib($3) " - " mib($3+$2) " mib"
- ++numspec
- }
- function mib(blocks){
- return ( (+blocks)/ 2048 )
- }
- function cent(blocks){
- return ( ((+blocks) * 100.0 ) / totsec )
- }
- /^ [a-z]:/{
- if ( index(options,"v") ) {
- #OFS = "\t"
- OFMT = "%.2f"
- printf("# %2.2f%% = ", cent($2))
- print mib($2) "mb","+" mib($3) "mb"
- }
- }
- /^ [a-ce-z]:/{
- item = "p" substr($1,1,1)
- disktab[item]= $2 # size
- item = "o" substr($1,1,1)
- disktab[item]= $3 # offset
- item = "t" substr($1,1,1)
- disktab[item]= $4 # type
- item = "f" substr($1,1,1)
- disktab[item]= $5 # -f
- item = "b" substr($1,1,1)
- disktab[item]= $6 # -b
- }
- /^ d:/{
- disktab["su"]=totsec
- disktab["se"]=BytesPerSec
- disktab["dp"]=totsec
- disktab["do"]="0"
- }
- /^ [a-z]\+/{ # save lines to process later , a+
- slice = substr($1,1,1)
- calctab[slice]=$0
- }
- function docalc(slice,line){
- split(line,field)
- ORS=" "
- print " " slice ": "
- if ( match( field[2],"^[0-9]+m$" )) {
- i = index( field[2], "m")
- f = substr( field[2], 1, i-1)
- field[2] = (+f) * 2048
- } else if ( match(field[2],"^[0-9]+g$" )) {
- i= index( field[2],"g")
- f = substr( field[2], 1, i-1)
- field[2] = (+f) * 2048 * 1024
- } else if ( match(field[2],"^-[a-z]$" )) {
- #print "*** match -letter fill in partition"
- L = substr(field[2],2,1)
- f = "o" slice
- g = "o" L
- o = (+disktab[f])
- oo = (+disktab[g])
- f = "p" slice
- g = "p" L
- p = (+disktab[f])
- pp = (+disktab[g])
- # two cases slice is either lies inside of partition
- if ( (o >= oo || slice == "c" ) && o <= ( oo + pp ) )
- field[2] = (( ( oo + pp ) - 1 ) - o )
- else { # or, slice lies outside of it, so should be before it
- print "*oo*" oo "*o*" o "**"
- field[2] = (( oo - 1 ) - o )
- }
- } else
- field[2] = anyfield(field[2])
- print field[2] #size
- if ( match(field[3], "^[a-z]$" )) { # offset + size of referenced slice
- #print "*** match field 3 single letter"
- f = "o" field[3]
- g = "p" field[3]
- if ( (f in disktab) && (g in disktab) ) {
- #print "*** both in tab"
- o = (+disktab[f])
- s = (+disktab[g])
- field[3] = o + s # offset + size
- } else
- field[3] = "???"
- } else
- field[3] = anyfield(field[3])
- print " " field[3] #offset
- field[4] = anyfield(field[4])
- print " " field[4] #type
- field[5] = anyfield(field[5])
- print " " field[5] #fragment
- field[6] = anyfield(field[6])
- print " " field[6] #block
- #ORS="\n"
- print " " "0 "
- # it would be better to print mib comments here as well
- ORS="\n"
- print "# " mib(field[3]) " - " mib(field[3]+field[2]) " mib"
- }
- function anyfield(s){
- if (s in disktab)
- return disktab[s]
- else
- return s
- }
Add Comment
Please, Sign In to add comment