Skip to content

Commit 7ffc5e3

Browse files
teiglandjohannbg
authored andcommitted
feat(lvm): use generated filter when none is set
Previously, the lvm device filter generated by dracut would not be used if any lvm.conf file existed in the initrd. Change this so that the generated filter will be used when the included lvm.conf has no filter set.
1 parent c0a54f2 commit 7ffc5e3

File tree

1 file changed

+75
-16
lines changed

1 file changed

+75
-16
lines changed

modules.d/90lvm/lvm_scan.sh

+75-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LVS=$(getargs rd.lvm.lv -d rd_LVM_LV=)
1010

1111
# shellcheck disable=SC2174
1212
[ -d /etc/lvm ] || mkdir -m 0755 -p /etc/lvm
13+
[ -d /run/lvm ] || mkdir -m 0755 -p /run/lvm
1314
# build a list of devices to scan
1415
lvmdevs=$(
1516
for f in /tmp/.lvm_scan-*; do
@@ -18,22 +19,6 @@ lvmdevs=$(
1819
done
1920
)
2021

21-
if [ ! -e /etc/lvm/lvm.conf ]; then
22-
{
23-
echo 'devices {'
24-
printf ' filter = [ '
25-
for dev in $lvmdevs; do
26-
printf '"a|^/dev/%s$|", ' "$dev"
27-
done
28-
echo '"r/.*/" ]'
29-
echo '}'
30-
31-
echo 'global {'
32-
echo '}'
33-
} > /etc/lvm/lvm.conf
34-
lvmwritten=1
35-
fi
36-
3722
check_lvm_ver() {
3823
maj=$1
3924
min=$2
@@ -47,6 +32,75 @@ check_lvm_ver() {
4732
return 1
4833
}
4934

35+
no_lvm_conf_filter() {
36+
if [ ! -e /etc/lvm/lvm.conf ]; then
37+
return 0
38+
fi
39+
40+
if [ -e /run/lvm/initrd_no_filter ]; then
41+
return 0
42+
fi
43+
44+
if [ -e /run/lvm/initrd_filter ]; then
45+
return 1
46+
fi
47+
48+
if [ -e /run/lvm/initrd_global_filter ]; then
49+
return 1
50+
fi
51+
52+
# Save lvm config results in /run to avoid running
53+
# lvm config commands for every PV that's scanned.
54+
55+
filter=$(lvm config devices/filter | grep "$filter=")
56+
if [ -n "$filter" ]; then
57+
printf '%s\n' "$filter" > /run/lvm/initrd_filter
58+
return 1
59+
fi
60+
61+
global_filter=$(lvm config devices/global_filter | grep "$global_filter=")
62+
if [ -n "$global_filter" ]; then
63+
printf '%s\n' "$global_filter" > /run/lvm/initrd_global_filter
64+
return 1
65+
fi
66+
67+
# /etc/lvm/lvm.conf exists with no filter setting
68+
true > /run/lvm/initrd_no_filter
69+
return 0
70+
}
71+
72+
# If no lvm.conf exists, create a basic one with a global section.
73+
if [ ! -e /etc/lvm/lvm.conf ]; then
74+
{
75+
echo 'global {'
76+
echo '}'
77+
} > /etc/lvm/lvm.conf
78+
lvmwritten=1
79+
fi
80+
81+
# Save the original lvm.conf before appending a filter setting.
82+
if [ ! -e /etc/lvm/lvm.conf.orig ]; then
83+
cp /etc/lvm/lvm.conf /etc/lvm/lvm.conf.orig
84+
fi
85+
86+
# If the original lvm.conf does not contain a filter setting,
87+
# then generate a filter and append it to the original lvm.conf.
88+
# The filter is generated from the list PVs that have been seen
89+
# so far (each has been processed by the lvm udev rule.)
90+
if no_lvm_conf_filter; then
91+
{
92+
echo 'devices {'
93+
printf ' filter = [ '
94+
for dev in $lvmdevs; do
95+
printf '"a|^/dev/%s$|", ' "$dev"
96+
done
97+
echo '"r/.*/" ]'
98+
echo '}'
99+
} > /etc/lvm/lvm.conf.filter
100+
lvmfilter=1
101+
cat /etc/lvm/lvm.conf.orig /etc/lvm/lvm.conf.filter > /etc/lvm/lvm.conf
102+
fi
103+
50104
# hopefully this output format will never change, e.g.:
51105
# LVM version: 2.02.53(1) (2009-09-25)
52106
OLDIFS=$IFS
@@ -99,8 +153,13 @@ fi
99153

100154
if [ "$lvmwritten" ]; then
101155
rm -f -- /etc/lvm/lvm.conf
156+
elif [ "$lvmfilter" ]; then
157+
# revert filter that was appended to existing lvm.conf
158+
cp /etc/lvm/lvm.conf.orig /etc/lvm/lvm.conf
159+
rm -f -- /etc/lvm/lvm.conf.filter
102160
fi
103161
unset lvmwritten
162+
unset lvmfilter
104163

105164
udevadm settle
106165

0 commit comments

Comments
 (0)