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 dmsetup info -c --noheadings -o subsystem $node
164 # resolve any dm node to it's full path in /dev/mapper
167 printf "/dev/mapper/%s" $(dm_name "$node")