3 USE_LVM=${USE_LVM:-yes}
5 # if we should init LVM at boot
8 # LVM volumes that are needed to activate
13 # VG for suspend resume dev
18 # LVM version. Autodetected if not set.
22 # LVM devices that should not be included in vgscan on initrd.
26 # setup geninitrd module
29 lvm=$(find_tool $initrd_dir/lvm /sbin/initrd-lvm)
31 if [ ! -x "$lvm" ]; then
35 if is_yes "$USE_LVM" && [ -z "$LVMTOOLSVERSION" ]; then
36 LVMTOOLSVERSION=$(LC_ALL=C $lvm vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
37 if [ -z "$LVMTOOLSVERSION" ]; then
38 die "Can't determine LVM tools version (is /sys mounted?). Please set LVMTOOLSVERSION and rerun $PROGRAM."
43 # return true if node is lvm node
44 # @param string $node device node to be examined
50 if is_no "$USE_LVM"; then
54 if [ ! -e "$node" ]; then
55 die "check_lvm(): node $node doesn't exist!"
59 # block-major-58 is lvm1
60 ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
68 $lvm lvdisplay "$node" > /dev/null 2>&1
70 if [ $rc -gt 127 ]; then
71 # lvdisplay terminated by signal! most likely it segfaulted.
72 die "Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
78 # find modules for $devpath
79 # @param $devpath device to be examined
84 verbose "LVM: $devpath is LVM node"
86 local vg=$(find_lvm_vg "$devpath")
87 verbose "LVM VG for $devpath: $vg"
88 LVM_VGVOLUMES=$(echo $LVM_VGVOLUMES $vg | tr ' ' '\n' | sort -u)
90 local pv=$(find_lvm_pv "$vg")
91 verbose "LVM PV for $vg: $pv"
92 PVDEVICES=$(echo $PVDEVICES $pv | tr ' ' '\n' | sort -u)
94 if [ -n "$PVDEVICES" ]; then
95 for device in $PVDEVICES; do
96 find_modules_for_devpath $device
99 die "I wasn't able to find PV. You can try to set PVDEVICES in /etc/sysconfig/geninitrd."
102 if [ "$LVMTOOLSVERSION" = "2" ]; then
103 find_module "-dm-mod"
104 elif [ "$LVMTOOLSVERSION" = "1" ]; then
106 find_module "-lvm-mod"
108 die "LVM version $LVMTOOLSVERSION is not supported."
111 verbose "LVM v$LVMTOOLSVERSION enabled"
116 # generate initrd fragment for lvm
119 if ! is_yes "$have_lvm"; then
123 verbose "Adding LVM support to initrd"
125 inst_exec $lvm /bin/lvm.static
127 # always make /dev on tmpfs for LVM2
128 if [ "$LVMTOOLSVERSION" = "2" ]; then
132 if ! is_yes "$dev_mounted"; then
134 mknod -m 600 $DESTDIR/dev/mapper/control c 10 63
135 for device in $PVDEVICES; do
136 # if LVM on RAID then device might be copied already in gen_md
137 [ -e "$DESTDIR/dev/$(basename $device)" ] && continue
144 if [ "$LVMTOOLSVERSION" = "1" ]; then
147 for vg in $LVM_VGVOLUMES; do
148 lvm vgchange -T -a y $vg
152 cat <<-EOF > "$DESTDIR/etc/lvm.conf"
160 if is_yes "$have_md"; then
161 echo " md_component_detection=1" >> "$DESTDIR/etc/lvm.conf"
163 if is_yes "$have_dmraid" || is_yes "$have_multipath"; then
164 echo ' types = [ "device-mapper", 254 ]' >> "$DESTDIR/etc/lvm.conf"
166 if [ "$lvm_ignore_devices" ]; then
167 # TODO: think of merging with lvm dumpconfig output
168 echo ' filter = [' >> "$DESTDIR/etc/lvm.conf"
170 for dev in $lvm_ignore_devices; do
171 verbose "LVM v2: ignore device $dev"
172 printf ' "r|^%s.*|",\n' $dev
173 done >> "$DESTDIR/etc/lvm.conf"
174 echo ']' >> "$DESTDIR/etc/lvm.conf"
176 # XXX filter= must be on one line!
177 $lvm dumpconfig | awk '/filter=/' >> "$DESTDIR/etc/lvm.conf"
178 echo "}" >> "$DESTDIR/etc/lvm.conf"
180 echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
186 LVM_ROOTVG="$LVM_VGVOLUMES"
187 LVM_SUSPENDVG="$LVM_SUSPENDVG"
190 # need awk for "s/--/-/g" subst when parsing kernel root commandline
193 # parse rootdev from kernel commandline if it begins with /
198 # /dev/mapper/sys-rootfs -> /dev/sys/rootfs
199 # /dev/mapper/blodnatt-blah--bleh -> /dev/blodnatt/blah-bleh
200 # /dev/mapper/vg--meaw-root -> /dev/vg-meaw/root
203 # change "--" to / (as "/" is impossible in LV name)
204 local dev=$(awk -vdev="${ROOT#/dev/mapper/}" 'BEGIN{gsub(/--/, "/", dev); print dev}')
205 local VG=$(awk -vdev="$dev" 'BEGIN{split(dev, v, "-"); gsub("/", "-", v[1]); print v[1]}')
206 local LV=$(awk -vdev="$dev" 'BEGIN{split(dev, v, "-"); gsub("/", "-", v[2]); print v[2]}')
211 if [ "$ROOT" != "$ROOTDEV" ]; then
214 echo "LVM: Using 'root=$ROOTDEV' from kernel commandline"
215 local tmp=${ROOTDEV#/dev/}
216 if [ "$tmp" != "$ROOTDEV" ]; then
218 echo "LVM: Using Volume Group '$LVM_ROOTVG' for rootfs"
225 if [ "$LVM_SUSPENDVG" = "$LVM_ROOTVG" ]; then
226 LVM_VGVOLUMES="$LVM_ROOTVG"
228 LVM_VGVOLUMES="$LVM_SUSPENDVG $LVM_ROOTVG"
231 # disable noise from LVM accessing devices that aren't ready.
232 read printk < /proc/sys/kernel/printk
233 if [ ! "$DEBUGINITRD" ]; then
234 echo 0 > /proc/sys/kernel/printk
237 export LVM_SYSTEM_DIR=/tmp
239 : 'Scanning for Volume Groups'
240 lvm.static vgscan --mknodes --ignorelockingfailure
242 : 'Activating Volume Groups'
243 for vol in $LVM_VGVOLUMES; do
244 lvm.static vgchange --ignorelockingfailure -a y $vol --noudevsync
247 : 'Extra call to make device nodes for non lvm2-initrd (dynamic lvm2)'
248 lvm.static vgmknodes --ignorelockingfailure
250 echo "$printk" > /proc/sys/kernel/printk
252 # Find out major/minor
253 attrs="$(lvm.static lvdisplay --ignorelockingfailure -c $ROOTDEV)"
254 if [ "$attrs" ]; then
255 majmin="${attrs#*$ROOTDEV*:*:*:*:*:*:*:*:*:*:*:*}"
256 if [ "$majmin" != "$attrs" ]; then
262 if [ "$major" -a "$minor" ]; then
264 echo $((256 * $major + $minor)) > /proc/sys/kernel/real-root-dev
276 # find PV names from given VG name
277 # @param string $vg Volume Group name
282 local pv=$($lvm vgs --noheadings -o pv_name "$vg")
286 # find VG name from given devnode
287 # @param string $devnode device node to be examined
292 local vg=$($lvm lvs --noheadings -o vg_name "$devnode")