targetcli是建立起LIO用来把后台的存储资源映射给initiator的重要工具,它下面通过python
rtslib实现和iscsi(linux下对应iscsi_target_mod.ko驱动)和HBA卡(linux下对应tcm_qlax2xxx.ko驱动)等介质的交互,进而向initiator
展现逻辑卷(LUN)。为此,熟悉掌握targetcli工具对于我们搭建SAN/NAS环境,非常有必要。在实际项目当中,由于各种原因,可能导致targetcli命令执行失败。可以阅读targetcli的python代码调用过程,分析问题并解决问题。
笔者最近在使用targetcli 的过程中,碰到这样的错误:
[[email protected] ~]# targetcli
Traceback (most recent call last):
File "/usr/bin/targetcli", line 122, in <module>
main()
File "/usr/bin/targetcli", line 77, in main
shell = TargetCLI(‘~/.targetcli‘)
File "/usr/lib/python2.7/site-packages/configshell/shell.py", line 167, in __init__
self.prefs.load()
File "/usr/lib/python2.7/site-packages/configshell/prefs.py", line 147, in load
self._prefs = cPickle.load(fsock)
EOFError
刚开始怀疑版本问题,从正常运行的机器上重新拷贝了targetcli及其依赖的python库,问题依旧;重启机器和服务,现象还是没有得到解决。试图通过网上去搜索,也没有找到类似的现象。正在一筹莫展之际,突然注意到了错误提示中的一句话:
File "/usr/bin/targetcli", line 122, in <module>
main()
File "/usr/bin/targetcli", line 77, in main
shell = TargetCLI(‘~/.targetcli‘)
根据错误提示和下面的python源码,它好像是去访问./.targetcli目录,从里面加载一些缓存的偏好设置。
[[email protected] ~]# vim /usr/lib/python2.7/site-packages/configshell/shell.py +167
为此,直接进入目录,[[email protected] ~]# ls -arlt ~/.targetcli,检查下它到底有什么东西:
[[email protected] .targetcli]# pwd
/root/.targetcli
[[email protected] .targetcli]# ls -alrt
total 20
drwxr-xr-x. 2 root root 54 Aug 1 21:51 .
-rw-r--r--. 1 root root 503 Aug 1 21:51 prefs.bin
-rw-r--r--. 1 root root 8 Aug 11 06:57 history.txt
-rw-r--r--. 1 root root 7933 Sep 8 03:47 log.txt
dr-xr-x---. 41 root root 4096 Sep 20 07:32 ..
一看没有实际关键的内容,果断删除,然后重新运行targetcli:
[[email protected] ~]# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb37
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type ‘help‘.
/> ls
o-
/
......................................................................................................................
[...]
o- backstores
...........................................................................................................
[...]
| o- block
...............................................................................................
[Storage Objects: 0]
| o- fileio
..............................................................................................
[Storage Objects: 0]
| o- pscsi
...............................................................................................
[Storage Objects: 0]
| o- ramdisk
.............................................................................................
[Storage Objects: 0]
o- iscsi
.........................................................................................................
[Targets: 0]
o- loopback
......................................................................................................
[Targets: 0]
o- qla2xxx
.......................................................................................................
[Targets: 0]
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
好了,问题搞定! 这个问题,结合上面"EOFError ”来看,很可能是最近机器异常掉电导致prefs.bin文件损坏所致。
这个问题的解决给我的一个启迪是,了解了targetcli的后台实现层次之后,碰到问题,即便对不太熟悉的python,只有好好利用出错的输出和具体执行的脚本,仔细分析照样可以找到蛛丝马迹并解决问题。