mrrl-containers

MRRL version of container scripts
git clone https://ccx.te2000.cz/git/mrrl-containers
Log | Files | Refs

ns_umount_script.awk (1195B)


      1 #!/usr/bin/awk -f
      2 function el_quote(s) {
      3 	RESULT="\"${${${1}//\\/\\\\}//\"/\\\"}\""
      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 	# check if necessary variables were defined
     11 	if(BIN == "") { exit 100 }
     12 	if(ROOT == "") { exit 100 }
     13 
     14 	# quote common stings
     15 	IF = el_quote(BIN "/if")
     16 	UMOUNT = el_quote(BIN "/busybox") " umount"
     17 
     18 	# mount IDs seem to be unsigned, so let's use -1 to signify not found
     19 	max_id = -1
     20 	root_id = -1
     21 }
     22 
     23 # read in /proc/self/mountinfo
     24 $5 == "/" { root_id = $1 }
     25 {
     26 	max_id = max_id < $1 ? $1 : max_id
     27 	parents[$1] = $2
     28 	mountpoints[$1] = $5
     29 }
     30 
     31 function _if(opts, s) {
     32 	return IF opts " { " s " } "
     33 }
     34 
     35 function umount(opts, s) {
     36 	return UMOUNT opts " " el_quote(ROOT s)
     37 }
     38 
     39 function print_umount(mtp){
     40 	print _if("", \
     41 	      _if(" -n -t",  umount("", mtp)) \
     42 	      umount(" -l", mtp) \
     43       )
     44 }
     45 
     46 function recursively_umount(mount_id,    id) {
     47 	for(id=max_id; id>=0; id--){
     48 		if(parents[id] == mount_id){
     49 			recursively_umount(id)
     50 		}
     51 	}
     52 	print_umount(mountpoints[mount_id])
     53 }
     54 
     55 END{
     56 	if(root_id == -1) { exit 111 }
     57 	recursively_umount(root_id)
     58 }