]>
Commit | Line | Data |
---|---|---|
aaf997b4 MF |
1 | #!/bin/bash |
2 | ||
e34b51f8 MF |
3 | umount_one() { |
4 | mounts=( $(gawk -v p="${root%/}" '$2 ~ "^"p { print $2 }' /proc/mounts | tac) ) | |
5 | ||
6 | if ! ${toplevel} && [[ ${#mounts[@]} -gt 0 ]] ; then | |
7 | if [[ ${mounts[-1]} == "${root%/}" ]] ; then | |
8 | unset "mounts[-1]" | |
9 | fi | |
10 | fi | |
11 | ||
12 | if [[ ${#mounts[@]} -eq 0 ]] ; then | |
13 | if ! ${quiet} ; then | |
14 | echo "No mounts found for ${root}:" | |
15 | mount | |
16 | fi | |
17 | return | |
18 | fi | |
19 | ||
20 | if ! ${quiet} ; then | |
21 | echo "Unmounting: " | |
22 | printf '\t%s\n' "${mounts[@]}" | |
23 | fi | |
24 | ||
25 | if ${prompt} ; then | |
26 | printf "OK? [y/N] " | |
27 | read v | |
28 | else | |
29 | v=y | |
30 | fi | |
31 | flags=() | |
32 | ${quiet} || flags+=( -v ) | |
33 | [[ ${v} == "y" ]] && sudo umount "${flags[@]}" "${mounts[@]}" | |
34 | } | |
35 | ||
36 | main() { | |
37 | if [[ -z $1 ]] ; then | |
38 | echo "Usage: $0 <root>" | |
39 | exit 1 | |
40 | fi | |
41 | ||
42 | prompt=true | |
43 | toplevel=true | |
44 | quiet=false | |
45 | while [[ $# -gt 0 ]] ; do | |
46 | case $1 in | |
47 | -y) prompt=false;; | |
48 | -s) toplevel=false;; | |
49 | -q) quiet=true;; | |
50 | *) break;; | |
51 | esac | |
52 | shift | |
53 | done | |
54 | ||
55 | while [[ $# -gt 0 ]] ; do | |
56 | root=$1 | |
57 | shift | |
58 | umount_one "${root}" | |
59 | done | |
60 | } | |
61 | main "$@" |