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