Skip to content

Commit

Permalink
Merge pull request #34 from arkane-systems/dev-0.14
Browse files Browse the repository at this point in the history
Dev 0.14
  • Loading branch information
cerebrate authored Nov 26, 2022
2 parents 860d5a2 + e5bac74 commit 06f091a
Show file tree
Hide file tree
Showing 27 changed files with 581 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ share/python-wheels/
*.egg
MANIFEST

# Dependencies
dp/

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
Expand Down
34 changes: 5 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

# Bottle-Imp version
IMPVERSION = 0.13
IMPVERSION = 1.0

# Determine this makefile's path.
# Be sure to place this BEFORE `include` directives, if any.
Expand All @@ -13,13 +13,12 @@ THIS_FILE := $(lastword $(MAKEFILE_LIST))
# the internal-package target.
INSTALLDIR = $(DESTDIR)/usr/lib/bottle-imp
BINDIR = $(DESTDIR)/usr/bin
SVCDIR = $(DESTDIR)/usr/lib/systemd/system
USRLIBDIR = $(DESTDIR)/usr/lib
GENDIR = $(DESTDIR)/lib/systemd/system-generators

# used only by TAR installer
MAN8DIR = $(DESTDIR)/usr/share/man/man8
DOCDIR = $(DESTDIR)/usr/share/doc/bottle-imp
ETCSVCDIR = $(DESTDIR)/etc/systemd/system

#
# Default target: list options
Expand Down Expand Up @@ -160,26 +159,13 @@ internal-package:
mkdir -p "$(BINDIR)"
install -Dm 6755 -o root "binsrc/imp-wrapper/imp" -t "$(BINDIR)"
install -Dm 0755 -o root "binsrc/out/imp" -t "$(INSTALLDIR)"
install -Dm 0755 -o root "binsrc/out/imp-generator" -t "$(GENDIR)"
install -Dm 0755 -o root "binsrc/out/imp-executor" -t "$(INSTALLDIR)"

# Runtime dir mapping and waiting
# scripts
install -Dm 0755 -o root "othersrc/scripts/imp-user-runtime-dir.sh" -t "$(INSTALLDIR)"
install -Dm 0755 -o root "othersrc/scripts/wait-forever.sh" -t "$(INSTALLDIR)"

# Systemd-as-container compensation services.
install -Dm 0644 -o root "othersrc/usr-lib/systemd/system/imp-fixshm.service" -T "$(SVCDIR)/imp-fixshm.service"
install -Dm 0644 -o root "othersrc/usr-lib/systemd/system/imp-pstorefs.service" -T "$(SVCDIR)/imp-pstorefs.service"
install -Dm 0644 -o root "othersrc/usr-lib/systemd/system/imp-securityfs.service" -T "$(SVCDIR)/imp-securityfs.service"
install -Dm 0644 -o root "othersrc/usr-lib/systemd/system/imp-remount-root-shared.service" -T "$(SVCDIR)/imp-remount-root-shared.service"

# WSLg mount file
install -Dm 0644 -o root "othersrc/usr-lib/systemd/system/imp-wslg-socket.service" -T "$(SVCDIR)/imp-wslg-socket.service"

# Unit override files.
install -Dm 0644 -o root "othersrc/usr-lib/systemd/system/user-runtime-dir@.service.d/override.conf" -t "$(SVCDIR)/user-runtime-dir@.service.d"

# binfmt.d
install -Dm 0644 -o root "othersrc/usr-lib/binfmt.d/WSLInterop.conf" -t "$(USRLIBDIR)/binfmt.d"

internal-clean:
make -C binsrc clean

Expand Down Expand Up @@ -209,16 +195,6 @@ internal-supplement:
mkdir -p $(MAN8DIR)
install -Dm 0644 -o root "$(TMPBUILDDIR)/imp.8.gz" -t $(MAN8DIR)

mkdir -p "$(ETCSVCDIR)/sysinit.target.wants"
ln -sr $(SVCDIR)/imp-fixshm.service $(ETCSVCDIR)/sysinit.target.wants/imp-fixshm.service
ln -sr $(SVCDIR)/imp-pstorefs.service $(ETCSVCDIR)/sysinit.target.wants/imp-pstorefs.service
ln -sr $(SVCDIR)/imp-securityfs.service $(ETCSVCDIR)/sysinit.target.wants/imp-securityfs.service
ln -sr $(SVCDIR)/imp-remount-root-shared.service $(ETCSVCDIR)/sysinit.target.wants/imp-remount-root-shared.service

mkdir -p "$(ETCSVCDIR)/multi-user.target.wants"
ln -sr $(SVCDIR)/imp-wslg-socket.service $(ETCSVCDIR)/multi-user.target.wants/imp-wslg-socket.service
ln -sr $(SVCDIR)/systemd-machined.service $(ETCSVCDIR)/multi-user.target.wants/systemd-machined.service

# Cleanup temporary directory
rm -rf $(TMPBUILDDIR)

Expand Down
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Maintainer: Alistair Young <avatar@arkane-systems.net>
pkgname=bottle-imp
pkgver=0.13
pkgver=1.0
pkgrel=1
pkgdesc="A helper for WSL's native systemd support."
arch=('x86_64')
Expand Down
23 changes: 20 additions & 3 deletions binsrc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,41 @@
#
# default target: build both for installation
#
build: build-wrapper build-imp
build: build-wrapper build-imp build-executor build-generator

build-wrapper:
mkdir -p out
make -C imp-wrapper

build-imp:
mkdir -p out
python3 -m pip install -r imp/requirements.txt --target imp --upgrade
python3 -m pip install -r imp/requirements.txt --target imp/dp --upgrade
python3 -m zipapp -o out/imp -p "/usr/bin/env python3" -c imp

build-executor:
mkdir -p out
python3 -m pip install -r imp-executor/requirements.txt --target imp-executor/dp --upgrade
python3 -m zipapp -o out/imp-executor -p "/usr/bin/env python3" -c imp-executor

build-generator:
mkdir -p out
python3 -m pip install -r imp-generator/requirements.txt --target imp-generator/dp --upgrade
python3 -m zipapp -o out/imp-generator -p "/usr/bin/env python3" -c imp-generator

#
# clean: clean up after a build/package
#
clean: clean-wrapper clean-imp
clean: clean-wrapper clean-imp clean-executor clean-generator
rm -rf out

clean-wrapper:
make -C imp-wrapper clean

clean-imp:
rm -f out/imp

clean-executor:
rm -f out/imp-executor

clean-generator:
rm -f out/imp-generator
91 changes: 91 additions & 0 deletions binsrc/imp-executor/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#! /usr/bin/env python3

import os
import sys

import mountie

def print_help_message():
"""Display a help message for imp-executor."""

print ("""imp-executor: internal functions for bottle-imp
You should not use this directly under normal circumstances.
It should only be called by bottle-imp provided services.
Operations:
help Display this message.
rrfs Remount root filesystem shared.
devshm Fix /dev/shm mount.
pstore Mount pstore filesystem.
security Mount security filesystem.
wslg Bind mount WSL .X11-unix.
""")


def fix_dev_shm():
"""Move the tmpfs for shared memory to /dev/shm and bind mount it from /run/shm."""
os.unlink ("/dev/shm")
os.mkdir ("/dev/shm")
mountie.mount ("/run/shm", "/dev/shm", "", mountie.MS_MOVE)
mountie.mount ("/dev/shm", "/run/shm", "", mountie.MS_BIND)
# os.rmdir ("/run/shm")
# os.symlink ("/dev/shm", "/run/shm")


def mount_pstore_filesystem():
"""Mount the pstore filesystem."""
mountie.mount ("pstore", "/sys/fs/pstore", "pstore",
mountie.MS_NOSUID | mountie.MS_NODEV | mountie.MS_NOEXEC)


def mount_security_filesystem():
"""Mount the security filesystem."""
mountie.mount ("securityfs", "/sys/kernel/security", "securityfs",
mountie.MS_NOSUID | mountie.MS_NODEV | mountie.MS_NOEXEC)


def remount_root_shared():
"""Remount the root filesystem shared."""
mountie.mount ("none", "/", "", mountie.MS_REC | mountie.MS_SHARED, "")


def remount_wslg():
"""Remount the WSLg socket in the appropriate place."""
mountie.mount ("/mnt/wslg/.X11-unix", "/tmp/.X11-unix", "",
mountie.MS_BIND | mountie.MS_RDONLY)
# required because flags other than MS_BIND ignored in first call.
mountie.mount ("none", "/tmp/.X11-unix", "",
mountie.MS_REMOUNT | mountie.MS_BIND | mountie.MS_RDONLY)


def entrypoint():
"""Entry point for the imp-executor."""

# Check the command-line arguments.
if (len (sys.argv) < 2):
sys.exit ("imp-executor requires the operation to perform")

operation = sys.argv[1]

if operation == "help":
print_help_message()
elif operation == "rrfs":
remount_root_shared()
elif operation == "pstore":
mount_pstore_filesystem()
elif operation == "security":
mount_security_filesystem()
elif operation == "devshm":
fix_dev_shm()
elif operation == "wslg":
remount_wslg()
else:
print ("imp-executor: operation not recognized")


entrypoint()

# End of file.
1 change: 1 addition & 0 deletions binsrc/imp-executor/configuration.py
62 changes: 62 additions & 0 deletions binsrc/imp-executor/mountie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Mount/unmount helper functions

import ctypes
import ctypes.util
import os


# Mount flags
MS_NONE = 0 # No flags.
MS_RDONLY = 1 # Mount read-only.
MS_NOSUID = 2 # Ignore suid and sgid bits.
MS_NODEV = 4 # Disallow access to device special files.
MS_NOEXEC = 8 # Disallow program execution.
MS_SYNCHRONOUS = 16 # Writes are synced at once.
MS_REMOUNT = 32 # Alter flags of a mounted filesystem.
MS_MANDLOCK = 64 # Allow mandatory locks on an FS.
MS_DIRSYNC = 128 # Directory modifications are synchronous.
MS_NOSYMFOLLOW = 256 # Do not follow symlinks.
MS_NOATIME = 1024 # Do not update access times.
MS_NODIRATIME = 2048 # Do not update directory access times.
MS_BIND = 4096 # Bind directory at different place.
MS_MOVE = 8192
MS_REC = 16384
MS_SILENT = 32768
MS_POSIXACL = 1 << 16 # VFS does not apply the umask.
MS_UNBINDABLE = 1 << 17 # Change to unbindable.
MS_PRIVATE = 1 << 18 # Change to private.
MS_SLAVE = 1 << 19 # Change to slave.
MS_SHARED = 1 << 20 # Set propagation type to shared.
MS_RELATIME = 1 << 21 # Update atime relative to mtime/ctime.
MS_KERNMOUNT = 1 << 22 # This is a kern_mount call.
MS_I_VERSION = 1 << 23 # Update inode I_version field.
MS_STRICTATIME = 1 << 24 # Always perform atime updates.
MS_LAZYTIME = 1 << 25 # Update the on-disk [acm]times lazily.
MS_ACTIVE = 1 << 30
MS_NOUSER = 1 << 31


# Unmount flags
MU_NONE = 0 # No flags.
# ... TODO: add these


libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p)
libc.umount2.argtypes = (ctypes.c_char_p, ctypes.c_int)


def mount (source: str, target: str, fs: str, flags: int = MS_NONE, options=''):
"""Mount a filesystem."""
ret = libc.mount(source.encode(), target.encode(), fs.encode(), flags, options.encode())
if ret < 0:
errno = ctypes.get_errno()
raise OSError(errno, f"Error mounting {source} ({fs}) on {target} with flags {flags} and options '{options}': {os.strerror(errno)}")


def unmount (target: str, flags: int = MU_NONE):
"""Unmount a filesystem."""
ret = libc.umount2(target.encode(), flags)
if ret < 0:
errno = ctypes.get_errno()
raise OSError(errno, f"Error unmounting {target}: {os.strerror(errno)}")
Empty file.
33 changes: 33 additions & 0 deletions binsrc/imp-generator/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! /usr/bin/env python3

import os
import sys

from definitions import Generatee

import genhelper
import generatees

def entrypoint():
"""Entry point for the imp-generator."""

# Check the command-line arguments; test the resulting normal dir path.
if (len (sys.argv) < 2):
sys.exit ("imp-generator requires the path of the target directory")

normal_dir = os.path.abspath(sys.argv[1])

if not os.path.exists(normal_dir):
sys.exit ("generated-file directory must exist")

# Create generator helper.
gh = genhelper.GeneratorHelper(normal_dir)

# Iterate through generatees.
for g in generatees.generatees:
g.generate(gh)


entrypoint()

# End of file.
1 change: 1 addition & 0 deletions binsrc/imp-generator/configuration.py
Loading

0 comments on commit 06f091a

Please sign in to comment.