Made LVM backing disk persistent

Previously, loop devices for LVM volume groups backing files were not
created after reboots, causing e.g. Cinder to fail with messages such
as

  ERROR cinder.service [-] Manager for service cinder-volume
  devstack@lvmdriver-1 is reporting problems, not sending
  heartbeat. Service will appear "down".

Now, we use systemd services to manage loop devices for backing files.

Change-Id: I27ec027834966e44aa9a99999358f5b4debc43e0
This commit is contained in:
Jakob Meng 2022-02-08 11:05:14 +01:00
parent f9f6208960
commit accd99e7cd
2 changed files with 43 additions and 22 deletions

View File

@ -0,0 +1,16 @@
[Unit]
Description=Activate LVM backing file %BACKING_FILE%
DefaultDependencies=no
After=systemd-udev-settle.service
Before=lvm2-activation-early.service
Wants=systemd-udev-settle.service
[Service]
ExecStart=/sbin/losetup --find --show %DIRECTIO% %BACKING_FILE%
ExecStop=/bin/sh -c '/sbin/losetup -d $$(/sbin/losetup --associated %BACKING_FILE% -O NAME -n)'
RemainAfterExit=yes
Type=oneshot
[Install]
WantedBy=local-fs.target
Also=systemd-udev-settle.service

49
lib/lvm
View File

@ -53,28 +53,10 @@ function _remove_lvm_volume_group {
sudo vgremove -f $vg
}
# _clean_lvm_backing_file() removes the backing file of the
# volume group
#
# Usage: _clean_lvm_backing_file() $backing_file
function _clean_lvm_backing_file {
local backing_file=$1
# If the backing physical device is a loop device, it was probably setup by DevStack
if [[ -n "$backing_file" ]] && [[ -e "$backing_file" ]]; then
local vg_dev
vg_dev=$(sudo losetup -j $backing_file | awk -F':' '/'$BACKING_FILE_SUFFIX'/ { print $1}')
if [[ -n "$vg_dev" ]]; then
sudo losetup -d $vg_dev
fi
rm -f $backing_file
fi
}
# clean_lvm_volume_group() cleans up the volume group and removes the
# backing file
#
# Usage: clean_lvm_volume_group $vg
# Usage: clean_lvm_volume_group() $vg
function clean_lvm_volume_group {
local vg=$1
@ -83,11 +65,22 @@ function clean_lvm_volume_group {
# if there is no logical volume left, it's safe to attempt a cleanup
# of the backing file
if [[ -z "$(sudo lvs --noheadings -o lv_name $vg 2>/dev/null)" ]]; then
_clean_lvm_backing_file $DATA_DIR/$vg$BACKING_FILE_SUFFIX
local backing_file=$DATA_DIR/$vg$BACKING_FILE_SUFFIX
if [[ -n "$vg$BACKING_FILE_SUFFIX" ]] && \
[[ -e "/etc/systemd/system/$vg$BACKING_FILE_SUFFIX.service" ]]; then
sudo systemctl disable --now $vg$BACKING_FILE_SUFFIX.service
sudo rm -f /etc/systemd/system/$vg$BACKING_FILE_SUFFIX.service
sudo systemctl daemon-reload
fi
# If the backing physical device is a loop device, it was probably setup by DevStack
if [[ -n "$backing_file" ]] && [[ -e "$backing_file" ]]; then
rm -f $backing_file
fi
fi
}
# _create_lvm_volume_group creates default volume group
#
# Usage: _create_lvm_volume_group() $vg $size
@ -106,8 +99,20 @@ function _create_lvm_volume_group {
directio="--direct-io=on"
fi
# Only create systemd service if it doesn't already exists
if [[ ! -e "/etc/systemd/system/$vg$BACKING_FILE_SUFFIX.service" ]]; then
sed -e "
s|%DIRECTIO%|${directio}|g;
s|%BACKING_FILE%|${backing_file}|g;
" $FILES/lvm-backing-file.template | sudo tee \
/etc/systemd/system/$vg$BACKING_FILE_SUFFIX.service
sudo systemctl daemon-reload
sudo systemctl enable --now $vg$BACKING_FILE_SUFFIX.service
fi
local vg_dev
vg_dev=$(sudo losetup -f --show $directio $backing_file)
vg_dev=$(sudo losetup --associated $backing_file -O NAME -n)
# Only create volume group if it doesn't already exist
if ! sudo vgs $vg; then