From 462fcfdf751307703e174aca28008827f4a1c282 Mon Sep 17 00:00:00 2001 From: Kroese Date: Fri, 14 Mar 2025 06:57:32 +0100 Subject: [PATCH] feat: Support file sharing (#198) --- readme.md | 17 +++++++++++++++++ src/config.sh | 9 ++++++++- src/proc.sh | 27 ++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 6d8a204..ec463a3 100644 --- a/readme.md +++ b/readme.md @@ -276,6 +276,23 @@ kubectl apply -f https://raw.githubusercontent.com/qemus/qemu-arm/refs/heads/mas - /dev/bus/usb ``` +### How do I share files with the host? + + To share files with the host, first ensure that your guest OS has `9pfs` support compiled in or available as a kernel module. If so, add the following volume to your compose file: + + ```yaml + volumes: + - ./example:/shared + ``` + + Then start the container and execute the following command in the guest: + + ```shell + mount -t 9p -o trans=virtio shared /mnt/example + ``` + + Now the `./example` directory on the host will be available as `/mnt/example` in the guest. + ### How can I provide custom arguments to QEMU? You can create the `ARGUMENTS` environment variable to provide additional arguments to QEMU at runtime: diff --git a/src/config.sh b/src/config.sh index 2a20a84..6dbcba6 100644 --- a/src/config.sh +++ b/src/config.sh @@ -17,8 +17,15 @@ MAC_OPTS="-machine type=${MACHINE},secure=${SECURE},dump-guest-core=off${KVM_OPT [ -n "$UUID" ] && MAC_OPTS="$MAC_OPTS -uuid $UUID" DEV_OPTS="-object rng-random,id=objrng0,filename=/dev/urandom" DEV_OPTS+=" -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0" -[[ "${BOOT_MODE,,}" != "windows"* ]] && DEV_OPTS+=" -device virtio-balloon-pci,id=balloon0,bus=pcie.0" +if [[ "${BOOT_MODE,,}" != "windows"* ]]; then + DEV_OPTS+=" -device virtio-balloon-pci,id=balloon0,bus=pcie.0" + if [ -d "/shared" ]; then + DEV_OPTS+=" -fsdev local,id=fsdev0,path=/shared,security_model=none" + DEV_OPTS+=" -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=shared" + fi +fi + ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS ${USB_OPTS:-} $NET_OPTS $DISK_OPTS $BOOT_OPTS $DEV_OPTS $ARGUMENTS" ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') diff --git a/src/proc.sh b/src/proc.sh index 784a822..de2b7bb 100644 --- a/src/proc.sh +++ b/src/proc.sh @@ -24,7 +24,32 @@ if [[ "$CPU" == "Rockchip RK3588"* ]] && [[ "$CORES" == "8" ]]; then [ -z "$CPU_PIN" ] && CPU_PIN="4,5,6,7" fi - if [[ "${ARCH,,}" != "arm64" ]]; then +MSRS="/sys/module/kvm/parameters/ignore_msrs" +if [ -e "$MSRS" ]; then + result=$(<"$MSRS") + if [[ "$result" == "0" ]] || [[ "${result^^}" == "N" ]]; then + echo 1 | tee "$MSRS" > /dev/null 2>&1 || true + fi +fi + +CLOCKSOURCE="tsc" +[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter" +CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource" + +if [ ! -f "$CLOCK" ]; then + warn "file \"$CLOCK\" cannot not found?" +else + result=$(<"$CLOCK") + case "${result,,}" in + "${CLOCKSOURCE,,}" ) ;; + "kvm-clock" ) info "Nested KVM virtualization detected.." ;; + "hyperv_clocksource_tsc_page" ) info "Nested Hyper-V virtualization detected.." ;; + "hpet" ) warn "unsupported clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'" ;; + *) warn "unexpected clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE'" ;; + esac +fi + +if [[ "${ARCH,,}" != "arm64" ]]; then KVM="N" warn "your CPU architecture is ${ARCH^^} and cannot provide KVM acceleration for ARM64 instructions, this will cause a major loss of performance." fi