Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkt-38155: fix(middlewared/jail): handle start/stop gracefully using iocage #1591

Merged
merged 3 commits into from Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/freenas/etc/rc.shutdown.local
@@ -1,3 +1,4 @@
/usr/local/bin/midclt call core.event_send system ADDED '{"id": "shutdown"}' > /dev/null
/bin/sh /usr/local/sbin/save_rrds.sh
if [ -f /etc/killpower ]; then
/usr/local/sbin/upsdrvctl shutdown
Expand Down
34 changes: 25 additions & 9 deletions src/middlewared/middlewared/plugins/jail.py
@@ -1,3 +1,4 @@
import asyncio
import os
import re
import time
Expand All @@ -21,6 +22,8 @@
from middlewared.utils import filter_list
from middlewared.client import ClientException

SHUTDOWN_LOCK = asyncio.Lock()


class JailService(CRUDService):

Expand Down Expand Up @@ -613,19 +616,32 @@ def get_plugin_version(self, pkg):

return version

@private
def start_on_boot(self):
ioc.IOCage(rc=True).start()

@private
def stop_on_shutdown(self):
ioc.IOCage(rc=True).stop()

@private
async def terminate(self):
await SHUTDOWN_LOCK.acquire()


async def __event_system_ready(middleware, event_type, args):
async def __event_system(middleware, event_type, args):
"""
Method called when system is ready, supposed to start jails
Method called when system is ready or shutdown, supposed to start/stop jails
flagged that way.
"""
if args['id'] != 'ready':
return

jails = await middleware.call('jail.query', [('boot', '=', 'on')])
for jail in sorted(jails, key=lambda j: int(j['priority'])):
await middleware.call('jail.start', jail['id'])
# We need to call a method in Jail service to make sure it runs in the
# process pool because of py-libzfs thread safety issue with iocage and middlewared
if args['id'] == 'ready':
await middleware.call('jail.start_on_boot')
elif args['id'] == 'shutdown':
async with SHUTDOWN_LOCK:
await middleware.call('jail.stop_on_shutdown')


def setup(middleware):
middleware.event_subscribe('system', __event_system_ready)
middleware.event_subscribe('system', __event_system)