Bug 1419300

Summary: not a bug: logrotate -d makes no changes to logs (and state file)
Product: [Fedora] Fedora Reporter: Todd Warner <taw>
Component: logrotateAssignee: Kamil Dudka <kdudka>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 25CC: jkaluza, kdudka, taw, will_darton
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-02-06 07:52:16 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description Todd Warner 2017-02-05 01:58:49 UTC
Description of problem:

In order to test logrotate configurations, you use the commandline and the logrorate command directly, like this...

[root@XXX test]# /usr/sbin/logrotate -dvf /etc/logrotate.d/test

Well, it doesn't work.

Worse, logrotate *says* it worked without issue, when in fact it performed a no-op. If you run logrotate from the cron script... it does work just fine:
/etc/cron.daily/logrotate


This is maddening. Logrotate is 1000 years old. Heck, I am acquainted to two of the three original authors. Ugh. This needs to be fixed...


Version-Release number of selected component (if applicable):
logrotate-3.8.6-12.el7.x86_64
logrotate-3.9.2-5.fc24.x86_64
logrotate-3.10.0-1.fc25.x86_64



How reproducible:
Every time



##### Steps to Reproduce (the reproducer is trivial):

## SET THINGS UP...

# Become root
sudo su -

# Create a test directory and fake 1M debug.log in /var/log
cd /var/log
mkdir test
cd test
dd if=/dev/zero of=debug.log bs=1024 count=1024



# Let's look at that file...
[root@XXX test]# ll /var/log/test
total 1024
-rw-r--r--. 1 root root 1048576 Feb  4 20:20 debug.log
[root@XXX test]#



# Create logrotate config file to rotate this log file...
echo "/var/log/test/debug.log {
    rotate 5
    missingok
    notifempty
    compress
    maxsize 500k
    copytruncate
}" > /etc/logrotate.d/test




## DUPLICATE THE BUG...

[root@XXX test]# /usr/sbin/logrotate -dvf /etc/logrotate.d/test 

reading config file /etc/logrotate.d/test
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/test/debug.log  forced from command line (5 rotations)
empty log files are not rotated, log files >= 512000 are rotated earlier, old logs are removed
considering log /var/log/test/debug.log
  log needs rotating
rotating log /var/log/test/debug.log, log->rotateCount is 5
dateext suffix '-20170204'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/test/debug.log.5.gz to /var/log/test/debug.log.6.gz (rotatecount 5, logstart 1, i 5), 
renaming /var/log/test/debug.log.4.gz to /var/log/test/debug.log.5.gz (rotatecount 5, logstart 1, i 4), 
renaming /var/log/test/debug.log.3.gz to /var/log/test/debug.log.4.gz (rotatecount 5, logstart 1, i 3), 
renaming /var/log/test/debug.log.2.gz to /var/log/test/debug.log.3.gz (rotatecount 5, logstart 1, i 2), 
renaming /var/log/test/debug.log.1.gz to /var/log/test/debug.log.2.gz (rotatecount 5, logstart 1, i 1), 
renaming /var/log/test/debug.log.0.gz to /var/log/test/debug.log.1.gz (rotatecount 5, logstart 1, i 0), 
copying /var/log/test/debug.log to /var/log/test/debug.log.1
truncating /var/log/test/debug.log
compressing log with: /bin/gzip
removing old log /var/log/test/debug.log.6.gz
error: error opening /var/log/test/debug.log.6.gz: No such file or directory
[root@XXX test]#


## Notice that there is only an error associated to it running through the rotation and erroring on that debug.log.6.gz? Weird. NONE of those files exist.

## Looking in that directory... all of the above *suggests* we had log files changed, copied, and truncated. But nothing changed...

[root@XXX test]# ll /var/log/test
total 1024
-rw-r--r--. 1 root root 1048576 Feb  4 20:20 debug.log
[root@XXX test]#


## Running logrotate from cron scripts *does* work:

[root@XXX test]# /etc/cron.daily/logrotate
[root@XXX test]# ll
total 4
-rw-r--r--. 1 root root    0 Feb  4 20:41 debug.log
-rw-r--r--. 1 root root 1051 Feb  4 20:20 debug.log-20170204.gz
[root@XXX test]# 


This makes testing hard. Please fix it. Thank you. -todd

PS. Note: Found a years old ubuntu bug that may be related...
https://bugs.launchpad.net/ubuntu/+source/logrotate/+bug/1278193

Special thanks for Will Darton of Navy Federal for helping me troubleshoot this. :)

Comment 1 Kamil Dudka 2017-02-06 07:52:16 UTC
(In reply to Todd Warner from comment #0)
> In order to test logrotate configurations, you use the commandline and the
> logrorate command directly, like this...
> 
> [root@XXX test]# /usr/sbin/logrotate -dvf /etc/logrotate.d/test

The above command works as expected but you forget to look at logrotate(8) man page to check meaning of the options you are using.  If you want logrotate to make any changes to logs and/or state file, just do not use the -d option.  This is clearly documented:

    -d, --debug
        Turns on debug mode and implies -v.  In debug mode, no changes will
        be made to the logs or to the logrotate state file.

To avoid further confusion (and creation of misleading bug reports), you should also check what the -f option does:

    -f, --force
        Tells logrotate to force the rotation, even if it doesn't think this
        is necessary.  Sometimes this is useful after adding new entries to
        a logrotate config file, or if old log files have been removed by
        hand, as the new files will be created, and logging will continue
        correctly.