diff --git a/x.py b/x.py index 6f2470d..85dd432 100755 --- a/x.py +++ b/x.py @@ -13,6 +13,10 @@ TS_VRB = 2 CWD = os.path.dirname(os.path.abspath(__file__)) BUILD_D = f"{CWD}/build" +INITRAMFS_DIR = f"{BUILD_D}/linux/initramfs" +INITRAMFS_FILE_TMP = f"{BUILD_D}/linux/initramfs.cpio" +INITRAMFS_FILE = f"{BUILD_D}/linux/initramfs.cpio.gz" +KERNEL_FILE = F"{BUILD_D}/linux/KernelImage" def shift(arr): @@ -25,10 +29,10 @@ def timestamp(dt=datetime.now(), fmt="hours"): return f"{dt.hour:0>2}:{dt.minute:0>2}:{dt.second:0>2}" -def run(*cmds, parallel=False, tag=None, interactive=False): +def run(*cmds, parallel=False, tag=None, interactive=False, chdir=None): threads = [] - def run_single(cmd, tag): + def run_single(cmd, tag, chdir): env = os.environ.copy() env["MAKE_TERMOUT"] = "true" env["MAKE_TERMERR"] = "true" @@ -38,6 +42,7 @@ def run(*cmds, parallel=False, tag=None, interactive=False): cmd, shell=True, stdout=None if interactive else subprocess.PIPE, stderr=None if interactive else subprocess.STDOUT, + cwd=chdir if chdir else None, # text=None if interactive else True, ) as proc: if not interactive: @@ -62,11 +67,11 @@ def run(*cmds, parallel=False, tag=None, interactive=False): f"{tag}-{i}" if tag else None) if parallel: t = threading.Thread( - target=run_single, args=(command, current_tag)) + target=run_single, args=(command, current_tag, chdir)) t.start() threads.append(t) else: - run_single(command, current_tag) + run_single(command, current_tag, chdir) for t in threads: t.join() @@ -82,9 +87,9 @@ def cmd_configure(args): def cmd_help(): print(f"Usage: {__FILENAME} configure [component]") print("Components:") - print(" help - Show this help") - print(" kernel - The linux kernel") - print(" busybox - Busybox") + print(" help - Show this help") + print(" kernel - The linux kernel") + print(" busybox - Busybox") # $(MAKE) -C $(MAKEFILE_DIR)/kernel O=$(KERNEL_BUILD_DIR) KCONFIG_CONFIG="$(MAKEFILE_DIR)/.kernel-config" menuconfig subc = shift(args) @@ -107,13 +112,14 @@ def cmd_build(args): def cmd_help(): print(f"Usage: {__FILENAME} build [component]") print("Components:") - print(" help - Show this help") - print(" all - Builds everything") - print(" linux - Everything bellow") - print(" kernel - The linux kernel") - print(" busybox - Busybox") - print(" rust - Builds all of the rust components") - print(" img - Rebuilds the image") + print(" help - Show this help") + print(" all - Builds everything") + print(" linux - Everything bellow") + print(" kernel - The linux kernel") + print(" busybox - Busybox") + print(" initramfs - Busybox") + print(" rust - Builds all of the rust components") + print(" img - Rebuilds the image") def build_init(): run( @@ -125,6 +131,8 @@ def cmd_build(args): def build_kernel(args): run(f"make -C {CWD}/linux/kernel O={BUILD_D}/linux/kernel KCONFIG_CONFIG='{ CWD}/linux/.kernel-config' -j{MAX_JOBS}", tag="kernel") + run(f"cp { + BUILD_D}/linux/kernel/arch/x86_64/boot/bzImage {KERNEL_IMAGE}") def build_busybox(args): run( @@ -138,6 +146,38 @@ def cmd_build(args): def build_rust(args): todo("build_rust") + def build_initramfs(args): + run(f"rm -rf {INITRAMFS_DIR}") + run(f"mkdir -p {INITRAMFS_DIR}/{{etc,mnt,proc,run,srv,tmp,var,boot,dev,home,media,opt,root,sbin,sys,usr/bin,usr/lib}}") + + run("ln -s usr/bin bin", + "ln -s usr/lib lib", + chdir=INITRAMFS_DIR) + + init_script =\ + "#!/bin/sh\\n" +\ + "mount -t proc none /proc\\n" +\ + "mount -t sysfs none /sys\\n" +\ + "echo 'Successfully booted'\\n" +\ + "exec /bin/sh\\n" + + run(f"echo {init_script} > {INITRAMFS_DIR}/init") + + # run( + # f"chmod +x {INITRAMFS_DIR}/init", + # f"chmod +x {INITRAMFS_DIR}/sbin/init" + # ) + + run( + f"make -C {CWD}/linux/busybox O={BUILD_D}/linux/busybox KCONFIG_CONFIG='{ + CWD}/linux/.busybox-config' -j{MAX_JOBS} CONFIG_PREFIX={INITRAMFS_DIR} install", + ) + + run( + f"find {INITRAMFS_DIR} | cpio -o -H newc > {INITRAMFS_FILE_TMP}", + f"gzip -f {INITRAMFS_FILE_TMP}" + ) + def build_image(args): # for the later bootable.img or whatever file todo("build_image") @@ -169,16 +209,22 @@ def cmd_build(args): elif cmd == "img": build_init() build_image(args) + elif cmd == "initramfs": + build_initramfs(args) else: print(f"ERROR: Unknown component '{cmd}'") cmd_help() exit(1) +def cmd_qemu(args): + run(f"qemu-system-x86_64 -kernel {KERNEL_FILE} -initrd {INITRAMFS_FILE} -append \"rdinit=/usr/sbin/init console=ttyS0\" -serial mon:stdio") + AVAILABLE_SUBCOMMANDS = { "help": help, "configure": cmd_configure, - "build": cmd_build + "build": cmd_build, + "qemu": cmd_qemu, } @@ -188,6 +234,7 @@ def help(): print(" help - Show this help") print(" configure - Configure components") print(" build - Compile/build components") + print(" qemu - Run a vm with the current build") if __name__ == "__main__":