From c7903e30f4af40a47c3e797961fa15a607d7e5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Holz?= Date: Wed, 7 Aug 2024 22:28:26 +0200 Subject: [PATCH] Meta: Combine boot drive environment variables into SERENITY_BOOT_DRIVE This allows you to simply use e.g. SERENITY_BOOT_DRIVE=usb instead of setting SERENITY_NVME_ENABLE=0 and SERENITY_USE_USBDRIVE=1. --- Meta/build-image-qemu.sh | 3 +- Meta/run.py | 70 ++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/Meta/build-image-qemu.sh b/Meta/build-image-qemu.sh index 16b3310760b..03b909337d7 100755 --- a/Meta/build-image-qemu.sh +++ b/Meta/build-image-qemu.sh @@ -3,6 +3,7 @@ set -e SCRIPT_DIR="$(dirname "${0}")" +# shellcheck source=/dev/null . "${SCRIPT_DIR}/shell_include.sh" USE_FUSE2FS=0 @@ -69,7 +70,7 @@ nearest_power_of_2() { done echo $p } -if [ "$SERENITY_ARCH" = "aarch64" ] || { [ -n "$SERENITY_USE_SDCARD" ] && [ "$SERENITY_USE_SDCARD" -eq 1 ]; }; then +if [ "$SERENITY_ARCH" = "aarch64" ] || [ "$SERENITY_BOOT_DRIVE" = "pci-sd" ]; then # SD cards must have a size that is a power of 2. The Aarch64 port loads from an SD card. DISK_SIZE_BYTES=$(nearest_power_of_2 "$DISK_SIZE_BYTES") fi diff --git a/Meta/run.py b/Meta/run.py index d75c94af1b8..df9c146d01c 100755 --- a/Meta/run.py +++ b/Meta/run.py @@ -83,6 +83,14 @@ class MachineType(Enum): ] +@unique +class BootDriveType(Enum): + NVMe = "nvme" + PCI_SD = "pci-sd" + USB = "usb" + VirtIOBLK = "virtio" + + def arguments_generator(prefix: str) -> Any: """ Construct an argument generator that returns some prefix and the member value(s) if the member value @@ -126,15 +134,11 @@ class Configuration: machine_type: MachineType = MachineType.Default enable_gdb: bool = False enable_gl: bool = False - # FIXME: Replace these three flags by a boot drive enum, see FIXME for boot_drive below. - nvme_enable: bool = True - sd_enable: bool = False - usb_boot_enable: bool = False - virtio_block_enable: bool = False screen_count: int = 1 host_ip: str = "127.0.0.1" ethernet_device_type: str = "e1000" disk_image: Path = Path("_disk_image") + boot_drive_type: BootDriveType = BootDriveType.NVMe # ## Low-level QEMU configuration # QEMU -append @@ -165,7 +169,6 @@ class Configuration: # Note that often, there are other network devices in the generic device list, added by specific machine types. network_default_device: str | None = None # QEMU -drive - # FIXME: Make an enum for the various boot drive options to handle boot drive selection more cleanly. boot_drive: str | None = None # Each is a QEMU -chardev character_devices: list[str] = field(default_factory=list) @@ -311,6 +314,17 @@ def determine_machine_type() -> MachineType: return MachineType.Default +def determine_boot_drive_type() -> BootDriveType: + provided_boot_drive_type = environ.get("SERENITY_BOOT_DRIVE") + if provided_boot_drive_type is not None: + try: + value = BootDriveType(provided_boot_drive_type) + except ValueError: + raise RunError(f"{provided_boot_drive_type} is not a valid SerenityOS boot drive type") + return value + return BootDriveType.NVMe + + def detect_ram_size() -> str | None: return environ.get("SERENITY_RAM_SIZE", "2G") @@ -603,45 +617,30 @@ def set_up_display_device(config: Configuration): def set_up_boot_drive(config: Configuration): - provided_nvme_enable = environ.get("SERENITY_NVME_ENABLE") - if provided_nvme_enable is not None: - config.nvme_enable = provided_nvme_enable == "1" - provided_sdcard_enable = environ.get("SERENITY_USE_SDCARD") - if provided_sdcard_enable is not None: - config.sd_enable = provided_sdcard_enable == "1" - provided_usb_boot_enable = environ.get("SERENITY_USE_USBDRIVE") - if provided_usb_boot_enable is not None: - config.usb_boot_enable = provided_usb_boot_enable == "1" - provided_virtio_block_enable = environ.get("SERENITY_USE_VIRTIOBLOCK") - if provided_virtio_block_enable is not None: - config.virtio_block_enable = provided_virtio_block_enable == "1" - if config.architecture == Arch.Aarch64: - config.boot_drive = f"file={config.disk_image},if=sd,format=raw,id=disk" - elif config.nvme_enable: - config.boot_drive = f"file={config.disk_image},format=raw,index=0,media=disk,if=none,id=disk" + config.boot_drive = f"file={config.disk_image},if=sd,format=raw,id=boot-drive" + return + + config.boot_drive = f"file={config.disk_image},if=none,format=raw,id=boot-drive" + + if config.boot_drive_type == BootDriveType.NVMe: config.add_devices( [ "i82801b11-bridge,id=bridge4", - "nvme,serial=deadbeef,drive=disk,bus=bridge4,logical_block_size=4096,physical_block_size=4096", + "nvme,serial=deadbeef,drive=boot-drive,bus=bridge4,logical_block_size=4096,physical_block_size=4096", ] ) config.kernel_cmdline.append("root=nvme0:1:0") - elif config.sd_enable: - config.boot_drive = f"id=sd-boot-drive,if=none,format=raw,file={config.disk_image}" - config.add_devices(["sdhci-pci", "sd-card,drive=sd-boot-drive"]) + elif config.boot_drive_type == BootDriveType.PCI_SD: + config.add_devices(["sdhci-pci", "sd-card,drive=boot-drive"]) config.kernel_cmdline.append("root=sd0:0:0") - elif config.usb_boot_enable: - config.boot_drive = f"if=none,id=usbstick,format=raw,file={config.disk_image}" - config.add_device("usb-storage,drive=usbstick") + elif config.boot_drive_type == BootDriveType.USB: + config.add_device("usb-storage,drive=boot-drive") # FIXME: Find a better way to address the usb drive config.kernel_cmdline.append("root=block3:0") - elif config.virtio_block_enable: - config.boot_drive = f"if=none,id=virtio-root,format=raw,file={config.disk_image}" - config.add_device("virtio-blk-pci,drive=virtio-root") - config.kernel_cmdline.append("root=lun3:0:0") - else: - config.boot_drive = f"file={config.disk_image},format=raw,index=0,media=disk,id=disk" + elif config.boot_drive_type == BootDriveType.VirtIOBLK: + config.add_device("virtio-blk-pci,drive=boot-drive") + config.kernel_cmdline.append("root=lun2:0:0") def determine_host_address() -> str: @@ -861,6 +860,7 @@ def configure_and_run(): config.machine_type = determine_machine_type() config.ram_size = detect_ram_size() config.host_ip = determine_host_address() + config.boot_drive_type = determine_boot_drive_type() serenity_src = environ.get("SERENITY_SOURCE_DIR") if serenity_src is None: