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