|
|
@@ -16,10 +16,31 @@ def get_jid(path): |
|
|
|
return str(lst[0]['jid'])
|
|
|
|
|
|
|
|
|
|
|
|
def jail_run(path, command):
|
|
|
|
def do_mounts(path, mounts):
|
|
|
|
print('mounts:', mounts)
|
|
|
|
for (source, target) in mounts:
|
|
|
|
if source.startswith('/'):
|
|
|
|
name = source
|
|
|
|
else:
|
|
|
|
name, _ = zfs_find(source, focker_type='volume')
|
|
|
|
name = zfs_mountpoint(name)
|
|
|
|
while target.startswith('/'):
|
|
|
|
target = target[1:]
|
|
|
|
subprocess.check_output(['mount', '-t', 'nullfs', name, os.path.join(path, target)])
|
|
|
|
|
|
|
|
|
|
|
|
def undo_mounts(path, mounts):
|
|
|
|
for (_, target) in reversed(mounts):
|
|
|
|
while target.startswith('/'):
|
|
|
|
target = target[1:]
|
|
|
|
subprocess.check_output(['umount', '-f', os.path.join(path, target)])
|
|
|
|
|
|
|
|
|
|
|
|
def jail_run(path, command, mounts=[]):
|
|
|
|
command = ['jail', '-c', 'host.hostname=' + os.path.split(path)[1], 'persist=1', 'mount.devfs=1', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + path, 'command', '/bin/sh', '-c', command]
|
|
|
|
print('Running:', ' '.join(command))
|
|
|
|
try:
|
|
|
|
do_mounts(path, mounts)
|
|
|
|
shutil.copyfile('/etc/resolv.conf', os.path.join(path, 'etc/resolv.conf'))
|
|
|
|
res = subprocess.run(command)
|
|
|
|
finally:
|
|
|
@@ -28,6 +49,7 @@ def jail_run(path, command): |
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
subprocess.run(['umount', '-f', os.path.join(path, 'dev')])
|
|
|
|
undo_mounts(path, mounts)
|
|
|
|
if res.returncode != 0:
|
|
|
|
# subprocess.run(['umount', os.path.join(path, 'dev')])
|
|
|
|
raise RuntimeError('Command failed')
|
|
|
@@ -43,7 +65,8 @@ def command_jail_run(args): |
|
|
|
break
|
|
|
|
zfs_run(['zfs', 'clone', base, name])
|
|
|
|
try:
|
|
|
|
jail_run(zfs_mountpoint(name), args.command)
|
|
|
|
mounts = list(map(lambda a: a.split(':'), args.mounts))
|
|
|
|
jail_run(zfs_mountpoint(name), args.command, mounts)
|
|
|
|
# subprocess.check_output(['jail', '-c', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + zfs_mountpoint(name), 'command', command])
|
|
|
|
finally:
|
|
|
|
# subprocess.run(['umount', zfs_mountpoint(name) + '/dev'])
|
|
|
|