Merge "XenAPI: Refactor rotate_xen_guest_logs to avoid races"

This commit is contained in:
Jenkins
2015-07-20 13:00:18 +00:00
committed by Gerrit Code Review
4 changed files with 43 additions and 37 deletions
+16 -32
View File
@@ -18,48 +18,32 @@ log_dir="/var/log/xen/guest"
kb=1024
max_size_bytes=$(($kb*$kb))
truncated_size_bytes=$((5*$kb))
list_domains=/opt/xensource/bin/list_domains
log_file_base="${log_dir}/console."
tmp_file_base="${log_dir}/tmp.console."
# Ensure logging is setup correctly for all domains
xenstore-write /local/logconsole/@ "${log_file_base}%d"
# Move logs we want to keep
domains=$($list_domains | sed '/^id*/d' | sed 's/|.*|.*$//g' | xargs)
for i in $domains; do
log="${log_file_base}$i"
tmp="${tmp_file_base}$i"
mv $log $tmp || true
# Ensure the last_dom_id is set + updated for all running VMs
for vm in $(xe vm-list power-state=running --minimal | tr ',' ' '); do
xe vm-param-set uuid=$vm other-config:last_dom_id=$(xe vm-param-get uuid=$vm param-name=dom-id)
done
# Delete all console logs,
# mostly to remove logs from recently killed domains
rm -f ${log_dir}/console.*
# Get the last_dom_id for all VMs
valid_last_dom_ids=$(xe vm-list params=other-config --minimal | tr ';,' '\n\n' | grep last_dom_id | sed -e 's/last_dom_id: //g' | xargs)
# Reload domain list, in case it changed
# (note we may have just deleted a new console log)
domains=$($list_domains | sed '/^id*/d' | sed 's/|.*|.*$//g' | xargs)
for i in $domains; do
log="${log_file_base}$i"
tmp="${tmp_file_base}$i"
# Remove console files that do not correspond to valid last_dom_id's
allowed_consoles=".*console.\(${valid_last_dom_ids// /\\|}\)$"
find $log_dir -type f -not -regex $allowed_consoles -delete
if [ -e "$tmp" ]; then
size=$(stat -c%s "$tmp")
# Trim the log if required
if [ "$size" -gt "$max_size_bytes" ]; then
tail -c $truncated_size_bytes $tmp > $log || true
else
mv $tmp $log || true
fi
fi
# Truncate all remaining logs
for log in `find $log_dir -type f -regex '.*console.*' -size +${max_size_bytes}c`; do
tmp="$log.tmp"
tail -c $truncated_size_bytes $log > $tmp
mv -f $tmp $log
# Notify xen that it needs to reload the file
xenstore-write /local/logconsole/$i $log
xenstore-rm /local/logconsole/$i
domid=${log##*.}
xenstore-write /local/logconsole/$domid $log
xenstore-rm /local/logconsole/$domid
done
# Delete all the tmp files
rm -f ${tmp_file_base}* || true