diff --git a/focker/focker.py b/focker/focker.py index 15c8034..140b56f 100644 --- a/focker/focker.py +++ b/focker/focker.py @@ -38,6 +38,7 @@ from .compose import \ command_compose_build, \ command_compose_run from .bootstrap import command_bootstrap +from .misc import focker_lock class ListForwarderFunctor(object): @@ -203,6 +204,7 @@ def create_parser(): def main(): + focker_lock() zfs_init() parser = create_parser() args = parser.parse_args() diff --git a/focker/jail.py b/focker/jail.py index a3898b2..793f21e 100644 --- a/focker/jail.py +++ b/focker/jail.py @@ -15,8 +15,6 @@ import os import jailconf from .mount import getmntinfo import shlex -import fcntl -# import pdb import stat @@ -38,18 +36,9 @@ def backup_file(fname, nbackups=10): def jail_conf_write(conf): - os.makedirs('/var/lock', exist_ok=True) - with open('/var/lock/focker.lock', 'a+') as f: - print('Waiting for /var/lock/focker.lock ...') - fcntl.flock(f, fcntl.LOCK_EX) - print('Lock acquired') - try: - bakname = backup_file('/etc/jail.conf') - os.chmod(bakname, 0o600) - conf.write('/etc/jail.conf') - finally: - fcntl.flock(f, fcntl.LOCK_UN) - print('Lock released') + bakname = backup_file('/etc/jail.conf') + os.chmod(bakname, 0o600) + conf.write('/etc/jail.conf') def jail_fs_create(image=None): diff --git a/focker/misc.py b/focker/misc.py index b6f837a..dee07d7 100644 --- a/focker/misc.py +++ b/focker/misc.py @@ -7,6 +7,8 @@ import random from .zfs import zfs_exists +import os +import fcntl def random_sha256_hexdigest(): @@ -19,3 +21,20 @@ def find_prefix(head, tail): if not zfs_exists(name): break return name + + +def focker_lock(): + os.makedirs('/var/lock', exist_ok=True) + if focker_lock.fd is None: + focker_lock.fd = open('/var/lock/focker.lock', 'a+') + print('Waiting for /var/lock/focker.lock ...') + fcntl.flock(focker_lock.fd, fcntl.LOCK_EX) + print('Lock acquired.') +focker_lock.fd = None + + +def focker_unlock(): + if focker_lock.fd is None: + return + fcntl.flock(focker_lock.fd, fcntl.LOCK_UN) + print('Lock released')