diff options
-rw-r--r-- | Makefile | 6 | ||||
-rwxr-xr-x | btrfs_mount | 74 |
2 files changed, 80 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..645e09a --- /dev/null +++ b/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | all: | ||
2 | |||
3 | sudo = $(shell [ $$(id -u) = 0 ] || echo sudo) | ||
4 | |||
5 | install: | ||
6 | $(sudo) install btrfs_mount -t /sbin | ||
diff --git a/btrfs_mount b/btrfs_mount new file mode 100755 index 0000000..50303d1 --- /dev/null +++ b/btrfs_mount | |||
@@ -0,0 +1,74 @@ | |||
1 | #!/bin/dash | ||
2 | |||
3 | usage() | ||
4 | { | ||
5 | echo "usage: $0 [--unscan] [btrfs options] <btrfs filesystem> <mountpoint>" | ||
6 | } | ||
7 | |||
8 | get_prop() | ||
9 | { | ||
10 | local prop="$1" | ||
11 | btrfs inspect-internal dump-super "$2" | sed -E -ne "s/^${prop} +//p" | ||
12 | } | ||
13 | |||
14 | device_count() | ||
15 | { | ||
16 | btrfs inspect-internal dump-super "$1" | grep -c '^dev_item.devid ' | ||
17 | } | ||
18 | |||
19 | competing_devices() | ||
20 | { | ||
21 | local uuid | ||
22 | uuid=$1 | ||
23 | btrfs filesystem show "$uuid" | sed -E -ne 's/^ devid.* //p' | ||
24 | } | ||
25 | |||
26 | remove_competition() | ||
27 | { | ||
28 | competing_devices "$1" | while read dev | ||
29 | do | ||
30 | [ "$dev" = "$source" ] || btrfs device scan -u "$dev" || true | ||
31 | done | ||
32 | } | ||
33 | |||
34 | getend() | ||
35 | { | ||
36 | eval "echo \${$(($#-$1))}" | ||
37 | } | ||
38 | |||
39 | btrfs_mount() | ||
40 | { | ||
41 | [ $# -ge 2 ] || { usage; exit 1; } | ||
42 | |||
43 | destdir=$(getend 0 "$@") | ||
44 | source=$(getend 1 "$@") | ||
45 | |||
46 | [ -e "$source" ] | ||
47 | [ -r "$source" ] | ||
48 | [ -d "$destdir" ] | ||
49 | |||
50 | for args | ||
51 | do | ||
52 | case "$1" in | ||
53 | --unscan) unscan=y;; | ||
54 | *) set -- "$@" "$1";; | ||
55 | esac | ||
56 | shift | ||
57 | done | ||
58 | |||
59 | if [ "$unscan" ] | ||
60 | then | ||
61 | device_count=$(device_count "$source") | ||
62 | if [ $device_count -eq 1 ] | ||
63 | then | ||
64 | source_uuid=$(get_prop fsid "$source") | ||
65 | remove_competition "$source_uuid" | ||
66 | fi | ||
67 | fi | ||
68 | |||
69 | mount "$@" | ||
70 | } | ||
71 | |||
72 | set -e | ||
73 | |||
74 | btrfs_mount "$@" | ||