ns_umount_env.awk (829B)
1 #!/usr/bin/awk -f 2 function el_quote(s) { 3 gsub(/\\/, "\\\\", s); # first double all backslashes 4 gsub(/"/, "\\\"", s); # then escape quote marks 5 return "\"" s "\"" # then surround with quote marks 6 } 7 8 BEGIN { 9 # mount IDs seem to be unsigned, so lets use -1 to signify not found 10 max_id = -1 11 root_id = -1 12 count = 0 13 } 14 15 # read in /proc/self/mountinfo 16 $5 == "/" { root_id = $1 } 17 { 18 max_id = max_id < $1 ? $1 : max_id 19 parents[$1] = $2 20 mountpoints[$1] = $5 21 } 22 23 function print_umount(mtp){ 24 print "NS_MTP_" (++count) "=" el_quote(mtp) 25 } 26 27 function recursively_umount(mount_id, id) { 28 for(id=max_id; id>=0; id--){ 29 if(parents[id] == mount_id){ 30 recursively_umount(id) 31 } 32 } 33 print_umount(mountpoints[mount_id]) 34 } 35 36 END{ 37 if(root_id == -1) { exit 111 } 38 print "env" 39 recursively_umount(root_id) 40 print "NS_MTP_COUNT=" count 41 }