From 231582dd0f1c53423d2297e875c1e4eed465c841 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Tue, 16 Jun 2020 16:36:02 -0400 Subject: [PATCH] Workaround mtools on cluster fs When a lock is acquired by mtools, it does not ensure it releases the lock before exit. Workaround this by doing a blocking lock and unlock after any invocation of mtools. --- confluent_client/bin/dir2img | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/confluent_client/bin/dir2img b/confluent_client/bin/dir2img index 362b2c77..331eb365 100644 --- a/confluent_client/bin/dir2img +++ b/confluent_client/bin/dir2img @@ -2,6 +2,7 @@ # This will take a given directory and make a 'big floppy image' # out of it, suitable for nodemedia upload. +import fcntl import glob import os import subprocess @@ -26,10 +27,22 @@ def create_image(directory, image): imgfile.write(b'\x00') subprocess.check_call(['mformat', '-i', image, '-r', '16', '-d', '1', '-t', str(datasz), '-s', '1','-h', '1', '::']) + # Some clustered filesystems will have the lock from mformat + # linger after close (mformat doesn't unlock) + # do a blocking wait for shared lock and then explicitly + # unlock between calls to mtools + with open(image, 'rb') as imgfile: + fcntl.flock(imgfile.fileno(), fcntl.LOCK_SH) + fcntl.flock(imgfile.fileno(), fcntl.LOCK_UN) cpycmd = ['mcopy', '-i', image, '-s'] cpycmd.extend(glob.glob('{0}/*'.format(directory))) cpycmd.append('::') subprocess.check_call(cpycmd) + # though not necessary for us, make sure dir2img doesn't have a lingering + # flock from mcopy for any subsequent commands + with open(image, 'rb') as imgfile: + fcntl.flock(imgfile.fileno(), fcntl.LOCK_SH) + fcntl.flock(imgfile.fileno(), fcntl.LOCK_UN) if __name__ == '__main__':