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