893e0b
#!/bin/bash
893e0b
# This util helps to reduce the workload of kdump service restarting
893e0b
# on udev event. When hotplugging memory / CPU, multiple udev
893e0b
# events may be triggered concurrently, and obviously, we don't want
893e0b
# to restart kdump service for each event.
893e0b
893e0b
# This script will be called by udev, and make sure kdump service is
893e0b
# restart after all events we are watching are settled.
893e0b
893e0b
# On each call, this script will update try to aquire the $throttle_lock
893e0b
# The first instance acquired the file lock will keep waiting for events
893e0b
# to settle and then reload kdump. Other instances will just exit
893e0b
# In this way, we can make sure kdump service is restarted immediately
893e0b
# and for exactly once after udev events are settled.
893e0b
893e0b
throttle_lock="/var/lock/kdump-udev-throttle"
893e0b
893e0b
exec 9>$throttle_lock
893e0b
if [ $? -ne 0 ]; then
893e0b
        echo "Failed to create the lock file! Fallback to non-throttled kdump service restart"
893e0b
        /bin/kdumpctl reload
893e0b
        exit 1
893e0b
fi
893e0b
893e0b
flock -n 9
893e0b
if [ $? -ne 0 ]; then
893e0b
        echo "Throttling kdump restart for concurrent udev event"
893e0b
        exit 0
893e0b
fi
893e0b
893e0b
# Wait for at least 1 second, at most 4 seconds for udev to settle
893e0b
# Idealy we will have a less than 1 second lag between udev events settle
893e0b
# and kdump reload
893e0b
sleep 1 && udevadm settle --timeout 3
893e0b
893e0b
# Release the lock, /bin/kdumpctl will block and make the process
893e0b
# holding two locks at the same time and we might miss some events
893e0b
exec 9>&-
893e0b
893e0b
/bin/kdumpctl reload
893e0b
893e0b
exit 0