Skip to content

Commit 43b390e

Browse files
authored
Merge pull request #623 from ChinYikMing/support-home-dir-vblk
Support ~ in virtio-blk paths and improve error handling
2 parents a657a17 + 457f621 commit 43b390e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

.ci/boot-linux.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ if [ "${ENABLE_VBLK}" -eq "1" ]; then
7777
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
7878
')
7979

80+
# Read-write using disk image with ~ home directory symbol
81+
TEST_OPTIONS+=("${OPTS_BASE} -x vblk:~$(pwd | sed "s|$HOME||")/${VBLK_IMG}")
82+
VBLK_EXPECT_CMDS='
83+
expect "buildroot login:" { send "root\n" } timeout { exit 1 }
84+
expect "# " { send "uname -a\n" } timeout { exit 2 }
85+
expect "riscv32 GNU/Linux" { send "mkdir mnt && mount /dev/vda mnt\n" } timeout { exit 3 }
86+
expect "# " { send "echo rv32emu > mnt/emu.txt\n" } timeout { exit 3 }
87+
expect "# " { send "sync\n" } timeout { exit 3 }
88+
expect "# " { send "umount mnt\n" } timeout { exit 3 }
89+
expect "# " { send "\x01"; send "x" } timeout { exit 3 }
90+
'
91+
EXPECT_CMDS+=("${VBLK_EXPECT_CMDS}")
92+
8093
# Read-write using disk image
8194
TEST_OPTIONS+=("${OPTS_BASE} -x vblk:${VBLK_IMG}")
8295
VBLK_EXPECT_CMDS='

src/riscv.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ riscv_t *rv_create(riscv_user_t rv_attr)
681681
/* Currently, only used for block image path and permission */
682682
#define MAX_OPTS 2
683683
char *vblk_device_str = attr->data.system.vblk_device[i];
684+
if (!vblk_device_str[0]) {
685+
rv_log_error("Disk path cannot be empty");
686+
exit(EXIT_FAILURE);
687+
}
688+
684689
char *vblk_opts[MAX_OPTS] = {NULL};
685690
int vblk_opt_idx = 0;
686691
char *opt = strtok(vblk_device_str, ",");
@@ -692,10 +697,34 @@ riscv_t *rv_create(riscv_user_t rv_attr)
692697
vblk_opts[vblk_opt_idx++] = opt;
693698
opt = strtok(NULL, ",");
694699
}
695-
char *vblk_device = vblk_opts[0];
696-
char *vblk_readonly = vblk_opts[1];
697700

701+
char *vblk_device;
702+
char *vblk_readonly = vblk_opts[1];
698703
bool readonly = false;
704+
705+
if (vblk_opts[0][0] == '~') {
706+
/* HOME environment variable should be common in macOS and Linux
707+
* distribution and it is set by the login program
708+
*/
709+
const char *home = getenv("HOME");
710+
if (!home) {
711+
rv_log_error(
712+
"HOME environment variable is not set, cannot access "
713+
"the disk %s",
714+
vblk_opts[0]);
715+
exit(EXIT_FAILURE);
716+
}
717+
718+
vblk_device = malloc(strlen(vblk_opts[0]) - 1 /* skip ~ */ +
719+
strlen(home) + 1);
720+
assert(vblk_device);
721+
722+
strcpy(vblk_device, home);
723+
strcat(vblk_device, vblk_opts[0] + 1 /* skip ~ */);
724+
} else {
725+
vblk_device = vblk_opts[0];
726+
}
727+
699728
if (vblk_readonly) {
700729
if (strcmp(vblk_readonly, "readonly") != 0) {
701730
rv_log_error("Unknown vblk option: %s", vblk_readonly);
@@ -708,6 +737,9 @@ riscv_t *rv_create(riscv_user_t rv_attr)
708737
attr->vblk[i]->ram = (uint32_t *) attr->mem->mem_base;
709738
attr->disk[i] =
710739
virtio_blk_init(attr->vblk[i], vblk_device, readonly);
740+
741+
if (vblk_opts[0][0] == '~')
742+
free(vblk_device);
711743
}
712744
}
713745

0 commit comments

Comments
 (0)