116 lines
5.1 KiB
Python
116 lines
5.1 KiB
Python
"""Private read/transform adapter using libostree; never edits boot entries.
|
|
|
|
The Rust service owns authorization, locking, durable intent and staged deployment.
|
|
"""
|
|
import ctypes as C
|
|
import json
|
|
import sys
|
|
|
|
ostree = C.CDLL('libostree-1.so.1')
|
|
glib = C.CDLL('libglib-2.0.so.0')
|
|
gobject = C.CDLL('libgobject-2.0.so.0')
|
|
P = C.c_void_p
|
|
S = C.c_char_p
|
|
|
|
|
|
def bind(lib, name, result, *args):
|
|
f = getattr(lib, name)
|
|
f.restype, f.argtypes = result, list(args)
|
|
return f
|
|
|
|
|
|
class Error(C.Structure):
|
|
_fields_ = [('domain', C.c_uint), ('code', C.c_int), ('message', S)]
|
|
|
|
|
|
class Array(C.Structure):
|
|
_fields_ = [('pdata', C.POINTER(P)), ('len', C.c_uint)]
|
|
|
|
|
|
def checked(f, *args):
|
|
error = C.POINTER(Error)()
|
|
if not f(*args, C.byref(error)):
|
|
message = error.contents.message.decode() if error else 'libostree operation failed'
|
|
if error:
|
|
bind(glib, 'g_error_free', None, P)(error)
|
|
raise ValueError(message)
|
|
|
|
|
|
def arguments(options, changes):
|
|
kargs = bind(ostree, 'ostree_kernel_args_from_string', P, S)(options)
|
|
try:
|
|
for change in changes:
|
|
op, arg = change['op'], change['argument']
|
|
if not arg or len(arg) > 4096 or any(c in arg for c in '\x00\r\n') or arg.split('=', 1)[0] == 'ostree':
|
|
raise ValueError('Invalid kernel argument or protected ostree selector')
|
|
# Native operations split their input again. Validate the complete
|
|
# token, not only its first key, before passing it to libostree.
|
|
if arg.count('"') % 2 or arguments(arg.encode(), []) != [arg]:
|
|
raise ValueError('Expected exactly one kernel argument with balanced quotes')
|
|
if op in ('append', 'append-if-missing'):
|
|
name = 'ostree_kernel_args_' + op.replace('-', '_')
|
|
bind(ostree, name, None, P, S)(kargs, arg.encode())
|
|
elif op in ('delete', 'delete-if-present', 'replace'):
|
|
name = 'ostree_kernel_args_' + ('new_replace' if op == 'replace' else op.replace('-', '_'))
|
|
checked(bind(ostree, name, C.c_int, P, S, C.POINTER(C.POINTER(Error))), kargs, arg.encode())
|
|
else:
|
|
raise ValueError('Unknown kernel argument operation')
|
|
vector = bind(ostree, 'ostree_kernel_args_to_strv', C.POINTER(S), P)(kargs)
|
|
try:
|
|
result, i = [], 0
|
|
while vector[i] is not None:
|
|
token = vector[i].decode()
|
|
if token.split('=', 1)[0] != 'ostree':
|
|
result.append(token)
|
|
i += 1
|
|
return result
|
|
finally:
|
|
bind(glib, 'g_strfreev', None, C.POINTER(S))(vector)
|
|
finally:
|
|
bind(ostree, 'ostree_kernel_args_free', None, P)(kargs)
|
|
|
|
|
|
def snapshot(changes):
|
|
root = bind(ostree, 'ostree_sysroot_new_default', P)()
|
|
try:
|
|
checked(bind(ostree, 'ostree_sysroot_load', C.c_int, P, P, C.POINTER(C.POINTER(Error))), root, None)
|
|
booted = bind(ostree, 'ostree_sysroot_get_booted_deployment', P, P)(root)
|
|
deployments = bind(ostree, 'ostree_sysroot_get_deployments', C.POINTER(Array), P)(root)
|
|
try:
|
|
result = []
|
|
options_by_id = {}
|
|
for index in range(deployments.contents.len):
|
|
d = deployments.contents.pdata[index]
|
|
osname = bind(ostree, 'ostree_deployment_get_osname', S, P)(d).decode()
|
|
if osname != 'archipelago':
|
|
continue
|
|
checksum = bind(ostree, 'ostree_deployment_get_csum', S, P)(d).decode()
|
|
serial = bind(ostree, 'ostree_deployment_get_deployserial', C.c_int, P)(d)
|
|
config = bind(ostree, 'ostree_deployment_get_bootconfig', P, P)(d)
|
|
if not config:
|
|
raise ValueError('Deployment boot configuration unavailable')
|
|
options = bind(ostree, 'ostree_bootconfig_parser_get', S, P, S)(config, b'options')
|
|
if options is None:
|
|
raise ValueError('Deployment kernel arguments unavailable')
|
|
result.append(dict(deployment_id=f'{osname}/{checksum}.{serial}', checksum=checksum,
|
|
booted=d == booted, staged=bool(bind(ostree, 'ostree_deployment_is_staged', C.c_int, P)(d)),
|
|
arguments=arguments(options, [])))
|
|
options_by_id[result[-1]['deployment_id']] = options
|
|
if changes is not None:
|
|
selected = next((d for d in result if d['staged']), None) or next((d for d in result if d['booted']), None)
|
|
if selected is None:
|
|
raise ValueError('No booted deployment')
|
|
selected['edited'] = arguments(options_by_id[selected['deployment_id']], changes)
|
|
return result
|
|
finally:
|
|
bind(glib, 'g_ptr_array_unref', None, C.POINTER(Array))(deployments)
|
|
finally:
|
|
bind(gobject, 'g_object_unref', None, P)(root)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
print(json.dumps(snapshot(json.loads(sys.argv[1]) if len(sys.argv) > 1 else None)))
|
|
except Exception as error:
|
|
print(str(error), file=sys.stderr)
|
|
sys.exit(1)
|