5 # Find root device from fstab.
7 # @param string $fstab location of /etc/fstab
8 # @return false on failure
10 # Sets global variables:
17 local function="${PROGRAM:+$PROGRAM: }find_root"
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"
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; } } }')
31 if [ ! -x /sbin/blkid ]; then
32 echo >&2 "$function: /sbin/blkid is missing"
36 local label=${rootdev#"LABEL="}
37 local dev=$(/sbin/blkid -l -t LABEL="$label" -o device)
39 if [ ! -r "$dev" ]; then
40 echo >&2 "$function: blkid returned device $dev which doesn't exist"
48 if [ ! -x /sbin/blkid ]; then
49 echo >&2 "$function: /sbin/blkid is missing"
53 local uuid=${rootdev#"UUID="}
54 local dev=$(/sbin/blkid -l -t UUID="$uuid" -o device)
57 if [ ! -r "$dev" ]; then
58 echo >&2 "$function: blkid returned device $dev which doesn't exist"
69 node=$(dm_node "$rootdev") || return 1
78 local dm_subsystem=$(dm_subsystem "$rootdev")
82 node=$(dm_lvm2_name "$rootdev") || return 1
94 if [ "$rootFs" = "nfs" ]; then
99 if [ ! -r "$rootdev" ]; then
100 echo >&2 "$function: rootfs device file $rootdev doesn't exist"
107 # resolve /dev/dm-0 to lvm2 node
108 # which they got from blkid program fs was specifed as UUID= in fstab
113 if [ ! -b "$node" ]; then
114 echo >&2 "dm_lvm2_name: $node is not a block device"
120 dm_minor=${node#/dev/dm-}
123 stat=$(stat -L -c %T "$node") || die "stat failed: $node"
124 dm_minor=$((0x$stat))
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?
132 LVM doesn't recognize device-mapper node with minor $dm_minor
134 In case your Physical Volumes are device mapper nodes, you should add to lvm.conf:
135 types = [ "device-mapper", 254 ]
137 In case the LVM nodes are not present yet, it could be fixed by running:
142 if [ ! -r "$lvm_path" ]; then
143 echo >&2 "lvdisplay returned $lvm_path which doesn't exist in filesystem; try running 'vgscan --mknodes'."
149 # resolve /dev/dm-0, /dev/mapper/name
153 dmsetup info -c --noheadings -o name $node
156 # get subsystem name for DM node
157 # node can be /dev/dm-0, /dev/mapper/name
158 # @return subsystem name
161 out=$(dmsetup info -c --noheadings -o subsystem $node)
162 if [ $? -eq 0 -a -n "$out" ]; then
167 # for very old kernels (2.6.16), subsystem is empty, assume LVM
168 # TODO: fix this if needed to have crypt as well
172 # resolve any dm node to it's full path in /dev/mapper
175 printf "/dev/mapper/%s" $(dm_name "$node")
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 $@
185 find_modules_by_class_mit $@
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
195 if [ ! -d "/sys/devices" ]; then
196 warn "No /sys/devices/ found. Is /sys mounted?"
200 for i in $(grep -li "^0x${req_class}" /sys/devices/pci*/*/class); do
202 modaliases="$modaliases $(cat $j/modalias)"
205 if [ -z "$modaliases" ]; then
209 echo $modaliases | xargs modprobe --set-version $kernel -aRn | awk '
210 BEGIN { skip_modules[notexisting_module]=""; modules[1]=""; xhci=""; ehci=""; ohci=""; uhci="" }
213 if (module == "xhci_hcd") {
215 } else if (module == "ehci_hcd") {
217 } else if (module == "ohci_hcd") {
219 } else if (module == "uhci_hcd") {
221 } else if (!(module in skip_modules)) {
224 skip_modules[module]=1;
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]; };
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
240 pcimap="/lib/modules/$kernel/modules.pcimap"
242 lspci=$(find_tool /sbin/lspci)
243 if [ ! -x "$lspci" ]; then
244 warn "Failed to execute lspci. Is pciutils package installed?"
247 # no pcimap, nothing to lookup from
248 if [ ! -f "$pcimap" ]; then
249 warn "No $pcimap file. Cannot find modules for desired class!"
253 if [ -z "$lspci" ]; then
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="" }
260 /^Class:/ { if (req_class == $2) { found = 1 } }
261 /^Driver:/ { if (found) {
263 if (module == "xhci_hcd") {
265 } else if (module == "ehci_hcd") {
267 } else if (module == "ohci_hcd") {
269 } else if (module == "uhci_hcd") {
271 } else if (!(module in skip_modules)) {
272 modules[cnt] = module
274 skip_modules[module] = 1;
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]; }