Some fixes for karmic
[mapsector.git] / mapsector
1 #!/bin/sh
2 #
3 # mapsector -- Map sector number to file name(s)
4 #
5 # Copyright (C) 2009,2010 Stefan Bund <stefan@j32.de>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the
19 # Free Software Foundation, Inc.,
20 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 #
22
23 ###########################################################################
24
25 mappers=""
26 scanners=""
27 version="unknown"
28 name="`basename "$0"`"
29
30 # Don't use custom language since we parse the output of lots of standard
31 # utilities
32 unset LANG
33
34 register_mapper()
35 {
36     mappers="${mappers}${mappers:+ }$1"
37 }
38
39 register_scanner()
40 {
41     scanners="${scanners}${scanners:+ }$1"
42 }
43
44 load()
45 {
46     local X
47     local lib
48
49     X="`dirname "$0"`"
50     if [ "`basename "$X"`" == "bin" ]; then # `"
51         libdir="`dirname "$X"`/share/$name" #`"
52     fi
53     if [ -z "$libdir" -o ! -d "$libdir" ]; then
54         libdir="$X/lib"
55     fi
56
57     if [ ! -d "$libdir" ]; then
58         echo "! Library directory not found" 1>&2
59         exit 1
60     fi
61
62     for lib in `ls "$libdir"/[0-9][0-9]_*.sh | sort`; do
63         source $lib
64     done
65 }
66
67 # Map device/sector through blockdevice mappings (e.g. lvm)
68
69 map()
70 {
71     local found
72     found=1
73     while [ -n "$found" ]; do
74         found=""
75         for name in $mappers; do
76             if detect_$name; then
77                 found=1
78                 if do_$name; then
79                     return
80                 fi
81                 break
82             fi
83         done
84     done
85 }
86
87 scan()
88 {
89     for name in $scanners; do
90         if detect_$name; then
91             if do_$name; then
92                 return
93             fi
94         fi
95     done
96 }
97
98 ###########################################################################
99
100 #### Load library modules
101
102 load
103
104 #### Parse command line arguments
105
106 X=`getopt -o "hV" --long "help,noscan,version" -n "$name" -- "$@"`
107 if [ $? != 0 ]; then exit 1; fi
108 eval set -- "$X"
109
110 noscan=""
111 help=""
112
113 while true; do
114     case "$1" in
115         -h|--help) help="0"; shift ;;
116         --noscan) noscan="0"; shift ;;
117         -V|--version) echo "$name $version"; exit 0 ;;
118         --) shift; break ;;
119         *) echo "! internal error"; exit 1 ;;
120     esac
121 done
122
123 if [ -n "$help" -o -z "$2" ]; then
124     cat <<EOF
125 Usage: $name [-h|--help] [--noscan] <device> <sector>"
126
127 mapsector -- Map sector number to file name(s)
128
129 Given a device and a sector number, mapsector will try to find the
130 file name(s) mapping to this sector. It will try to gather as much
131 information about the given sector as possible.
132
133 mapsector currently has support for the following mapping schemes:
134
135 EOF
136     for mapper in $mappers; do
137         describe_$mapper
138     done
139     cat <<EOF
140
141 mapsector currently supports the following filesystems
142
143 EOF
144     for scanner in $scanners; do
145         describe_$scanner
146     done
147 cat <<EOF
148
149 mapsector will try it's best to find an associated file name but
150 depending on the filesystem state and the type of sector (e.g. if
151 the sector is part of a filesystem metadata block) this may not be
152 possible.
153
154 For mapsector to work, the filesystem must be currently active
155 (e.g. LVM must be running, crypted devices must have been set up). The
156 filesystem must not necessarily be mounted (though if mounted,
157 mapsector will give you the mountpoint).
158
159 if '--noscan' is given, the possibly lengthy (!!) filesystem scan for
160 filenames is skipped.
161
162 -- mapsector $version
163 EOF
164     exit "${help:-1}"
165 fi
166
167 device="$1"
168 sector="$2"
169
170 echo "device $device"
171 echo "sector $sector"
172
173 map
174 if [ -z "$noscan" ]; then
175     scan
176 fi