我有以下 Python 代码在 Linux 机器上创建 lvm 快照。
#!/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 运行良好并以 0 退出,然后应该在 if 语句中运行 logging.debug 行。但是,这并没有发生,而是我从脚本中收到了以下输出:
> /tmp/lvmsnap.py
File descriptor 3 (/var/log/lvsnap.log) leaked on lvcreate invocation. Parent PID 7860: python3.1
Logical volume "root-snapshot" created
>
日志的输出是:
> cat /var/log/lvsnap.log
DEBUG:root:logging is working before lvm snapshot
DEBUG:root:logging is working after lvm snapshot
>
如您所见,其中缺少 lvm logging.debug 消息(它应该出现在我创建的 2 条测试日志消息之间)。
为什么会发生这种情况,我该如何解决?