]> git.pld-linux.org Git - projects/geninitrd.git/blob - functions
We have /dev/urandom, so also store /dev/random (luks can use it for example).
[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_uuid=$(dm_uuid "$rootdev")
79                 case $dm_uuid 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: can't find real device for $rootdev"
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
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                 dm_minor=$(ls -l $node | awk '{print $6}')
124         ;;
125         esac
126
127         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)
128         if [ -z "$lvm_path" ]; then
129                 # XXX: this could happen also for non-lvm nodes?
130                 cat >&2 <<-EOF
131                 LVM doesn't recognize device-mapper node with minor $dm_minor
132
133                 In case your Physical Volumes are device mapper nodes, you should add to lvm.conf:
134                     types = [ "device-mapper", 254 ]
135
136                 In case the LVM nodes are not present yet, it could be fixed by running:
137                 # vgscan --mknodes
138                 EOF
139                 return 2
140         fi
141         if [ ! -r "$lvm_path" ]; then
142                 echo >&2 "lvdisplay returned $lvm_path which doesn't exist in filesystem; try running 'vgscan --mknodes'."
143                 return 2
144         fi
145         echo $lvm_path
146 }
147
148 # resolve /dev/dm-0, /dev/mapper/name
149 # @return       DM name
150 dm_name() {
151         local node="$1"
152         dmsetup info -c --noheadings $node | awk -F: '{print $1}'
153 }
154
155 # get UUID for DM node
156 # node can be /dev/dm-0, /dev/mapper/name
157 # @return       UUID
158 dm_uuid() {
159         local node="$1"
160         dmsetup info -c --noheadings $node | awk -F: '{print $8}'
161 }
162
163 # resolve any dm node to it's full path in /dev/mapper
164 dm_node() {
165         local node="$1"
166         printf "/dev/mapper/%s" $(dm_name "$node")
167 }
This page took 0.043436 seconds and 4 git commands to generate.