From 342007c397a5aaf54610dd0fdb88359653f008a9 Mon Sep 17 00:00:00 2001 From: VasilyZa <450270247@qq.com> Date: Tue, 24 Nov 2026 16:57:52 +0800 Subject: [PATCH 1/2] feat(storage): integrate VirtIO-Block into ReliefOS Package native VirtIO-Block modules for early boot, recognize VirtIO root volumes and disks, and provide an opt-in QEMU controller. Keep AHCI unchanged by default and reject unsupported 4Kn partition mutations. --- Documentation/admin-guide/installation.rst | 26 ++++ .../LC_MESSAGES/admin-guide/installation.po | 42 +++++ .../LC_MESSAGES/admin-guide/installation.po | 36 +++++ kernel/reliefnt | 2 +- scripts/reliefos/Makefile.modules | 6 +- scripts/reliefos/Makefile.run | 20 ++- system/initramfs/init | 2 +- tools/tests/blockdev_errno_test.c | 59 ++++++- userland/runtime/src/blockdev.c | 145 +++++++++++------- userland/runtime/src/bootloader.c | 2 +- 10 files changed, 268 insertions(+), 72 deletions(-) diff --git a/Documentation/admin-guide/installation.rst b/Documentation/admin-guide/installation.rst index 2fea1c7a..d9dc724f 100644 --- a/Documentation/admin-guide/installation.rst +++ b/Documentation/admin-guide/installation.rst @@ -14,6 +14,32 @@ 安装器运行时、Live 会话和安装后的系统是不同的根文件系统。 只有实际安装并移除 ISO 后启动成功,才能证明安装后的系统可用。 +QEMU/KVM 的 VirtIO 磁盘 +----------------------- + +默认内核可以识别现代 VirtIO-Block PCI 磁盘,设备名为 ``/dev/vda``、 +``/dev/vdb`` 等,分区名例如 ``/dev/vda1``。使用 QEMU 时,可将目标镜像 +连接到 ``virtio-blk-pci``;显式指定 ``disable-legacy=on`` 可以创建只暴露 +现代接口的设备。项目的运行目标可用 ``RELIEFOS_QEMU_VIRTIO=1`` 选择 +此控制器;默认仍使用 AHCI。直接使用 QEMU 时,可用以下参数连接一个单独 +准备的 ``target.raw`` 镜像: + +.. code-block:: sh + + -drive if=none,id=vblk0,format=raw,file=target.raw \ + -device virtio-blk-pci,drive=vblk0,disable-legacy=on + +将 ``CONFIG_VIRTIO_BLK`` 设为 ``m`` 时,镜像构建会把匹配内核的 +``virtio_blk.ko`` 和所需依赖放入 initramfs,在查找和挂载真实根之前加载。 +可以用 ``root=PARTUUID=...``、``root=UUID=...`` 或 ``root=LABEL=...`` 指定根; +不要用磁盘连接顺序代替持久身份。 + +安装器新建或修改分区及格式化时仍要求目标磁盘的逻辑块为 512 字节。 +4Kn 磁盘支持现有 GPT 分区的查询和文件系统探测,可挂载预先准备好的 +ext4 分区;这不等同于安装器支持在 4Kn 磁盘上全新安装,也不等同于 +固件或引导器已从该磁盘启动。VirtIO-Block 当前不能作为 swap 后端。 +这不是 VMware PVSCSI;VMware 验证未完成。 + 安装介质布局 ============ diff --git a/Documentation/locale/en/LC_MESSAGES/admin-guide/installation.po b/Documentation/locale/en/LC_MESSAGES/admin-guide/installation.po index 1113d9e7..422fcc78 100644 --- a/Documentation/locale/en/LC_MESSAGES/admin-guide/installation.po +++ b/Documentation/locale/en/LC_MESSAGES/admin-guide/installation.po @@ -568,3 +568,45 @@ msgstr "" "and continue in the console. Configure the desktop and swap file in the " "separate System options step. The final review presents the choices for " "verification without adding new settings just before installation." + +msgid "QEMU/KVM 的 VirtIO 磁盘" +msgstr "VirtIO disks in QEMU/KVM" + +msgid "" +"默认内核可以识别现代 VirtIO-Block PCI 磁盘,设备名为 ``/dev/vda``、 ``/dev/vdb`` 等,分区名例如 " +"``/dev/vda1``。使用 QEMU 时,可将目标镜像 连接到 ``virtio-blk-pci``;显式指定 ``disable-" +"legacy=on`` 可以创建只暴露 现代接口的设备。项目的运行目标可用 ``RELIEFOS_QEMU_VIRTIO=1`` 选择 " +"此控制器;默认仍使用 AHCI。直接使用 QEMU 时,可用以下参数连接一个单独 准备的 ``target.raw`` 镜像:" +msgstr "" +"The default kernel recognizes modern VirtIO-Block PCI disks as " +"``/dev/vda``, ``/dev/vdb`` and so on, with partition names such as " +"``/dev/vda1``. In QEMU, attach the target image to ``virtio-blk-pci``. " +"Set ``disable-legacy=on`` to expose only the modern interface. Set " +"``RELIEFOS_QEMU_VIRTIO=1`` with the project run targets to select this " +"controller; AHCI remains the default. To attach a separately prepared " +"``target.raw`` image directly with QEMU, use these options:" + +msgid "" +"将 ``CONFIG_VIRTIO_BLK`` 设为 ``m`` 时,镜像构建会把匹配内核的 ``virtio_blk.ko`` 和所需依赖放入 " +"initramfs,在查找和挂载真实根之前加载。 可以用 ``root=PARTUUID=...``、``root=UUID=...`` 或 " +"``root=LABEL=...`` 指定根; 不要用磁盘连接顺序代替持久身份。" +msgstr "" +"With ``CONFIG_VIRTIO_BLK=m``, the image build puts ``virtio_blk.ko`` and " +"any required dependencies for the matching kernel in the initramfs. They " +"load before the real root is located and mounted. Specify the root with " +"``root=PARTUUID=...``, ``root=UUID=...``, or ``root=LABEL=...`` rather " +"than relying on disk attachment order." + +msgid "" +"安装器新建或修改分区及格式化时仍要求目标磁盘的逻辑块为 512 字节。 4Kn 磁盘支持现有 GPT 分区的查询和文件系统探测,可挂载预先准备好的" +" ext4 分区;这不等同于安装器支持在 4Kn 磁盘上全新安装,也不等同于 固件或引导器已从该磁盘启动。VirtIO-Block 当前不能作为 " +"swap 后端。 这不是 VMware PVSCSI;VMware 验证未完成。" +msgstr "" +"The installer still requires 512-byte logical blocks when creating or " +"editing partitions or formatting the target. On 4Kn disks it can inspect " +"existing GPT partitions and identify their filesystems; a prepared ext4 " +"partition can be mounted. This does not mean that the installer can " +"perform a fresh installation on a 4Kn disk or that firmware and the " +"bootloader have been tested booting from it. VirtIO-Block is not yet " +"supported as a swap backend. It is not VMware PVSCSI; VMware validation " +"is incomplete." diff --git a/Documentation/locale/ja/LC_MESSAGES/admin-guide/installation.po b/Documentation/locale/ja/LC_MESSAGES/admin-guide/installation.po index 9e7a0803..3b52dc9d 100644 --- a/Documentation/locale/ja/LC_MESSAGES/admin-guide/installation.po +++ b/Documentation/locale/ja/LC_MESSAGES/admin-guide/installation.po @@ -435,3 +435,39 @@ msgid "" msgstr "" "インストール前は「戻る」で前の設定を変更でき、グラフィカル画面のサイドバーからも訪問済みの設定へ戻れます。ディスク、起動方式、パーティション、システム設定を変更すると、現在の選択から確認画面を再生成します。パスワードは確認画面やログに表示しません。グラフィカル画面ではライセンス確認欄に明示的にチェックし、コンソールでは「同意して続行」で確認します。デスクトップと" " swap ファイルは独立したシステム設定の手順で指定し、最後の確認画面ではインストール直前に新しい設定項目を追加しません。" + +msgid "QEMU/KVM 的 VirtIO 磁盘" +msgstr "QEMU/KVM の VirtIO ディスク" + +msgid "" +"默认内核可以识别现代 VirtIO-Block PCI 磁盘,设备名为 ``/dev/vda``、 ``/dev/vdb`` 等,分区名例如 " +"``/dev/vda1``。使用 QEMU 时,可将目标镜像 连接到 ``virtio-blk-pci``;显式指定 ``disable-" +"legacy=on`` 可以创建只暴露 现代接口的设备。项目的运行目标可用 ``RELIEFOS_QEMU_VIRTIO=1`` 选择 " +"此控制器;默认仍使用 AHCI。直接使用 QEMU 时,可用以下参数连接一个单独 准备的 ``target.raw`` 镜像:" +msgstr "" +"既定のカーネルはモダンな VirtIO-Block PCI ディスクを ``/dev/vda``、``/dev/vdb`` " +"などとして認識し、パーティション名は ``/dev/vda1`` のようになります。QEMU では対象イメージを ``virtio-blk-" +"pci`` に接続します。``disable-legacy=on`` " +"を指定するとモダンインターフェースだけを公開できます。プロジェクトの実行ターゲットで ``RELIEFOS_QEMU_VIRTIO=1`` " +"を指定するとこのコントローラーを選択できます。既定は引き続き AHCI です。別途用意した ``target.raw`` を QEMU " +"で直接接続する場合は、次のオプションを使います。" + +msgid "" +"将 ``CONFIG_VIRTIO_BLK`` 设为 ``m`` 时,镜像构建会把匹配内核的 ``virtio_blk.ko`` 和所需依赖放入 " +"initramfs,在查找和挂载真实根之前加载。 可以用 ``root=PARTUUID=...``、``root=UUID=...`` 或 " +"``root=LABEL=...`` 指定根; 不要用磁盘连接顺序代替持久身份。" +msgstr "" +"``CONFIG_VIRTIO_BLK=m`` の場合、イメージのビルド時に対応するカーネルの ``virtio_blk.ko`` " +"と必要な依存モジュールが initramfs " +"に入ります。実ルートを探してマウントする前に読み込まれます。接続順序には依存せず、``root=PARTUUID=...``、``root=UUID=...``、``root=LABEL=...``" +" のいずれかでルートを指定してください。" + +msgid "" +"安装器新建或修改分区及格式化时仍要求目标磁盘的逻辑块为 512 字节。 4Kn 磁盘支持现有 GPT 分区的查询和文件系统探测,可挂载预先准备好的" +" ext4 分区;这不等同于安装器支持在 4Kn 磁盘上全新安装,也不等同于 固件或引导器已从该磁盘启动。VirtIO-Block 当前不能作为 " +"swap 后端。 这不是 VMware PVSCSI;VMware 验证未完成。" +msgstr "" +"インストーラーでパーティションの作成・変更やフォーマットを行うには、引き続き論理ブロックが 512 バイトのディスクが必要です。4Kn " +"ディスクでは既存の GPT パーティションとファイルシステムを調べることができ、準備済みの ext4 パーティションをマウントできます。4Kn " +"ディスクへの新規インストールや、そのディスクからのファームウェア・ブートローダー起動を確認したわけではありません。VirtIO-Block " +"は現時点でスワップのバックエンドに使えません。VMware PVSCSI とは別のドライバーであり、VMware での検証は未完了です。" diff --git a/kernel/reliefnt b/kernel/reliefnt index 8cf925a2..b5a7f1bf 160000 --- a/kernel/reliefnt +++ b/kernel/reliefnt @@ -1 +1 @@ -Subproject commit 8cf925a2c1bd1ed4279c1223a253ab1569dbaf68 +Subproject commit b5a7f1bf40342b0a947475d0ed3c3ae488448332 diff --git a/scripts/reliefos/Makefile.modules b/scripts/reliefos/Makefile.modules index e773dcc1..bbf36c85 100644 --- a/scripts/reliefos/Makefile.modules +++ b/scripts/reliefos/Makefile.modules @@ -7,9 +7,9 @@ RELIEFOS_KERNEL_RECOVERY := $(O)/userland/initramfs/kernel-bundle.elf RELIEFOS_KERNEL_PAYLOAD := $(O_STAGE)/kernel-payload RELIEFOS_KERNEL_PAYLOAD_MANIFEST := $(RELIEFOS_KERNEL_PAYLOAD)/kernel-payload.json RELIEFOS_MODULE_TOOLS := $(RELIEFOS_KERNEL_TOOLS) -# Images support all three currently offered virtual disk transports. Disabling -# one explicitly rejects this portable profile instead of silently forcing y. -RELIEFOS_BOOT_FEATURES ?= SATA_AHCI PATA_IDE BLK_DEV_NVME EXT4_FS FAT_FS ISO9660_FS +# Portable images support the listed virtual disk transports. Disabling +# one explicitly rejects this profile instead of silently forcing y. +RELIEFOS_BOOT_FEATURES ?= SATA_AHCI PATA_IDE BLK_DEV_NVME VIRTIO_BLK EXT4_FS FAT_FS ISO9660_FS $(RELIEFOS_MODULES_MANIFEST): $(RELIEFNT_DEST)/install.bundle.json $(INITRAMFS_BUSYBOX_ELF) $(RELIEFOS_MODULE_TOOLS) $(Q)$(RELIEFOS_KERNEL_TOOLS) modules-stage $(abspath $(RELIEFNT_DEST)) \ $(abspath $(INITRAMFS_BUSYBOX_ELF)) $(abspath $(RELIEFOS_MODULES_STAGE)) $(RELIEFOS_BOOT_FEATURES) diff --git a/scripts/reliefos/Makefile.run b/scripts/reliefos/Makefile.run index 0ca54728..72480bfb 100644 --- a/scripts/reliefos/Makefile.run +++ b/scripts/reliefos/Makefile.run @@ -40,6 +40,8 @@ endif QEMU_KVM ?= $(if $(filter y,$(CONFIG_QEMU_ENABLE_KVM)),1,0) RELIEFOS_QEMU_IDE ?= $(LEONOS_QEMU_IDE) RELIEFOS_QEMU_NVME ?= $(LEONOS_QEMU_NVME) +RELIEFOS_QEMU_VIRTIO ?= 0 +QEMU_VIRTIO_BLK := $(filter 1 y yes true,$(RELIEFOS_QEMU_VIRTIO)) QEMU_MEMORY := $(or $(MEMORY),$(CONFIG_QEMU_MEMORY_MB),4096) QEMU_CPUS := $(or $(CPUS),$(CONFIG_QEMU_SMP_CPUS),4) comma := , @@ -50,11 +52,18 @@ QEMU_NETWORK_FLAGS := $(if $(QEMU_NETWORK),-netdev user$(comma)id=net0 -device $ ifneq ($(strip $(QEMU_INSTALLER_DISK)),) QEMU_INSTALLER_DISK_PATH := $(if $(filter /%,$(QEMU_INSTALLER_DISK)),$(QEMU_INSTALLER_DISK),$(RELIEFOS_SRC)/$(QEMU_INSTALLER_DISK)) QEMU_INSTALLER_DISK_CHECK := test -f '$(QEMU_INSTALLER_DISK_PATH)' || { echo 'missing installer target disk: $(QEMU_INSTALLER_DISK_PATH)' >&2; exit 1; } -QEMU_INSTALLER_DISK_FLAGS := -drive file=$(QEMU_INSTALLER_DISK_PATH)$(comma)if=none$(comma)id=installer-disk$(comma)format=$(QEMU_INSTALLER_DISK_FORMAT) -device ich9-ahci$(comma)id=installer-ahci -device ide-hd$(comma)drive=installer-disk$(comma)bus=installer-ahci.0 +QEMU_INSTALLER_DISK_FLAGS := -drive file=$(QEMU_INSTALLER_DISK_PATH),if=none,id=installer-disk,format=$(QEMU_INSTALLER_DISK_FORMAT) +ifneq ($(QEMU_VIRTIO_BLK),) +QEMU_INSTALLER_DISK_FLAGS += -device virtio-blk-pci,drive=installer-disk,disable-legacy=on +else +QEMU_INSTALLER_DISK_FLAGS += -device ich9-ahci,id=installer-ahci -device ide-hd,drive=installer-disk,bus=installer-ahci.0 +endif endif QEMU_FLAGS := -machine $(if $(filter 1 y yes true,$(RELIEFOS_QEMU_IDE)),pc,q35) $(if $(filter 1,$(QEMU_KVM)),-enable-kvm -cpu host,-cpu max) -m $(QEMU_MEMORY) -smp $(QEMU_CPUS) $(QEMU_FIRMWARE_FLAGS) -display $(QEMU_DISPLAY) -serial $(QEMU_SERIAL) $(QEMU_GPU_FLAGS) $(QEMU_NETWORK_FLAGS) -audiodev $(QEMU_AUDIO),id=snd0 $(QEMU_SOUND_FLAGS) QEMU_DISK := -drive file=$(DISK_VMDK),if=none,id=disk0,format=vmdk -ifneq ($(filter 1 y yes true,$(RELIEFOS_QEMU_NVME)),) +ifneq ($(QEMU_VIRTIO_BLK),) +QEMU_DISK += -device virtio-blk-pci,drive=disk0,disable-legacy=on +else ifneq ($(filter 1 y yes true,$(RELIEFOS_QEMU_NVME)),) QEMU_DISK += -device nvme,drive=disk0,serial=leonosnvme else ifneq ($(filter 1 y yes true,$(RELIEFOS_QEMU_IDE)),) QEMU_DISK += -device piix3-ide,id=ide -device ide-hd,drive=disk0,bus=ide.0,unit=0 @@ -66,7 +75,12 @@ QEMU_RUN_DISK_FORMAT ?= qcow2 ifneq ($(strip $(QEMU_RUN_DISK)),) QEMU_RUN_DISK_PATH := $(if $(filter /%,$(QEMU_RUN_DISK)),$(QEMU_RUN_DISK),$(RELIEFOS_SRC)/$(QEMU_RUN_DISK)) QEMU_RUN_DISK_CHECK := test -f '$(QEMU_RUN_DISK_PATH)' || { echo 'missing run disk: $(QEMU_RUN_DISK_PATH)' >&2; exit 1; } -QEMU_RUN_DISK_FLAGS := -drive file=$(QEMU_RUN_DISK_PATH)$(comma)if=none$(comma)id=run-disk$(comma)format=$(QEMU_RUN_DISK_FORMAT) -device ich9-ahci$(comma)id=run-ahci -device ide-hd$(comma)drive=run-disk$(comma)bus=run-ahci.0 +QEMU_RUN_DISK_FLAGS := -drive file=$(QEMU_RUN_DISK_PATH),if=none,id=run-disk,format=$(QEMU_RUN_DISK_FORMAT) +ifneq ($(QEMU_VIRTIO_BLK),) +QEMU_RUN_DISK_FLAGS += -device virtio-blk-pci,drive=run-disk,disable-legacy=on +else +QEMU_RUN_DISK_FLAGS += -device ich9-ahci,id=run-ahci -device ide-hd,drive=run-disk,bus=run-ahci.0 +endif else QEMU_RUN_DISK_CHECK := test -f '$(DISK_VMDK)' || { echo 'missing disk image: $(DISK_VMDK)' >&2; exit 1; } QEMU_RUN_DISK_FLAGS := $(QEMU_DISK) diff --git a/system/initramfs/init b/system/initramfs/init index 3399cb98..ec72d71f 100644 --- a/system/initramfs/init +++ b/system/initramfs/init @@ -44,7 +44,7 @@ resolve_device() { PARTUUID=*) resolved=$(partuuid_device "$1") || return 1 ;; UUID=*|LABEL=*) key=${1%%=*}; value=${1#*=}; resolved= - for candidate in /dev/sd* /dev/nvme* /dev/ram* /dev/sr*; do + for candidate in /dev/sd* /dev/vd* /dev/nvme* /dev/ram* /dev/sr*; do [ -b "$candidate" ] || continue identity=$(blkid "$candidate") || continue case " $identity " in *" $key=\"$value\" "*) diff --git a/tools/tests/blockdev_errno_test.c b/tools/tests/blockdev_errno_test.c index 46201c7a..834cb600 100644 --- a/tools/tests/blockdev_errno_test.c +++ b/tools/tests/blockdev_errno_test.c @@ -3,11 +3,14 @@ #include "../../userland/runtime/src/blockdev.c" _Static_assert(BLKROGET == 0x125e, "Linux BLKROGET uses the historical _IO encoding"); -static int fail_open, fail_ioctl, fail_seek, fail_io; +static int fail_open, fail_ioctl, fail_seek, fail_io, virtio, partition; +static off_t read_offset; +static uint8_t gpt4k[6 * 4096], fs4k[4096]; int open(const char *path, int flags, ...) { (void)flags; - if (fail_open || strcmp(path, "/dev/sda")) { + partition = virtio && !strcmp(path, "/dev/vda1"); + if (fail_open || (!partition && strcmp(path, virtio ? "/dev/vda" : "/dev/sda"))) { errno = fail_open ? EACCES : ENOENT; return -1; } @@ -20,8 +23,9 @@ int ioctl(int fd, unsigned long request, ...) if (fail_ioctl) { errno = EIO; return -1; } va_list args; va_start(args, request); - if (request == BLKGETSIZE64) *va_arg(args, uint64_t *) = 2ULL * 1024 * 1024 * 1024; - else if (request == BLKSSZGET) *va_arg(args, int *) = 512; + if (request == BLKGETSIZE64) + *va_arg(args, uint64_t *) = partition ? 12000ULL * 4096 : 2ULL * 1024 * 1024 * 1024; + else if (request == BLKSSZGET) *va_arg(args, int *) = virtio ? 4096 : 512; else if (request == BLKROGET) *va_arg(args, int *) = 0; else assert(request == BLKRRPART); va_end(args); @@ -31,6 +35,7 @@ off_t lseek(int fd, off_t offset, int whence) { assert(fd == 42 && whence == SEEK_SET); if (fail_seek) { errno = ESPIPE; return -1; } + read_offset = offset; return offset; } ssize_t read(int fd, void *buffer, size_t length) @@ -38,6 +43,16 @@ ssize_t read(int fd, void *buffer, size_t length) assert(fd == 42); if (fail_io) { errno = EIO; return -1; } memset(buffer, 0, length); + if (virtio) { + assert(read_offset >= 0 && read_offset % 4096 == 0 && length % 4096 == 0); + const uint8_t *fixture = partition ? fs4k : gpt4k; + size_t size = partition ? sizeof(fs4k) : sizeof(gpt4k); + if ((uint64_t)read_offset < size) { + assert(length <= size - (size_t)read_offset); + memcpy(buffer, fixture + read_offset, length); + } + } + read_offset += (off_t)length; return length; } ssize_t write(int fd, const void *buffer, size_t length) @@ -65,6 +80,42 @@ int main(void) fail_seek = 0; fail_io = 1; assert(block_io(42, 0, buffer, sizeof(buffer), 0) == -EIO); assert(block_io(42, 0, buffer, sizeof(buffer), 1) == -ENOSPC); + fail_io = 0; + virtio = 1; + assert(!reliefos_block_list_disks(disks, RELIEFOS_BLOCK_MAX_DISKS, &count)); + assert(count == 1 && !strcmp(disks[0].path, "/dev/vda")); + assert(disks[0].sector_count == 524288 && disks[0].sector_size == 4096); + uint32_t index; + assert(!block_disk_index("/dev/vdaa", &index) && index == 26); + assert(block_disk_index("/dev/vda1", &index) == -EINVAL); + assert(reliefos_block_gpt_initialize("/dev/vda", 1) == -ENOTSUP); + { + const uint64_t sectors = disks[0].sector_count; + struct block_gpt_entry entry = {.first_lba = 256, .last_lba = 12255}; + memcpy(entry.type_guid, block_guid_linux, sizeof(entry.type_guid)); + entry.unique_guid[0] = 1; + entry.name[0] = 'R'; + memcpy(gpt4k + 2 * 4096, &entry, sizeof(entry)); + struct block_gpt_header header = { + .signature = UINT64_C(0x5452415020494645), .revision = 0x10000, + .header_size = sizeof(header), .current_lba = 1, .backup_lba = sectors - 1, + .first_usable_lba = 6, .last_usable_lba = sectors - 6, .disk_guid = {2}, + .partition_entries_lba = 2, .partition_entry_count = 128, .partition_entry_size = 128, + .partition_entries_crc32 = block_crc32(gpt4k + 2 * 4096, 4 * 4096), + }; + header.header_crc32 = block_crc32(&header, sizeof(header)); + memcpy(gpt4k + 4096, &header, sizeof(header)); + fs4k[1024 + 56] = 0x53; fs4k[1024 + 57] = 0xef; + fs4k[1024 + 96] = 0x40; /* extents */ + struct reliefos_block_partition part; + assert(!reliefos_block_list_partitions("/dev/vda", &part, 1, &count) && count == 1); + assert(!strcmp(part.path, "/dev/vda1") && !strcmp(part.name, "R")); + assert(part.first_lba == 256 && part.sector_count == 12000); + assert(part.gpt_type == RELIEFOS_BLOCK_GPT_LINUX && part.filesystem == RELIEFOS_BLOCK_FILESYSTEM_EXT4); + char uuid[37]; + assert(!reliefos_block_partition_uuid("/dev/vda", 0, uuid)); + assert(!strcmp(uuid, "00000001-0000-0000-0000-000000000000")); + } puts("PASS block device errors: missing disks, open/ioctl/seek/read/write and cleanup"); return 0; } diff --git a/userland/runtime/src/blockdev.c b/userland/runtime/src/blockdev.c index 5fd084b0..096c5b3a 100644 --- a/userland/runtime/src/blockdev.c +++ b/userland/runtime/src/blockdev.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,7 @@ #include #define BLOCK_SECTOR_SIZE 512u +#define BLOCK_MAX_SECTOR_SIZE 4096u #define GPT_ENTRY_MAX 128u #define GPT_ENTRY_SIZE 128u #define GPT_DEFAULT_TABLE_BYTES (GPT_ENTRY_MAX * GPT_ENTRY_SIZE) @@ -57,7 +59,8 @@ struct __attribute__((packed)) block_gpt_header { uint32_t partition_entries_crc32; }; -struct __attribute__((packed)) block_gpt_entry { +/* The GPT entry layout also has natural alignment on x86-64. */ +struct block_gpt_entry { uint8_t type_guid[16]; uint8_t unique_guid[16]; uint64_t first_lba; @@ -65,6 +68,8 @@ struct __attribute__((packed)) block_gpt_entry { uint64_t attrs; uint16_t name[36]; }; +_Static_assert(sizeof(struct block_gpt_entry) == GPT_ENTRY_SIZE && + offsetof(struct block_gpt_entry, name) == 56, "GPT entry layout"); struct __attribute__((packed)) block_fat32_bpb { uint8_t jump[3]; @@ -296,9 +301,9 @@ static int block_check_unmounted(const char *path) return result; } -/** @brief Open an owned descriptor with supported geometry and write prechecks. */ -static int block_open_info(const char *path, int writable, int *out_fd, - uint64_t *out_sectors, uint32_t *out_sector_size) +/** @brief Read native geometry; only passive inventory allows non-512-byte sectors. */ +static int block_open_geometry(const char *path, int writable, int native, int *out_fd, + uint64_t *out_sectors, uint32_t *out_sector_size) { int fd; uint64_t bytes = 0; @@ -319,11 +324,13 @@ static int block_open_info(const char *path, int writable, int *out_fd, (void)close(fd); return -error; } - if (sector_size != BLOCK_SECTOR_SIZE) { + if (native ? sector_size < (int)BLOCK_SECTOR_SIZE || sector_size > (int)BLOCK_MAX_SECTOR_SIZE || + (sector_size & (sector_size - 1)) + : sector_size != BLOCK_SECTOR_SIZE) { (void)close(fd); return -BLOCK_ENOTSUP; } - if (!bytes || bytes % BLOCK_SECTOR_SIZE) { + if (!bytes || bytes % (uint32_t)sector_size) { (void)close(fd); return -BLOCK_EINVAL; } @@ -335,22 +342,32 @@ static int block_open_info(const char *path, int writable, int *out_fd, if (ret < 0) { (void)close(fd); return (int)ret; } } *out_fd = fd; - *out_sectors = bytes / BLOCK_SECTOR_SIZE; + *out_sectors = bytes / (uint32_t)sector_size; *out_sector_size = (uint32_t)sector_size; return 0; } +/* The built-in partition editor and formatters still generate 512-byte layouts. */ +static int block_open_info(const char *path, int writable, int *out_fd, + uint64_t *out_sectors, uint32_t *out_sector_size) +{ + return block_open_geometry(path, writable, 0, out_fd, out_sectors, out_sector_size); +} + static int block_disk_index(const char *path, uint32_t *out_index) { const char *p; uint32_t value = 0; if (!path || !out_index || strncmp(path, "/dev/", 5) != 0) return -BLOCK_EINVAL; p = path + 5; - if (p[0] == 's' && p[1] == 'd' && p[2] >= 'a' && p[2] <= 'z') { + if ((p[0] == 's' || p[0] == 'v') && p[1] == 'd' && p[2] >= 'a' && p[2] <= 'z') { uint32_t letters = 0; + p += 2; do { if (*p < 'a' || *p > 'z') return -BLOCK_EINVAL; - letters = letters * 26u + (uint32_t)(*p - 'a' + 1u); + uint32_t digit = (uint32_t)(*p - 'a' + 1u); + if (letters > (UINT32_MAX - digit) / 26u) return -BLOCK_EINVAL; + letters = letters * 26u + digit; ++p; } while (*p >= 'a' && *p <= 'z'); if (*p) return -BLOCK_EINVAL; @@ -520,7 +537,8 @@ static void block_name_get(char *text, uint32_t capacity, const uint16_t name[36 text[written] = 0; } -static int block_gpt_header_valid(const struct block_gpt_header *header, uint64_t sectors) +static int block_gpt_header_valid(const struct block_gpt_header *header, uint64_t sectors, + uint32_t sector_size) { struct block_gpt_header copy; uint64_t table_bytes; @@ -538,7 +556,7 @@ static int block_gpt_header_valid(const struct block_gpt_header *header, uint64_ header->partition_entries_lba < 2u || header->partition_entries_lba >= sectors) return -BLOCK_EINVAL; table_bytes = (uint64_t)header->partition_entry_count * header->partition_entry_size; - table_sectors = (table_bytes + BLOCK_SECTOR_SIZE - 1u) / BLOCK_SECTOR_SIZE; + table_sectors = (table_bytes + sector_size - 1u) / sector_size; if (table_bytes > GPT_DEFAULT_TABLE_BYTES || table_sectors == 0 || table_sectors > sectors - header->partition_entries_lba) { return -BLOCK_EINVAL; @@ -564,14 +582,10 @@ static void block_gpt_free(struct block_gpt_table *table) } static void block_gpt_rebuild_backup(const struct block_gpt_header *primary, - uint64_t sectors, + uint64_t sectors, uint32_t table_sectors, struct block_gpt_header *backup) { - uint64_t table_bytes; - uint64_t table_sectors; if (!primary || !backup) return; - table_bytes = (uint64_t)primary->partition_entry_count * primary->partition_entry_size; - table_sectors = (table_bytes + BLOCK_SECTOR_SIZE - 1u) / BLOCK_SECTOR_SIZE; if (!table_sectors || sectors <= table_sectors + 1u) return; *backup = *primary; backup->current_lba = sectors - 1u; @@ -604,29 +618,30 @@ static int block_gpt_entries_valid(const struct block_gpt_table *table) return 0; } -static int block_gpt_load_fd(int fd, uint64_t sectors, struct block_gpt_table *table) +static int block_gpt_load_fd(int fd, uint64_t sectors, uint32_t sector_size, + struct block_gpt_table *table) { - uint8_t sector[BLOCK_SECTOR_SIZE]; + uint8_t sector[BLOCK_MAX_SECTOR_SIZE]; uint32_t checksum; int ret; if (!table) return -BLOCK_EINVAL; memset(table, 0, sizeof(*table)); - ret = block_io(fd, BLOCK_SECTOR_SIZE, sector, sizeof(sector), 0); + ret = block_io(fd, sector_size, sector, sector_size, 0); if (ret == 0) memcpy(&table->primary, sector, sizeof(table->primary)); if (ret < 0) return ret; if (table->primary.signature != 0x5452415020494645ULL) return -BLOCK_ENOENT; - ret = block_gpt_header_valid(&table->primary, sectors); + ret = block_gpt_header_valid(&table->primary, sectors, sector_size); if (ret < 0 || table->primary.current_lba != 1u) { return ret < 0 ? ret : -BLOCK_EINVAL; } table->entry_bytes = table->primary.partition_entry_count * table->primary.partition_entry_size; - table->entry_sectors = (table->entry_bytes + BLOCK_SECTOR_SIZE - 1u) / - BLOCK_SECTOR_SIZE; - table->entries = calloc(table->entry_sectors, BLOCK_SECTOR_SIZE); + table->entry_sectors = (table->entry_bytes + sector_size - 1u) / + sector_size; + table->entries = calloc(table->entry_sectors, sector_size); if (!table->entries) return -12; - ret = block_io(fd, table->primary.partition_entries_lba * BLOCK_SECTOR_SIZE, - table->entries, table->entry_sectors * BLOCK_SECTOR_SIZE, 0); + ret = block_io(fd, table->primary.partition_entries_lba * sector_size, + table->entries, table->entry_sectors * sector_size, 0); if (ret < 0) { block_gpt_free(table); return ret; } checksum = block_crc32(table->entries, table->entry_bytes); if (checksum != table->primary.partition_entries_crc32) { @@ -642,10 +657,10 @@ static int block_gpt_load_fd(int fd, uint64_t sectors, struct block_gpt_table *t * backup header is optional here because older installers could leave it * stale after an interrupted write. Rebuild its canonical location so * the next GPT update repairs both copies. */ - ret = block_io(fd, table->primary.backup_lba * BLOCK_SECTOR_SIZE, - sector, sizeof(sector), 0); + ret = block_io(fd, table->primary.backup_lba * sector_size, + sector, sector_size, 0); if (ret == 0) memcpy(&table->backup, sector, sizeof(table->backup)); - if (ret < 0 || block_gpt_header_valid(&table->backup, sectors) < 0 || + if (ret < 0 || block_gpt_header_valid(&table->backup, sectors, sector_size) < 0 || table->backup.current_lba != table->primary.backup_lba || table->backup.backup_lba != 1u || table->backup.partition_entries_lba != sectors - table->entry_sectors - 1u || @@ -656,7 +671,7 @@ static int block_gpt_load_fd(int fd, uint64_t sectors, struct block_gpt_table *t table->backup.first_usable_lba != table->primary.first_usable_lba || table->backup.last_usable_lba != table->primary.last_usable_lba || table->backup.partition_entries_crc32 != checksum) { - block_gpt_rebuild_backup(&table->primary, sectors, &table->backup); + block_gpt_rebuild_backup(&table->primary, sectors, table->entry_sectors, &table->backup); } table->backup.partition_entries_crc32 = checksum; return 0; @@ -707,7 +722,7 @@ int reliefos_block_get_info(const char *path, struct reliefos_block_disk_info *o int ret; if (!out || !path) return -BLOCK_EINVAL; memset(out, 0, sizeof(*out)); - ret = block_open_info(path, 0, &fd, §ors, §or_size); + ret = block_open_geometry(path, 0, 1, &fd, §ors, §or_size); if (ret < 0) return ret; out->sector_count = sectors; out->sector_size = sector_size; @@ -724,20 +739,23 @@ int reliefos_block_list_disks(struct reliefos_block_disk_info *disks, uint32_t c uint32_t count = 0; int first_error = 0; if (!out_count || (capacity && !disks)) return -BLOCK_EINVAL; - for (uint32_t index = 0; index < RELIEFOS_BLOCK_MAX_DISKS; ++index) { - char path[RELIEFOS_BLOCK_PATH_LEN]; - struct reliefos_block_disk_info info; - snprintf(path, sizeof(path), "/dev/sd%c", (char)('a' + index)); - int ret = reliefos_block_get_info(path, &info); - if (ret < 0) { - if (ret != -BLOCK_ENOENT && !first_error) first_error = ret; - continue; + const char *prefixes[] = {"sd", "vd"}; + for (unsigned family = 0; family < sizeof(prefixes) / sizeof(prefixes[0]); ++family) { + for (uint32_t index = 0; index < RELIEFOS_BLOCK_MAX_DISKS && count < RELIEFOS_BLOCK_MAX_DISKS; ++index) { + char path[RELIEFOS_BLOCK_PATH_LEN]; + struct reliefos_block_disk_info info; + snprintf(path, sizeof(path), "/dev/%s%c", prefixes[family], (char)('a' + index)); + int ret = reliefos_block_get_info(path, &info); + if (ret < 0) { + if (ret != -BLOCK_ENOENT && !first_error) first_error = ret; + continue; + } + info.id = count; + if (count < capacity) disks[count] = info; + ++count; } - info.id = index; - if (count < capacity) disks[count] = info; - ++count; } - for (uint32_t controller = 0; controller < RELIEFOS_BLOCK_MAX_DISKS; ++controller) { + for (uint32_t controller = 0; controller < RELIEFOS_BLOCK_MAX_DISKS && count < RELIEFOS_BLOCK_MAX_DISKS; ++controller) { for (uint32_t namespace_id = 1; namespace_id <= RELIEFOS_BLOCK_MAX_PARTITIONS / 16u; ++namespace_id) { char path[RELIEFOS_BLOCK_PATH_LEN]; @@ -771,7 +789,7 @@ int reliefos_block_gpt_initialize(const char *disk_path, int force) ret = block_open_info(disk_path, 1, &fd, §ors, §or_size); if (ret < 0) return ret; if (sectors < 655360ULL) { (void)close(fd); return -BLOCK_ENOSPC; } - if (block_gpt_load_fd(fd, sectors, &existing) == 0) { + if (block_gpt_load_fd(fd, sectors, sector_size, &existing) == 0) { block_gpt_free(&existing); if (!force) { (void)close(fd); return -BLOCK_EEXIST; } } @@ -816,28 +834,37 @@ int reliefos_block_gpt_initialize(const char *disk_path, int force) return ret; } -static int block_partition_probe_fd(int fd, uint32_t *out_filesystem) +static int block_partition_probe_fd(int fd, uint64_t sectors, uint32_t sector_size, + uint32_t *out_filesystem) { - uint8_t sector[BLOCK_SECTOR_SIZE]; + uint8_t sector[BLOCK_MAX_SECTOR_SIZE]; int ret; if (!out_filesystem) return -BLOCK_EINVAL; *out_filesystem = RELIEFOS_BLOCK_FILESYSTEM_UNKNOWN; - ret = block_io(fd, 0, sector, sizeof(sector), 0); + ret = block_io(fd, 0, sector, sector_size, 0); if (ret < 0) return ret; if (sector[510] == 0x55 && sector[511] == 0xaa && memcmp(sector + 82, "FAT32 ", 8) == 0) *out_filesystem = RELIEFOS_BLOCK_FILESYSTEM_FAT32; else if (sector[510] == 0x55 && sector[511] == 0xaa && memcmp(sector + 3, "EXFAT ", 8) == 0) *out_filesystem = RELIEFOS_BLOCK_FILESYSTEM_EXFAT; else { - ret = block_io(fd, 1024, sector, sizeof(sector), 0); - if (ret < 0) return ret; + /* The ext superblock starts at byte 1024, irrespective of the + * device's logical sector size. On 4Kn it is already in sector 0. */ + const uint8_t *super; + if (sector_size >= 2048u) super = sector + 1024; + else { + if (sectors <= 1024u / sector_size) return 0; + ret = block_io(fd, 1024, sector, sector_size, 0); + if (ret < 0) return ret; + super = sector; + } uint16_t magic; - memcpy(&magic, sector + 56, sizeof(magic)); + memcpy(&magic, super + 56, sizeof(magic)); if (magic == EXT2_SUPER_MAGIC) { uint32_t compat, incompat, ro_compat; - memcpy(&compat, sector + 92, sizeof(compat)); - memcpy(&incompat, sector + 96, sizeof(incompat)); - memcpy(&ro_compat, sector + 100, sizeof(ro_compat)); + memcpy(&compat, super + 92, sizeof(compat)); + memcpy(&incompat, super + 96, sizeof(incompat)); + memcpy(&ro_compat, super + 100, sizeof(ro_compat)); /* Match the kernel's conservative ext-family feature classification. */ if ((compat & ~0x3fu) || (incompat & ~0x6u) || (ro_compat & ~0x7u)) *out_filesystem = RELIEFOS_BLOCK_FILESYSTEM_EXT4; @@ -855,9 +882,9 @@ int reliefos_block_probe_filesystem(const char *partition_path, uint32_t *out_fi int fd; uint64_t sectors; uint32_t sector_size; - int ret = block_open_info(partition_path, 0, &fd, §ors, §or_size); + int ret = block_open_geometry(partition_path, 0, 1, &fd, §ors, §or_size); if (ret < 0) return ret; - ret = block_partition_probe_fd(fd, out_filesystem); + ret = block_partition_probe_fd(fd, sectors, sector_size, out_filesystem); (void)close(fd); return ret; } @@ -874,9 +901,9 @@ int reliefos_block_list_partitions(const char *disk_path, int ret; if (!out_count || (capacity && !partitions)) return -BLOCK_EINVAL; *out_count = 0; - ret = block_open_info(disk_path, 0, &fd, §ors, §or_size); + ret = block_open_geometry(disk_path, 0, 1, &fd, §ors, §or_size); if (ret < 0) return ret; - ret = block_gpt_load_fd(fd, sectors, &table); + ret = block_gpt_load_fd(fd, sectors, sector_size, &table); (void)close(fd); if (ret == -BLOCK_ENOENT) { *out_count = 0; @@ -911,9 +938,9 @@ int reliefos_block_partition_uuid(const char *disk_path, uint32_t index, char uu uint32_t sector_size; struct block_gpt_table table; if (!uuid) return -BLOCK_EINVAL; - int ret = block_open_info(disk_path, 0, &fd, §ors, §or_size); + int ret = block_open_geometry(disk_path, 0, 1, &fd, §ors, §or_size); if (ret < 0) return ret; - ret = block_gpt_load_fd(fd, sectors, &table); + ret = block_gpt_load_fd(fd, sectors, sector_size, &table); (void)close(fd); if (ret < 0) return ret; if (index >= table.primary.partition_entry_count || @@ -936,7 +963,7 @@ static int block_gpt_update(const char *disk_path, struct block_gpt_table table; int ret = block_open_info(disk_path, 1, &fd, §ors, §or_size); if (ret < 0) return ret; - ret = block_gpt_load_fd(fd, sectors, &table); + ret = block_gpt_load_fd(fd, sectors, sector_size, &table); if (ret == 0) ret = update(&table, context); if (ret == 0) ret = block_gpt_write_fd(fd, &table); if (ret == 0) ret = block_reread(fd); diff --git a/userland/runtime/src/bootloader.c b/userland/runtime/src/bootloader.c index 509ef90b..b2b49560 100644 --- a/userland/runtime/src/bootloader.c +++ b/userland/runtime/src/bootloader.c @@ -139,7 +139,7 @@ static int whole_disk(const char *path) { if (!path) return 0; const char *p; - if (!strncmp(path, "/dev/sd", 7)) { + if (!strncmp(path, "/dev/sd", 7) || !strncmp(path, "/dev/vd", 7)) { p = path + 7; if (*p < 'a' || *p > 'z') return 0; while (*p >= 'a' && *p <= 'z') ++p; From eb07fd23d919b9a830d37704c8a1c434f69b5ef6 Mon Sep 17 00:00:00 2001 From: LeonMMcoset Date: Tue, 24 Nov 2026 10:30:39 +0000 Subject: [PATCH 2/2] chore(kernel): track merged ReliefNT PR #42 --- kernel/reliefnt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/reliefnt b/kernel/reliefnt index b5a7f1bf..117bcc5f 160000 --- a/kernel/reliefnt +++ b/kernel/reliefnt @@ -1 +1 @@ -Subproject commit b5a7f1bf40342b0a947475d0ed3c3ae488448332 +Subproject commit 117bcc5f7615d9ab771ca4e397cf6c1d57053c95