#!/usr/bin/awk -f
function el_quote(s) {
RESULT="\"${${${1}//\\/\\\\}//\"/\\\"}\""
gsub(/\\/, "\\\\", s); # first double all backslashes
gsub(/"/, "\\\"", s); # then escape quote marks
return "\"" s "\"" # then surround with quote marks
}
BEGIN {
# check if necessary variables were defined
if(BIN == "") { exit 100 }
if(ROOT == "") { exit 100 }
# quote common stings
IF = el_quote(BIN "/if")
UMOUNT = el_quote(BIN "/busybox") " umount"
# mount IDs seem to be unsigned, so let's use -1 to signify not found
max_id = -1
root_id = -1
}
# read in /proc/self/mountinfo
$5 == "/" { root_id = $1 }
{
max_id = max_id < $1 ? $1 : max_id
parents[$1] = $2
mountpoints[$1] = $5
}
function _if(opts, s) {
return IF opts " { " s " } "
}
function umount(opts, s) {
return UMOUNT opts " " el_quote(ROOT s)
}
function print_umount(mtp){
print _if("", \
_if(" -n -t", umount("", mtp)) \
umount(" -l", mtp) \
)
}
function recursively_umount(mount_id, id) {
for(id=max_id; id>=0; id--){
if(parents[id] == mount_id){
recursively_umount(id)
}
}
print_umount(mountpoints[mount_id])
}
END{
if(root_id == -1) { exit 111 }
recursively_umount(root_id)
}