Skip to content

Commit

Permalink
don't use lvm.conf to disable locking (#65)
Browse files Browse the repository at this point in the history
* don't use lvm.conf to disable locking

add /usr/bin/sort

use nolocking options

changelog

fix shellcheck findings

add /usr/libexec/lvresize_fs_helper

add lvresize_fs_helper dependencies

* use full path
  • Loading branch information
swapdisk committed Apr 25, 2024
1 parent a7c37f7 commit c20329f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 32 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/fix_lvm_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Fix how locking is disabled for newer LVM versions
24 changes: 21 additions & 3 deletions roles/bigboot/files/bigboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BOOT_PARTITION_NUMBER=
PARTITION_PREFIX=

# Script parameters
NOLOCKING=
ADJACENT_PARTITION_NUMBER=
BOOT_FS_TYPE=
EXTENDED_PARTITION_TYPE=extended
Expand Down Expand Up @@ -184,6 +185,22 @@ ensure_extendable_fs_type() {
BOOT_FS_TYPE=$ret
}

get_nolocking_opts() {
local lvm_version
lvm_version="$(/usr/sbin/lvm version | /usr/bin/grep 'LVM version:')"
status=$?
if [[ $status -ne 0 ]]; then
echo "Error getting LVM version '$lvm_version'"
exit $status
fi
# true when LVM version is older than 2.03
if echo -e "${lvm_version##*:}\n2.03" | /usr/bin/sed 's/^ *//' | /usr/bin/sort -V -C; then
NOLOCKING='--config=global{locking_type=0}'
else
NOLOCKING='--nolocking'
fi
}

