]> git.pld-linux.org Git - projects/geninitrd.git/blob - functions
luks: initial support for luks2 tokens
[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 "WARNING: $function: rootfs device file $rootdev doesn't exist"
113         fi
114
115         return 0
116 }
117
118 # resolve /dev/dm-0 to lvm2 node
119 # which they got from blkid program fs was specifed as UUID= in fstab
120 dm_lvm2_name() {
121         local node="$1"
122         local dm_minor stat
123
124         if [ ! -b "$node" ]; then
125                 echo >&2 "dm_lvm2_name: $node is not a block device"
126                 return 1
127         fi
128
129         case $node in
130         /dev/dm-*)
131                 dm_minor=${node#/dev/dm-}
132                 ;;
133         /dev/mapper/*)
134                 stat=$(stat -L -c %T "$node") || die "stat failed: $node"
135                 dm_minor=$((0x$stat))
136         ;;
137         esac
138
139         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)
140         if [ -z "$lvm_path" ]; then
141                 # XXX: this could happen also for non-lvm nodes?
142                 cat >&2 <<-EOF
143                 LVM doesn't recognize device-mapper node with minor $dm_minor
144
145                 In case your Physical Volumes are device mapper nodes, you should add to lvm.conf:
146                     types = [ "device-mapper", 254 ]
147
148                 In case the LVM nodes are not present yet, it could be fixed by running:
149                 # vgscan --mknodes
150                 EOF
151                 return 2
152         fi
153         if [ ! -r "$lvm_path" ]; then
154                 echo >&2 "lvdisplay returned $lvm_path which doesn't exist in filesystem; try running 'vgscan --mknodes'."
155                 return 2
156         fi
157         echo $lvm_path
158 }
159
160 # resolve /dev/dm-0, /dev/mapper/name
161 # @return       DM name
162 dm_name() {
163         local node="$1"
164         dmsetup info -c --noheadings -o name $node
165 }
166
167 # get subsystem name for DM node
168 # node can be /dev/dm-0, /dev/mapper/name
169 # @return       subsystem name
170 dm_subsystem() {
171         local node="$1" out
172         out=$(dmsetup info -c --noheadings -o subsystem $node)
173         if [ $? -eq 0 -a -n "$out" ]; then
174                 echo "$out"
175                 return
176         fi
177
178         # for very old kernels (2.6.16), subsystem is empty, assume LVM
179         # TODO: fix this if needed to have crypt as well
180         echo "LVM"
181 }
182
183 # resolve any dm node to it's full path in /dev/mapper
184 dm_node() {
185         local node="$1"
186         printf "/dev/mapper/%s" $(dm_name "$node")
187 }
188
189 is_kmod() {
190         modprobe --version | grep -q "^kmod"
191 }
192
193 # find modules by class
194 # find_modules_by_class 0106 - finds modules for SATA devices in the system
195 # find_modules_by_class 0c03 - finds modules for USB controllers
196 find_modules_by_class() {
197         if is_kmod; then
198                 find_modules_by_class_kmod $@
199         else
200                 find_modules_by_class_mit $@
201         fi
202 }
203
204 # find modules by class (kmod version)
205 # find_modules_by_class 0106 - finds modules for SATA devices in the system
206 # find_modules_by_class 0c03 - finds modules for USB controllers
207 find_modules_by_class_kmod() {
208         local req_class="$1" i j modaliases
209
210         if [ ! -d "/sys/devices" ]; then
211                 warn "No /sys/devices/ found. Is /sys mounted?"
212                 return
213         fi
214
215         if ls /sys/devices | grep -q '^pci'; then
216                 for i in $(grep -li "^0x${req_class}" /sys/devices/pci*/*/class); do
217                         j=$(dirname $i)
218                         modaliases="$modaliases $(cat $j/modalias)"
219                 done
220         fi
221
222         if [ -z "$modaliases" ]; then
223                 return
224         fi
225
226         echo $modaliases | xargs modprobe --set-version $kernel -aRn | awk '
227                 BEGIN { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci_pci=""; ehci_hcd=""; ehci_platform=""; ohci=""; uhci="" }
228                 {
229                         module=$1
230                         if (module == "xhci_hcd") {
231                                 xhci="xhci_hcd"
232                         } else if (module == "ehci_hcd") {
233                                 ehci_hcd="ehci_hcd"
234                         } else if (module == "ehci_pci") {
235                                 ehci_pci="ehci_pci"
236                         } else if (module == "ehci_platform") {
237                                 ehci_platform="ehci_platform"
238                         } else if (module == "ohci_hcd") {
239                                 ohci="ohci_hcd"
240                         } else if (module == "uhci_hcd") {
241                                 uhci="uhci_hcd"
242                         } else if (!(module in skip_modules)) {
243                                 modules[cnt]=module
244                         }
245                         skip_modules[module]=1;
246                 }
247                 END {
248                         # ehci/ohci/uhci/xhci hack to preserve such order
249                         printf "%s %s %s %s %s %s ", ehci_hcd, ehci_pci, ehci_platform, ohci, uhci, xhci;
250                         for (i in modules) { printf "%s ", modules[i]; };
251                 }
252         '
253 }
254
255 # find modules by class (module-init-tools version)
256 # find_modules_by_class 0106 - finds modules for SATA devices in the system
257 # find_modules_by_class 0c03 - finds modules for USB controllers
258 find_modules_by_class_mit() {
259         local req_class="$1" pcimap lspci
260
261         pcimap="/lib/modules/$kernel/modules.pcimap"
262
263         lspci=$(find_tool /sbin/lspci)
264         if [ ! -x "$lspci" ]; then
265                 warn "Failed to execute lspci. Is pciutils package installed?"
266         fi
267
268         # no pcimap, nothing to lookup from
269         if [ ! -f "$pcimap" ]; then
270                 warn "No $pcimap file. Cannot find modules for desired class!"
271                 return
272         fi
273
274         if [ -z "$lspci" ]; then
275                 return
276         fi
277
278         LC_ALL=C lspci -p "$pcimap" -kvmmn | awk -vreq_class="${req_class}" '
279                 BEGIN      { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci_pci=""; ehci_hcd=""; ehci_platform=""; ohci=""; uhci="" }
280                 /^Slot:/   { found=0 }
281                 /^Class:/  { if (req_class == $2) { found = 1 } }
282                 /^Driver:/ { if (found) {
283                                 module = $2;
284                                 if (module == "xhci_hcd") {
285                                         xhci = "xhci_hcd"
286                                 } else if (module == "ehci_hcd") {
287                                         ehci_hcd = "ehci_hcd"
288                                 } else if (module == "ehci_pci") {
289                                         ehci_pci="ehci_pci"
290                                 } else if (module == "ehci_platform") {
291                                         ehci_platform="ehci_platform"
292                                 } else if (module == "ohci_hcd") {
293                                         ohci = "ohci_hcd"
294                                 } else if (module == "uhci_hcd") {
295                                         uhci = "uhci_hcd"
296                                 } else if (!(module in skip_modules)) {
297                                         modules[cnt] = module
298                                 }
299                                 skip_modules[module] = 1;
300                    }
301                    found=0
302                 }
303                 END {
304                    # ehci/ohci/uhci/xhci hack to preserve such order
305                    printf "%s %s %s %s %s %s ", ehci_hcd, ehci_pci, ehci_platform, ohci, uhci, xhci;
306                    for (i in modules) { printf "%s ", modules[i]; }
307                 }
308         '
309 }
310
311 # get possible paths for specifed patter containing LIBDIR
312 get_libdir() {
313         for dir in lib lib64 libx32; do
314                 echo -n "${1/LIBDIR/$dir} "
315         done
316 }
This page took 0.059845 seconds and 3 git commands to generate.