Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Perform a bind mount, copying existing files as we do so to ensure the |
| 4 | # overlaid path has the necessary content. |
| 5 | |
| 6 | if [ $# -lt 2 ]; then |
| 7 | echo >&2 "Usage: $0 spec mountpoint [OPTIONS]" |
| 8 | exit 1 |
| 9 | fi |
| 10 | |
| 11 | spec=$1 |
| 12 | mountpoint=$2 |
| 13 | |
| 14 | if [ $# -gt 2 ]; then |
| 15 | options=$3 |
| 16 | else |
| 17 | options= |
| 18 | fi |
| 19 | |
| 20 | [ -n "$options" ] && options=",$options" |
| 21 | |
| 22 | mkdir -p "${spec%/*}" |
| 23 | if [ -d "$mountpoint" ]; then |
| 24 | if [ ! -d "$spec" ]; then |
| 25 | mkdir "$spec" |
| 26 | cp -pPR "$mountpoint"/. "$spec/" |
| 27 | fi |
| 28 | elif [ -f "$mountpoint" ]; then |
| 29 | if [ ! -f "$spec" ]; then |
| 30 | cp -pP "$mountpoint" "$spec" |
| 31 | fi |
| 32 | fi |
| 33 | |
| 34 | mount -o "bind$options" "$spec" "$mountpoint" |