get_successive_partition_number() {
boot_line_number=$(/usr/sbin/parted -m "$DEVICE_NAME" print | /usr/bin/sed -n '/^'"$BOOT_PARTITION_NUMBER"':/ {=}')
status=$?
Expand Down Expand Up @@ -214,6 +231,7 @@ get_successive_partition_number() {
init_variables() {
parse_flags "$@"
validate_parameters
get_nolocking_opts
get_successive_partition_number
}

Expand Down Expand Up @@ -299,7 +317,7 @@ check_device() {
evict_end_PV() {
local device="${DEVICE_NAME}${PARTITION_PREFIX}${ADJACENT_PARTITION_NUMBER}"
local shrinking_start_PE=$1
ret=$(/usr/sbin/lvm pvmove --alloc anywhere "$device":"$shrinking_start_PE"- 2>&1)
ret=$(/usr/sbin/lvm pvmove "$NOLOCKING" --alloc anywhere "$device":"$shrinking_start_PE"- 2>&1)
status=$?
if [[ $status -ne 0 ]]; then
echo "Failed to evict PEs in PV $device: $ret"
Expand All @@ -313,7 +331,7 @@ shrink_physical_volume() {
pv_new_size_in_bytes=$((partition_size_in_bytes-SHRINK_SIZE_IN_BYTES))
shrink_start_PE=$((LVM2_PV_PE_COUNT-1-(SHRINK_SIZE_IN_BYTES/LVM2_VG_EXTENT_SIZE)))
# Test mode pvresize
ret=$(/usr/sbin/lvm pvresize --setphysicalvolumesize "$pv_new_size_in_bytes"B -t "$device" -y 2>&1)
ret=$(/usr/sbin/lvm pvresize "$NOLOCKING" --setphysicalvolumesize "$pv_new_size_in_bytes"B -t "$device" -y 2>&1)
status=$?
if [[ $status -ne 0 ]]; then
if [[ $status -eq 5 ]]; then
Expand All @@ -326,7 +344,7 @@ shrink_physical_volume() {
fi
fi
echo "Shrinking PV $device to $pv_new_size_in_bytes bytes" >&2
ret=$(/usr/sbin/lvm pvresize --setphysicalvolumesize "$pv_new_size_in_bytes"B "$device" -y 2>&1)
ret=$(/usr/sbin/lvm pvresize "$NOLOCKING" --setphysicalvolumesize "$pv_new_size_in_bytes"B "$device" -y 2>&1)
status=$?
if [[ $status -ne 0 ]]; then
echo "Failed to resize PV $device during retry: $ret"
Expand Down
2 changes: 1 addition & 1 deletion roles/bigboot/files/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ check(){
}

install() {
inst_multiple -o /usr/bin/mount /usr/bin/umount /usr/sbin/parted /usr/bin/mktemp /usr/bin/wc /usr/bin/date /usr/bin/sed /usr/bin/awk /usr/bin/basename /usr/sbin/resize2fs /usr/sbin/tune2fs /usr/sbin/partprobe /usr/bin/numfmt /usr/sbin/lvm /usr/bin/lsblk /usr/sbin/e2fsck /usr/sbin/fdisk /usr/bin/findmnt /usr/bin/tail /usr/ /usr/sbin/xfs_growfs /usr/sbin/xfs_db
inst_multiple -o /usr/bin/mount /usr/bin/umount /usr/sbin/parted /usr/bin/mktemp /usr/bin/wc /usr/bin/date /usr/bin/sed /usr/bin/awk /usr/bin/sort /usr/bin/basename /usr/sbin/resize2fs /usr/sbin/tune2fs /usr/sbin/partprobe /usr/bin/numfmt /usr/sbin/lvm /usr/bin/lsblk /usr/sbin/e2fsck /usr/sbin/fdisk /usr/bin/findmnt /usr/bin/tail /usr/ /usr/sbin/xfs_growfs /usr/sbin/xfs_db
# shellcheck disable=SC2154
inst_hook pre-mount 99 "$moddir/increase-boot-partition.sh"
inst_binary "$moddir/sfdisk.static" "/usr/sbin/sfdisk"
Expand Down
13 changes: 0 additions & 13 deletions roles/bigboot/templates/increase-boot-partition.sh.j2
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#!/bin/bash

disable_lvm_lock(){
tmpfile=$(/usr/bin/mktemp)
sed -e 's/\(^[[:space:]]*\)locking_type[[:space:]]*=[[:space:]]*[[:digit:]]/\1locking_type = 1/' /etc/lvm/lvm.conf >"$tmpfile"
status=$?
if [[ status -ne 0 ]]; then
echo "Failed to disable lvm lock: $status" >/dev/kmsg
exit 1
fi
# replace lvm.conf. There is no need to keep a backup since it's an ephemeral file, we are not replacing the original in the initramfs image file
mv "$tmpfile" /etc/lvm/lvm.conf
}

activate_volume_groups(){
for vg in `/usr/sbin/lvm vgs -o name --noheading 2>/dev/null`; do
/usr/sbin/lvm vgchange -ay $vg
Expand All @@ -22,7 +10,6 @@ main() {
name=$(basename "$0")
start=$(/usr/bin/date +%s)
activate_volume_groups
disable_lvm_lock
# run bigboot.sh to increase boot partition and file system size
ret=$(sh /usr/bin/bigboot.sh -d="{{ bigboot_boot_device_name }}" -s="{{ bigboot_size }}" -b="{{ bigboot_boot_partition_number }}" -p="{{ bigboot_boot_device_partition_prefix }}" 2>/dev/kmsg)
status=$?
Expand Down
2 changes: 1 addition & 1 deletion roles/shrink_lv/files/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ check(){
}

install() {
inst_multiple -o /usr/bin/numfmt /usr/bin/findmnt /usr/bin/lsblk /usr/sbin/lvm /usr/bin/awk /usr/bin/sed /usr/bin/mktemp /usr/bin/date /usr/bin/head /usr/sbin/blockdev /usr/sbin/tune2fs /usr/sbin/resize2fs /usr/bin/cut /usr/sbin/fsadm /usr/sbin/fsck.ext4
inst_multiple -o /usr/bin/numfmt /usr/bin/findmnt /usr/bin/lsblk /usr/sbin/lvm /usr/bin/awk /usr/bin/sed /usr/bin/sort /usr/bin/mktemp /usr/bin/date /usr/bin/head /usr/sbin/blockdev /usr/sbin/tune2fs /usr/sbin/resize2fs /usr/bin/cut /usr/sbin/fsadm /usr/sbin/fsck.ext4 /usr/libexec/lvresize_fs_helper /usr/sbin/cryptsetup /usr/bin/logger /usr/bin/basename /usr/bin/getopt
# shellcheck disable=SC2154
inst_hook pre-mount 99 "$moddir/shrink-start.sh"
inst_simple "$moddir/shrink.sh" "/usr/bin/shrink.sh"
Expand Down
19 changes: 18 additions & 1 deletion roles/shrink_lv/files/shrink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function parse_uuid() {
}

function shrink_volume() {
/usr/sbin/lvm lvreduce --resizefs -L "$2b" "$1"
/usr/sbin/lvm lvreduce "$NOLOCKING" --resizefs -L "$2"b "$1"
return $?
}

Expand Down Expand Up @@ -198,12 +198,29 @@ function parse_entry() {
expected_size="${strarr[1]}"
}

function get_nolocking_opts() {
local lvm_version
lvm_version="$(/usr/sbin/lvm version | /usr/bin/grep 'LVM version:')"
status=$?
if [[ $status -ne 0 ]]; then
echo "Error getting LVM version '$lvm_version'"
exit $status
fi
# true when LVM version is older than 2.03
if echo -e "${lvm_version##*:}\n2.03" | /usr/bin/sed 's/^ *//' | /usr/bin/sort -V -C; then
NOLOCKING='--config=global{locking_type=0}'
else
NOLOCKING='--nolocking'
fi
}

function main() {

local -a entries=()
local run_status=0

parse_flags "$@"
get_nolocking_opts

for entry in "${entries[@]}"
do
Expand Down
13 changes: 0 additions & 13 deletions roles/shrink_lv/templates/shrink-start.sh.j2
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#!/bin/bash

disable_lvm_lock(){
tmpfile=$(/usr/bin/mktemp)
sed -e 's/\(^[[:space:]]*\)locking_type[[:space:]]*=[[:space:]]*[[:digit:]]/\1locking_type = 1/' /etc/lvm/lvm.conf >"$tmpfile"
status=$?
if [[ status -ne 0 ]]; then
echo "Failed to disable lvm lock: $status" >/dev/kmsg
exit 1
fi
# replace lvm.conf. There is no need to keep a backup since it's an ephemeral file, we are not replacing the original in the initramfs image file
mv "$tmpfile" /etc/lvm/lvm.conf
}

activate_volume_groups(){
for vg in `/usr/sbin/lvm vgs -o name --noheading 2>/dev/null`; do
/usr/sbin/lvm vgchange -ay $vg
Expand All @@ -20,7 +8,6 @@ activate_volume_groups(){

main() {
activate_volume_groups
disable_lvm_lock
/usr/bin/shrink.sh {% for device in shrink_lv_devices %}--device={{ device.device }}:{{ device.size }} {% endfor %} 1>&2 >/dev/kmsg
}

Expand Down

0 comments on commit c20329f

Please sign in to comment.