Advertisement
s243a

sandbox.awk

Jan 21st, 2020
3,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 3.58 KB | None | 0 0
  1. #!/usr/bin/gawk -f
  2.     function mount(MNT_PT,MNT_PATH){
  3.       system("mkdir -p " MNT_PT)
  4.       system("mount " MNT_PATH " " MNT_PT 2>/dev/null)
  5.     }
  6.     function mount_if_valid(MNT_PT,MNT_PATH,PROOT,PDRV                  ,F_MNT_PATH,D_MNT_PATH){
  7.           if (system("[ -f " MNT_PATH " ] && exit 0 || exit 1") == 0){
  8.             F_MNT_PATH=MNT_PATH
  9.           } else if (system("[ -f " PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0){
  10.            F_MNT_PATH=PROOT "/" MNT_PATH
  11.          } else if (system("[ -f " PDRV "/" PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0){
  12.            F_MNT_PATH=PROOT "/" MNT_PATH
  13.           } else if (system("[ -d " MNT_PATH " ] && exit 0 || exit 1") == 0){
  14.             D_MNT_PATH=MNT_PATH
  15.          } else if (system("[ -d " PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0){
  16.            D_MNT_PATH=PROOT "/" MNT_PATH
  17.          } else if (system("[ -d " PDRV "/" PROOT "/" MNT_PATH " ]  && exit 0 || exit 1") == 0 ){
  18.            D_MNT_PATH=PROOT "/" MNT_PATH
  19.          } else {
  20.            F_MNT_PATH=""
  21.            D_MNT_PATH=""
  22.         }
  23.         if (length(F_MNT_PATH)>0){
  24.             mount(MNT_PT,F_MNT_PATH)
  25.             if (system("cat /proc/mounts | grep -c -m1 " MNT_PT) > 0){
  26.               result=sucess }
  27.             else {
  28.               result=fail }
  29.         }
  30.         else if (length(D_MNT_PATH)>0) {
  31.            result=directory
  32.         } else {
  33.           result=fail
  34.         }
  35.         return result    
  36.     }
  37.     BEGIN {OFS=" "}
  38.     /^\s*$/ {next}
  39.     /==mount==/ { mode=1 }
  40.     /==losetup==/ { mode=2 }
  41.     /==branches==/ { mode=3 }
  42.     {
  43.     if (mode == 1) {
  44.       # get list of mount points, types, and devices - index is $3 (mount points)
  45.       mountdev[$2]=$1
  46.       mounttypes[$2]=$3
  47.      
  48.       field2[$2]=$3
  49.     } else if (mode == 2) {
  50.       # get list of loop devices and files - index is $1 (loop devs)
  51.       sub(/:/,"",$1)
  52.       sub(/.*[/]/,"",$3); sub(/)/,"",$3)
  53.       loopdev[$1]=$3
  54.     } else if (mode == 3) {
  55.       # map mount types to loop files if mount devices is a loop
  56.       for (m in mountdev) {
  57.         if ( loopdev[mountdev[m]] != "" ){
  58.              BNAME=loopdev[mountdev[m]]
  59.              sub(/.*[/]/,"",BNAME)    
  60.              field2[m]=BNAME
  61.              mountpath[m]=loopdev[mountdev[m]]
  62.              system("echo 'BNAME=" BNAME " m=" m "' > /dev/stderr")
  63.         }
  64.        
  65.       }
  66.       # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
  67.       mode=4
  68.     } else if (mode==4) {
  69.       # print the branches and its mappings
  70.       system( "echo 'AWK (mode==4):" $0 "' >/dev/stderr" )      
  71.       if ( !($1 in field2)){
  72.         MNT_PT=$1
  73.         MNT_PATH=$2
  74.         if (length(MNT_PATH)==0){
  75.           MNT_PATH=$1
  76.         }
  77.         mountpath[$1]=MNT_PATH
  78.        
  79.         if (length(MNT_PATH) ==0){
  80.            next
  81.         }
  82.         mnt_status=mount_if_valid(MNT_PT,MNT_PATH,PDRV)
  83.         if(mnt_status == "fail"){
  84.             next
  85.         }
  86.         BNAME=MNT_PATH
  87.         sub(/.*[/]/,"",BNAME)    
  88.          field2[MNT_PT]=BNAME
  89.       }
  90.        if (NF>2){
  91.           STATE=$3
  92.        } else {
  93.             STATE="on"
  94.        }      
  95.        if ("$STATE" !~ /(on|off)/){
  96.            STATE="on"
  97.        }        
  98.        start=length($1)-MAX_STR_LEN
  99.        if (start<1) start=1
  100.        #field1[$1]=substr($1,start)
  101.        field1[$1]=$1
  102.          #out1=field1[$1] " " field2[$1] " " STATE
  103.          #out2=$1 " " mounttypes[$1] " " mountpath[$1]
  104.          #print out1
  105.          #print out2 > OUTFILE
  106.        print field1[$1],field2[$1],STATE
  107.        print field1[$1],field2[$1],STATE,$1,mounttypes[$1],mountpath[$1] > OUTFILE
  108.     }
  109.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement