From 83e7fbbf54f4ac52f639ee673d625730b65f463c Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Fri, 29 Dec 2023 03:03:49 +0100 Subject: [PATCH] mounter.py --- docs/_not_used/loop_test.sh | 3 +- init-server.py | 2 + scripts/http_host/downloader.py | 8 +-- scripts/http_host/mount_btrfs.sh | 5 -- scripts/http_host/mounter.py | 86 ++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 9 deletions(-) delete mode 100644 scripts/http_host/mount_btrfs.sh create mode 100755 scripts/http_host/mounter.py diff --git a/docs/_not_used/loop_test.sh b/docs/_not_used/loop_test.sh index 6787e0a..1be909b 100644 --- a/docs/_not_used/loop_test.sh +++ b/docs/_not_used/loop_test.sh @@ -4,9 +4,10 @@ source_folder="20231228_201550_pt" # Define the number of copies you want to make -number_of_copies=30 +number_of_copies=40 # Loop and copy the folder into c1, c2, c3, c4, ... for i in $(seq 1 $number_of_copies); do cp -r "$source_folder" "c$i" + btrfstune -m "c$i/tiles.btrfs" done \ No newline at end of file diff --git a/init-server.py b/init-server.py index 6d43924..d46c4ad 100755 --- a/init-server.py +++ b/init-server.py @@ -109,6 +109,7 @@ def prepare_http_host(c): for file in [ 'downloader.py', + 'mounter.py', ]: put( c, @@ -137,6 +138,7 @@ def debug_tmp(c): for file in [ 'downloader.py', + 'mounter.py', ]: put( c, diff --git a/scripts/http_host/downloader.py b/scripts/http_host/downloader.py index eaec808..626049e 100755 --- a/scripts/http_host/downloader.py +++ b/scripts/http_host/downloader.py @@ -12,7 +12,7 @@ DEFAULT_RUNS_DIR = Path('/data/ofm/http_host/runs') @click.command() -@click.option('--area', default='planet', help='The area to process') +@click.argument('area', required=False) @click.option('--version', default='latest', help='Version string, like "20231227_043106_pt"') @click.option( '--runs-dir', @@ -22,7 +22,7 @@ DEFAULT_RUNS_DIR = Path('/data/ofm/http_host/runs') @click.option('--list-versions', is_flag=True, help='List all versions in an area and terminate') def cli(area: str, version: str, list_versions: bool, runs_dir: Path): if area not in {'planet', 'monaco'}: - sys.exit('Area must be planet or monaco') + sys.exit('Please specify are: "planet" or "monaco"') r = requests.get(f'https://{area}.openfreemap.com/dirs.txt') r.raise_for_status() @@ -57,7 +57,9 @@ def download(area: str, version: str, runs_dir: Path): return temp_dir = runs_dir / '_tmp' - shutil.rmtree(temp_dir, ignore_errors=True) + if temp_dir.exists(): + sys.exit(f'{temp_dir} dir exists, please delete it first') + temp_dir.mkdir(parents=True) url = f'https://{area}.openfreemap.com/{version}/tiles.btrfs.gz' diff --git a/scripts/http_host/mount_btrfs.sh b/scripts/http_host/mount_btrfs.sh deleted file mode 100644 index 88883d9..0000000 --- a/scripts/http_host/mount_btrfs.sh +++ /dev/null @@ -1,5 +0,0 @@ -mkdir -p mnt_ro -sudo mount -v \ - -t btrfs \ - -o ro \ - image.btrfs mnt_ro \ No newline at end of file diff --git a/scripts/http_host/mounter.py b/scripts/http_host/mounter.py new file mode 100755 index 0000000..8b941a8 --- /dev/null +++ b/scripts/http_host/mounter.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +import os +import subprocess +import sys +from pathlib import Path + +import click + + +DEFAULT_RUNS_DIR = Path('/data/ofm/http_host/runs') + + +@click.command() +@click.option( + '--runs-dir', + help='Specify /runs directory', + type=click.Path(dir_okay=True, file_okay=False, path_type=Path), +) +def cli(runs_dir: Path): + if os.geteuid() != 0: + sys.exit('Needs sudo') + + if not runs_dir and not Path('/data/ofm').exists(): + sys.exit('Please specify a runs dir with --runs-dir') + + if not Path('/etc/fstab').exists(): + sys.exit('Needs to be run on Linux') + + if not runs_dir: + runs_dir = DEFAULT_RUNS_DIR + + clean_up_mounts() + + fstab_new = [] + + for area in ['planet', 'monaco']: + area_dir = (runs_dir / area).resolve() + if not area_dir.exists(): + continue + + versions = sorted(area_dir.iterdir()) + for version in versions: + version_str = version.name + btrfs_file = area_dir / version_str / 'tiles.btrfs' + if not btrfs_file.is_file(): + continue + + mnt_folder = Path('/mnt/ofm') / f'{area}-{version_str}' + mnt_folder.mkdir(exist_ok=True, parents=True) + + fstab_new.append(f'{btrfs_file} {mnt_folder} btrfs loop,ro 0 0\n') + print(f'Created fstab entry for {btrfs_file} -> {mnt_folder}') + + with open('/etc/fstab') as fp: + fstab_orig = [l for l in fp.readlines() if '/mnt/ofm/' not in l] + + with open('/etc/fstab', 'w') as fp: + fp.writelines(fstab_orig + fstab_new) + + print('Running mount -a') + subprocess.run(['mount', '-a'], check=True) + + clean_up_mounts() + print('DONE') + + +def clean_up_mounts(): + mnt_dir = Path('/mnt/ofm') + if not mnt_dir.exists(): + return + + print('Cleaning up mounts') + + with open('/etc/fstab') as fp: + fstab_str = fp.read() + + for subdir in mnt_dir.iterdir(): + if f'{subdir} ' in fstab_str: + continue + + subprocess.run(['umount', subdir]) + subdir.rmdir() + + +if __name__ == '__main__': + cli()