Skip to content

Instantly share code, notes, and snippets.

@T0aD
Last active December 19, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T0aD/6004343 to your computer and use it in GitHub Desktop.
Save T0aD/6004343 to your computer and use it in GitHub Desktop.
Sexy script to remove GlusterFS extended attributes
#! /usr/bin/python
import os
import sys
from ctypes import *
# Attributes starting with the following will be REMOVED:
##
to_be_removed = ("trusted.gfid", "trusted.glusterfs", "trusted.afr")
"""
root@gluster1:/home/toad# setfattr -n trusted.sexy.boy -v toad /home/users/glusterfs-3.4.0.tar.gz
root@gluster1:/home/toad# ./remove_gluster_attr.py /home/users/
Removing GlusterFS attributes on /home/users
-- /home/users/glusterfs-3.4.0.tar.gz (trusted.sexy.boy)
"""
libc = CDLL('libc.so.6')
__listxattr = libc.listxattr
__removexattr = libc.removexattr
def listxattr(target):
nvals = []
val = '\x00'
rv = -1
i = 0
while rv == -1:
i += 1
val = '\x00' * i * 128
rv = __listxattr(target, val, len(val))
if rv > 0:
vals = val[:rv].split('\x00')
for v in vals:
if len(v) != 0:
nvals.append(v)
else:
break
# print target, nvals
return nvals
def remxattr(target, at):
__removexattr(target, at)
print '-- %s (%s)' % (target, at)
def checkpath(full):
attrs = listxattr(full)
# print full, attrs
# return
if len(attrs) != 0:
rem = []
for cur in attrs:
for a in to_be_removed:
if cur.startswith(a):
remxattr(full, cur)
# os.system("getfattr -m . -d %s" % full)
# for attr in attrs:
# os.system("setfattr -x %s %s 2> /dev/null" % (attr, full))
def remove_glusterfs_attributes(target):
print 'Removing GlusterFS attributes on', target
checkpath(target)
for p, d, f in os.walk(target):
for n in d:
full = os.path.join(p, n)
checkpath(full)
for n in f:
full = os.path.join(p, n)
checkpath(full)
if __name__ == "__main__":
cwd = os.getcwd() + '/'
# Get the target directory
# If no parameter was passed use the current working directory
if len(sys.argv) == 1:
sys.argv.append(cwd)
target = sys.argv[1]
# Generate the absolute path
if not target.startswith('/'):
target = cwd + target
target = os.path.realpath(target)
remove_glusterfs_attributes(target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment