mirror of
https://github.com/fosslinux/live-bootstrap.git
synced 2026-03-17 00:35:25 +01:00
Support file:// mirrors
Spawns a simple HTTP server to host the mirror. Useful for testing mirror support or CI in chroot or bwrap modes.
This commit is contained in:
parent
ce1522db64
commit
7d50a224c6
4 changed files with 59 additions and 10 deletions
|
|
@ -37,8 +37,13 @@ download_source() {
|
||||||
check_source() {
|
check_source() {
|
||||||
local distfiles="${1}"
|
local distfiles="${1}"
|
||||||
local url="${2}"
|
local url="${2}"
|
||||||
local checksum="${3}"
|
shift 2
|
||||||
local fname="${4}"
|
if [[ "${url}" == git://* ]]; then
|
||||||
|
url="${1}"
|
||||||
|
shift
|
||||||
|
fi
|
||||||
|
local checksum="${1}"
|
||||||
|
local fname="${2}"
|
||||||
# Default to basename of url if not given
|
# Default to basename of url if not given
|
||||||
fname="${fname:-$(basename "${url}")}"
|
fname="${fname:-$(basename "${url}")}"
|
||||||
|
|
||||||
|
|
|
||||||
14
lib/simple_mirror.py
Normal file
14
lib/simple_mirror.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import http.server
|
||||||
|
import socketserver
|
||||||
|
|
||||||
|
class SimpleMirror(socketserver.TCPServer):
|
||||||
|
def __init__(self, directory: str):
|
||||||
|
self.directory = directory
|
||||||
|
super().__init__(("localhost", 0), self._handler)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def port(self):
|
||||||
|
return self.server_address[1]
|
||||||
|
|
||||||
|
def _handler(self, *args, **kwargs):
|
||||||
|
return http.server.SimpleHTTPRequestHandler(*args, directory=self.directory, **kwargs)
|
||||||
|
|
@ -12,7 +12,7 @@ import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def run(*args, **kwargs):
|
def run(*args, cleanup=None, **kwargs):
|
||||||
"""A small wrapper around subprocess.run"""
|
"""A small wrapper around subprocess.run"""
|
||||||
arguments = [str(arg) for arg in args if arg is not None]
|
arguments = [str(arg) for arg in args if arg is not None]
|
||||||
|
|
||||||
|
|
@ -23,6 +23,8 @@ def run(*args, **kwargs):
|
||||||
return subprocess.run(arguments, check=True, **kwargs)
|
return subprocess.run(arguments, check=True, **kwargs)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
print("Bootstrapping failed")
|
print("Bootstrapping failed")
|
||||||
|
if cleanup:
|
||||||
|
cleanup()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def run_as_root(*args, **kwargs):
|
def run_as_root(*args, **kwargs):
|
||||||
|
|
|
||||||
42
rootfs.py
42
rootfs.py
|
|
@ -16,10 +16,13 @@ you can run bootstap inside chroot.
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
|
import threading
|
||||||
|
|
||||||
from lib.utils import run, run_as_root
|
|
||||||
from lib.target import Target
|
|
||||||
from lib.generator import Generator, stage0_arch_map
|
from lib.generator import Generator, stage0_arch_map
|
||||||
|
from lib.simple_mirror import SimpleMirror
|
||||||
|
from lib.target import Target
|
||||||
|
from lib.utils import run, run_as_root
|
||||||
|
|
||||||
def create_configuration_file(args):
|
def create_configuration_file(args):
|
||||||
"""
|
"""
|
||||||
|
|
@ -177,6 +180,19 @@ def main():
|
||||||
# Set constant umask
|
# Set constant umask
|
||||||
os.umask(0o022)
|
os.umask(0o022)
|
||||||
|
|
||||||
|
# file:// mirrors
|
||||||
|
mirror_servers = []
|
||||||
|
if args.mirrors:
|
||||||
|
for i, mirror in enumerate(args.mirrors):
|
||||||
|
if mirror.startswith("file://"):
|
||||||
|
path = mirror.removeprefix("file://")
|
||||||
|
if not path.startswith("/"):
|
||||||
|
raise ValueError("A file:// mirror must be an absolute path.")
|
||||||
|
|
||||||
|
server = SimpleMirror(path)
|
||||||
|
args.mirrors[i] = f"http://127.0.0.1:{server.port}"
|
||||||
|
mirror_servers.append(server)
|
||||||
|
|
||||||
# bootstrap.cfg
|
# bootstrap.cfg
|
||||||
try:
|
try:
|
||||||
os.remove(os.path.join('steps', 'bootstrap.cfg'))
|
os.remove(os.path.join('steps', 'bootstrap.cfg'))
|
||||||
|
|
@ -193,15 +209,25 @@ def main():
|
||||||
if args.tmpfs:
|
if args.tmpfs:
|
||||||
target.tmpfs(size=args.tmpfs_size)
|
target.tmpfs(size=args.tmpfs_size)
|
||||||
|
|
||||||
|
for server in mirror_servers:
|
||||||
|
thread = threading.Thread(target=server.serve_forever)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def cleanup(*_):
|
||||||
|
for server in mirror_servers:
|
||||||
|
server.shutdown()
|
||||||
|
signal.signal(signal.SIGINT, cleanup)
|
||||||
|
|
||||||
generator = Generator(arch=args.arch,
|
generator = Generator(arch=args.arch,
|
||||||
external_sources=args.external_sources,
|
external_sources=args.external_sources,
|
||||||
repo_path=args.repo,
|
repo_path=args.repo,
|
||||||
early_preseed=args.early_preseed,
|
early_preseed=args.early_preseed,
|
||||||
mirrors=args.mirrors)
|
mirrors=args.mirrors)
|
||||||
|
|
||||||
bootstrap(args, generator, target, args.target_size)
|
bootstrap(args, generator, target, args.target_size, cleanup)
|
||||||
|
cleanup()
|
||||||
|
|
||||||
def bootstrap(args, generator, target, size):
|
def bootstrap(args, generator, target, size, cleanup):
|
||||||
"""Kick off bootstrap process."""
|
"""Kick off bootstrap process."""
|
||||||
print(f"Bootstrapping {args.arch}", flush=True)
|
print(f"Bootstrapping {args.arch}", flush=True)
|
||||||
if args.chroot:
|
if args.chroot:
|
||||||
|
|
@ -216,7 +242,8 @@ print(shutil.which('chroot'))
|
||||||
|
|
||||||
arch = stage0_arch_map.get(args.arch, args.arch)
|
arch = stage0_arch_map.get(args.arch, args.arch)
|
||||||
init = os.path.join(os.sep, 'bootstrap-seeds', 'POSIX', arch, 'kaem-optional-seed')
|
init = os.path.join(os.sep, 'bootstrap-seeds', 'POSIX', arch, 'kaem-optional-seed')
|
||||||
run_as_root('env', '-i', 'PATH=/bin', chroot_binary, generator.target_dir, init)
|
run_as_root('env', '-i', 'PATH=/bin', chroot_binary, generator.target_dir, init,
|
||||||
|
cleanup=cleanup)
|
||||||
|
|
||||||
elif args.bwrap:
|
elif args.bwrap:
|
||||||
init = '/init'
|
init = '/init'
|
||||||
|
|
@ -245,7 +272,8 @@ print(shutil.which('chroot'))
|
||||||
'--proc', '/proc',
|
'--proc', '/proc',
|
||||||
'--bind', '/sys', '/sys',
|
'--bind', '/sys', '/sys',
|
||||||
'--tmpfs', '/tmp',
|
'--tmpfs', '/tmp',
|
||||||
init)
|
init,
|
||||||
|
cleanup=cleanup)
|
||||||
|
|
||||||
elif args.bare_metal:
|
elif args.bare_metal:
|
||||||
if args.kernel:
|
if args.kernel:
|
||||||
|
|
@ -303,7 +331,7 @@ print(shutil.which('chroot'))
|
||||||
]
|
]
|
||||||
if not args.interactive:
|
if not args.interactive:
|
||||||
arg_list += ['-no-reboot', '-nographic']
|
arg_list += ['-no-reboot', '-nographic']
|
||||||
run(args.qemu_cmd, *arg_list)
|
run(args.qemu_cmd, *arg_list, cleanup=cleanup)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue