挂载zookeeper到文件系统 (mount zookeeper)
-
zookeeper源码包中提供一个小工具zkfuse,可以用于将zookeeper挂载到文件系统,本文介绍编译及使用这个小工具的方法,并附有此小工具的部分改进及Bug修复代码。
-
编译使用zookeeper-3.4.6版本,从官网下载源码包:
wget http://apache.fayea.com/apache-mirror/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
-
解压后在
zookeeper-3.4.6/src/contrib/zkfuse
可找到zkfuse代码。 -
进入zookeeper-3.4.6/src/contrib/zkfuse,查看README.txt,里面有如下提示:
ZooKeeper FUSE (File System in Userspace) ========================================= Pre-requisites -------------- 1. Linux system with 2.6.X kernel. 2. Fuse (Filesystem in Userspace) must be installed on the build node. 3. Development build libraries: a. fuse b. log4cxx c. pthread d. boost
根据上面的文字提示,编译zkfuse需要的依赖包有:fuse、log4cxx、pthread、boost
在ubuntu 14.04下,fuse、boost、log4cxx均可用apt-get快速安装,pthread为系统自带支持,无需安装:
sudo apt-get install libfuse-dev sudo apt-get install libboost-dev sudo apt-get install liblog4cxx-dev
以上软件包均有源码包下载,其他版本的Unix/Linux系统可如无快速安装方法,可使用源码安装。
-
安装Zookeeper C Api
zkfuse的编译需要依赖Zookeeper C Api,此步在
zookeeper-3.4.6/src/c
下完成cd zookeeper-3.4.6/src/c ./configure make sudo make install # 确保有足够权限
-
编译zkfuse
此步在
zookeeper-3.4.6/src/contrib/zkfuse
下,按README.txt中的提示做即可:Build instructions ------------------ 1. cd into this directory 2. autoreconf -if 3. ./configure 4. make 5. zkfuse binary is under the src directory
如果未安装autoreconf,则先
sudo apt-get install autoconf
安装好。cd zookeeper-3.4.6/src/contrib/zkfuse autoreconf -if ./configure make
自动生成的makefile可能会有点小问题,如果编译提示类似如下内容:
undefined reference to `ZOO_CHILD_EVENT' undefined reference to `ZOO_CREATED_EVENT' undefined reference to `zoo_exists'
则将
src/Makefile
中:LIBS = -lulockmgr -lnsl -lrt -lpthread -llog4cxx -lfuse
修改添加上
-lzookeeper_mt
:LIBS = -lulockmgr -lnsl -lrt -lpthread -llog4cxx -lfuse -lzookeeper_mt
再次make,就能编译成功。
生成的可执行文件在 src/zkfuse 下
-
使用zkfuse挂载zookeeper到文件系统
README.txt中有如下使用说明:
Testing Zkfuse -------------- 1. Depending on permission on /dev/fuse, you may need to sudo -u root. * If /dev/fuse has permissions 0600, then you have to run Zkfuse as root. * If /dev/fuse has permissions 0666, then you can run Zkfuse as any user. 2. Create or find a mount point that you have "rwx" permission. * e.g. mkdir -p /tmp/zkfuse 3. Run Zkfuse as follows: zkfuse -z <hostspec> -m /tmp/zkfuse -d -z specifies ZooKeeper address(es) <host>:<port> -m specifies the mount point -d specifies the debug mode. For additional command line options, try "zkfuse -h".
在
zookeeper-3.4.6/src/contrib/zkfuse
下,执行:# 本例中zookeeper部署在127.0.0.1:2181端口 su root mkdir -p /zkfuse nohup ./zkfuse -z 127.0.0.1:2181 -m /zkfuse -d >> zkfuse.log 2>&1 &
清除挂载的话,需要kill掉zkfuse进程,然后umount挂载点(本例中为:
umount /zkfuse
) -
zkfuse已知BUG修复
目前实测,zkfuse一处BUG:读取数值为NULL的结点会coredump挂掉 另外zkfuse只支持大小不超过1k的结点,这是在代码中一处宏定义决定的,所以此处对zkfuse做了一些改造和BUG修复,改动的文件如下,可直接拷贝到
zookeeper-3.4.6/src/contrib/zkfuse/src
覆盖源文件即可修复。