Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/gawk -f
- function mount(MNT_PT,MNT_PATH){
- system("mkdir -p " MNT_PT)
- system("mount " MNT_PATH " " MNT_PT 2>/dev/null)
- }
- function mount_if_valid(MNT_PT,MNT_PATH,PROOT,PDRV ,F_MNT_PATH,D_MNT_PATH){
- if (system("[ -f " MNT_PATH " ] && exit 0 || exit 1") == 0){
- F_MNT_PATH=MNT_PATH
- } else if (system("[ -f " PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0){
- F_MNT_PATH=PROOT "/" MNT_PATH
- } else if (system("[ -f " PDRV "/" PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0){
- F_MNT_PATH=PROOT "/" MNT_PATH
- } else if (system("[ -d " MNT_PATH " ] && exit 0 || exit 1") == 0){
- D_MNT_PATH=MNT_PATH
- } else if (system("[ -d " PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0){
- D_MNT_PATH=PROOT "/" MNT_PATH
- } else if (system("[ -d " PDRV "/" PROOT "/" MNT_PATH " ] && exit 0 || exit 1") == 0 ){
- D_MNT_PATH=PROOT "/" MNT_PATH
- } else {
- F_MNT_PATH=""
- D_MNT_PATH=""
- }
- if (length(F_MNT_PATH)>0){
- mount(MNT_PT,F_MNT_PATH)
- if (system("cat /proc/mounts | grep -c -m1 " MNT_PT) > 0){
- result=sucess }
- else {
- result=fail }
- }
- else if (length(D_MNT_PATH)>0) {
- result=directory
- } else {
- result=fail
- }
- return result
- }
- BEGIN {OFS=" "}
- /^\s*$/ {next}
- /==mount==/ { mode=1 }
- /==losetup==/ { mode=2 }
- /==branches==/ { mode=3 }
- {
- if (mode == 1) {
- # get list of mount points, types, and devices - index is $3 (mount points)
- mountdev[$2]=$1
- mounttypes[$2]=$3
- field2[$2]=$3
- } else if (mode == 2) {
- # get list of loop devices and files - index is $1 (loop devs)
- sub(/:/,"",$1)
- sub(/.*[/]/,"",$3); sub(/)/,"",$3)
- loopdev[$1]=$3
- } else if (mode == 3) {
- # map mount types to loop files if mount devices is a loop
- for (m in mountdev) {
- if ( loopdev[mountdev[m]] != "" ){
- BNAME=loopdev[mountdev[m]]
- sub(/.*[/]/,"",BNAME)
- field2[m]=BNAME
- mountpath[m]=loopdev[mountdev[m]]
- system("echo 'BNAME=" BNAME " m=" m "' > /dev/stderr")
- }
- }
- # for (m in mountdev) print m " on " mountdev[m] " type " mounttypes[m]
- mode=4
- } else if (mode==4) {
- # print the branches and its mappings
- system( "echo 'AWK (mode==4):" $0 "' >/dev/stderr" )
- if ( !($1 in field2)){
- MNT_PT=$1
- MNT_PATH=$2
- if (length(MNT_PATH)==0){
- MNT_PATH=$1
- }
- mountpath[$1]=MNT_PATH
- if (length(MNT_PATH) ==0){
- next
- }
- mnt_status=mount_if_valid(MNT_PT,MNT_PATH,PDRV)
- if(mnt_status == "fail"){
- next
- }
- BNAME=MNT_PATH
- sub(/.*[/]/,"",BNAME)
- field2[MNT_PT]=BNAME
- }
- if (NF>2){
- STATE=$3
- } else {
- STATE="on"
- }
- if ("$STATE" !~ /(on|off)/){
- STATE="on"
- }
- start=length($1)-MAX_STR_LEN
- if (start<1) start=1
- #field1[$1]=substr($1,start)
- field1[$1]=$1
- #out1=field1[$1] " " field2[$1] " " STATE
- #out2=$1 " " mounttypes[$1] " " mountpath[$1]
- #print out1
- #print out2 > OUTFILE
- print field1[$1],field2[$1],STATE
- print field1[$1],field2[$1],STATE,$1,mounttypes[$1],mountpath[$1] > OUTFILE
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement