I have the following Python code to create an lvm snapshot on a Linux machine.
#!/usr/bin/env python3.1
import subprocess
import logging
logging.basicConfig(filename='/var/log/lvsnap.log', filemode='w', level=logging.DEBUG)
lvm_vg = 'vg00-crunchbang'
lvm_name = 'root'
lvm_snapshot_size = '100'
def lvmCreateSnapshot(lvm_vg, lvm_name, lvm_snapshot_size):
return subprocess.check_call(['lvcreate', '-s', '-l', '+' + lvm_snapshot_size + '%FREE', '-n', lvm_name + '-snapshot', lvm_vg + '/' + lvm_name])
logging.debug('logging is working before lvm snapshot')
''' create lvm snapshot '''
lvm_create_snapshot = lvmCreateSnapshot(lvm_vg, lvm_name, lvm_snapshot_size)
if lvm_create_snapshot:
logging.debug('create lvm snapshot of %s/%s exited with status %s', lvm_vg, lvm_name, lvm_create_snapshot)
logging.debug('logging is working after lvm snapshot')
lvmCreateSnapshot runs fine and exits with 0 which should then run the logging.debug line in the if statement. However this does not happen and instead I received the following output from the script:
> /tmp/lvmsnap.py
File descriptor 3 (/var/log/lvsnap.log) leaked on lvcreate invocation. Parent PID 7860: python3.1
Logical volume "root-snapshot" created
>
The output of the log is:
> cat /var/log/lvsnap.log
DEBUG:root:logging is working before lvm snapshot
DEBUG:root:logging is working after lvm snapshot
>
Which, as you can see has the lvm logging.debug message missing (it should appear between the 2 test logging messages I created).
Why is this happening and how can I fix it?