debian-forge/stages/io.weldr.grub2
Tom Gundersen cebed27cd9 osbuild: drop the concept of an input_dir
This removes the possibility of passing in arbitrary input data. We
now restrict ourselves to explicitly specified files/directories or
a base tree given by its pipeline id.

This drops the tar/tree stages/assemblers, as the tree/untree ones
are implicit in osbuild, and if we wish to also support compressed
trees, then we should add that to osbuild core as an option.

Signed-off-by: Tom Gundersen <teg@jklm.no>
2019-07-03 13:11:37 +02:00

75 lines
2.5 KiB
Python
Executable file

#!/usr/bin/python3
import contextlib
import json
import math
import os
import shutil
import subprocess
import sys
import tempfile
def tree_size(tree):
size = 0
for root, dirs, files in os.walk(tree):
for entry in files + dirs:
path = os.path.join(root, entry)
size += os.stat(path, follow_symlinks=False).st_size
return size
@contextlib.contextmanager
def mount(source, dest, *options):
os.makedirs(dest, 0o755, True)
subprocess.run(["mount", *options, source, dest], check=True)
try:
yield
finally:
subprocess.run(["umount", "-R", dest], check=True)
@contextlib.contextmanager
def mount_api(dest):
with mount("/dev", f"{dest}/dev", "-o", "rbind"), \
mount("/proc", f"{dest}/proc", "-o", "rbind"), \
mount("/sys", f"{dest}/sys", "-o", "rbind"), \
mount("none", f"{dest}/run", "-t", "tmpfs"):
yield
@contextlib.contextmanager
def loop_device(image):
r = subprocess.run(["losetup", "--show", "--find", image], stdout=subprocess.PIPE, encoding="utf-8", check=True)
loop = r.stdout.strip()
try:
yield loop
finally:
subprocess.run(["losetup", "-d", loop], check=True)
def main(tree, options):
partition_table_id = options["partition_table_id"]
root_fs_uuid = options["root_fs_uuid"]
# Create the configuration file that determines how grub.cfg is generated.
os.makedirs(f"{tree}/etc/default", exist_ok=True)
with open(f"{tree}/etc/default/grub", "w") as default:
default.write("GRUB_TIMEOUT=0\n"
"GRUB_ENABLE_BLSCFG=true\n")
os.makedirs(f"{tree}/boot/grub2", exist_ok=True)
with open(f"{tree}/boot/grub2/grubenv", "w") as env:
env.write("# GRUB Environment Block\n"
f"GRUB2_ROOT_FS_UUID={root_fs_uuid}\n"
f"GRUB2_BOOT_FS_UUID={root_fs_uuid}\n"
f"kernelopts=root=UUID={root_fs_uuid} ro\n")
with open(f"{tree}/boot/grub2/grub.cfg", "w") as cfg:
cfg.write("set timeout=0\n"
"load_env\n"
"search --no-floppy --fs-uuid --set=root ${GRUB2_ROOT_FS_UUID}\n"
"search --no-floppy --fs-uuid --set=boot ${GRUB2_BOOT_FS_UUID}\n"
"function load_video {\n"
" insmod all_video\n"
"}\n"
"blscfg\n")
if __name__ == '__main__':
args = json.load(sys.stdin)
r = main(args["tree"], args["options"])
sys.exit(r)