Skip to content

Commit

Permalink
Merge pull request #61 from hartwork/infer-lib-lib32-lib64-from-host-…
Browse files Browse the repository at this point in the history
…system

Infer `/lib{,32,64}` behavior from host system (alternative to #34)
  • Loading branch information
hartwork authored Sep 6, 2024
2 parents 4bc8230 + a1bc7f5 commit bda3476
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions sandwine/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ class AccessMode(Enum):


class MountMode(Enum):
DEVTMPFS = auto()
BIND_DEV = auto()
BIND_RO = auto()
BIND_RW = auto()
BIND_DEV = auto()
TMPFS = auto()
DEVTMPFS = auto()
PROC = auto()
SYMLINK = auto()
TMPFS = auto()


def parse_command_line(args):
Expand Down Expand Up @@ -225,6 +226,16 @@ class MountTask:
required: bool = True


def infer_mount_task(mode: MountMode, abs_target_path: str, required: bool = True) -> MountTask:
if os.path.islink(abs_target_path):
mode = MountMode.SYMLINK
source = os.readlink(abs_target_path)
else:
source = None

return MountTask(mode=mode, target=abs_target_path, source=source, required=required)


def random_hostname():
return ''.join(hex(random.randint(0, 15))[2:] for _ in range(12))

Expand All @@ -237,9 +248,9 @@ def create_bwrap_argv(config):
MountTask(MountMode.DEVTMPFS, '/dev'),
MountTask(MountMode.BIND_DEV, '/dev/dri'),
MountTask(MountMode.BIND_RO, '/etc'),
MountTask(MountMode.BIND_RO, '/lib'),
MountTask(MountMode.BIND_RO, '/lib32', required=False),
MountTask(MountMode.BIND_RO, '/lib64'),
infer_mount_task(MountMode.BIND_RO, '/lib'),
infer_mount_task(MountMode.BIND_RO, '/lib32', required=False),
infer_mount_task(MountMode.BIND_RO, '/lib64'),
MountTask(MountMode.PROC, '/proc'),
MountTask(MountMode.BIND_RO, '/sys'),
MountTask(MountMode.TMPFS, '/tmp'),
Expand Down Expand Up @@ -341,15 +352,17 @@ def create_bwrap_argv(config):
argv.add('--dev', mount_task.target)
elif mount_task.mode == MountMode.PROC:
argv.add('--proc', mount_task.target)
elif mount_task.mode in (MountMode.BIND_RO, MountMode.BIND_RW, MountMode.BIND_DEV):
elif mount_task.mode in (MountMode.BIND_RO, MountMode.BIND_RW, MountMode.BIND_DEV,
MountMode.SYMLINK):
if mount_task.source is None:
mount_task.source = mount_task.target

# NOTE: The X11 Unix socket will only show up later
keep_missing_source = X11Mode(
config.x11) != X11Mode.NONE and mount_task.target == x11_unix_socket

if not os.path.exists(mount_task.source) and not keep_missing_source:
if (mount_task.mode != MountMode.SYMLINK and not os.path.exists(mount_task.source)
and not keep_missing_source):
if mount_task.required:
_logger.error(
f'Path {mount_task.source!r} does not exist on the host, aborting.')
Expand All @@ -365,6 +378,8 @@ def create_bwrap_argv(config):
argv.add('--bind', mount_task.source, mount_task.target)
elif mount_task.mode == MountMode.BIND_DEV:
argv.add('--dev-bind', mount_task.source, mount_task.target)
elif mount_task.mode == MountMode.SYMLINK:
argv.add('--symlink', mount_task.source, mount_task.target)
else:
assert False, f'Mode {mount_task.mode} not supported'
else:
Expand Down

0 comments on commit bda3476

Please sign in to comment.