我正在尝试做以下事情:
/mnt/mnt-swapniku
umount /mnt/mnt-swapniku
并释放循环设备示例代码如下。一切正常,直到卸载操作。 kpartx -d
和losetup -d
操作失败。
此处讨论了同样的问题:http://www.redhat.com/archives/libguestfs/2011-July/msg00007.html
2011年,Hivex修复了这个问题。不幸的是,它似乎并没有在我的机器上运行。令人惊讶的是,当我在终端上手动执行kpartx -d /dev/loop0
和losetup -d /dev/loop0
时,它运行正常。最初,我认为Hivex需要一些时间来释放资源,因此我也引入了延迟。但没有用。
任何人都可以在Python中帮我解决这个问题吗?
环境:
CentOs 6.5 + hivex-1.3.3-4.2.el6.x86_64 + python-hivex-1.3.3-4.2.el6.x86_64
代码(runonce_registry.py):
import hivex
import os
import time
cfg_root = '/mnt/mnt-swapniku/'
win_install_run_once_service_reg_keys = [
{'key':"Foo Service", 't':1, 'value':"C:\Windows\Temp\foo.exe\0".encode('utf-16le')}
]
def win_add_run_once_service(config_root, service_keys, silent = False):
'''
Add RunOnce Windows service registry
config_root:
Path of the C drive
silent:
Set True/False for logging info
Ref - http://libguestfs.org/hivex.3.html
'''
try:
h = hivex.Hivex(config_root + '/Windows/System32/config/SOFTWARE', write = True)
key = h.root()
for child in ('Microsoft', 'Windows', 'CurrentVersion'):
key = h.node_get_child(key, child)
runonce = h.node_get_child(key, "RunOnce")
if runonce is None:
runonce = h.node_add_child(key, "RunOnce")
h.node_set_values(runonce, service_keys)
h.commit(None)
if not silent:
print("Added Windows RunOnce service")
# Hivex cleanup
del h # or h.close()
time.sleep(1)
except RuntimeError:
print("Hivex encountered RuntimeError while adding Windows RunOnce service")
return False
return True
if __name__ == '__main__':
win_add_run_once_service(cfg_root, win_install_run_once_service_reg_keys)
os.system('umount ' + cfg_root)
os.system('kpartx -d /dev/loop0')
os.system('losetup -d /dev/loop0')
输出:
[root@dhcp-xxx-xx-xx-xx Windows_Registry]# losetup /dev/loop0 win2k8r2.x86_64.img
[root@dhcp-xxx-xx-xx-xx Windows_Registry]# kpartx -a /dev/loop0
[root@dhcp-xxx-xx-xx-xx Windows_Registry]# mount /dev/mapper/loop0p2 /mnt/mnt-swapniku/
[root@dhcp-xxx-xx-xx-xx Windows_Registry]# python runonce_registry.py
Added Windows RunOnce service
device-mapper: remove ioctl on loop0p2 failed: Device or resource busy
loop: can't delete device /dev/loop0: Device or resource busy