ccx-utils

Miscellaneous utilities written in C
git clone https://ccx.te2000.cz/git/ccx-utils
Log | Files | Refs

ns_mounts_to_env (908B)


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