]> git.pld-linux.org Git - projects/geninitrd.git/blame_incremental - functions
Version 12531.
[projects/geninitrd.git] / functions
... / ...
CommitLineData
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#
15find_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
109dm_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
151dm_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
159dm_subsystem() {
160 local node="$1"
161 dmsetup info -c --noheadings -o subsystem $node
162}
163
164# resolve any dm node to it's full path in /dev/mapper
165dm_node() {
166 local node="$1"
167 printf "/dev/mapper/%s" $(dm_name "$node")
168}
169
170# find modules by class
171# find_modules_by_class 0106 - finds modules for SATA devices in the system
172# find_modules_by_class 0c03 - finds modules for USB controllers
173find_modules_by_class() {
174 if modprobe --version | grep -q "^kmod"; then
175 find_modules_by_class_kmod $@
176 else
177 find_modules_by_class_mit $@
178 fi
179}
180
181# find modules by class (kmod version)
182# find_modules_by_class 0106 - finds modules for SATA devices in the system
183# find_modules_by_class 0c03 - finds modules for USB controllers
184find_modules_by_class_kmod() {
185 local req_class="$1" i j modaliases
186
187 if [ ! -d "/sys/devices" ]; then
188 warn "No /sys/devices/ found. Is /sys mounted?"
189 return
190 fi
191
192 for i in $(grep -li "^0x${req_class}" /sys/devices/pci*/*/class); do
193 j=$(dirname $i)
194 modaliases="$modaliases $(cat $j/modalias)"
195 done
196
197 if [ -z "$modaliases" ]; then
198 return
199 fi
200
201 echo $modaliases | xargs modprobe --set-version $kernel -aRn | awk '
202 BEGIN { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci=""; ohci=""; uhci="" }
203 {
204 module=$1
205 if (module == "xhci_hcd") {
206 xhci="xhci_hcd"
207 } else if (module == "ehci_hcd") {
208 ehci="ehci_hcd"
209 } else if (module == "ohci_hcd") {
210 ohci="ohci_hcd"
211 } else if (module == "uhci_hcd") {
212 uhci="uhci_hcd"
213 } else if (!(module in skip_modules)) {
214 modules[cnt]=module
215 }
216 skip_modules[module]=1;
217 }
218 END {
219 # xhci/ehci/ohci/uhci hack to preserve such order
220 printf "%s %s %s %s ", xhci, ehci, ohci, uhci;
221 for (i in modules) { printf "%s ", modules[i]; };
222 }
223 '
224}
225
226# find modules by class (module-init-tools version)
227# find_modules_by_class 0106 - finds modules for SATA devices in the system
228# find_modules_by_class 0c03 - finds modules for USB controllers
229find_modules_by_class_mit() {
230 local req_class="$1" pcimap lspci
231
232 pcimap="/lib/modules/$kernel/modules.pcimap"
233
234 lspci=$(find_tool /sbin/lspci)
235 if [ ! -x "$lspci" ]; then
236 warn "Failed to execute lspci. Is pciutils package installed?"
237 fi
238
239 # no pcimap, nothing to lookup from
240 if [ ! -f "$pcimap" ]; then
241 warn "No $pcimap file. Cannot find modules for desired class!"
242 return
243 fi
244
245 if [ -z "$lspci" ]; then
246 return
247 fi
248
249 LC_ALL=C lspci -p "$pcimap" -kvmmn | awk -vreq_class="${req_class}" '
250 BEGIN { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci=""; ohci=""; uhci="" }
251 /^Slot:/ { found=0 }
252 /^Class:/ { if (req_class == $2) { found = 1 } }
253 /^Driver:/ { if (found) {
254 module = $2;
255 if (module == "xhci_hcd") {
256 xhci = "xhci_hcd"
257 } else if (module == "ehci_hcd") {
258 ehci = "ehci_hcd"
259 } else if (module == "ohci_hcd") {
260 ohci = "ohci_hcd"
261 } else if (module == "uhci_hcd") {
262 uhci = "uhci_hcd"
263 } else if (!(module in skip_modules)) {
264 modules[cnt] = module
265 }
266 skip_modules[module] = 1;
267 }
268 found=0
269 }
270 END {
271 # xhci/ehci/ohci/uhci hack to preserve such order
272 printf "%s %s %s %s ", xhci, ehci, ohci, uhci;
273 for (i in modules) { printf "%s ", modules[i]; }
274 }
275 '
276}
277
This page took 0.054707 seconds and 4 git commands to generate.