From 5a8499e317af35206b475cc82521d53cea224bdb Mon Sep 17 00:00:00 2001 From: ChinYikMing Date: Thu, 16 Oct 2025 01:36:31 +0800 Subject: [PATCH 1/2] Support ~ in virtio-blk paths and improve error handling This commit allows the virtio block device to resolve disk image paths starting with ~ to the user's home directory, making it more convenient. Additionally, it adds error handling for cases where the -x vblk: option is used without specifying a path. --- src/riscv.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/riscv.c b/src/riscv.c index dcaaa94a..5ee8a955 100644 --- a/src/riscv.c +++ b/src/riscv.c @@ -681,6 +681,11 @@ riscv_t *rv_create(riscv_user_t rv_attr) /* Currently, only used for block image path and permission */ #define MAX_OPTS 2 char *vblk_device_str = attr->data.system.vblk_device[i]; + if (!vblk_device_str[0]) { + rv_log_error("Disk path cannot be empty"); + exit(EXIT_FAILURE); + } + char *vblk_opts[MAX_OPTS] = {NULL}; int vblk_opt_idx = 0; char *opt = strtok(vblk_device_str, ","); @@ -692,10 +697,34 @@ riscv_t *rv_create(riscv_user_t rv_attr) vblk_opts[vblk_opt_idx++] = opt; opt = strtok(NULL, ","); } - char *vblk_device = vblk_opts[0]; - char *vblk_readonly = vblk_opts[1]; + char *vblk_device; + char *vblk_readonly = vblk_opts[1]; bool readonly = false; + + if (vblk_opts[0][0] == '~') { + /* HOME environment variable should be common in macOS and Linux + * distribution and it is set by the login program + */ + const char *home = getenv("HOME"); + if (!home) { + rv_log_error( + "HOME environment variable is not set, cannot access " + "the disk %s", + vblk_opts[0]); + exit(EXIT_FAILURE); + } + + vblk_device = malloc(strlen(vblk_opts[0]) - 1 /* skip ~ */ + + strlen(home) + 1); + assert(vblk_device); + + strcpy(vblk_device, home); + strcat(vblk_device, vblk_opts[0] + 1 /* skip ~ */); + } else { + vblk_device = vblk_opts[0]; + } + if (vblk_readonly) { if (strcmp(vblk_readonly, "readonly") != 0) { rv_log_error("Unknown vblk option: %s", vblk_readonly); @@ -708,6 +737,9 @@ riscv_t *rv_create(riscv_user_t rv_attr) attr->vblk[i]->ram = (uint32_t *) attr->mem->mem_base; attr->disk[i] = virtio_blk_init(attr->vblk[i], vblk_device, readonly); + + if (vblk_opts[0][0] == '~') + free(vblk_device); } } From 457f6216393dfb7c57062ce53d1135b61be3764f Mon Sep 17 00:00:00 2001 From: ChinYikMing Date: Sun, 19 Oct 2025 14:41:00 +0800 Subject: [PATCH 2/2] CI: Add test case for virtio-blk access with ~ symbol --- .ci/boot-linux.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.ci/boot-linux.sh b/.ci/boot-linux.sh index 3f6abb29..4d8161ee 100755 --- a/.ci/boot-linux.sh +++ b/.ci/boot-linux.sh @@ -77,6 +77,19 @@ if [ "${ENABLE_VBLK}" -eq "1" ]; then expect "# " { send "\x01"; send "x" } timeout { exit 3 } ') + # Read-write using disk image with ~ home directory symbol + TEST_OPTIONS+=("${OPTS_BASE} -x vblk:~$(pwd | sed "s|$HOME||")/${VBLK_IMG}") + VBLK_EXPECT_CMDS=' + expect "buildroot login:" { send "root\n" } timeout { exit 1 } + expect "# " { send "uname -a\n" } timeout { exit 2 } + expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 } + expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 } + expect "# " { send "sync\n" } timeout { exit 3 } + expect "# " { send "umount mnt\n" } timeout { exit 3 } + expect "# " { send "\x01"; send "x" } timeout { exit 3 } + ' + EXPECT_CMDS+=("${VBLK_EXPECT_CMDS}") + # Read-write using disk image TEST_OPTIONS+=("${OPTS_BASE} -x vblk:${VBLK_IMG}") VBLK_EXPECT_CMDS='