]> git.pld-linux.org Git - projects/geninitrd.git/blob - functions
dm_subsystem: ignore dmsetup output if exit code is not zero
[projects/geninitrd.git] / functions
1 #!/bin/sh
2 #
3 # geninitrd functions
4
5 # Find root device from fstab.
6 #
7 # @param        string  $fstab location of /etc/fstab
8 # @return       false on failure
9 #
10 # Sets global variables:
11 # - $rootdev
12 # - $rootdev_add
13 # - $rootFS
14 #
15 find_root() {
16         local fstab="$1"
17         local function="${PROGRAM:+$PROGRAM: }find_root"
18         local rootopt
19
20         eval $(awk '!/^[\t ]*#/ && $2 == "/" {printf("rootdev=\"%s\"\nrootFs=\"%s\"\nrootopt=\"%s\"\n", $1, $3, $4)}' $fstab)
21         if [ -z "$rootdev" ]; then
22                 echo >&2 "$function: can't find fstab entry for root mountpoint"
23                 return 1
24         fi
25
26         # additional devices needed (xfs logdev)
27         rootdev_add=$(echo "$rootopt" | awk -F',' '{ for (i=1; i<=NF; i++) { if ($i ~ /^logdev=/) { gsub(/^logdev=/, NIL, $i); print $i; } } }')
28
29         case $rootdev in
30         LABEL=*)
31                 if [ ! -x /sbin/blkid ]; then
32                         echo >&2 "$function: /sbin/blkid is missing"
33                         return 2
34                 fi
35
36                 local label=${rootdev#"LABEL="}
37                 local dev=$(/sbin/blkid -l -t LABEL="$label" -o device)
38                 if [ "$dev" ]; then
39                         if [ ! -r "$dev" ]; then
40                                 echo >&2 "$function: blkid returned device $dev which doesn't exist"
41                                 return 2
42                         fi
43                         rootdev=$dev
44                 fi
45                 ;;
46
47         UUID=*)
48                 if [ ! -x /sbin/blkid ]; then
49                         echo >&2 "$function: /sbin/blkid is missing"
50                         return 2
51                 fi
52
53                 local uuid=${rootdev#"UUID="}
54                 local dev=$(/sbin/blkid -l -t UUID="$uuid" -o device)
55
56                 if [ "$dev" ]; then
57                         if [ ! -r "$dev" ]; then
58                                 echo >&2 "$function: blkid returned device $dev which doesn't exist"
59                                 return 2
60                         fi
61                         rootdev=$dev
62                 fi
63                 ;;
64         esac
65
66         case $rootdev in
67         /dev/dm-*)
68                 local node
69                 node=$(dm_node "$rootdev") || return 1
70                 if [ "$node" ]; then
71                         rootdev="$node"
72                 fi
73                 ;;
74         esac
75
76         case $rootdev in
77         /dev/mapper/*)
78                 local dm_subsystem=$(dm_subsystem "$rootdev")
79                 case $dm_subsystem in
80                 LVM)
81                         local node
82                         node=$(dm_lvm2_name "$rootdev") || return 1
83                         if [ "$node" ]; then
84                                 rootdev="$node"
85                         fi
86                         return 0
87                         ;;
88                 CRYPT)
89                         return 0
90                         ;;
91                 esac
92         esac
93
94         if [ "$rootFs" = "nfs" ]; then
95                 rootdev="/dev/nfs"
96                 return 0
97         fi
98
99         if [ ! -r "$rootdev" ]; then
100                 echo >&2 "$function: rootfs device file $rootdev doesn't exist"
101                 return 1
102         fi
103
104         return 0
105 }
106
107 # resolve /dev/dm-0 to lvm2 node
108 # which they got from blkid program fs was specifed as UUID= in fstab
109 dm_lvm2_name() {
110         local node="$1"
111         local dm_minor stat
112
113         if [ ! -b "$node" ]; then
114                 echo >&2 "dm_lvm2_name: $node is not a block device"
115                 return 1
116         fi
117
118         case $node in
119         /dev/dm-*)
120                 dm_minor=${node#/dev/dm-}
121                 ;;
122         /dev/mapper/*)
123                 stat=$(stat -L -c %T "$node") || die "stat failed: $node"
124                 dm_minor=$((0x$stat))
125         ;;
126         esac
127
128         local lvm_path=$(/sbin/lvdisplay -c 2>/dev/null | awk -F: -vn=$dm_minor '{node=$1; major=$12; minor=$13; if (n == minor) print node}' | xargs)
129         if [ -z "$lvm_path" ]; then
130                 # XXX: this could happen also for non-lvm nodes?
131                 cat >&2 <<-EOF
132                 LVM doesn't recognize device-mapper node with minor $dm_minor
133
134                 In case your Physical Volumes are device mapper nodes, you should add to lvm.conf:
135                     types = [ "device-mapper", 254 ]
136
137                 In case the LVM nodes are not present yet, it could be fixed by running:
138                 # vgscan --mknodes
139                 EOF
140                 return 2
141         fi
142         if [ ! -r "$lvm_path" ]; then
143                 echo >&2 "lvdisplay returned $lvm_path which doesn't exist in filesystem; try running 'vgscan --mknodes'."
144                 return 2
145         fi
146         echo $lvm_path
147 }
148
149 # resolve /dev/dm-0, /dev/mapper/name
150 # @return       DM name
151 dm_name() {
152         local node="$1"
153         dmsetup info -c --noheadings -o name $node
154 }
155
156 # get subsystem name for DM node
157 # node can be /dev/dm-0, /dev/mapper/name
158 # @return       subsystem name
159 dm_subsystem() {
160         local node="$1" out
161         out=$(dmsetup info -c --noheadings -o subsystem $node)
162         if [ $? -eq 0 -a -n "$out" ]; then
163                 echo "$out"
164                 return
165         fi
166
167         # for very old kernels (2.6.16), subsystem is empty, assume LVM
168         # TODO: fix this if needed to have crypt as well
169         echo "LVM"
170 }
171
172 # resolve any dm node to it's full path in /dev/mapper
173 dm_node() {
174         local node="$1"
175         printf "/dev/mapper/%s" $(dm_name "$node")
176 }
177
178 # find modules by class
179 # find_modules_by_class 0106 - finds modules for SATA devices in the system
180 # find_modules_by_class 0c03 - finds modules for USB controllers
181 find_modules_by_class() {
182         if modprobe --version | grep -q "^kmod"; then
183                 find_modules_by_class_kmod $@
184         else
185                 find_modules_by_class_mit $@
186         fi
187 }
188
189 # find modules by class (kmod version)
190 # find_modules_by_class 0106 - finds modules for SATA devices in the system
191 # find_modules_by_class 0c03 - finds modules for USB controllers
192 find_modules_by_class_kmod() {
193         local req_class="$1" i j modaliases
194
195         if [ ! -d "/sys/devices" ]; then
196                 warn "No /sys/devices/ found. Is /sys mounted?"
197                 return
198         fi
199
200         for i in $(grep -li "^0x${req_class}" /sys/devices/pci*/*/class); do
201                 j=$(dirname $i)
202                 modaliases="$modaliases $(cat $j/modalias)"
203         done
204
205         if [ -z "$modaliases" ]; then
206                 return
207         fi
208
209         echo $modaliases | xargs modprobe --set-version $kernel -aRn | awk '
210                 BEGIN { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci=""; ohci=""; uhci="" }
211                 {
212                         module=$1
213                         if (module == "xhci_hcd") {
214                                 xhci="xhci_hcd"
215                         } else if (module == "ehci_hcd") {
216                                 ehci="ehci_hcd"
217                         } else if (module == "ohci_hcd") {
218                                 ohci="ohci_hcd"
219                         } else if (module == "uhci_hcd") {
220                                 uhci="uhci_hcd"
221                         } else if (!(module in skip_modules)) {
222                                 modules[cnt]=module
223                         }
224                         skip_modules[module]=1;
225                 }
226                 END {
227                         # xhci/ehci/ohci/uhci hack to preserve such order
228                         printf "%s %s %s %s ", xhci, ehci, ohci, uhci;
229                         for (i in modules) { printf "%s ", modules[i]; };
230                 }
231         '
232 }
233
234 # find modules by class (module-init-tools version)
235 # find_modules_by_class 0106 - finds modules for SATA devices in the system
236 # find_modules_by_class 0c03 - finds modules for USB controllers
237 find_modules_by_class_mit() {
238         local req_class="$1" pcimap lspci
239
240         pcimap="/lib/modules/$kernel/modules.pcimap"
241
242         lspci=$(find_tool /sbin/lspci)
243         if [ ! -x "$lspci" ]; then
244                 warn "Failed to execute lspci. Is pciutils package installed?"
245         fi
246
247         # no pcimap, nothing to lookup from
248         if [ ! -f "$pcimap" ]; then
249                 warn "No $pcimap file. Cannot find modules for desired class!"
250                 return
251         fi
252
253         if [ -z "$lspci" ]; then
254                 return
255         fi
256
257         LC_ALL=C lspci -p "$pcimap" -kvmmn | awk -vreq_class="${req_class}" '
258                 BEGIN      { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci=""; ohci=""; uhci="" }
259                 /^Slot:/   { found=0 }
260                 /^Class:/  { if (req_class == $2) { found = 1 } }
261                 /^Driver:/ { if (found) {
262                                 module = $2;
263                                 if (module == "xhci_hcd") {
264                                         xhci = "xhci_hcd"
265                                 } else if (module == "ehci_hcd") {
266                                         ehci = "ehci_hcd"
267                                 } else if (module == "ohci_hcd") {
268                                         ohci = "ohci_hcd"
269                                 } else if (module == "uhci_hcd") {
270                                         uhci = "uhci_hcd"
271                                 } else if (!(module in skip_modules)) {
272                                         modules[cnt] = module
273                                 }
274                                 skip_modules[module] = 1;
275                    }
276                    found=0
277                 }
278                 END {
279                    # xhci/ehci/ohci/uhci hack to preserve such order
280                    printf "%s %s %s %s ", xhci, ehci, ohci, uhci;
281                    for (i in modules) { printf "%s ", modules[i]; }
282                 }
283         '
284 }
285
This page took 0.077621 seconds and 4 git commands to generate.