Unverified Commit e3c3618c authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Support file sharing (#680)

parent 1e1874a6
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -115,7 +115,7 @@ kubectl apply -f https://raw.githubusercontent.com/qemus/qemu/refs/heads/master/

  You can supply a `boot.iso`, `boot.img` or `boot.qcow2` file by replacing the example path `./example.iso` with the filename of your desired image. The value of `BOOT` will be ignored in this case.

### How do I boot ARM images?
### How do I boot ARM64 images?

  You can use the [qemu-arm](https://github.com/qemus/qemu-arm/) container to run ARM64-based images.

@@ -265,6 +265,23 @@ kubectl apply -f https://raw.githubusercontent.com/qemus/qemu/refs/heads/master/
    - /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:
+9 −4
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ set -Eeuo pipefail
: "${MONITOR:="telnet:localhost:7100,server,nowait,nodelay"}"
: "${SMP:="$CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1"}"

DEV_OPTS=""
DEF_OPTS="-nodefaults"
SERIAL_OPTS="-serial $SERIAL"
CPU_OPTS="-cpu $CPU_FLAGS -smp $SMP"
@@ -17,12 +18,16 @@ MON_OPTS="-monitor $MONITOR -name $PROCESS,process=$PROCESS,debug-threads=on"
MAC_OPTS="-machine type=${MACHINE},smm=${SECURE},graphics=off,vmport=${VMPORT},dump-guest-core=off,hpet=${HPET}${KVM_OPTS}"
[ -n "$UUID" ] && MAC_OPTS="$MAC_OPTS -uuid $UUID"

if [[ "${MACHINE,,}" == "pc-i440fx-2"* ]]; then
  DEV_OPTS=""
else
if [[ "${MACHINE,,}" != "pc-i440fx-2"* ]]; then
  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
  [ -n "$USB" ] && [[ "${USB,,}" != "no"* ]] && USB_OPTS="-device $USB -device usb-tablet"
fi

+25 −16
Original line number Diff line number Diff line
@@ -10,6 +10,31 @@ set -Eeuo pipefail
: "${CPU_MODEL:=""}"
: "${DEF_MODEL:="qemu64"}"

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,,}" != "amd64" ]]; then
  KVM="N"
  warn "your CPU architecture is ${ARCH^^} and cannot provide KVM acceleration for x64 instructions, this will cause a major loss of performance."
@@ -48,7 +73,6 @@ fi
if [[ "$KVM" != [Nn]* ]]; then

  CPU_FEATURES="kvm=on,l3-cache=on,+hypervisor"
  CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource"
  KVM_OPTS=",accel=kvm -enable-kvm -global kvm-pit.lost_tick_policy=discard"

  if [ -z "$CPU_MODEL" ]; then
@@ -56,21 +80,6 @@ if [[ "$KVM" != [Nn]* ]]; then
    CPU_FEATURES+=",migratable=no"
  fi

  if [ -e /sys/module/kvm/parameters/ignore_msrs ]; then
    if [ "$(cat /sys/module/kvm/parameters/ignore_msrs)" == "N" ]; then
      echo 1 | tee /sys/module/kvm/parameters/ignore_msrs > /dev/null 2>&1 || true
    fi
  fi

  if [ -f "$CLOCK" ]; then
    CLOCK=$(<"$CLOCK")
    if [[ "${CLOCK,,}" != "tsc" ]]; then
      warn "unexpected clocksource: $CLOCK"
    fi
  else
    warn "file \"$CLOCK\" cannot not found?"
  fi

  if grep -qw "svm" <<< "$flags"; then

    # AMD processor