binfmt: unify arch checking, rename is_registered to binfmt_is_registered
This commit is contained in:
parent
07ccc26d95
commit
162691e4b5
1 changed files with 15 additions and 7 deletions
22
binfmt.py
22
binfmt.py
|
@ -3,6 +3,7 @@
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
from constants import Arch, QEMU_ARCHES
|
from constants import Arch, QEMU_ARCHES
|
||||||
from exec.cmd import run_root_cmd
|
from exec.cmd import run_root_cmd
|
||||||
from utils import mount
|
from utils import mount
|
||||||
|
@ -39,16 +40,24 @@ def binfmt_info():
|
||||||
return full
|
return full
|
||||||
|
|
||||||
|
|
||||||
def is_registered(arch: Arch) -> bool:
|
def is_arch_known(arch: Arch, raise_exception: bool = False, action: Optional[str] = None) -> bool:
|
||||||
|
if arch not in QEMU_ARCHES:
|
||||||
|
if raise_exception:
|
||||||
|
raise Exception(f'binfmt{f".{action}()" if action else ""}: unknown arch {arch} (not in QEMU_ARCHES)')
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def binfmt_is_registered(arch: Arch) -> bool:
|
||||||
|
is_arch_known(arch, True, 'is_registered')
|
||||||
qemu_arch = QEMU_ARCHES[arch]
|
qemu_arch = QEMU_ARCHES[arch]
|
||||||
return os.path.exists("/proc/sys/fs/binfmt_misc/qemu-" + qemu_arch)
|
return os.path.exists("/proc/sys/fs/binfmt_misc/qemu-" + qemu_arch)
|
||||||
|
|
||||||
|
|
||||||
def register(arch: Arch):
|
def register(arch: Arch):
|
||||||
if arch not in QEMU_ARCHES:
|
is_arch_known(arch, True, 'register')
|
||||||
raise Exception(f'binfmt.register(): unknown arch {arch} (not in QEMU_ARCHES)')
|
|
||||||
qemu_arch = QEMU_ARCHES[arch]
|
qemu_arch = QEMU_ARCHES[arch]
|
||||||
if is_registered(arch):
|
if binfmt_is_registered(arch):
|
||||||
return
|
return
|
||||||
|
|
||||||
lines = binfmt_info()
|
lines = binfmt_info()
|
||||||
|
@ -69,14 +78,13 @@ def register(arch: Arch):
|
||||||
# Register in binfmt_misc
|
# Register in binfmt_misc
|
||||||
logging.info(f"Registering qemu binfmt ({arch})")
|
logging.info(f"Registering qemu binfmt ({arch})")
|
||||||
run_root_cmd(f'echo "{code}" > {register} 2>/dev/null')
|
run_root_cmd(f'echo "{code}" > {register} 2>/dev/null')
|
||||||
if not is_registered(arch):
|
if not binfmt_is_registered(arch):
|
||||||
logging.debug(f'binfmt line: {code}')
|
logging.debug(f'binfmt line: {code}')
|
||||||
raise Exception(f'Failed to register qemu-user for {arch} with binfmt_misc, {binfmt}/{info["name"]} not found')
|
raise Exception(f'Failed to register qemu-user for {arch} with binfmt_misc, {binfmt}/{info["name"]} not found')
|
||||||
|
|
||||||
|
|
||||||
def unregister(arch):
|
def unregister(arch):
|
||||||
if arch not in QEMU_ARCHES:
|
is_arch_known(arch, True, 'unregister')
|
||||||
raise Exception(f'binfmt.unregister(): unknown arch {arch} (not in QEMU_ARCHES)')
|
|
||||||
qemu_arch = QEMU_ARCHES[arch]
|
qemu_arch = QEMU_ARCHES[arch]
|
||||||
binfmt_file = "/proc/sys/fs/binfmt_misc/qemu-" + qemu_arch
|
binfmt_file = "/proc/sys/fs/binfmt_misc/qemu-" + qemu_arch
|
||||||
if not os.path.exists(binfmt_file):
|
if not os.path.exists(binfmt_file):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue