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 '$lvm 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
143 if [ "$LVMTOOLSVERSION" = "1" ]; then
146 for vg in $LVM_VGVOLUMES; do
147 lvm vgchange -T -a y $vg
151 cat <<-EOF > "$DESTDIR/etc/lvm.conf"
159 if is_yes "$have_md"; then
160 echo " md_component_detection=1" >> "$DESTDIR/etc/lvm.conf"
162 if is_yes "$have_dmraid" || is_yes "$have_multipath"; then
163 echo ' types = [ "device-mapper", 254 ]' >> "$DESTDIR/etc/lvm.conf"
165 if [ "$lvm_ignore_devices" ]; then
166 # TODO: think of merging with lvm dumpconfig output
167 echo ' filter = [' >> "$DESTDIR/etc/lvm.conf"
169 for dev in $lvm_ignore_devices; do
170 verbose "LVM v2: ignore device $dev"
171 printf ' "r|^%s.*|",\n' $dev
172 done >> "$DESTDIR/etc/lvm.conf"
173 echo ']' >> "$DESTDIR/etc/lvm.conf"
175 # XXX filter= must be on one line!
176 $lvm dumpconfig | awk '/filter=/' >> "$DESTDIR/etc/lvm.conf"
177 echo "}" >> "$DESTDIR/etc/lvm.conf"
179 echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
185 LVM_ROOTVG="$LVM_VGVOLUMES"
186 LVM_SUSPENDVG="$LVM_SUSPENDVG"
189 # need awk for "s/--/-/g" subst when parsing kernel root commandline
192 # parse rootdev from kernel commandline if it begins with /
197 # /dev/mapper/sys-rootfs -> /dev/sys/rootfs
198 # /dev/mapper/blodnatt-blah--bleh -> /dev/blodnatt/blah-bleh
199 # /dev/mapper/vg--meaw-root -> /dev/vg-meaw/root
202 # change "--" to / (as "/" is impossible in LV name)
203 local dev=$(awk -vdev="${ROOT#/dev/mapper/}" 'BEGIN{gsub(/--/, "/", dev); print dev}')
204 local VG=$(awk -vdev="$dev" 'BEGIN{split(dev, v, "-"); gsub("/", "-", v[1]); print v[1]}')
205 local LV=$(awk -vdev="$dev" 'BEGIN{split(dev, v, "-"); gsub("/", "-", v[2]); print v[2]}')
210 if [ "$ROOT" != "$ROOTDEV" ]; then
213 echo "LVM: Using 'root=$ROOTDEV' from kernel commandline"
214 local tmp=${ROOTDEV#/dev/}
215 if [ "$tmp" != "$ROOTDEV" ]; then
217 echo "LVM: Using Volume Group '$LVM_ROOTVG' for rootfs"
224 if [ "$LVM_SUSPENDVG" = "$LVM_ROOTVG" ]; then
225 LVM_VGVOLUMES="$LVM_ROOTVG"
227 LVM_VGVOLUMES="$LVM_SUSPENDVG $LVM_ROOTVG"
230 # disable noise from LVM accessing devices that aren't ready.
231 read printk < /proc/sys/kernel/printk
232 if [ ! "$DEBUGINITRD" ]; then
233 echo 0 > /proc/sys/kernel/printk
236 export LVM_SYSTEM_DIR=/tmp
238 : 'Scanning for Volume Groups'
239 lvm.static vgscan --mknodes --ignorelockingfailure
241 : 'Activating Volume Groups'
242 for vol in $LVM_VGVOLUMES; do
243 lvm.static vgchange --ignorelockingfailure -a y $vol --noudevsync
246 : 'Extra call to make device nodes for non lvm2-initrd (dynamic lvm2)'
247 lvm.static vgmknodes --ignorelockingfailure
249 echo "$printk" > /proc/sys/kernel/printk
251 # Find out major/minor
252 attrs="$(lvm.static lvdisplay --ignorelockingfailure -c $ROOTDEV)"
253 if [ "$attrs" ]; then
254 majmin="${attrs#*$ROOTDEV*:*:*:*:*:*:*:*:*:*:*:*}"
255 if [ "$majmin" != "$attrs" ]; then
261 if [ "$major" -a "$minor" ]; then
263 echo $((256 * $major + $minor)) > /proc/sys/kernel/real-root-dev
275 # find PV names from given VG name
276 # @param string $vg Volume Group name
281 local pv=$($lvm vgs --noheadings -o pv_name "$vg")
285 # find VG name from given devnode
286 # @param string $devnode device node to be examined
291 local vg=$($lvm lvs --noheadings -o vg_name "$devnode")