a704e11659a70c5e512aa1c198e4456bd74b995e
[mapsector.git] / mapsector
1 #!/bin/sh
2
3 ###########################################################################
4
5 mappers=""
6 scanners=""
7 version="unknown"
8
9 register_mapper()
10 {
11     mappers="${mappers}${mappers:+ }$1"
12 }
13
14 register_scanner()
15 {
16     scanners="${scanners}${scanners:+ }$1"
17 }
18
19 load()
20 {
21     for lib in `ls "$1"/[0-9][0-9]_*.sh | sort`; do
22         source $lib
23     done
24 }
25
26 # Map device/sector through blockdevice mappings (e.g. lvm)
27
28 run()
29 {
30     local found
31     foundm=1
32     while [ -n "$found" ]; do
33         found=""
34         for name in "$@"; do
35             if detect_$name; then
36                 found=1
37                 if do_$name; then
38                     return
39                 fi
40                 break
41             fi
42         done
43     done
44 }
45
46 ###########################################################################
47
48 unset LANG
49
50 #### Find library directory and load library files
51
52 name="`basename "$0"`"
53 X="`dirname "$0"`"
54 if [ "`basename "$X"`" == "bin" ]; then # `"
55     libdir="`dirname "$X"`/lib/$name" #`"
56 fi
57 if [ -z "$libdir" -o ! -d "$libdir" ]; then
58     libdir="$X/lib"
59 fi
60
61 if [ ! -d "$libdir" ]; then
62     echo "! Library directory not found" 1>&2
63     exit 1
64 fi
65
66 load "$libdir"
67
68 #### Parse command line arguments
69
70 X=`getopt -o "h" --long "help,noscan" -n "$name" -- "$@"`
71 if [ $? != 0 ]; then exit 1; fi
72 eval set -- "$X"
73
74 noscan=""
75 help=""
76
77 while true; do
78     case "$1" in
79         -h|--help) help="0"; shift ;;
80         --noscan) noscan="0"; shift ;;
81         --) shift; break ;;
82         *) echo "! internal error"; exit 1 ;;
83     esac
84 done
85
86 if [ -n "$help" -o -z "$2" ]; then
87     cat <<EOF
88 Usage: $name [-h|--help] [--noscan] <device> <sector>"
89
90 mapsector -- Map sector numbers to file name(s)
91
92 Given a device and a sector number, mapsector will try to find the
93 file name(s) mapping to this sector. It will try to gather as much
94 information about the given sector as possible.
95
96 mapsector currently has support for the following mapping schemes:
97
98 EOF
99     for mapper in $mappers; do
100         describe_$mapper
101     done
102     cat <<EOF
103
104 mapsector currently supports the following filesystems
105
106 EOF
107     for scanner in $scanners; do
108         describe_$scanner
109     done
110 cat <<EOF
111
112 mapsector will try it's best to find an associated file name but
113 depending on the filesystem state and the type of sector (e.g. if
114 the sector is part of a filesystem metadata block) this may not be
115 possible.
116
117 For mapsector to work, the filesystem must be currently active
118 (e.g. LVM must be running, crypted devices must have been set up). The
119 filesystem must not necessarily be mounted (though if mounted,
120 mapsector will give you the mountpoint).
121
122 if '--noscan' is given, the possibly lengthy (!!) filesystem scan for
123 filenames is skipped.
124
125 -- mapsector Version $version
126 EOF
127     exit "${help:-1}"
128 fi
129
130 device="$1"
131 sector="$2"
132
133 echo "device $device"
134 echo "sector $sector"
135
136 run $mappers
137 if [ -z "$noscan" ]; then
138     run $scanners
139 fi